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 | * TextWindow.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jan 7, 2010
|
---|
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 <boost/shared_ptr.hpp>
|
---|
24 |
|
---|
25 | #include "Menu/Menu.hpp"
|
---|
26 | #include "Menu/MenuDescription.hpp"
|
---|
27 | #include "Menu/TextMenu.hpp"
|
---|
28 | #include "Menu/ActionMenuItem.hpp"
|
---|
29 | #include "Menu/SeperatorItem.hpp"
|
---|
30 | #include "Menu/DisplayMenuItem.hpp"
|
---|
31 | #include "Menu/SubMenuItem.hpp"
|
---|
32 | #include "TextUI/TextStatusIndicator.hpp"
|
---|
33 | #include "TextUI/TextWindow.hpp"
|
---|
34 | #include "Actions/MethodAction.hpp"
|
---|
35 | #include "Actions/ErrorAction.hpp"
|
---|
36 | #include "Actions/ActionRegistry.hpp"
|
---|
37 | #include "Actions/ActionTraits.hpp"
|
---|
38 | #include "Parser/ChangeTracker.hpp"
|
---|
39 | #include "Views/StreamStringView.hpp"
|
---|
40 | #include "Views/MethodStringView.hpp"
|
---|
41 |
|
---|
42 | #include "defs.hpp"
|
---|
43 | #include "Helpers/Log.hpp"
|
---|
44 | #include "Helpers/Verbose.hpp"
|
---|
45 |
|
---|
46 | // all needed due to config::SaveAll()
|
---|
47 | #include "config.hpp"
|
---|
48 | #include "periodentafel.hpp"
|
---|
49 |
|
---|
50 | // config::SaveAll() and enumerate()
|
---|
51 | #include "molecule.hpp"
|
---|
52 |
|
---|
53 | #include <iostream>
|
---|
54 | #include <map>
|
---|
55 |
|
---|
56 | // TODO: see what code can be moved to a base class for Graphic and Text Windows
|
---|
57 | TextWindow::TextWindow()
|
---|
58 | {
|
---|
59 | map <std::string, TextMenu *> NametoTextMenuMap;
|
---|
60 | std::set <char> ShortcutList;
|
---|
61 | // reserve s for save and q for quite
|
---|
62 | ShortcutList.insert('s');
|
---|
63 | ShortcutList.insert('q');
|
---|
64 |
|
---|
65 | // build the main menu
|
---|
66 | main_menu = new TextMenu(Log() << Verbose(0), "Main Menu");
|
---|
67 |
|
---|
68 | moleculeView = new StreamStringView(boost::bind(&MoleculeListClass::Enumerate,World::getInstance().getMolecules(),_1));
|
---|
69 | new DisplayMenuItem(main_menu,moleculeView,"Molecule List");
|
---|
70 |
|
---|
71 | new SeperatorItem(main_menu);
|
---|
72 |
|
---|
73 | Action* undoAction = ActionRegistry::getInstance().getActionByName("undo");
|
---|
74 | new ActionMenuItem(getSuitableShortForm(ShortcutList,"Undo last operation"),"Undo last operation",main_menu,undoAction);
|
---|
75 |
|
---|
76 | Action* redoAction = ActionRegistry::getInstance().getActionByName("redo");
|
---|
77 | new ActionMenuItem(getSuitableShortForm(ShortcutList,"Redo last operation"),"Redo last operation",main_menu,redoAction);
|
---|
78 |
|
---|
79 | new SeperatorItem(main_menu);
|
---|
80 |
|
---|
81 | MenuDescription menudescriptions;
|
---|
82 | for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
|
---|
83 | TextMenu *Menu = new TextMenu((ostream &)std::cout, menudescriptions.getDescription(*iter));
|
---|
84 | NametoTextMenuMap.insert( pair <std::string, TextMenu *> (*iter, Menu) );
|
---|
85 | new SubMenuItem(getSuitableShortForm(ShortcutList,*iter),menudescriptions.getDescription(*iter).c_str(),main_menu,Menu);
|
---|
86 | }
|
---|
87 |
|
---|
88 | new SeperatorItem(main_menu);
|
---|
89 |
|
---|
90 | // save has reserved key 's'
|
---|
91 | Action *saveConfigAction = ActionRegistry::getInstance().getActionByName("output");
|
---|
92 | new ActionMenuItem('s',"save current setup to config files",main_menu,saveConfigAction);
|
---|
93 |
|
---|
94 | ActionTraits quitTrait(OptionTrait("quitAction", &typeid(void), "quits the program"));
|
---|
95 | quitAction = new MethodAction(quitTrait,boost::bind(&TextMenu::doQuit,main_menu),false);
|
---|
96 | new ActionMenuItem('q',"quit",main_menu,quitAction);
|
---|
97 |
|
---|
98 | // quit has reserved key 'q'
|
---|
99 | // go through all menus and create them
|
---|
100 | for (map <std::string, TextMenu *>::iterator MenuRunner = NametoTextMenuMap.begin(); MenuRunner != NametoTextMenuMap.end(); ++MenuRunner)
|
---|
101 | populateMenu(MenuRunner->second, MenuRunner->first);
|
---|
102 |
|
---|
103 | // Add status indicators etc...
|
---|
104 |
|
---|
105 | statusIndicator = new TextStatusIndicator();
|
---|
106 | }
|
---|
107 |
|
---|
108 | TextWindow::~TextWindow()
|
---|
109 | {
|
---|
110 | for (std::list<Action *>::iterator iter = returnFromActions.begin(); !returnFromActions.empty(); ++iter)
|
---|
111 | delete (*iter);
|
---|
112 | returnFromActions.clear();
|
---|
113 | delete quitAction;
|
---|
114 | delete moleculeView;
|
---|
115 | delete statusIndicator;
|
---|
116 | delete main_menu;
|
---|
117 | }
|
---|
118 |
|
---|
119 | void TextWindow::display() {
|
---|
120 | main_menu->display();
|
---|
121 | }
|
---|
122 |
|
---|
123 | char TextWindow::getSuitableShortForm(std::set <char> &ShortcutList, const std::string name) const
|
---|
124 | {
|
---|
125 | for (std::string::const_iterator CharRunner = name.begin(); CharRunner != name.end(); ++CharRunner) {
|
---|
126 | if (ShortcutList.find(*CharRunner) == ShortcutList.end()) {
|
---|
127 | ShortcutList.insert(*CharRunner);
|
---|
128 | return *CharRunner;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | // if no letter matches, take digits
|
---|
132 | int i=0;
|
---|
133 | for (;i<10;++i) {
|
---|
134 | if (ShortcutList.find((char)i + '0') == ShortcutList.end())
|
---|
135 | break;
|
---|
136 | }
|
---|
137 | if (i != 10) {
|
---|
138 | ShortcutList.insert((char)i + '0');
|
---|
139 | return ((char)i + '0');
|
---|
140 | } else {
|
---|
141 | DoeLog(1) && (eLog() << Verbose(1) << "Could not find a suitable shortform for " << name << "." << endl);
|
---|
142 | return '#';
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void TextWindow::populateMenu(TextMenu* Menu, const std::string &MenuName)
|
---|
147 | {
|
---|
148 | Action *ActionItem = NULL;
|
---|
149 | set <char> ShortcutList;
|
---|
150 | // reserve 'q' for quit
|
---|
151 | ShortcutList.insert('q');
|
---|
152 | // through all actions for this menu
|
---|
153 | MenuDescription md;
|
---|
154 | std::multimap <std::string, std::string> MenuItems = md.getMenuItemsMap();
|
---|
155 | std::pair < std::multimap <std::string, std::string>::iterator, std::multimap <std::string, std::string>::iterator > MenuActions = MenuItems.equal_range(MenuName);
|
---|
156 | for (std::multimap <std::string, std::string>::const_iterator MenuRunner = MenuActions.first; MenuRunner != MenuActions.second; ++MenuRunner) {
|
---|
157 | ActionItem = ActionRegistry::getInstance().getActionByName(MenuRunner->second);
|
---|
158 | new ActionMenuItem(getSuitableShortForm(ShortcutList, MenuRunner->second),ActionItem->Traits.getDescription().c_str(),Menu,ActionItem);
|
---|
159 | }
|
---|
160 | // finally add default quit item
|
---|
161 | ActionTraits LeaveActionTraits("Leave menu: "+Menu->getTitle());
|
---|
162 | Action *returnFromAction = new TextMenu::LeaveAction(Menu, LeaveActionTraits);
|
---|
163 | //returnFromActions.push_back(returnFromAction);
|
---|
164 | MenuItem *returnFromItem = new ActionMenuItem('q',"return to Main menu",Menu,returnFromAction);
|
---|
165 | Menu->addDefault(returnFromItem);
|
---|
166 | }
|
---|