DUDS
Distributed Update of Data from Something
MenuItem.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2019 Jeff Jackowski
9  */
10 #ifndef MENUITEM_HPP
11 #define MENUITEM_HPP
12 
14 #include <boost/noncopyable.hpp>
15 #include <memory>
16 #include <string>
17 
18 namespace duds { namespace ui { namespace menu {
19 
20 class Menu;
21 class MenuView;
22 class MenuAccess;
23 
58 class MenuItem : public std::enable_shared_from_this<MenuItem>, boost::noncopyable {
59 public:
69  static constexpr Flags Disabled = Flags::Bit(0);
74  static constexpr Flags Invisible = Flags::Bit(1);
79  static constexpr Flags HasValue = Flags::Bit(2);
84  static constexpr Flags Toggle = Flags::Bit(3);
90  static constexpr Flags ToggledOn = Flags::Bit(4);
91 private:
95  std::string lbl;
100  std::string descr;
104  std::string val;
108  Menu *parent = nullptr;
112  Flags flgs;
113  friend Menu;
114 protected:
119  struct MenuItemToken { };
120 public:
131  const std::string &label,
132  Flags flags = Flags::Zero()
133  ) : lbl(label), flgs(flags) { }
146  const std::string &label,
147  const std::string &description,
148  Flags flags = Flags::Zero()
149  ) : lbl(label), descr(description), flgs(flags) { }
165  const std::string &label,
166  const std::string &description,
167  const std::string &value,
168  Flags flags = Flags::Zero()
169  ) : lbl(label), descr(description), val(value), flgs(flags | HasValue) { }
176  lbl(mi.lbl), descr(mi.descr), val(mi.val), flgs(mi.flgs) { }
180  const std::string &label() const {
181  return lbl;
182  }
190  void label(const std::string &l);
194  const std::string &description() const {
195  return descr;
196  }
204  void description(const std::string &d);
210  const std::string &value() const {
211  return val;
212  }
222  void value(const std::string &v);
230  void changeEnabledState(bool state);
237  void disable() {
238  changeEnabledState(false);
239  }
246  void enable() {
247  changeEnabledState(true);
248  }
256  void changeVisibility(bool vis);
263  void hide() {
264  changeVisibility(false);
265  }
272  void show() {
273  changeVisibility(true);
274  }
282  bool toggle();
292  void changeToggle(bool state);
300  void clearToggle() {
301  changeToggle(false);
302  }
310  void setToggle() {
311  changeToggle(true);
312  }
316  Menu *menu() const {
317  return parent;
318  }
322  Flags flags() const {
323  return flgs;
324  }
328  bool isDisabled() const {
329  return flgs & Disabled;
330  }
334  bool isEnabled() const {
335  return !(flgs & Disabled);
336  }
340  bool isInvisible() const {
341  return flgs & Invisible;
342  }
346  bool isVisible() const {
347  return !(flgs & Invisible);
348  }
352  bool hasValue() const {
353  return flgs & HasValue;
354  }
358  bool isToggle() const {
359  return flgs & Toggle;
360  }
365  bool isToggledOn() const {
366  return flgs & ToggledOn;
367  }
371  bool isSelectable() const {
372  return !(flgs & (Disabled | Invisible));
373  }
381  void remove();
386  virtual void select(MenuView &invokingView, const MenuAccess &access);
395  virtual void deselect(MenuView &invokingView, const MenuAccess &access);
405  virtual void chose(MenuView &invokingView, const MenuAccess &access) = 0;
406 };
407 
411 typedef std::shared_ptr<MenuItem> MenuItemSptr;
412 
416 typedef std::weak_ptr<MenuItem> MenuItemWptr;
417 
418 } } }
419 
420 #endif // #ifndef MENUITEM_HPP
const std::string & description() const
Returns the optional description text for this item.
Definition: MenuItem.hpp:194
MenuItem(MenuItemToken, const std::string &label, const std::string &description, const std::string &value, Flags flags=Flags::Zero())
Constructs a new MenuItem with an associated value.
Definition: MenuItem.hpp:163
void changeVisibility(bool vis)
Changes the visibility of the item.
Definition: MenuItem.cpp:78
bool toggle()
Toggles the toggle state of the item and returns the new toggle state.
Definition: MenuItem.cpp:100
static constexpr Flags Invisible
Indicates that the item will not be rendered.
Definition: MenuItem.hpp:74
void changeEnabledState(bool state)
Changes the state of the item to either enabled or disabled.
Definition: MenuItem.cpp:67
bool isEnabled() const
True if the item is flagged as enabled.
Definition: MenuItem.hpp:334
void setToggle()
Sets the toggle state of the item.
Definition: MenuItem.hpp:310
std::string descr
Additional text that may be shown to provide users with a better idea of what the option does...
Definition: MenuItem.hpp:100
MenuItem(MenuItemToken, const MenuItem &mi)
Copy constructs a new MenuItem.
Definition: MenuItem.hpp:175
virtual void select(MenuView &invokingView, const MenuAccess &access)
Invoked when the user has selected, but has not chosen, the item.
Definition: MenuItem.cpp:157
duds::general::BitFlags< struct MenuItemFlags > Flags
A set of option and state flags for menu items.
Definition: MenuItem.hpp:63
const std::string & label() const
Returns the label text for this item.
Definition: MenuItem.hpp:180
static constexpr BitFlags Zero()
Makes a bit flags container with all flags cleared.
Definition: BitFlags.hpp:133
bool isInvisible() const
True if the item is flagged as invisible.
Definition: MenuItem.hpp:340
bool hasValue() const
True if the item is flagged as having a value.
Definition: MenuItem.hpp:352
This token is needed to construct MenuItem objects; use to force all objects to be managed by shared ...
Definition: MenuItem.hpp:119
void clearToggle()
Clears the toggle state of the item.
Definition: MenuItem.hpp:300
MenuItem(MenuItemToken, const std::string &label, const std::string &description, Flags flags=Flags::Zero())
Constructs a new MenuItem.
Definition: MenuItem.hpp:144
Stores the data that defines a menu and provides thread-safe access to that data. ...
Definition: Menu.hpp:45
static constexpr BitFlags Bit(int b)
Makes a bit flags container with a single bit set that is specified by digit number rather than value...
Definition: BitFlags.hpp:141
std::string lbl
The text shown to represent the item.
Definition: MenuItem.hpp:95
Flags flgs
The item&#39;s option flags.
Definition: MenuItem.hpp:112
Menu * menu() const
Returns the menu object that owns this item.
Definition: MenuItem.hpp:316
virtual void chose(MenuView &invokingView, const MenuAccess &access)=0
Called by MenuView when the user choses this MenuItem.
void hide()
Makes the item invisible.
Definition: MenuItem.hpp:263
void disable()
Makes the item disabled.
Definition: MenuItem.hpp:237
static constexpr Flags Disabled
Indicates that the item may not be chosen by the user.
Definition: MenuItem.hpp:69
bool isSelectable() const
Returns true if the item is both visible and enabled.
Definition: MenuItem.hpp:371
void changeToggle(bool state)
Changes the toggle state of the item to the indicated state.
Definition: MenuItem.cpp:122
bool isToggledOn() const
True if the item is in the toggled on (true) state.
Definition: MenuItem.hpp:365
Flags flags() const
Returns the option flags for the item.
Definition: MenuItem.hpp:322
void show()
Makes the item visible.
Definition: MenuItem.hpp:272
bool isDisabled() const
True if the item is flagged as disabled.
Definition: MenuItem.hpp:328
std::shared_ptr< MenuItem > MenuItemSptr
A shared pointer to a MenuItem.
Definition: MenuItem.hpp:411
static constexpr Flags ToggledOn
The toggle state; true when the state is on, when the item is true.
Definition: MenuItem.hpp:90
Keeps track of the selected menu item, and updates it based on user input.
Definition: MenuView.hpp:46
Menu * parent
The owning Menu object.
Definition: MenuItem.hpp:108
virtual void deselect(MenuView &invokingView, const MenuAccess &access)
Invoked when the user has selected another item, and before the select() function for that item is in...
Definition: MenuItem.cpp:159
Provides an exclusive lock on a Menu to allow the menu to be changed.
Definition: MenuAccess.hpp:32
Represents an option that a user can chose from a menu of options.
Definition: MenuItem.hpp:58
std::string val
An optional string for the current setting of the item.
Definition: MenuItem.hpp:104
bool isVisible() const
True if the item is flagged as visible.
Definition: MenuItem.hpp:346
const std::string & value() const
Returns the optional value text for the item.
Definition: MenuItem.hpp:210
void enable()
Makes the item enabled.
Definition: MenuItem.hpp:246
bool isToggle() const
True if the item is flagged as being a toggle.
Definition: MenuItem.hpp:358
MenuItem(MenuItemToken, const std::string &label, Flags flags=Flags::Zero())
Constructs a new MenuItem.
Definition: MenuItem.hpp:129
static constexpr Flags Toggle
Denotes that the item is a toggle, and that the toggle state should be visible on the menu...
Definition: MenuItem.hpp:84
std::weak_ptr< MenuItem > MenuItemWptr
A weak pointer to a MenuItem.
Definition: MenuItem.hpp:416
static constexpr Flags HasValue
Indicates that the item has a value, or setting, that should be shown in the menu if possible...
Definition: MenuItem.hpp:79