libyui  3.12.1
YMenuItem.h
1 /*
2  Copyright (C) 2000-2018 Novell, Inc
3  Copyright (c) [2019-2020] SUSE LLC
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 
18 /*-/
19 
20  File: YMenuItem.h
21 
22  Author: Stefan Hundhammer <shundhammer@suse.de>
23 
24 /-*/
25 
26 #ifndef YMenuItem_h
27 #define YMenuItem_h
28 
29 #include "YTreeItem.h"
30 
31 
32 
33 /**
34  * Item class for menu items.
35  *
36  * This is used for plain menu items as well as for menus as well as for menu
37  * separators:
38  *
39  * A menu is a menu item with child items.
40  * A plain menu item does not have child items.
41  * A menu separator has an empty label.
42  **/
43 class YMenuItem: public YTreeItem
44 {
45 public:
46  /**
47  * Constructor for toplevel items.
48  **/
49  YMenuItem( const std::string & label,
50  const std::string & iconName = "" )
52  , _enabled( true )
53  , _visible( true )
54  , _uiItem( 0 )
55  {}
56 
57  /**
58  * Constructors for items that have a parent item.
59  *
60  * They will automatically register this item with the parent item. The
61  * parent assumes ownership of this item and will delete it in its (the
62  * parent's) destructor.
63  *
64  * Consider using addItem() or addMenu() instead.
65  **/
67  const std::string & label,
68  const std::string & iconName = "" )
70  , _enabled( true )
71  , _visible( true )
72  , _uiItem( 0 )
73  {}
74 
75 
76  /**
77  * Destructor.
78  *
79  * This will delete all children.
80  **/
81  virtual ~YMenuItem() {}
82 
83  /**
84  * Create a new menu item as a child of the current instance and return it.
85  * The newly created object is owned by this instance.
86  * This is meant for plain menu items, not for submenus.
87  **/
88  YMenuItem * addItem( const std::string & label,
89  const std::string & iconName = "" );
90 
91  /**
92  * Create a new submenu as a child of the current instance and return it.
93  * The newly created object is owned by this instance.
94  * This is meant to be used for menu items that have children.
95  **/
96  YMenuItem * addMenu( const std::string & label,
97  const std::string & iconName = "" );
98 
99  /**
100  * Create a menu separator as a child of the current instance and return it.
101  * The newly created object is owned by this instance.
102  **/
104 
105  /**
106  * Returns this item's parent item or 0 if it is a toplevel item.
107  **/
108  YMenuItem * parent() const
109  { return dynamic_cast<YMenuItem *> ( YTreeItem::parent() ); }
110 
111  /**
112  * Return 'true' if this is a menu (or submenu), i.e. if it has any child
113  * items.
114  **/
115  bool isMenu() const { return hasChildren(); }
116 
117  /**
118  * Return 'true' if this is a menu separator, i.e. if it has an empty label.
119  **/
120  bool isSeparator() const { return label().empty(); }
121 
122  /**
123  * Return 'true' if this item is enabled (which is the default).
124  **/
125  bool isEnabled() const { return _enabled; }
126 
127  /**
128  * Enable or disable this item.
129  *
130  * Applications should use YMenuWidget::setItemEnabled() instead because
131  * that will also notify the widget so it can update the item's visual
132  * representation.
133  **/
134  void setEnabled( bool enabled = true ) { _enabled = enabled; }
135 
136  /**
137  * Return 'true' if this item is visible (which is the default).
138  **/
139  bool isVisible() const { return _visible; }
140 
141  /**
142  * Show or hide this item.
143  *
144  * Applications should use YMenuWidget::setItemVisible() instead because
145  * that will also notify the widget so it can update the item's visual
146  * representation.
147  **/
148  void setVisible( bool visible = true ) { _visible = visible; }
149 
150  /**
151  * Return the UI counterpart of this item (if the UI set any).
152  **/
153  void * uiItem() const { return _uiItem; }
154 
155  /**
156  * Set the UI counterpart of this item. This can be used to store a pointer
157  * to the equivalent of this item in the concrete UI: For example, the Qt
158  * UI will store a pointer to the corresponding QMenu or QAction.
159  **/
160  void setUiItem( void * uiItem ) { _uiItem = uiItem; }
161 
162 
163 private:
164 
165  // Disable unwanted base class methods
166 
167  bool isOpen() const { return false; }
168  void setOpen( bool ) {}
169 
170 
171  // Data members
172 
173  bool _enabled;
174  bool _visible;
175  void * _uiItem;
176 };
177 
178 
179 #endif // YMenuItem_h
YMenuItem::setEnabled
void setEnabled(bool enabled=true)
Enable or disable this item.
Definition: YMenuItem.h:134
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
YItem::label
std::string label() const
Return this item's label.
Definition: YItem.h:97
YMenuItem::YMenuItem
YMenuItem(YMenuItem *parent, const std::string &label, const std::string &iconName="")
Constructors for items that have a parent item.
Definition: YMenuItem.h:66
YMenuItem::YMenuItem
YMenuItem(const std::string &label, const std::string &iconName="")
Constructor for toplevel items.
Definition: YMenuItem.h:49
YMenuItem::addMenu
YMenuItem * addMenu(const std::string &label, const std::string &iconName="")
Create a new submenu as a child of the current instance and return it.
Definition: YMenuItem.cc:36
YMenuItem::addSeparator
YMenuItem * addSeparator()
Create a menu separator as a child of the current instance and return it.
Definition: YMenuItem.cc:43
YMenuItem::isEnabled
bool isEnabled() const
Return 'true' if this item is enabled (which is the default).
Definition: YMenuItem.h:125
YItem::iconName
std::string iconName() const
Return this item's icon name.
Definition: YItem.h:107
YMenuItem::uiItem
void * uiItem() const
Return the UI counterpart of this item (if the UI set any).
Definition: YMenuItem.h:153
YMenuItem::isMenu
bool isMenu() const
Return 'true' if this is a menu (or submenu), i.e.
Definition: YMenuItem.h:115
YTreeItem::hasChildren
virtual bool hasChildren() const
Return 'true' if this item has any child items.
Definition: YTreeItem.h:82
YMenuItem::setVisible
void setVisible(bool visible=true)
Show or hide this item.
Definition: YMenuItem.h:148
YMenuItem::isVisible
bool isVisible() const
Return 'true' if this item is visible (which is the default).
Definition: YMenuItem.h:139
YTreeItem::parent
virtual YTreeItem * parent() const
Returns this item's parent item or 0 if it is a toplevel item.
Definition: YTreeItem.h:134
YMenuItem::addItem
YMenuItem * addItem(const std::string &label, const std::string &iconName="")
Create a new menu item as a child of the current instance and return it.
Definition: YMenuItem.cc:29
YMenuItem::parent
YMenuItem * parent() const
Returns this item's parent item or 0 if it is a toplevel item.
Definition: YMenuItem.h:108
YMenuItem::setUiItem
void setUiItem(void *uiItem)
Set the UI counterpart of this item.
Definition: YMenuItem.h:160
YMenuItem
Item class for menu items.
Definition: YMenuItem.h:44
YMenuItem::isSeparator
bool isSeparator() const
Return 'true' if this is a menu separator, i.e.
Definition: YMenuItem.h:120
YMenuItem::~YMenuItem
virtual ~YMenuItem()
Destructor.
Definition: YMenuItem.h:81