libyui  3.12.1
YMenuWidget.h
1 /*
2  Copyright (c) [2020] 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: YMenuWidget.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef YMenuWidget_h
26 #define YMenuWidget_h
27 
28 #include "YSelectionWidget.h"
29 #include "YMenuItem.h"
30 
31 class YMenuWidgetPrivate;
32 
33 
34 /**
35  * Abstract base class for widgets that handle menus, e.g. YMenuButton or
36  * YMenuBar.
37  **/
39 {
40 protected:
41  /**
42  * Constructor.
43  *
44  * 'label' is only passed through to the inherited class. For a menu
45  * button, this would be the label on the button; a menu bar does not have
46  * a label.
47  **/
48  YMenuWidget( YWidget * parent, const std::string & label = "" );
49 
50 public:
51 
52  /**
53  * Destructor.
54  **/
55  virtual ~YMenuWidget();
56 
57  /**
58  * Returns a descriptive name of this widget class for logging,
59  * debugging etc.
60  **/
61  virtual const char * widgetClass() const { return "YMenuWidget"; }
62 
63  /**
64  * Rebuild the displayed menu tree from the internally stored YMenuItems.
65  *
66  * The application should call this (once) after all items have been added
67  * with addItem(). YMenuButton::addItems() calls this automatically.
68  *
69  * Derived classes are required to implement this.
70  **/
71  virtual void rebuildMenuTree() = 0;
72 
73  /**
74  * Add multiple items. For some UIs, this can be more efficient than
75  * calling addItem() multiple times. This function also automatically calls
76  * resolveShortcutConflicts() and rebuildMenuTree() at the end.
77  *
78  * Derived classes can overwrite this function, but they should call this
79  * base class function at the end of the new implementation.
80  *
81  * Reimplemented from YSelectionWidget.
82  **/
83  virtual void addItems( const YItemCollection & itemCollection );
84 
85  /**
86  * Add one item. This widget assumes ownership of the item object and will
87  * delete it in its destructor.
88  *
89  * This reimplementation will assign an index to the item that is unique
90  * for all items in this MenuButton. That index can be used later with
91  * findMenuItem() to find the item by that index.
92  *
93  * @note Do not forget to call #resolveShortcutConflicts and
94  * #rebuildMenuTree (in this order!) after adding all elements.
95  *
96  * Reimplemented from YSelectionWidget.
97  **/
98  virtual void addItem( YItem * item_disown );
99 
100  /**
101  * Delete all items.
102  *
103  * Reimplemented from YSelectionWidget.
104  **/
105  virtual void deleteAllItems();
106 
107  /**
108  * Resolve keyboard shortcut conflicts: Change shortcuts of menu items if
109  * there are duplicates in the respective menu level.
110  *
111  * This has to be called after all items are added, but before
112  * rebuildMenuTree() (see above). YMenuWidget::addItems() calls this
113  * automatically.
114  **/
116 
117  /**
118  * Enable or disable an item. This default implementation only updates the
119  * item's 'enabled' field.
120  *
121  * Derived classes should overwrite this method and either update the
122  * item's 'enabled' field in their implementation or call this default
123  * implementation.
124  **/
125  virtual void setItemEnabled( YMenuItem * item, bool enabled );
126 
127  /**
128  * Show or hide an item. This default implementation only updates the
129  * item's 'visible' field.
130  *
131  * Derived classes should overwrite this method and either update the
132  * item's 'visible' field in their implementation or call this default
133  * implementation.
134  **/
135  virtual void setItemVisible( YMenuItem * item, bool visible );
136 
137  /**
138  * Support for the Rest API for UI testing:
139  *
140  * Return the item in the tree which matches a path of labels. This
141  * returns 0 if there is no such item or if it is not a leaf menu item.
142  *
143  * 'path' specifies the user-visible labels (i.e. the translated texts) of
144  * each menu level ( ["File", "Export", "As XML"] ).
145  **/
146  YMenuItem * findItem( std::vector<std::string> & path ) const;
147 
148  /**
149  * Support for the Rest API for UI testing:
150  *
151  * Activate the item selected in the tree.
152  * This can be used in tests to simulate user input.
153  *
154  * Derived classes are required to implement this.
155  **/
156  virtual void activateItem( YMenuItem * item ) = 0;
157 
158  /**
159  * Recursively find the first menu item with the specified index.
160  * Returns 0 if there is no such item.
161  **/
162  YMenuItem * findMenuItem( int index );
163 
164 
165  // No propertySet(), setProperty(), getProperty() on this level:
166  //
167  // This is left to derived widgets (very much like for YSelectionWidget)
168  // so they can be tailored to what they really can provide.
169  //
170  // There is infrastructure for properties like "EnabledItems" here and in
171  // the UI bindings, though, to easily support those properties.
172 
173 protected:
174 
175  /**
176  * Resolve keyboard shortcut conflicts between iterators 'begin' and 'end'.
177  **/
180 
181  /**
182  * Recursively find the first menu item with the specified index
183  * from iterator 'begin' to iterator 'end'.
184  *
185  * Returns 0 if there is no such item.
186  **/
187  YMenuItem * findMenuItem( int index,
190 
191  /**
192  * Recursively looks for the first item in the tree of the menu items
193  * using depth first search.
194  * Return nullptr if item which matches full path is not found.
195  * Path is a vector of strings, where next element is a child item, so
196  * in case one needs to select File->Export->As PDF, for instance,
197  * Vector will look like [ "File", "Export", "As PDF" ].
198  */
199  YMenuItem * findItem( std::vector<std::string>::iterator path_begin,
200  std::vector<std::string>::iterator path_end,
202  YItemConstIterator end ) const;
203 
204  /**
205  * Alias for findMenuItem(). Reimplemented to ensure consistent behaviour
206  * with YSelectionWidget::itemAt().
207  **/
208  YMenuItem * itemAt( int index )
209  { return findMenuItem( index ); }
210 
211  /**
212  * Assign a unique index to all items from iterator 'begin' to iterator
213  * 'end'.
214  **/
216 
217 
218 private:
219 
221 };
222 
223 
224 #endif // YMenuWidget_h
YWidget::parent
YWidget * parent() const
Return this widget's parent or 0 if it doesn't have a parent.
Definition: YWidget.cc:271
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YSelectionWidget
Base class for various kinds of multi-value widgets.
Definition: YSelectionWidget.h:46
YWidget::end
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
YMenuWidget::itemAt
YMenuItem * itemAt(int index)
Alias for findMenuItem().
Definition: YMenuWidget.h:208
YItemIterator
YItemCollection::iterator YItemIterator
Mutable iterator over YItemCollection.
Definition: YItem.h:42
YItemCollection
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:39
YMenuWidget::deleteAllItems
virtual void deleteAllItems()
Delete all items.
Definition: YMenuWidget.cc:103
YMenuWidget::assignUniqueIndex
void assignUniqueIndex(YItemIterator begin, YItemIterator end)
Assign a unique index to all items from iterator 'begin' to iterator 'end'.
Definition: YMenuWidget.cc:88
YMenuWidget::setItemEnabled
virtual void setItemEnabled(YMenuItem *item, bool enabled)
Enable or disable an item.
Definition: YMenuWidget.cc:111
YMenuWidget::setItemVisible
virtual void setItemVisible(YMenuItem *item, bool visible)
Show or hide an item.
Definition: YMenuWidget.cc:119
YMenuWidget::addItem
virtual void addItem(YItem *item_disown)
Add one item.
Definition: YMenuWidget.cc:77
YWidget::begin
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
YSelectionWidget::label
std::string label() const
Return this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:99
YMenuWidget
Abstract base class for widgets that handle menus, e.g.
Definition: YMenuWidget.h:39
ImplPtr< YMenuWidgetPrivate >
YMenuWidget::rebuildMenuTree
virtual void rebuildMenuTree()=0
Rebuild the displayed menu tree from the internally stored YMenuItems.
YMenuWidgetPrivate
Definition: YMenuWidget.cc:40
YMenuWidget::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YMenuWidget.cc:68
YMenuWidget::resolveShortcutConflicts
void resolveShortcutConflicts()
Resolve keyboard shortcut conflicts: Change shortcuts of menu items if there are duplicates in the re...
Definition: YMenuWidget.cc:262
YMenuWidget::YMenuWidget
YMenuWidget(YWidget *parent, const std::string &label="")
Constructor.
Definition: YMenuWidget.cc:51
YMenuItem
Item class for menu items.
Definition: YMenuItem.h:44
YMenuWidget::~YMenuWidget
virtual ~YMenuWidget()
Destructor.
Definition: YMenuWidget.cc:61
YMenuWidget::findMenuItem
YMenuItem * findMenuItem(int index)
Recursively find the first menu item with the specified index.
Definition: YMenuWidget.cc:127
YMenuWidget::findItem
YMenuItem * findItem(std::vector< std::string > &path) const
Support for the Rest API for UI testing:
Definition: YMenuWidget.cc:269
YMenuWidget::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YMenuWidget.h:61
YItemConstIterator
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:45
YItem
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:56
YMenuWidget::activateItem
virtual void activateItem(YMenuItem *item)=0
Support for the Rest API for UI testing: