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 | */
|
---|
42 | TxMenu::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 | */
|
---|
55 | TxMenu::~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 | */
|
---|
64 | void 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 | */
|
---|
71 | void TxMenu::removeItem(MenuItem* item) {
|
---|
72 | items.remove(item);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /** Function to quit this TxMenu.
|
---|
76 | */
|
---|
77 | void TxMenu::doQuit(){
|
---|
78 | quit = true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Return the current state of quitting.
|
---|
82 | * \return quit boolean
|
---|
83 | */
|
---|
84 | bool 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 | */
|
---|
91 | void 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 | */
|
---|
105 | void 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 | */
|
---|
144 | std::string TxMenu::getTitle(){
|
---|
145 | return title;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Return the internally stored outputter of the menu.
|
---|
149 | * \return output stream reference
|
---|
150 | */
|
---|
151 | std::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 | */
|
---|
159 | void 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 | */
|
---|
169 | TxMenu::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 | */
|
---|
177 | TxMenu::LeaveAction::~LeaveAction(){}
|
---|
178 |
|
---|
179 | /** We can't undo the leave action.
|
---|
180 | * \return false
|
---|
181 | */
|
---|
182 | bool TxMenu::LeaveAction::canUndo(){
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /** We should never undo the leave action.
|
---|
187 | * \return false
|
---|
188 | */
|
---|
189 | bool 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 | */
|
---|
196 | void 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 | */
|
---|
202 | Dialog* 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 | */
|
---|
210 | Action::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 | */
|
---|
219 | Action::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 | */
|
---|
228 | Action::state_ptr TxMenu::LeaveAction::performRedo(Action::state_ptr){
|
---|
229 | ASSERT(0,"Cannot redo leaving a menu");
|
---|
230 | return Action::failure;
|
---|
231 | }
|
---|