[bcf653] | 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 |
|
---|
[65b6e0] | 8 | /*
|
---|
[b59da6] | 9 | * TxMenu.cpp
|
---|
[65b6e0] | 10 | *
|
---|
| 11 | * Created on: Dec 10, 2009
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[112b09] | 20 | #include "Helpers/MemDebug.hpp"
|
---|
| 21 |
|
---|
[65b6e0] | 22 | #include <boost/bind.hpp>
|
---|
[b59da6] | 23 | #include <algorithm>
|
---|
[65b6e0] | 24 | #include <iostream>
|
---|
[5b9bba] | 25 | #include <cmath>
|
---|
[b59da6] | 26 | #include "Actions/Action.hpp"
|
---|
| 27 | #include "Actions/ActionTraits.hpp"
|
---|
| 28 | #include "Menu/TextMenu/TxMenu.hpp"
|
---|
| 29 | #include "Menu/TextMenu/MenuItem.hpp"
|
---|
[d56640] | 30 | #include "Helpers/Assert.hpp"
|
---|
[5b9bba] | 31 |
|
---|
[ef81b0] | 32 |
|
---|
[8f3f40] | 33 | /** Constructor for class TxMenu.
|
---|
| 34 | *
|
---|
[ef81b0] | 35 | * produce a text menu with a given title.
|
---|
| 36 | * The text will later be displayed using the stream passed to the constructor.
|
---|
[8f3f40] | 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
|
---|
[ef81b0] | 41 | */
|
---|
[b59da6] | 42 | TxMenu::TxMenu(std::ostream& _outputter, const std::string _title, char _spacer,int _length) :
|
---|
[24a5e0] | 43 | defaultItem(0),
|
---|
| 44 | outputter(_outputter),
|
---|
| 45 | title(_title),
|
---|
| 46 | spacer(_spacer),
|
---|
| 47 | length(_length),
|
---|
| 48 | quit(false)
|
---|
[5b9bba] | 49 | {
|
---|
| 50 | }
|
---|
[65b6e0] | 51 |
|
---|
[8f3f40] | 52 | /** Destructor for class TxMenu.
|
---|
| 53 | *
|
---|
| 54 | */
|
---|
[b59da6] | 55 | TxMenu::~TxMenu()
|
---|
[65b6e0] | 56 | {
|
---|
[b59da6] | 57 | for(std::list<MenuItem*>::iterator it=items.begin(); it != items.end(); it++)
|
---|
[f767d4] | 58 | delete (*it);
|
---|
[65b6e0] | 59 | }
|
---|
| 60 |
|
---|
[8f3f40] | 61 | /** Adds an MenuItem to the internal list.
|
---|
| 62 | * \param *item item to add
|
---|
| 63 | */
|
---|
[b59da6] | 64 | void TxMenu::addItem(MenuItem* item) {
|
---|
[65b6e0] | 65 | items.push_back(item);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[8f3f40] | 68 | /** Removes an MenuItem to the internal list.
|
---|
| 69 | * \param *item item to remove
|
---|
| 70 | */
|
---|
[b59da6] | 71 | void TxMenu::removeItem(MenuItem* item) {
|
---|
[65b6e0] | 72 | items.remove(item);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[8f3f40] | 75 | /** Function to quit this TxMenu.
|
---|
| 76 | */
|
---|
[b59da6] | 77 | void TxMenu::doQuit(){
|
---|
[65b6e0] | 78 | quit = true;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[8f3f40] | 81 | /** Return the current state of quitting.
|
---|
| 82 | * \return quit boolean
|
---|
| 83 | */
|
---|
[b59da6] | 84 | bool TxMenu::hasQuit(){
|
---|
[65b6e0] | 85 | return quit;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[8f3f40] | 88 | /** Display in a formatted manner a given entry of this menu.
|
---|
| 89 | * \param *entry MenuItem to show
|
---|
| 90 | */
|
---|
[b59da6] | 91 | void TxMenu::showEntry(MenuItem* entry){
|
---|
[f9352d] | 92 | if(entry->isActive()==false){
|
---|
| 93 | outputter << "(";
|
---|
| 94 | }
|
---|
| 95 | outputter << entry->formatEntry();
|
---|
| 96 | if(entry->isActive()==false){
|
---|
| 97 | outputter << ")";
|
---|
| 98 | }
|
---|
| 99 | outputter << "\n";
|
---|
[65b6e0] | 100 | }
|
---|
| 101 |
|
---|
[8f3f40] | 102 | /** Display this menu.
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
[b59da6] | 105 | void TxMenu::display() {
|
---|
[65b6e0] | 106 | char choice;
|
---|
[8f113e] | 107 | bool somethingChosen = false;
|
---|
[7c6f73] | 108 | quit = false;
|
---|
[65b6e0] | 109 | do {
|
---|
[5b9bba] | 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';
|
---|
[b59da6] | 118 | for_each(items.begin(), items.end(), boost::bind(&TxMenu::showEntry,this,_1));
|
---|
[65b6e0] | 119 | outputter.flush();
|
---|
| 120 |
|
---|
[b59da6] | 121 | std::cin >> choice;
|
---|
[65b6e0] | 122 |
|
---|
[b59da6] | 123 | std::list<MenuItem*>::iterator iter;
|
---|
[8f113e] | 124 | for(iter = items.begin(); iter!=items.end();iter++){
|
---|
[f9352d] | 125 | if((*iter)->isActive()){
|
---|
| 126 | somethingChosen |= (*iter)->checkTrigger(choice);
|
---|
| 127 | }
|
---|
[8f113e] | 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{
|
---|
[b59da6] | 135 | outputter << "Invalid Choice!" << std::endl;
|
---|
[8f113e] | 136 | }
|
---|
| 137 | }
|
---|
[65b6e0] | 138 | }while (!hasQuit());
|
---|
| 139 | }
|
---|
[8f113e] | 140 |
|
---|
[8f3f40] | 141 | /** Return the internally stored title of the menu.
|
---|
| 142 | * \return title string
|
---|
| 143 | */
|
---|
[b59da6] | 144 | std::string TxMenu::getTitle(){
|
---|
[d56640] | 145 | return title;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[8f3f40] | 148 | /** Return the internally stored outputter of the menu.
|
---|
| 149 | * \return output stream reference
|
---|
| 150 | */
|
---|
[b59da6] | 151 | std::ostream& TxMenu::getOutputter()
|
---|
| 152 | {
|
---|
| 153 | return outputter;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[8f3f40] | 156 | /** Add a default item to the menu.
|
---|
| 157 | * \param *_defaultItem MenuItem to act as default item.
|
---|
| 158 | */
|
---|
[b59da6] | 159 | void TxMenu::addDefault(MenuItem* _defaultItem) {
|
---|
[8f113e] | 160 | defaultItem = _defaultItem;
|
---|
| 161 | }
|
---|
[d56640] | 162 |
|
---|
| 163 | /****************************** Contained Actions ****************/
|
---|
| 164 |
|
---|
[8f3f40] | 165 | /** Constructor for class TxMenu::LeaveAction.
|
---|
| 166 | * \param _menu pointer to the containing TxMenu
|
---|
| 167 | * \param &LeaveActionTrait ActionTraits for this Action
|
---|
| 168 | */
|
---|
[b59da6] | 169 | TxMenu::LeaveAction::LeaveAction(TxMenu* const _menu, const ActionTraits & LeaveActionTrait) :
|
---|
| 170 | Action(LeaveActionTrait, true),
|
---|
[97b825] | 171 | menu(_menu)
|
---|
[d56640] | 172 | {}
|
---|
| 173 |
|
---|
[8f3f40] | 174 | /** Destructor for class TxMenu::LeaveAction.
|
---|
| 175 | *
|
---|
| 176 | */
|
---|
[b59da6] | 177 | TxMenu::LeaveAction::~LeaveAction(){}
|
---|
[d56640] | 178 |
|
---|
[8f3f40] | 179 | /** We can't undo the leave action.
|
---|
| 180 | * \return false
|
---|
| 181 | */
|
---|
[b59da6] | 182 | bool TxMenu::LeaveAction::canUndo(){
|
---|
[d56640] | 183 | return false;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[8f3f40] | 186 | /** We should never undo the leave action.
|
---|
| 187 | * \return false
|
---|
| 188 | */
|
---|
[b59da6] | 189 | bool TxMenu::LeaveAction::shouldUndo(){
|
---|
[d56640] | 190 | return false;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[8f3f40] | 193 | /** Internal function to obtain parameters from a storage.
|
---|
| 194 | * We do not use this one as we don't need any parameters.
|
---|
| 195 | */
|
---|
[b59da6] | 196 | void TxMenu::LeaveAction::getParametersfromValueStorage()
|
---|
[0b2ce9] | 197 | {}
|
---|
| 198 |
|
---|
[8f3f40] | 199 | /** Internal function to construct the dialog.
|
---|
| 200 | * We do not need this function as there is no dialog to construct.
|
---|
| 201 | */
|
---|
[b59da6] | 202 | Dialog* TxMenu::LeaveAction::fillDialog(Dialog *dialog){
|
---|
[047878] | 203 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
| 204 | return dialog;
|
---|
[c46d63] | 205 | }
|
---|
| 206 |
|
---|
[8f3f40] | 207 | /** Calls TxMenu::doQuit() on the stored menu reference.
|
---|
| 208 | * \return ActionState pointer with success
|
---|
| 209 | */
|
---|
[b59da6] | 210 | Action::state_ptr TxMenu::LeaveAction::performCall(){
|
---|
[d56640] | 211 | menu->doQuit();
|
---|
| 212 | return Action::success;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[8f3f40] | 215 | /** Implementation of undo function for an Action.
|
---|
| 216 | * We do not use this functionality.
|
---|
| 217 | * \return ActionState pointer with failure
|
---|
| 218 | */
|
---|
[b59da6] | 219 | Action::state_ptr TxMenu::LeaveAction::performUndo(Action::state_ptr){
|
---|
[d56640] | 220 | ASSERT(0,"Cannot undo leaving a menu");
|
---|
[8f3f40] | 221 | return Action::failure;
|
---|
[d56640] | 222 | }
|
---|
| 223 |
|
---|
[8f3f40] | 224 | /** Implementation of redo function for an Action.
|
---|
| 225 | * We do not use this functionality.
|
---|
| 226 | * \return ActionState pointer with failure
|
---|
| 227 | */
|
---|
[b59da6] | 228 | Action::state_ptr TxMenu::LeaveAction::performRedo(Action::state_ptr){
|
---|
[d56640] | 229 | ASSERT(0,"Cannot redo leaving a menu");
|
---|
[8f3f40] | 230 | return Action::failure;
|
---|
[d56640] | 231 | }
|
---|