comparison m-toolbox/m/gui/gltpda/pan6globals.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 function pan6globals(varargin)
2
3 % ========================================================================
4 % ====================== Panel for the globals ===========================
5 % ========================================================================
6 % This is the GUI m-file to show that the panel relative to the global
7 % variables set by the user.
8 %
9 % $Id: pan6globals.m,v 1.6 2008/08/30 12:32:25 nicola Exp $
10
11 global gl currSys
12 currPanel = varargin{1};
13 panelDimens = get(currPanel, 'Position');
14 backColor = get(currPanel, 'BackgroundColor');
15 selBlocks = 0;
16 enlargedFont = 0;
17 currSys = '';
18
19 alltimers = timerfindall;
20 stop(alltimers(1));
21 set(alltimers(1),'TimerFcn',@ContinuousParamCheck,'Period',0.3);
22 start(alltimers(1));
23
24 %----------------------------------------------------------------------
25 function buildGlobalPanel(varargin)
26 %----------------------------------------------------------------------
27
28 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
29 if isempty(globBlock) || (~isempty(varargin) && isnumeric(varargin{3}) && varargin{3}==-1)
30 gl = struct();
31 updateGlobalBlock()
32 else
33 gl = get_param(globBlock{1},'UserData');
34 end
35
36 % if isempty(gl) || (~isempty(varargin) && isnumeric(varargin{3}) && varargin{3}==-1), gl=struct(); end
37
38 panelDimens = get(currPanel, 'Position');
39 delete(findobj(gcf,'Parent',currPanel))
40 varnames = fieldnames(gl);
41 y = numel(varnames);
42
43 % 'List of global variables' text:
44 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[20 panelDimens(4)-40 panelDimens(3)-40 20],'String','List of global variables:','HorizontalAlignment','center','FontSize',enlargedFont+10,'FontWeight','bold','Visible','on','Style','text');
45 % 'Global names' text:
46 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2-150-20 panelDimens(4)-70 150 20],'String','Names:','HorizontalAlignment','right','FontSize',enlargedFont+9,'Visible','on','Style','text');
47 % 'Global values' text:
48 uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2+20 panelDimens(4)-70 150 20],'String','Values:','HorizontalAlignment','left','FontSize',enlargedFont+9,'Visible','on','Style','text');
49
50 for i=1:y
51 % % 'gl.' text:
52 % uicontrol('Parent',currPanel,'BackgroundColor',backColor,'Units','pixels','Position',[panelDimens(3)/2-200-20-15 panelDimens(4)-20-24*(i+2)-5 12 20],'String','gl.','FontSize',enlargedFont+8,'Visible','on','Enable','off','Style','text');
53 % Variable name edit field:
54 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-200-20 panelDimens(4)-20-24*(i+2) 200 20],'String',['gl.',varnames{i}],'FontSize',enlargedFont+8,'Visible','on','Enable','on','UserData',i,'Callback',@globalName,'Style','edit');
55 % Variable value edit field:
56 currVal = gl.(varnames{i});
57 if iscell(currVal), currVal = cell2str(currVal);
58 else currVal = mat2str(currVal);
59 end
60 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2+20 panelDimens(4)-20-24*(i+2) 200 20],'String',currVal,'FontSize',enlargedFont+8,'Visible','on','Enable','on','UserData',i,'Callback',@globalValue,'Style','edit');
61 % Remove input button
62 uicontrol('Parent',currPanel,'Units','pixels','Position',[700 panelDimens(4)-20-24*(i+2) 20 20],'String','-','Visible','on','Enable','on','Callback', @RemoveGlobal,'UserData',i,'Style','pushbutton');
63 end
64
65 % Add input button
66 uicontrol('Parent',currPanel,'Units','pixels','Position',[panelDimens(3)/2-10 panelDimens(4)-50-24*(y+2) 20 20],'String','+','TooltipString','Add a global variable','Visible','on','Enable','on','Callback', @AddGlobal,'Style','pushbutton');
67
68 % Clear all button:
69 uicontrol('Parent',currPanel,'Units','pixels','HorizontalAlignment','center','Position',[panelDimens(3)-100 10 80 16],'FontAngle','italic','FontSize',enlargedFont+8,'String','Clear all','TooltipString','Clear all the global variables','Visible','on','Callback',{@buildGlobalPanel,-1},'Style','pushbutton');
70
71
72 end
73 %----------------------------------------------------------------------
74
75 %%
76 %----------------------------------------------------------------------
77 function ContinuousParamCheck(varargin)
78 % This is the function to execute a continuous check on the status
79 % of the current selection (system and block).
80
81 % Clear if nothing's selected
82 if isempty(bdroot) || strcmp(get_param(bdroot,'BlockDiagramType'),'library') || isempty(find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model'))
83 delete(findobj(gcf,'Parent',currPanel))
84 currSys = gcs;
85 gl = struct();
86 return
87 else
88 if ~strcmp(gcs,currSys)
89 % The selection changed.
90 currSys = gcs;
91 buildGlobalPanel();
92 end
93 end
94
95 % if ~isempty(find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model'))
96 % % It's selected a ltpda simulink analysis diagram.
97 % if ~strcmp(gcs,currSys)
98 % % The selection changed.
99 % currSys = gcs;
100 % buildGlobalPanel();
101 % end
102 % end
103
104 end
105 %----------------------------------------------------------------------
106
107 %%
108 %----------------------------------------------------------------------
109 function AddGlobal(varargin)
110
111 newGlob = 'global';
112 ii = 1;
113 while isfield(gl,[newGlob,num2str(ii)]), ii = ii+1; end
114 newGlob = [newGlob,num2str(ii)];
115 gl.(newGlob) = [];
116 updateGlobalBlock()
117 buildGlobalPanel()
118
119 end
120 %----------------------------------------------------------------------
121
122 %----------------------------------------------------------------------
123 function RemoveGlobal(hObject,varargin)
124
125 currGlob = get(hObject,'Userdata');
126 varnames = fieldnames(gl);
127 gl = rmfield(gl,varnames{currGlob});
128 updateGlobalBlock()
129 buildGlobalPanel()
130
131 end
132 %----------------------------------------------------------------------
133
134 %----------------------------------------------------------------------
135 function globalName(hObject,varargin)
136
137 currGlob = get(hObject,'Userdata');
138 varnames = fieldnames(gl);
139 newGlobName = get(hObject,'String');
140 if numel(newGlobName)<4 || ~strcmp(newGlobName(1:3),'gl.'), newGlobName = ['gl.',strrep(newGlobName,'.','')]; end
141
142 newGlobName = ['gl.',genvarname(newGlobName(4:end))];
143 if isempty(get(hObject,'String')) || isfield(gl,newGlobName(4:end)), set(hObject,'String',['gl.',varnames{currGlob}]); return; end
144
145 set(hObject,'String',newGlobName);
146 gl = utils.prog.rnfield(gl,varnames{currGlob},newGlobName(4:end));
147 updateGlobalBlock()
148 buildGlobalPanel()
149
150 end
151 %----------------------------------------------------------------------
152
153 %----------------------------------------------------------------------
154 function globalValue(hObject,varargin)
155
156 currGlob = get(hObject,'Userdata');
157 newValue = get(hObject,'String');
158 varnames = fieldnames(gl);
159 try newValue = eval(newValue); catch end
160 gl.(varnames{currGlob}) = newValue;
161 updateGlobalBlock()
162 buildGlobalPanel()
163
164 end
165 %----------------------------------------------------------------------
166
167 %----------------------------------------------------------------------
168 function updateGlobalBlock(varargin)
169
170 varNames = fieldnames(gl);
171
172 if numel(varNames)==0
173 % Remove the block if no globals are left:
174 globBlock = find_system(bdroot,'BlockType','SubSystem','Tag','globals');
175 if ~isempty(globBlock)
176 delete_block(globBlock)
177 return;
178 end
179 else
180 globBlock = find_system(bdroot,'SearchDepth',1,'BlockType','SubSystem','Tag','globals');
181 try globBlock = globBlock{1}; catch end
182 % Check if the globals block exists:
183 if isempty(globBlock)
184 globBlock = add_block('ltpda_library/Commonly Used Blocks/Globals',[bdroot '/Globals']);
185 annotation = find_system(bdroot,'FindAll','on','Type','Annotation','Tag','ltpda model');
186 position = get_param(annotation,'Position');
187 set_param(globBlock,'Position',[position(1)+20 position(2)-25 position(1)+150 position(2)-10 ])
188 set_param(globBlock,'DropShadow','on')
189 end
190
191 % Update the globals block:
192 set_param(globBlock,'UserData',gl)
193 set_param(globBlock,'UserDataPersistent','on')
194 end
195
196
197 end
198 %----------------------------------------------------------------------
199
200 %%
201 end