libyui-qt  2.52.4
YQItemSelector.h
1 /*
2  Copyright (C) 2019 SUSE LLC
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQItemSelector.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 
26 #ifndef YQItemSelector_h
27 #define YQItemSelector_h
28 
29 #include <QScrollArea>
30 #include <QMap>
31 
32 #include <yui/YItemSelector.h>
33 
34 class QScrollArea;
35 class QHBoxLayout;
36 class QVBoxLayout;
37 class QButtonGroup;
38 class QLabel;
40 
41 
42 class YQItemSelector: public QScrollArea, public YItemSelector
43 {
44  Q_OBJECT
45 
46 public:
47 
48  /**
49  * Standard constructor.
50  **/
51  YQItemSelector( YWidget * parent,
52  bool enforceSingleSelection = true );
53 
54 public:
55 
56  /**
57  * Destructor.
58  **/
59  virtual ~YQItemSelector();
60 
61  /**
62  * Add an item.
63  *
64  * Reimplemented from YSelectionWidget.
65  **/
66  virtual void addItem( YItem * item );
67 
68  /**
69  * Add multiple items.
70  *
71  * Reimplemented for efficiency from YSelectionWidget.
72  **/
73  virtual void addItems( const YItemCollection & itemCollection );
74 
75  /**
76  * Select or deselect an item.
77  *
78  * Reimplemented from YSelectionWidget.
79  **/
80  virtual void selectItem( YItem * item, bool selected = true );
81 
82  /**
83  * Deselect all items.
84  *
85  * Reimplemented from YSelectionWidget.
86  **/
87  virtual void deselectAllItems();
88 
89  /**
90  * Delete all items.
91  *
92  * Reimplemented from YSelectionWidget.
93  **/
94  virtual void deleteAllItems();
95 
96  /**
97  * Set enabled/disabled state.
98  *
99  * Reimplemented from YWidget.
100  **/
101  virtual void setEnabled( bool enabled );
102 
103  /**
104  * Preferred width of the widget.
105  *
106  * Reimplemented from YWidget.
107  **/
108  virtual int preferredWidth();
109 
110  /**
111  * Preferred height of the widget.
112  *
113  * Reimplemented from YWidget.
114  **/
115  virtual int preferredHeight();
116 
117  /**
118  * Set the new size of the widget.
119  *
120  * Reimplemented from YWidget.
121  **/
122  virtual void setSize( int newWidth, int newHeight );
123 
124  /**
125  * Accept the keyboard focus.
126  *
127  * Reimplemented from YWidget.
128  **/
129  virtual bool setKeyboardFocus();
130 
131  /**
132  * Return the QWidget that will accept the item widgets.
133  **/
134  QWidget * itemContainer() const { return _itemContainer; }
135 
136  /**
137  * Return the QButtonGroup that manages exclusive buttons in single
138  * selection mode.
139  **/
140  QButtonGroup * buttonGroup() const { return _buttonGroup; }
141 
142  /**
143  * Add an item widget to the appropriate layout.
144  * Not to confuse with addItem( YItem * ).
145  **/
146  void addItemWidget( YQSelectorItemWidget * itemWidget );
147 
148  /**
149  * Activate selected item. Can be used in tests to simulate user input.
150  *
151  * Derived classes are required to implement this.
152  **/
153  virtual void activateItem( YItem * item );
154 
155 protected slots:
156 
157  /**
158  * Notification that an item has been selected.
159  * This is only relevant if `opt(`notify ) is set.
160  **/
161  void slotSelectionChanged( YQSelectorItemWidget * itemWidget,
162  bool selected );
163 
164 
165 
166 protected:
167 
168  /**
169  * Constructor for custom item status values. This is intended for derived
170  * classes; this class does not support custom item status values directly.
171  *
172  * See YItemSelector and YQCustomStatusItemSelector for more details.
173  **/
174  YQItemSelector( YWidget * parent,
175  const YItemCustomStatusVector & customStates );
176 
177  /**
178  * Common initializations for all constructors.
179  **/
180  void init();
181 
182  /**
183  * Deselect all items except 'selectedItem'.
184  **/
185  void deselectOtherItems( YItem * selectedItem );
186 
187  //
188  // Data members
189  //
190 
191  QWidget * _itemContainer;
192  QButtonGroup * _buttonGroup;
193  QVBoxLayout * _itemLayout;
194 
195  QMap<YItem *, YQSelectorItemWidget *> _itemWidgets;
196 
197 }; // class YQItemSelector
198 
199 
200 
201 /**
202  * Class for the widgets of one ItemSelector item
203  **/
204 class YQSelectorItemWidget: public QFrame
205 {
206  Q_OBJECT
207 
208 public:
209 
210  /**
211  * Constructor.
212  **/
214  YItem * item );
215  /**
216  * Destructor.
217  **/
218  virtual ~YQSelectorItemWidget();
219 
220  /**
221  * Create the subwidgets. This needs to be called from the outside
222  * immediately after creating an instance of this class.
223  **/
224  virtual void createWidgets();
225 
226  /**
227  * Select the appropriate widget according to the parent's selection policy
228  * (single or multi selection).
229  **/
230  virtual void setSelected( bool sel = true );
231 
232  /**
233  * Return 'true' if this item is selected, 'false' otherwise.
234  **/
235  virtual bool selected() const;
236 
237  /**
238  * Return 'true' if the parent YItemSelector has single selection (1-of-n).
239  **/
240  bool singleSelection() const;
241 
242  /**
243  * Return 'true' if the parent YItemSelector has multi selection (n-of-m).
244  **/
245  bool multiSelection() const { return ! singleSelection(); }
246 
247  /**
248  * Return the widget that handles the selection: Either a QRadioButton or a
249  * QCheckBox. Both inherit QAbstractButton which has 'isChecked()' and
250  * 'setChecked()'.
251  **/
252  QAbstractButton * headingToggle() const { return _headingToggle; }
253 
254  //
255  // Getters
256  //
257 
258  YQItemSelector * parent() const { return _parent; }
259  QLabel * descriptionLabel() const { return _descriptionLabel; }
260  QLabel * iconLabel() const { return _iconLabel; }
261  YItem * item() const { return _item; }
262 
263 signals:
264 
265  void selectionChanged( YQSelectorItemWidget * itemWidget, bool selected );
266 
267 
268 protected slots:
269 
270  void slotSelectionChanged( bool selected );
271 
272 
273 protected:
274 
275  virtual void createWidgets( const std::string & label,
276  const std::string & description,
277  const std::string & iconName,
278  bool selected = false );
279 
280  /**
281  * Create the appropriate toggle button for this item and connect it to
282  * appropriate slots.
283  *
284  * This base class will create a QRadioButton or a QCheckBox, depending on
285  * the parent YQItemSelector's single or multi selection mode.
286  *
287  * Derived classes can overwrite this to create a different widget.
288  **/
289  virtual QAbstractButton * createHeadingToggle( const std::string & label,
290  QWidget * parent );
291 
292  /**
293  * Return the amount of indentation in pixels for the description text.
294  **/
295  virtual int itemDescriptionIndent() const;
296 
297  //
298  // Data members
299  //
300 
301  YQItemSelector * _parent;
302  YItem * _item;
303 
304  QHBoxLayout * _hBox;
305  QVBoxLayout * _vBox;
306  QAbstractButton * _headingToggle;
307  QLabel * _descriptionLabel;
308  QLabel * _iconLabel;
309 
310 }; // class YQSelectorItemWidget
311 
312 
313 
314 #endif // YQItemSelector_h
virtual int itemDescriptionIndent() const
Return the amount of indentation in pixels for the description text.
QWidget * itemContainer() const
Return the QWidget that will accept the item widgets.
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual void createWidgets()
Create the subwidgets.
virtual ~YQItemSelector()
Destructor.
QButtonGroup * buttonGroup() const
Return the QButtonGroup that manages exclusive buttons in single selection mode.
bool multiSelection() const
Return &#39;true&#39; if the parent YItemSelector has multi selection (n-of-m).
YQItemSelector(YWidget *parent, bool enforceSingleSelection=true)
Standard constructor.
virtual int preferredWidth()
Preferred width of the widget.
QAbstractButton * headingToggle() const
Return the widget that handles the selection: Either a QRadioButton or a QCheckBox.
void init()
Common initializations for all constructors.
void deselectOtherItems(YItem *selectedItem)
Deselect all items except &#39;selectedItem&#39;.
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
virtual ~YQSelectorItemWidget()
Destructor.
virtual void deselectAllItems()
Deselect all items.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Class for the widgets of one ItemSelector item.
void slotSelectionChanged(YQSelectorItemWidget *itemWidget, bool selected)
Notification that an item has been selected.
virtual int preferredHeight()
Preferred height of the widget.
virtual void setSelected(bool sel=true)
Select the appropriate widget according to the parent&#39;s selection policy (single or multi selection)...
virtual void activateItem(YItem *item)
Activate selected item.
bool singleSelection() const
Return &#39;true&#39; if the parent YItemSelector has single selection (1-of-n).
virtual void addItem(YItem *item)
Add an item.
virtual bool selected() const
Return &#39;true&#39; if this item is selected, &#39;false&#39; otherwise.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
YQSelectorItemWidget(YQItemSelector *parent, YItem *item)
Constructor.
virtual QAbstractButton * createHeadingToggle(const std::string &label, QWidget *parent)
Create the appropriate toggle button for this item and connect it to appropriate slots.
virtual void deleteAllItems()
Delete all items.
void addItemWidget(YQSelectorItemWidget *itemWidget)
Add an item widget to the appropriate layout.
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.