DUDS
Distributed Update of Data from Something
MenuItem.cpp
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  */
11 #include <duds/ui/menu/Menu.hpp>
13 #include <duds/general/Errors.hpp>
14 
15 namespace duds { namespace ui { namespace menu {
16 
22 
23 void MenuItem::label(const std::string &l) {
24  if (parent) {
26  lbl = l;
27  ++parent->updateIdx;
29  } else {
30  lbl = l;
31  }
32 }
33 
34 void MenuItem::description(const std::string &d) {
35  if (parent) {
37  descr = d;
38  ++parent->updateIdx;
40  } else {
41  descr = d;
42  }
43 }
44 
45 void MenuItem::value(const std::string &v) {
46  if (parent) {
47  if (!(flgs & HasValue)) {
49  MenuObject(parent->shared_from_this()) <<
50  MenuItemObject(shared_from_this())
51  );
52  }
54  val = v;
55  ++parent->updateIdx;
57  } else {
58  if (!(flgs & HasValue)) {
60  MenuItemObject(shared_from_this())
61  );
62  }
63  val = v;
64  }
65 }
66 
67 void MenuItem::changeEnabledState(bool state) {
68  if (parent) {
70  }
71  flgs.setTo(Disabled, !state);
72  if (parent) {
73  ++parent->updateIdx;
75  }
76 }
77 
79  if (flgs.test(Invisible) != vis) {
80  return;
81  }
82  if (parent) {
84  // update inivisble count on the menu
85  if (flgs & Invisible) {
86  // there must be at least one invisble item on the menu
87  assert(parent->invis);
88  --parent->invis;
89  } else {
90  ++parent->invis;
91  }
92  }
93  flgs.setTo(Invisible, !vis);
94  if (parent) {
95  ++parent->updateIdx;
97  }
98 }
99 
101  if (parent) {
102  if (!(flgs & Toggle)) {
104  MenuObject(parent->shared_from_this()) <<
105  MenuItemObject(shared_from_this())
106  );
107  }
109  } else if (!(flgs & Toggle)) {
111  MenuItemObject(shared_from_this())
112  );
113  }
115  if (parent) {
116  ++parent->updateIdx;
118  }
119  return flgs & ToggledOn;
120 }
121 
122 void MenuItem::changeToggle(bool state) {
123  // in requested toggle state already?
124  if (flgs.test(ToggledOn) == state) {
125  return;
126  }
127  if (parent) {
128  if (!(flgs & Toggle)) {
130  MenuObject(parent->shared_from_this()) <<
131  MenuItemObject(shared_from_this())
132  );
133  }
135  } else if (!(flgs & Toggle)) {
137  MenuItemObject(shared_from_this())
138  );
139  }
140  flgs.setTo(ToggledOn, state);
141  if (parent) {
142  ++parent->updateIdx;
144  }
145 }
146 
148  if (parent) {
149  // parent will be changed to nullptr during this function
150  Menu *p = parent;
151  p->exclusiveLock();
152  p->remove(shared_from_this());
153  p->exclusiveUnlock();
154  }
155 }
156 
157 void MenuItem::select(MenuView &invokingView, const MenuAccess &access) { }
158 
159 void MenuItem::deselect(MenuView &invokingView, const MenuAccess &access) { }
160 
161 } } }
const std::string & description() const
Returns the optional description text for this item.
Definition: MenuItem.hpp:194
BitFlags setTo(const BitFlags &bf, bool val)
Make all bits in bf set or clear based on the value of val.
Definition: BitFlags.hpp:267
void exclusiveUnlock()
Performs a recursive exclusive unlock on block.
Definition: Menu.cpp:41
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
constexpr bool test(const BitFlags &value, const BitFlags &mask) const
Returns true if the flags identified by mask have the same value as those flags do in value...
Definition: BitFlags.hpp:294
A request was made to change the toggle state of a MenuItem that is not a toggle item.
Definition: MenuErrors.hpp:56
BitFlags toggle(const BitFlags &bf)
Toggle the bits that are set in bf; all other bits remain unchanged.
Definition: BitFlags.hpp:285
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
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
Stores the data that defines a menu and provides thread-safe access to that data. ...
Definition: Menu.hpp:45
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
A request was made to change the value of a MenuItem that does not have a value.
Definition: MenuErrors.hpp:62
boost::error_info< struct Info_Menu, std::shared_ptr< const Menu > > MenuObject
The Menu object that is involved in an error.
Definition: MenuErrors.hpp:100
static constexpr Flags Disabled
Indicates that the item may not be chosen by the user.
Definition: MenuItem.hpp:69
void changeToggle(bool state)
Changes the toggle state of the item to the indicated state.
Definition: MenuItem.cpp:122
int updateIdx
This value is incremented every time the menu is changed.
Definition: Menu.hpp:97
std::size_t invis
The number of items that are currently flagged as invisible.
Definition: Menu.hpp:81
void exclusiveLock()
Performs a recursive exclusive lock on block.
Definition: Menu.cpp:28
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
boost::error_info< struct Info_MenuItem, std::shared_ptr< const MenuItem > > MenuItemObject
The MenuItem object that is involved in an error.
Definition: MenuErrors.hpp:94
std::string val
An optional string for the current setting of the item.
Definition: MenuItem.hpp:104
void remove()
Removes the item from its parent menu.
Definition: MenuItem.cpp:147
const std::string & value() const
Returns the optional value text for the item.
Definition: MenuItem.hpp:210
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
void remove(const std::shared_ptr< MenuItem > &mi)
Removes an item from the menu.
Definition: Menu.cpp:179
General errors.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
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