source: src/UIElements/Menu/TextMenu/TxMenu.cpp@ 8f3f40

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 8f3f40 was 8f3f40, checked in by Frederik Heber <heber@…>, 15 years ago

Added documentation on the new menu structure.

  • Property mode set to 100644
File size: 5.5 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * TxMenu.cpp
10 *
11 * Created on: Dec 10, 2009
12 * Author: crueger
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "Helpers/MemDebug.hpp"
21
22#include <boost/bind.hpp>
23#include <algorithm>
24#include <iostream>
25#include <cmath>
26#include "Actions/Action.hpp"
27#include "Actions/ActionTraits.hpp"
28#include "Menu/TextMenu/TxMenu.hpp"
29#include "Menu/TextMenu/MenuItem.hpp"
30#include "Helpers/Assert.hpp"
31
32
33/** Constructor for class TxMenu.
34 *
35 * produce a text menu with a given title.
36 * The text will later be displayed using the stream passed to the constructor.
37 * \param &_outputter output stream to use for displaying the text
38 * \param _title title of this menu
39 * \param _spacer key to separate trigger key from descriptive text shown
40 * \param _length maximum length of the descriptive text
41 */
42TxMenu::TxMenu(std::ostream& _outputter, const std::string _title, char _spacer,int _length) :
43 defaultItem(0),
44 outputter(_outputter),
45 title(_title),
46 spacer(_spacer),
47 length(_length),
48 quit(false)
49{
50}
51
52/** Destructor for class TxMenu.
53 *
54 */
55TxMenu::~TxMenu()
56{
57 for(std::list<MenuItem*>::iterator it=items.begin(); it != items.end(); it++)
58 delete (*it);
59}
60
61/** Adds an MenuItem to the internal list.
62 * \param *item item to add
63 */
64void TxMenu::addItem(MenuItem* item) {
65 items.push_back(item);
66}
67
68/** Removes an MenuItem to the internal list.
69 * \param *item item to remove
70 */
71void TxMenu::removeItem(MenuItem* item) {
72 items.remove(item);
73}
74
75/** Function to quit this TxMenu.
76 */
77void TxMenu::doQuit(){
78 quit = true;
79}
80
81/** Return the current state of quitting.
82 * \return quit boolean
83 */
84bool TxMenu::hasQuit(){
85 return quit;
86}
87
88/** Display in a formatted manner a given entry of this menu.
89 * \param *entry MenuItem to show
90 */
91void TxMenu::showEntry(MenuItem* entry){
92 if(entry->isActive()==false){
93 outputter << "(";
94 }
95 outputter << entry->formatEntry();
96 if(entry->isActive()==false){
97 outputter << ")";
98 }
99 outputter << "\n";
100}
101
102/** Display this menu.
103 *
104 */
105void TxMenu::display() {
106 char choice;
107 bool somethingChosen = false;
108 quit = false;
109 do {
110 int pre = floor((length - title.length()) /2.0);
111 int post = ceil((length - title.length()) /2.0);
112 for(int i=0;i<pre;i++)
113 outputter << spacer;
114 outputter << title;
115 for(int i=0;i<post;i++)
116 outputter << spacer;
117 outputter << '\n';
118 for_each(items.begin(), items.end(), boost::bind(&TxMenu::showEntry,this,_1));
119 outputter.flush();
120
121 std::cin >> choice;
122
123 std::list<MenuItem*>::iterator iter;
124 for(iter = items.begin(); iter!=items.end();iter++){
125 if((*iter)->isActive()){
126 somethingChosen |= (*iter)->checkTrigger(choice);
127 }
128 }
129 // see if something was chosen and call default Item if not
130 if(!somethingChosen) {
131 if(defaultItem){
132 defaultItem->doTrigger();
133 }
134 else{
135 outputter << "Invalid Choice!" << std::endl;
136 }
137 }
138 }while (!hasQuit());
139}
140
141/** Return the internally stored title of the menu.
142 * \return title string
143 */
144std::string TxMenu::getTitle(){
145 return title;
146}
147
148/** Return the internally stored outputter of the menu.
149 * \return output stream reference
150 */
151std::ostream& TxMenu::getOutputter()
152{
153 return outputter;
154}
155
156/** Add a default item to the menu.
157 * \param *_defaultItem MenuItem to act as default item.
158 */
159void TxMenu::addDefault(MenuItem* _defaultItem) {
160 defaultItem = _defaultItem;
161}
162
163/****************************** Contained Actions ****************/
164
165/** Constructor for class TxMenu::LeaveAction.
166 * \param _menu pointer to the containing TxMenu
167 * \param &LeaveActionTrait ActionTraits for this Action
168 */
169TxMenu::LeaveAction::LeaveAction(TxMenu* const _menu, const ActionTraits & LeaveActionTrait) :
170 Action(LeaveActionTrait, true),
171 menu(_menu)
172{}
173
174/** Destructor for class TxMenu::LeaveAction.
175 *
176 */
177TxMenu::LeaveAction::~LeaveAction(){}
178
179/** We can't undo the leave action.
180 * \return false
181 */
182bool TxMenu::LeaveAction::canUndo(){
183 return false;
184}
185
186/** We should never undo the leave action.
187 * \return false
188 */
189bool TxMenu::LeaveAction::shouldUndo(){
190 return false;
191}
192
193/** Internal function to obtain parameters from a storage.
194 * We do not use this one as we don't need any parameters.
195 */
196void TxMenu::LeaveAction::getParametersfromValueStorage()
197{}
198
199/** Internal function to construct the dialog.
200 * We do not need this function as there is no dialog to construct.
201 */
202Dialog* TxMenu::LeaveAction::fillDialog(Dialog *dialog){
203 ASSERT(dialog,"No Dialog given when filling action dialog");
204 return dialog;
205}
206
207/** Calls TxMenu::doQuit() on the stored menu reference.
208 * \return ActionState pointer with success
209 */
210Action::state_ptr TxMenu::LeaveAction::performCall(){
211 menu->doQuit();
212 return Action::success;
213}
214
215/** Implementation of undo function for an Action.
216 * We do not use this functionality.
217 * \return ActionState pointer with failure
218 */
219Action::state_ptr TxMenu::LeaveAction::performUndo(Action::state_ptr){
220 ASSERT(0,"Cannot undo leaving a menu");
221 return Action::failure;
222}
223
224/** Implementation of redo function for an Action.
225 * We do not use this functionality.
226 * \return ActionState pointer with failure
227 */
228Action::state_ptr TxMenu::LeaveAction::performRedo(Action::state_ptr){
229 ASSERT(0,"Cannot redo leaving a menu");
230 return Action::failure;
231}
Note: See TracBrowser for help on using the repository browser.