[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 |
|
---|
[3027f8] | 8 | /*
|
---|
[4cf323d] | 9 | * QtMainWindow.cpp
|
---|
[3027f8] | 10 | *
|
---|
| 11 | * Created on: Jan 14, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
[bbbad5] | 19 |
|
---|
[4cf323d] | 20 | #include "QtMainWindow.hpp"
|
---|
[3027f8] | 21 |
|
---|
[8f67e2] | 22 | #include<Qt/qapplication.h>
|
---|
[fa27ed] | 23 | #include<Qt/qlabel.h>
|
---|
| 24 | #include<Qt/qstring.h>
|
---|
| 25 | #include<Qt/qmenubar.h>
|
---|
[cef1d7] | 26 | #include<Qt/qsplitter.h>
|
---|
[8f67e2] | 27 |
|
---|
[3027f8] | 28 | #include<iostream>
|
---|
[b2531f] | 29 | #include<map>
|
---|
| 30 |
|
---|
[fa27ed] | 31 | #include<boost/bind.hpp>
|
---|
| 32 |
|
---|
[bbbad5] | 33 | #include "Helpers/MemDebug.hpp"
|
---|
| 34 |
|
---|
[fa27ed] | 35 | #include "atom.hpp"
|
---|
| 36 | #include "molecule.hpp"
|
---|
[952f38] | 37 | #include "Helpers/Verbose.hpp"
|
---|
[b2531f] | 38 | #include "Actions/Action.hpp"
|
---|
| 39 | #include "Actions/ActionRegistry.hpp"
|
---|
[e4afb4] | 40 | #include "Actions/ValueStorage.hpp"
|
---|
| 41 | #include "Menu/MenuDescription.hpp"
|
---|
[b2531f] | 42 | #include "Menu/Menu.hpp"
|
---|
[4cf323d] | 43 | #include "Menu/Qt4/QtMenu.hpp"
|
---|
[b2531f] | 44 | #include "Menu/ActionMenuItem.hpp"
|
---|
[ffb9ad] | 45 | #include "Menu/SubMenuItem.hpp"
|
---|
[4cf323d] | 46 | #include "Views/Qt4/QtWorldView.hpp"
|
---|
| 47 | #include "Views/Qt4/GLMoleculeView.hpp"
|
---|
| 48 | #include "Views/Qt4/QtMoleculeView.hpp"
|
---|
| 49 | #include "Views/Qt4/QtStatusBar.hpp"
|
---|
[3027f8] | 50 |
|
---|
[4cf323d] | 51 | QtMainWindow::QtMainWindow(QApplication *_theApp) :
|
---|
[8f67e2] | 52 | theApp(_theApp)
|
---|
[fa27ed] | 53 | {
|
---|
[cef1d7] | 54 | QSplitter *splitter1 = new QSplitter (Qt::Horizontal, this );
|
---|
| 55 | QSplitter *splitter2 = new QSplitter (Qt::Vertical, splitter1 );
|
---|
| 56 |
|
---|
[4cf323d] | 57 | worldDisplay = new QtWorldView(splitter2);
|
---|
[cef1d7] | 58 |
|
---|
[4cf323d] | 59 | moleculeDisplay = new QtMoleculeView();
|
---|
[cef1d7] | 60 | molecule3dDisplay = new GLMoleculeView();
|
---|
| 61 |
|
---|
[b2531f] | 62 | MenuBar = menuBar();
|
---|
| 63 |
|
---|
[ffb9ad] | 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);
|
---|
[b2531f] | 70 | }
|
---|
[b80021] | 71 |
|
---|
[cef1d7] | 72 | setCentralWidget(splitter1);
|
---|
| 73 | splitter1->addWidget(splitter2);
|
---|
[a77c96] | 74 | splitter1->addWidget(moleculeDisplay);
|
---|
[cef1d7] | 75 | splitter2->addWidget(molecule3dDisplay);
|
---|
[63b56a7] | 76 | splitter2->addWidget(worldDisplay);
|
---|
[cef1d7] | 77 |
|
---|
[4cf323d] | 78 | statusBar = new QtStatusBar(this);
|
---|
[326a43b] | 79 | setStatusBar(statusBar);
|
---|
[fa27ed] | 80 |
|
---|
[a77c96] | 81 | connect(worldDisplay,SIGNAL(moleculeSelected(molecule*)),moleculeDisplay,SLOT(moleculeSelected(molecule*)));
|
---|
| 82 | connect(worldDisplay,SIGNAL(moleculeUnSelected(molecule*)),moleculeDisplay,SLOT(moleculeUnSelected(molecule*)));
|
---|
[fa27ed] | 83 | }
|
---|
[3027f8] | 84 |
|
---|
[4cf323d] | 85 | QtMainWindow::~QtMainWindow()
|
---|
[992fd7] | 86 | {
|
---|
| 87 | menuBar()->clear();
|
---|
| 88 | }
|
---|
[3027f8] | 89 |
|
---|
[4cf323d] | 90 | void QtMainWindow::display() {
|
---|
[8f67e2] | 91 | this->show();
|
---|
| 92 | theApp->exec();
|
---|
[3027f8] | 93 | }
|
---|
[b2531f] | 94 |
|
---|
[ffb9ad] | 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
|
---|
[b2531f] | 102 | {
|
---|
[ffb9ad] | 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;
|
---|
[b2531f] | 115 | }
|
---|
[ffb9ad] | 116 | return newname;
|
---|
[b2531f] | 117 | }
|
---|
| 118 |
|
---|
[ffb9ad] | 119 | /** Instantiate all menus.
|
---|
| 120 | * \param NametoTextMenuMap lookup for token to menu reference
|
---|
| 121 | */
|
---|
| 122 | void QtMainWindow::populateMenu(QtMenuReferenceMap &NametoTextMenuMap, MenuShortcutMap &ShortcutMap)
|
---|
[b2531f] | 123 | {
|
---|
[ffb9ad] | 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
|
---|
[e4afb4] | 184 | MenuDescription md;
|
---|
[ffb9ad] | 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);
|
---|
[b2531f] | 205 | }
|
---|
| 206 | }
|
---|