1 | classdef CardPanel < uiextras.Container |
---|
2 | %CardPanel Show one element (card) from a list |
---|
3 | % |
---|
4 | % obj = uiextras.CardPanel() creates a new card panel which allows |
---|
5 | % selection between the different child objects contained, making the |
---|
6 | % selected child fill the space available and all other children |
---|
7 | % invisible. This is commonly used for creating wizards or quick |
---|
8 | % switching between different views of a single data-set. |
---|
9 | % |
---|
10 | % obj = uiextras.CardPanel(param,value,...) also sets one or more |
---|
11 | % property values. |
---|
12 | % |
---|
13 | % See the <a href="matlab:doc uiextras.CardPanel">documentation</a> for more detail and the list of properties. |
---|
14 | % |
---|
15 | % Examples: |
---|
16 | % >> f = figure(); |
---|
17 | % >> p = uiextras.CardPanel( 'Parent', f, 'Padding', 5 ); |
---|
18 | % >> uicontrol( 'Style', 'frame', 'Parent', p, 'Background', 'r' ); |
---|
19 | % >> uicontrol( 'Style', 'frame', 'Parent', p, 'Background', 'b' ); |
---|
20 | % >> uicontrol( 'Style', 'frame', 'Parent', p, 'Background', 'g' ); |
---|
21 | % >> p.SelectedChild = 2; |
---|
22 | % |
---|
23 | % See also: uiextras.Panel |
---|
24 | % uiextras.BoxPanel |
---|
25 | % uiextras.TabPanel |
---|
26 | |
---|
27 | % Copyright 2009-2010 The MathWorks, Inc. |
---|
28 | % 1.1 |
---|
29 | % 2012/05/08 08:02:58 |
---|
30 | |
---|
31 | |
---|
32 | properties |
---|
33 | Callback = [] |
---|
34 | Padding = 0 % padding around contents (pixels) |
---|
35 | end % public properties |
---|
36 | |
---|
37 | properties ( Dependent = true, SetObservable = true ) |
---|
38 | SelectedChild % Which child is visible [+ve integer or empty] |
---|
39 | end % dependent properties |
---|
40 | |
---|
41 | properties ( SetAccess = private, GetAccess = private, Hidden = true ) |
---|
42 | SelectedChild_ = [] % the index of the child that is currently being shown |
---|
43 | end % private properties |
---|
44 | |
---|
45 | methods |
---|
46 | |
---|
47 | function obj = CardPanel(varargin) |
---|
48 | % First step is to create the parent class. We pass the |
---|
49 | % arguments (if any) just incase the parent needs setting |
---|
50 | obj = obj@uiextras.Container( varargin{:} ); |
---|
51 | |
---|
52 | % Set some defaults |
---|
53 | obj.setPropertyFromDefault( 'Padding' ); |
---|
54 | |
---|
55 | % Set user-supplied property values (only if this is the leaf class) |
---|
56 | if nargin>0 && isequal( class( obj ), 'uiextras.CardPanel' ) |
---|
57 | set( obj, varargin{:} ); |
---|
58 | end |
---|
59 | obj.redraw(); |
---|
60 | end % CardPanel |
---|
61 | |
---|
62 | end % public methods |
---|
63 | |
---|
64 | methods |
---|
65 | |
---|
66 | function value = get.SelectedChild( obj ) |
---|
67 | value = obj.SelectedChild_; |
---|
68 | end % get.SelectedChild |
---|
69 | |
---|
70 | function set.SelectedChild( obj, value ) |
---|
71 | % Check |
---|
72 | if isempty( obj.Children ) |
---|
73 | obj.SelectedChild_ = []; |
---|
74 | else |
---|
75 | if ~isscalar( value ) || (round( value ) ~= value) || value < 0 |
---|
76 | error( 'GUILayout:InvalidPropertyValue', ... |
---|
77 | 'Property ''SelectedChild'' must be a positive integer.' ) |
---|
78 | elseif value > numel( obj.Children ) |
---|
79 | error( 'GUILayout:InvalidPropertyValue', ... |
---|
80 | 'Cannot select child %d of %d.', value, numel( obj.Children ) ) |
---|
81 | end |
---|
82 | |
---|
83 | % Set |
---|
84 | obj.SelectedChild_ = value; |
---|
85 | end |
---|
86 | |
---|
87 | % Redraw |
---|
88 | obj.redraw(); |
---|
89 | end % set.SelectedChild |
---|
90 | |
---|
91 | function set.Padding( obj, value ) |
---|
92 | % Check input |
---|
93 | if ~isnumeric( value ) || ~isscalar( value ) || ... |
---|
94 | ~isreal( value ) || isnan( value ) || ~isfinite( value ) || ... |
---|
95 | value < 0 || rem( value, 1 ) ~= 0 |
---|
96 | error( 'GUILayout:InvalidPropertyValue', ... |
---|
97 | 'Property ''Padding'' must be a nonnegative integer.' ) |
---|
98 | end |
---|
99 | % All OK, so set it and redraw using the new value |
---|
100 | obj.Padding = value; |
---|
101 | obj.redraw(); |
---|
102 | end % set.Padding |
---|
103 | |
---|
104 | end % accessor methods |
---|
105 | |
---|
106 | methods ( Access = protected ) |
---|
107 | |
---|
108 | function redraw(obj) |
---|
109 | %REDRAW redraw the contents |
---|
110 | % |
---|
111 | % Fort a card layout the only thing we really need to do is |
---|
112 | % show one of the children filling the view |
---|
113 | pos = getpixelposition( obj.UIContainer ); |
---|
114 | contentPos = [1 1 pos(3) pos(4)] + obj.Padding*[1 1 -2 -2]; |
---|
115 | obj.showSelectedChild( contentPos ); |
---|
116 | end % redraw |
---|
117 | |
---|
118 | function showSelectedChild( obj, contentPos ) |
---|
119 | % Generic function for showing just one child |
---|
120 | |
---|
121 | page_offset = 2500; % The amount by which widgets are moved when making invisible |
---|
122 | C = obj.Children; |
---|
123 | nC = numel(C); |
---|
124 | if ~isempty( obj.SelectedChild ) |
---|
125 | % Set all to be invisible except current page |
---|
126 | % We also have to move them offscreen to avoid problems with invisible |
---|
127 | % panels and things blocking out visible ones (an HG bug?) |
---|
128 | otherPages = 1:nC; |
---|
129 | otherPages( otherPages==obj.SelectedChild ) = []; |
---|
130 | for page=otherPages |
---|
131 | oldunits = get( C(page), 'Units' ); |
---|
132 | set( C(page), 'Units', 'pixels' ); |
---|
133 | p = get(C(page), 'Position'); |
---|
134 | if p(1)<page_offset || p(2)<page_offset |
---|
135 | newPosition = p + [page_offset page_offset 0 0]; |
---|
136 | obj.repositionChild( C(page), newPosition ) |
---|
137 | end |
---|
138 | set( C(page), 'Units', oldunits ); |
---|
139 | end |
---|
140 | |
---|
141 | % And put the selected one on view |
---|
142 | obj.repositionChild( C(obj.SelectedChild), contentPos ); |
---|
143 | end |
---|
144 | end % showSelectedChild |
---|
145 | |
---|
146 | function onChildAdded( obj, source, eventData ) %#ok<INUSD> |
---|
147 | %onChildAdded: Callback that fires when a child is added to a container. |
---|
148 | % Select the new addition |
---|
149 | C = obj.Children; |
---|
150 | N = numel( C ); |
---|
151 | obj.SelectedChild = N; |
---|
152 | end % onChildAdded |
---|
153 | |
---|
154 | function onChildRemoved( obj, source, eventData ) %#ok<INUSL> |
---|
155 | %onChildAdded: Callback that fires when a container child is destroyed or reparented. |
---|
156 | % If the missing child is the selected one, select something else |
---|
157 | if obj.SelectedChild >= eventData.ChildIndex |
---|
158 | % Changing the selection will force a redraw |
---|
159 | if isempty( obj.Children ) |
---|
160 | obj.SelectedChild = []; |
---|
161 | else |
---|
162 | obj.SelectedChild = max( 1, obj.SelectedChild - 1 ); |
---|
163 | end |
---|
164 | else |
---|
165 | % We don't need to change the selection, so explicitly |
---|
166 | % redraw |
---|
167 | obj.redraw(); |
---|
168 | end |
---|
169 | end % onChildRemoved |
---|
170 | |
---|
171 | end % protected methods |
---|
172 | |
---|
173 | end % classdef |
---|