IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
irobj.m
Go to the documentation of this file.
1 %> @brief Base class
2 %>
3 %> <h3>Inheriting the "irobj" class
4 %>
5 %> @arg Check for descendants, as there may be a more suitable class to be inherited
6 %> @arg Create a GUI to edit properties from @ref objtool or @ref datatool. This step is optional, but make sure you set @ref flag_ui or @ref flag_params to 0 at the constructor
7 %>
8 %> @warning From my own experience, it is common to start by duplicating an existing class file, but forget to rename the constructor
9 %> The constructor of the new class must have the same name as the class itself.
10 %>
11 %> <h3>Properties to set in the constructor of new object class</h3>
12 %>
13 %> The following properties within @ref irobj define how the GUI will handle the class
14 %> @arg irobj::classtitle
15 %> @arg irobj::flag_params
16 %> @arg irobj::flag_ui
17 %> @arg irobj::moreactions
18 %> @arg irobj::color
19 %>
20 %> <h3>Creating a properties Dialog Box</h3>
21 %> Properties GUI names follow the following pattern: <code>uip_<corresponding class name>.m</code>, and
22 %> <code>uip_<corresponding class name>.fig</code>. The latter is the FIG created using GUIDE.
23 %>
24 %> @arg set <code>flag_ui=1</code> and <code>flag_params=1</code> at the constructor
25 %> @arg Use an existing GUI as template. Open an existing properties GUI in GUIDE, e.g <code>guide uip_fcon_pca</code>, and save it with the appropriate name.
26 %> @arg Make the necessary changes to the GUI and its source code.
27 %>
28 %> <h3>To make it appear in objtool, type</h3>
29 %> @code
31 %> @endcode
32 %>
33 classdef irobj
34 
35  properties
36  title = [];
37  %> =[0, .8, 0]. multipurpose feature, routines may use it for different things. Major use is to change the background of objtool and
38  %> blockmenu. See also @ref classes_html.m
39  color = [0, .8, 0];
40  end;
41 
42  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43  %%% Considering reconfiguring these properties when inheriting from irobj or one or its descendants
44  properties (SetAccess=protected)
45  %> Class Title. Should have a descriptive name, as short as possible.
46  classtitle = 'Base Object';
47  %> Short for the method name
48  short = '';
49  %> =1. (GUI setting) Whether to call a GUI when the block is selected in @ref blockmenu.m . If true, a routine
50  %> called "uip_"&lt;class name&gt; will be called.
51  flag_params = 1;
52  %> (GUI setting) Whether to "publish" in @ref blockmenu and @ref datatool. Note that a class can be "published" without a GUI (set flag_params=0 in this case, at the class constructor).
53  flag_ui = 1;
54  %> (GUI setting) String cell containing names of methods that may be called from the GUIs
55  moreactions = {};
56  end;
57 
58 
59  methods (Access=protected)
60  %> Default report
61  function s = do_get_report(o)
62  s = get_matlab_output(o);
63  end;
64 
65  %> Abstract. HTML inner body
66  function s = do_get_html(o)
67  s = ['<h1 style=''background-color: #', color2hex(o.color), '''>', o.classtitle, '</h1>', 10, '<pre>', o.get_report(), '</pre>', 10];
68  end;
69  end;
70 
71  methods(Sealed)
72  %> Returns description string
73  %>
74  %> Precedence according with flag_short:
75  %> @arg 0: title > short > classtitle
76  %> @arg 1: short > title > classtitle
77  %>
78  %> @param flag_short=0
79  %>
80  %> I am sealing this to make sure that no class will try to improvise on this function.
81  function s = get_description(o, flag_short)
82  if nargin < 2 || isempty(flag_short)
83  flag_short = 0;
84  end;
85  if flag_short
86  ff = {'short', 'title', 'classtitle'};
87  else
88  ff = {'title', 'short', 'classtitle'};
89  end;
90  for i = 1:numel(ff)
91  x = o.(ff{i});
92  if ~isempty(x)
93  s = x; break;
94  end;
95  end;
96  % Note that classtitle is always non-empty, so s will be always set
97  end;
98  end;
99 
100  methods
101  %>@brief Sets several properties of an object at once
102  %>@param o
103  %>@param params Cell followint the pattern @verbatim {'property1', value1, 'property2', value2, ...} @endverbatim
104  function o = setbatch(o, params)
105  for i = 1:length(params)/2
106  varname = params{i*2-1};
107  value = params{i*2};
108  o.(varname) = value;
109  end;
110  end;
111 
112 
113  %> This is used only to compose sequence string e.g. xxx->yyy->zzz
114  %> @param flag_short=0
115  function s = get_methodname(o, flag_short)
116  if nargin < 2 || isempty(flag_short)
117  flag_short = 0;
118  end;
119  s = o.get_description(flag_short);
120  end;
121 
122  %> Object reports are plain text. HTML would be cool but c'mon, we don't need that sophistication
123  function s = get_report(o)
124  s = o.do_get_report();
125  end;
126 
127  %> @param flag_stylesheet=1 Whether to include the stylesheet in the HTML
128  function s = get_html(o, flag_stylesheet)
129 
130  if nargin < 2 || isempty(flag_stylesheet)
131  flag_stylesheet = 0;
132  end;
133  if flag_stylesheet
134  s = stylesheet();
135  else
136  s = '';
137  end;
138 
139  s = cat(2, s, '<h1>', o.get_description(), '</h1>', o.do_get_html());
140 % s = [s, '</body></html>', 10];
141  end;
142 
143  %> @brief Calls Parameters GUI
144  %>
145  %> If @c flag_params, tries uip_<class>.m. If fails, tries uip_<ancestor>.m and so on
146  function result = get_params(o, data)
147  if o.flag_params
148  if nargin < 2
149  data = [];
150  end;
151  mc = metaclass(o);
152  sclass = class(o);
153  flag_func = 0;
154  while 1
155  try
156  func = eval(['@(o, data) uip_' sclass '(o, data);']); %> Builds function pointer based on the block class
157  result = func(o, data);
158  flag_func = 1;
159  catch ME %#ok<NASGU>
160  irverbose(['irobj::get_params() Caught exception ', ME.message]);
161 % disp(ME.getReport());
162  end;
163  if flag_func
164  break;
165  end;
166 
167  if isempty(mc.SuperClasses)
168  break;
169  end;
170  mc = mc.SuperClasses{1};
171  sclass = mc.Name;
172  end;
173 
174  if ~flag_func
175  irerror(sprintf('Couldn''t find a parameters GUI for class "%s"!', class(o)));
176  end;
177  if ~isstruct(result)
178  irerror(sprintf('Result from %s properties GUI not a structure', sclass));
179  end;
180  if ~isfield(result, 'flag_ok')
181  irerror(sprintf('Result from %s properties GUI does not have a flag_ok', sclass));
182  end;
183  else
184  result.flag_ok = 1;
185  result.params = {};
186  end;
187  end;
188 
189  %> @param o
190  %> @return [o, log]
191  function [o, log] = extract_log(o)
192  if isempty(o.log)
193  irerror('Cannot extract log, log is empty!');
194  end;
195  log = o.log;
196  o.log = [];
197  end;
198 
199 % %> Default behaviour: returns class of object plus object's title
200 % function s = get_description(o)
201 % s = '';
202 % if ~isempty(o.title)
203 % s = o.title;
204 % else
205 % a = {o.get_methodname(), class(o)};
206 % qtd = 0;
207 % for i = 1:numel(a)
208 % if ~isempty(a{i})
209 % s = [s, iif(qtd > 0, ' ~ ', ''), a{i}];
210 % qtd = qtd+1;
211 % end;
212 % end;
213 % if ~isempty(s)
214 % s = ['[', s, ']'];
215 % s = replace_underscores(s);
216 % end;
217 % end;
218 % end;
219 
220  %> @param o
221  %> @param flag_title=1
222  function s = get_ancestry(o, flag_title)
223  if nargin == 1
224  flag_title = 1;
225  end;
226  ss = superclasses(o);
227  s = '';
228  for i = numel(ss):-1:1
229  if flag_title
230  o2 = eval(ss{i});
231  stemp = o2.classtitle;
232  else
233  stemp = ss{i};
234  end;
235  s = [s, stemp, '/'];
236  end;
237 
238  if flag_title
239  stemp = o.classtitle;
240  else
241  stemp = class(o);
242  end;
243  s = [s, stemp];
244  end;
245  end;
246 end
function irverbose(in s, in level)
Property color
Definition: irobj.m:42
function classmap_compile()
Property flag_params
Definition: irobj.m:57
Property moreactions
(GUI setting) String cell containing names of methods that may be called from the GUIs ...
Definition: irobj.m:63
function irerror(in s)
function objtool(in varargin)
function blockmenu(in varargin)
function setbatch(in o, in params)
Base Block class.
Definition: block.m:2
function iif(in cond, in x1, in x2)
function replace_underscores(in s)
function stylesheet()
Property flag_ui
(GUI setting) Whether to "publish" in blockmenu and datatool. Note that a class can be "published" wi...
Definition: irobj.m:60
Property classtitle
Class Title. Should have a descriptive name, as short as possible.
Definition: irobj.m:50
Analysis Session (AS) base class.
Definition: as.m:6
function get_matlab_output(in o, in flag_cell)
Base class.
Definition: irobj.m:33