Mercurial > octave-nkf
annotate scripts/plot/waitbar.m @ 13806:a73c0811d2fa
more updates for waitbar
* waitbar.m: Keep track of current waitbar.
Update patch and title objects instead of replacing them.
New demos.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 03 Nov 2011 14:50:34 -0400 |
parents | b3cdef33ac0e |
children | 72875370e2d1 |
rev | line source |
---|---|
13803 | 1 ## Copyright (C) 2011 John W. Eaton |
2 ## | |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
20 ## @deftypefn {Function File} {} waitbar (@var{frac}) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
21 ## @deftypefn {Function File} {} waitbar (@var{frac}, @var{msg}) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
22 ## @deftypefnx {Function File} {} waitbar (@var{frac}, @var{h}, @dots{}) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
23 ## @deftypefnx {Function File} {} waitbar (@dots{}, "FigureProperty", "Value", @dots{}) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{h} = } waitbar (@dots{}) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
25 ## Create a waitbar filled to fraction @var{frac} and display an optional |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
26 ## message @var{msg}. The waitbar fraction must be in the range [0, 1]. If |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
27 ## the optional input @var{h} is specified then update the waitbar in the |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
28 ## specified figure handle. Otherwise, a new waitbar is created. |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
29 ## |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
30 ## The display of the waitbar window can be configured by passing |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
31 ## property/value pairs to the function. |
13803 | 32 ## @end deftypefn |
33 | |
34 ## Author: jwe | |
35 | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
36 function retval = waitbar (varargin) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
37 |
13806 | 38 persistent curr_waitbar; |
39 | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
40 if (nargin < 1) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
41 print_usage (); |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
42 endif |
13803 | 43 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
44 frac = varargin{1}; |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
45 varargin(1) = []; |
13803 | 46 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
47 if (! (isnumeric (frac) && isscalar (frac) && frac >= 0 && frac <= 1)) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
48 error ("waitbar: FRAC must be between 0 and 1"); |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
49 endif |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
50 |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
51 msg = false; |
13806 | 52 |
53 if (isempty (varargin) && ! isempty (curr_waitbar)) | |
54 h = curr_waitbar; | |
55 else | |
56 h = false; | |
57 endif | |
13803 | 58 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
59 if (! isempty (varargin) && ishandle (varargin{1})) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
60 h = varargin{1}; |
13803 | 61 varargin(1) = []; |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
62 ## FIXME -- also check that H is really a waitbar? |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
63 if (! isfigure (h)) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
64 error ("waitbar: H must be a handle to a waitbar object"); |
13803 | 65 endif |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
66 endif |
13803 | 67 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
68 if (! isempty (varargin)) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
69 msg = varargin{1}; |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
70 varargin(1) = []; |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
71 if (! ischar (msg)) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
72 error ("waitbar: MSG must be a character string"); |
13803 | 73 endif |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
74 endif |
13803 | 75 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
76 if (rem (numel (varargin), 2) != 0) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
77 error ("waitbar: invalid number of property-value pairs"); |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
78 endif |
13803 | 79 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
80 if (h) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
81 p = findobj (h, "type", "patch"); |
13806 | 82 set (p, "xdata", [0; frac; frac; 0]); |
83 ax = findobj (h, "type", "axes"); | |
84 if (ischar (msg)) | |
85 th = get (ax, "title"); | |
86 curr_msg = get (th, "string"); | |
87 if (! strcmp (msg, curr_msg)) | |
88 set (th, "string", msg); | |
89 endif | |
13803 | 90 endif |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
91 else |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
92 h = __go_figure__ (Inf, "position", [250, 500, 400, 100], |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
93 "numbertitle", "off", |
13806 | 94 "toolbar", "none", "menubar", "none", |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
95 "handlevisibility", "callback", |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
96 varargin{:}); |
13803 | 97 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
98 ax = axes ("parent", h, "xtick", [], "ytick", [], |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
99 "xlim", [0, 1], "ylim", [0, 1], |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
100 "xlimmode", "manual", "ylimmode", "manual", |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
101 "position", [0.1, 0.3, 0.8, 0.2]); |
13806 | 102 patch (ax, [0; frac; frac; 0], [0; 0; 1; 1], [0, 0.35, 0.75]); |
103 | |
104 if (ischar (msg)) | |
105 title (ax, msg); | |
106 endif | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
107 endif |
13803 | 108 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
109 drawnow (); |
13803 | 110 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
111 if (nargout > 0) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
112 retval = h; |
13803 | 113 endif |
114 | |
13806 | 115 ## If there were no errors, update current waitbar. |
116 curr_waitbar = h; | |
117 | |
13803 | 118 endfunction |
119 | |
120 %!demo | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
121 %! h = waitbar (0, "0.00%"); |
13803 | 122 %! for i = 0:0.01:1 |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
123 %! waitbar (i, h, sprintf ("%.2f%%", 100*i)); |
13803 | 124 %! endfor |
13806 | 125 %! close (h); |
126 | |
127 %!demo | |
128 %! h = waitbar (0, "please wait..."); | |
129 %! for i = 0:0.01:1 | |
130 %! waitbar (i, h); | |
131 %! endfor | |
132 %! close (h); | |
133 | |
134 %!demo | |
135 %! h = waitbar (0, "please don't be impatient..."); | |
136 %! for i = 0:0.01:1 | |
137 %! waitbar (i); | |
138 %! endfor | |
139 %! close (h); | |
13805
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
140 |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
141 %% Test input validation |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
142 %!error <FRAC must be between 0 and 1> waitbar (-0.5) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
143 %!error <FRAC must be between 0 and 1> waitbar (1.5) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
144 %!error <MSG must be a character string> waitbar (0.5, 1) |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
145 %!error <invalid number of property-value pairs> waitbar (0.5, "msg", "Name") |
b3cdef33ac0e
waitbar.m: Update docstring. Only return output handle h if requested.
Rik <octave@nomad.inbox5.com>
parents:
13803
diff
changeset
|
146 |