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 | * QtMainWindow.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jan 14, 2010
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "QtMainWindow.hpp"
|
---|
21 |
|
---|
22 | #include<Qt/qapplication.h>
|
---|
23 | #include<Qt/qlabel.h>
|
---|
24 | #include<Qt/qstring.h>
|
---|
25 | #include<Qt/qmenubar.h>
|
---|
26 | #include<Qt/qsplitter.h>
|
---|
27 |
|
---|
28 | #include<iostream>
|
---|
29 | #include<map>
|
---|
30 |
|
---|
31 | #include<boost/bind.hpp>
|
---|
32 |
|
---|
33 | #include "Helpers/MemDebug.hpp"
|
---|
34 |
|
---|
35 | #include "atom.hpp"
|
---|
36 | #include "molecule.hpp"
|
---|
37 | #include "Helpers/Verbose.hpp"
|
---|
38 | #include "Actions/Action.hpp"
|
---|
39 | #include "Actions/ActionRegistry.hpp"
|
---|
40 | #include "Actions/ValueStorage.hpp"
|
---|
41 | #include "Menu/MenuDescription.hpp"
|
---|
42 | #include "Menu/Menu.hpp"
|
---|
43 | #include "Menu/Qt4/QtMenu.hpp"
|
---|
44 | #include "Menu/ActionMenuItem.hpp"
|
---|
45 | #include "Menu/SubMenuItem.hpp"
|
---|
46 | #include "Views/Qt4/QtWorldView.hpp"
|
---|
47 | #include "Views/Qt4/GLMoleculeView.hpp"
|
---|
48 | #include "Views/Qt4/QtMoleculeView.hpp"
|
---|
49 | #include "Views/Qt4/QtStatusBar.hpp"
|
---|
50 |
|
---|
51 | QtMainWindow::QtMainWindow(QApplication *_theApp) :
|
---|
52 | theApp(_theApp)
|
---|
53 | {
|
---|
54 | QSplitter *splitter1 = new QSplitter (Qt::Horizontal, this );
|
---|
55 | QSplitter *splitter2 = new QSplitter (Qt::Vertical, splitter1 );
|
---|
56 |
|
---|
57 | worldDisplay = new QtWorldView(splitter2);
|
---|
58 |
|
---|
59 | moleculeDisplay = new QtMoleculeView();
|
---|
60 | molecule3dDisplay = new GLMoleculeView();
|
---|
61 |
|
---|
62 | MenuBar = menuBar();
|
---|
63 |
|
---|
64 | // populate menus and add actions
|
---|
65 | {
|
---|
66 | QtMenuReferenceMap NametoTextMenuMap; // contains token to menu reference map
|
---|
67 | MenuShortcutMap ShortcutMap;
|
---|
68 | populateMenu(NametoTextMenuMap, ShortcutMap);
|
---|
69 | populateMenuWithActions(NametoTextMenuMap, ShortcutMap);
|
---|
70 | }
|
---|
71 |
|
---|
72 | setCentralWidget(splitter1);
|
---|
73 | splitter1->addWidget(splitter2);
|
---|
74 | splitter1->addWidget(moleculeDisplay);
|
---|
75 | splitter2->addWidget(molecule3dDisplay);
|
---|
76 | splitter2->addWidget(worldDisplay);
|
---|
77 |
|
---|
78 | statusBar = new QtStatusBar(this);
|
---|
79 | setStatusBar(statusBar);
|
---|
80 |
|
---|
81 | connect(worldDisplay,SIGNAL(moleculeSelected(molecule*)),moleculeDisplay,SLOT(moleculeSelected(molecule*)));
|
---|
82 | connect(worldDisplay,SIGNAL(moleculeUnSelected(molecule*)),moleculeDisplay,SLOT(moleculeUnSelected(molecule*)));
|
---|
83 | }
|
---|
84 |
|
---|
85 | QtMainWindow::~QtMainWindow()
|
---|
86 | {
|
---|
87 | menuBar()->clear();
|
---|
88 | }
|
---|
89 |
|
---|
90 | void QtMainWindow::display() {
|
---|
91 | this->show();
|
---|
92 | theApp->exec();
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Puts Qt's token, the ampersand, in front of the accelerator char in the menu name.
|
---|
96 | * \param ShortcutMap map to all already present accelerator keys
|
---|
97 | * \param MenuName Action of menu
|
---|
98 | * \param ActionName Action of menu
|
---|
99 | * \return name with ampersand added at the right place
|
---|
100 | */
|
---|
101 | std::string QtMainWindow::getNameWithAccelerator(MenuShortcutMap &ShortcutMap, const std::string &MenuName, const std::string &ActionName) const
|
---|
102 | {
|
---|
103 | std::string newname;
|
---|
104 | bool Inserted = false;
|
---|
105 | std::pair < MenuShortcutMap::iterator, bool > Inserter;
|
---|
106 | for (std::string::const_iterator CharRunner = ActionName.begin(); CharRunner != ActionName.end(); ++CharRunner) {
|
---|
107 | if (!Inserted) {
|
---|
108 | Inserter = ShortcutMap.insert( std::pair<std::string, char> (MenuName, *CharRunner) );
|
---|
109 | if (Inserter.second) {
|
---|
110 | newname += '&';
|
---|
111 | Inserted = true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | newname += *CharRunner;
|
---|
115 | }
|
---|
116 | return newname;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Instantiate all menus.
|
---|
120 | * \param NametoTextMenuMap lookup for token to menu reference
|
---|
121 | */
|
---|
122 | void QtMainWindow::populateMenu(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
|
---|
123 | {
|
---|
124 | // go through all menus and create them
|
---|
125 | std::map <std::string, int> TopPositions;
|
---|
126 | TopPositions.insert( std::pair<std::string, int> ("", 0) ); // contains which position was added last
|
---|
127 | QtMenu *Menu = NULL;
|
---|
128 | MenuDescription menudescriptions;
|
---|
129 | std::set <char> ShortcutList;
|
---|
130 | bool CompleteFlag = false;
|
---|
131 | while (!CompleteFlag) {
|
---|
132 | CompleteFlag = true;
|
---|
133 | for(MenuDescription::const_iterator iter = menudescriptions.getBeginIter(); iter != menudescriptions.getEndIter(); ++iter) {
|
---|
134 | // skip when already present
|
---|
135 | if (NametoTextMenuMap.find(iter->first) == NametoTextMenuMap.end()) {
|
---|
136 | // have some short refs to infos
|
---|
137 | const std::string &MenuName = iter->first;
|
---|
138 | const std::string &TopName = iter->second.first;
|
---|
139 | const int &MenuPosition = iter->second.second;
|
---|
140 | std::cout << "MenuName is " << MenuName << ", TopName is " << TopName << " and Position is " << MenuPosition << std::endl;
|
---|
141 |
|
---|
142 | // is it top level?
|
---|
143 | if (TopName == "") {
|
---|
144 | ASSERT(TopPositions.find(TopName) != TopPositions.end(),
|
---|
145 | "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
|
---|
146 | if (MenuPosition-1 == TopPositions[TopName]) {
|
---|
147 | std::cout << "Creating top-level menu " << MenuName << " at position " << MenuPosition << std::endl;
|
---|
148 | Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
|
---|
149 | MenuBar->addMenu(Menu);
|
---|
150 | NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
|
---|
151 | CompleteFlag = false;
|
---|
152 | TopPositions[TopName] = MenuPosition;
|
---|
153 | TopPositions[MenuName] = 0;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | // is it a submenu and the top-level menu is present?
|
---|
157 | else if (NametoTextMenuMap.find(TopName) != NametoTextMenuMap.end()) {
|
---|
158 | ASSERT(TopPositions.find(TopName) != TopPositions.end(),
|
---|
159 | "QtMainWindow::QtMainWindow() - "+TopName+" not present in TopPositions.");
|
---|
160 | if (MenuPosition-1 == TopPositions[TopName]) {
|
---|
161 | QtMenu *& TopMenu = NametoTextMenuMap[TopName];
|
---|
162 | std::cout << "Creating submenu " << MenuName << " to menu " << TopName << " at position " << MenuPosition << std::endl;
|
---|
163 | Menu = new QtMenu(getNameWithAccelerator(ShortcutMap, TopName, MenuName).c_str());
|
---|
164 | TopMenu->addMenu(Menu);
|
---|
165 | NametoTextMenuMap.insert( pair <std::string, QtMenu *> (MenuName, Menu) );
|
---|
166 | CompleteFlag = false;
|
---|
167 | TopPositions[TopName] = MenuPosition;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /** Instantiate all actions within the menus.
|
---|
176 | * \param NametoTextMenuMap lookup for token to menu reference
|
---|
177 | * \param ShortcutMap map for all shortcuts within one menu
|
---|
178 | */
|
---|
179 | void QtMainWindow::populateMenuWithActions(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
|
---|
180 | {
|
---|
181 | typedef std::multimap <std::string, std::string> MenuMap;
|
---|
182 |
|
---|
183 | // go through all menus
|
---|
184 | MenuDescription md;
|
---|
185 | MenuMap MenuItems = md.getMenuItemsMap();
|
---|
186 | QtMenu *Menu = NULL;
|
---|
187 | Action *ActionItem = NULL;
|
---|
188 | std::string OldMenuName;
|
---|
189 | for (MenuMap::const_iterator MenuRunner = MenuItems.begin(); MenuRunner != MenuItems.end(); ++MenuRunner) {
|
---|
190 | const std::string &ActionName = MenuRunner->second;
|
---|
191 | const std::string &MenuName = MenuRunner->first;
|
---|
192 | // add the actions to this menu
|
---|
193 | std::cout << " Adding " << ActionName << " to submenu " << MenuName << std::endl;
|
---|
194 | ActionItem = ActionRegistry::getInstance().getActionByName(ActionName);
|
---|
195 | ASSERT(NametoTextMenuMap.find(MenuName) != NametoTextMenuMap.end(),
|
---|
196 | "QtMainWindow::populateMenu() - cannot find reference for menu "+MenuName+" in NametoTextMenuMap.");
|
---|
197 | Menu = NametoTextMenuMap[MenuName];
|
---|
198 | //ASSERT(ShortcutMap.find(MenuName) != ShortcutMap.end(),
|
---|
199 | // "QtMainWindow::populateMenuWithActions() - missing "+MenuName+" in ShortcutMap.");
|
---|
200 | new ActionMenuItem(
|
---|
201 | 'a',
|
---|
202 | ActionItem->Traits.getDescription().c_str(),
|
---|
203 | Menu,
|
---|
204 | ActionItem);
|
---|
205 | }
|
---|
206 | }
|
---|