source: src/UIElements/Menu/TextMenu/TextMenu.hpp@ 670cd1

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 670cd1 was 670cd1, checked in by Frederik Heber <heber@…>, 14 years ago

FIX: Missing MemDebug.hpp before TextMenu template declaration.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * TextMenu.hpp
3 *
4 * Created on: Nov 5, 2010
5 * Author: heber
6 */
7
8#ifndef TEXTMENU_HPP_
9#define TEXTMENU_HPP_
10
11#include <map>
12#include <string>
13
14#include "Helpers/MemDebug.hpp"
15
16#include "Helpers/Log.hpp"
17#include "Helpers/Verbose.hpp"
18#include "Menu/Menu.hpp"
19#include "Menu/MenuInterface.hpp"
20#include "Actions/Action.hpp"
21#include "Actions/ActionRegistry.hpp"
22#include "Actions/ActionTraits.hpp"
23#include "Menu/TextMenu/TxMenuLeaveAction.hpp"
24#include "Menu/TextMenu/ActionMenuItem.hpp"
25#include "Menu/TextMenu/SeparatorMenuItem.hpp"
26#include "Menu/TextMenu/SubMenuItem.hpp"
27
28/** TextMenu is a specialization of MenuInterface to access TxMenu-like menus.
29 *
30 * Basically, this class is to be used in the MainWindow class of the TextUI.
31 * There, we simply instantiate the class and call init() in order to add all
32 * MenuItem's from MenuDescriptions and Action's ActionRegistry.
33 *
34 * \sa QtMenu.
35 */
36template <class T>
37class TextMenu : virtual public MenuInterface, public Menu
38{
39public:
40 /** Constructor for class TextMenu.
41 * Initializes outputter and token and takes note whether to delete the
42 * MenuInstance or not.
43 */
44 TextMenu(std::ostream &_outputter, const std::string &_token) :
45 MenuInterface(_token),
46 Menu(_token),
47 MenuInstance(new T(_outputter, _token)),
48 outputter(_outputter),
49 deleteMenu(true)
50 {}
51
52 explicit TextMenu(T *_MenuInstance) :
53 Menu(_MenuInstance->getTitle()),
54 MenuInstance(_MenuInstance),
55 outputter(_MenuInstance->getOutputter()),
56 deleteMenu(false)
57 {}
58
59 /** Destructor of MenuInstance.
60 *
61 */
62 virtual ~TextMenu()
63 {
64 if (deleteMenu)
65 delete MenuInstance;
66 }
67
68 /** Display this MenuInstance.
69 *
70 */
71 void display()
72 {
73 MenuInstance->display();
74 }
75
76 /** Returns a pointer to the contained/wrapped MenuInstance.
77 * \return pointer to template class pointer
78 */
79 T * const getMenuInstance()
80 {
81 return MenuInstance;
82 }
83
84 /** Reserves a specific trigger key such that it is not used during init().
85 * \param trigger trigger key
86 * \param &name token given for reference.
87 */
88 void reserveShortcut(char trigger, const std::string &name)
89 {
90 ShortcutMap.insert( std::pair < char, std::string> (trigger, name) );
91 }
92
93protected:
94 // We need to have a reference of the Menu, as Qt returns reference to added menu as well
95 T *MenuInstance;
96 std::ostream &outputter;
97
98private:
99 bool deleteMenu;
100
101 typedef std::map <char, std::string> MenuShortcutMap;
102 MenuShortcutMap ShortcutMap;
103
104 /** Adds an ActionItem by simply creating a new one.
105 * \param &token token of Action (token in ActionRegistry)
106 * \param &description descriptive text to be shown
107 */
108 virtual void addActionItem(const std::string &token, const std::string &description)
109 {
110 new ActionMenuItem(getSuitableShortForm(description), description, MenuInstance, token);
111 }
112
113 /** Adds a (dead) separator item.
114 *
115 */
116 virtual void addSeparatorItem()
117 {
118 new SeparatorMenuItem(MenuInstance);
119 }
120
121 /** Adds a Menu to this current Menu.
122 * We also create here a leave action for this submenu to be able to return
123 * to the current one again
124 * \param &token token of the menu
125 * \param &description descriptive text
126 */
127 virtual void addSubmenuItem(const std::string &token, const std::string &description)
128 {
129 TextMenu<TxMenu> *NewMenu = new TextMenu<TxMenu>(outputter, token);
130 new SubMenuItem(getSuitableShortForm(description), description, MenuInstance, NewMenu->getMenuInstance());
131 NewMenu->reserveShortcut('q',"leave"+token);
132 ActionTraits leaveTrait(
133 OptionTrait("leave"+token, &typeid(void), "leave menu "+token),
134 token,
135 ActionRegistry::getInstance().getLastPosition(token)+2); // have a separator in between
136 new TxMenu::LeaveAction(NewMenu->getMenuInstance(), leaveTrait);
137 NewMenu->init();
138 new ActionMenuItem('q',"leave"+token,NewMenu->getMenuInstance(),"leave"+token);
139 }
140
141 /** Return the next available trigger key suitable to this name.
142 * This function is used internally to make sure that each key is unique to
143 * each Action/Menu. The chosen trigger key is also stored in an internal ShortcutMap.
144 * \param &name text shown in menu
145 * \return trigger key
146 */
147 char getSuitableShortForm(const std::string &name)
148 {
149 for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
150 if (ShortcutMap.find(*CharRunner) == ShortcutMap.end()) {
151 ShortcutMap.insert( std::pair < char, std::string> (*CharRunner, name) );
152 return *CharRunner;
153 }
154 }
155 // if no letter matches, take digits
156 int i=0;
157 for (;i<10;++i) {
158 if (ShortcutMap.find((char)i + '0') == ShortcutMap.end())
159 break;
160 }
161 if (i != 10) {
162 ShortcutMap.insert( std::pair < char, std::string > ((char)i + '0', name));
163 return ((char)i + '0');
164 } else {
165 DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for " << name << "." << endl);
166 return '#';
167 }
168 }
169};
170
171#endif /* TEXTMENU_HPP_ */
Note: See TracBrowser for help on using the repository browser.