[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[ea94a8] | 23 | /*
|
---|
[4cf323d] | 24 | * QtUIFactory.cpp
|
---|
[ea94a8] | 25 | *
|
---|
| 26 | * Created on: Jan 14, 2010
|
---|
| 27 | * Author: crueger
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[bbbad5] | 30 | // include config.h
|
---|
| 31 | #ifdef HAVE_CONFIG_H
|
---|
| 32 | #include <config.h>
|
---|
| 33 | #endif
|
---|
| 34 |
|
---|
[ea94a8] | 35 | #include <cassert>
|
---|
[3054689] | 36 | #include <iostream>
|
---|
[08d042] | 37 | #include <fstream>
|
---|
[3054689] | 38 | #include <string.h>
|
---|
[ea94a8] | 39 |
|
---|
[15e197] | 40 | #include <boost/filesystem/path.hpp>
|
---|
| 41 |
|
---|
[4269ca] | 42 | #include <Qt/qapplication.h>
|
---|
[8f67e2] | 43 |
|
---|
[bbbad5] | 44 |
|
---|
[4cf323d] | 45 | #include "UIElements/Qt4/QtUIFactory.hpp"
|
---|
| 46 | #include "UIElements/Qt4/QtMainWindow.hpp"
|
---|
| 47 | #include "UIElements/Qt4/QtDialog.hpp"
|
---|
[bbbad5] | 48 |
|
---|
[15e197] | 49 | // boost::python uses placement new which is incompatible with MemDebug.
|
---|
| 50 | #ifdef HAVE_PYTHON
|
---|
| 51 | #include "Python/PythonScripting.hpp"
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
[ad011c] | 54 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 55 |
|
---|
[a87d1e2] | 56 | #include "Actions/ActionQueue.hpp"
|
---|
[e4fe8d] | 57 | #include "Helpers/defs.hpp"
|
---|
[bbbad5] | 58 |
|
---|
| 59 | using namespace std;
|
---|
[ea94a8] | 60 |
|
---|
[15e197] | 61 | QtUIFactory::QtUIFactory(int _argc, char **_argv) :
|
---|
| 62 | argc(1),
|
---|
| 63 | argv(new char*[1]),
|
---|
| 64 | testlauncher_Interrupted(false),
|
---|
| 65 | testlauncher_thread(NULL)
|
---|
[ea94a8] | 66 | {
|
---|
[15e197] | 67 |
|
---|
| 68 | // check whether we are in test mode
|
---|
| 69 | if ((_argc > 1) && (isTestMode(_argv[1]))) {
|
---|
| 70 | #ifdef HAVE_BOOST_THREAD_HPP
|
---|
| 71 | std::vector<std::string> scripts;
|
---|
| 72 | scripts.reserve(_argc-1);
|
---|
| 73 | for (int i=2;i<_argc;++i)
|
---|
| 74 | scripts.push_back(std::string(_argv[i]));
|
---|
| 75 |
|
---|
[08d042] | 76 | // check for line-by-line execution
|
---|
[883ea0] | 77 | const bool SingleLineStepping = (strncmp(&_argv[1][7], "single", 6) == 0);
|
---|
[08d042] | 78 |
|
---|
[15e197] | 79 | // prepare an extra thread
|
---|
| 80 | std::cout << "TESTLAUNCHER: Preparing " << std::endl;
|
---|
| 81 | testlauncher_thread = new boost::thread(
|
---|
[08d042] | 82 | boost::bind(&QtUIFactory::testrun, this, scripts, SingleLineStepping));
|
---|
[15e197] | 83 | #else
|
---|
| 84 | std::cerr << "Boost::thread support missing! Cannot launch test scripts.\n";
|
---|
| 85 | #endif
|
---|
| 86 | // use fake commands to not pass test stuff
|
---|
| 87 | const int length = strlen(_argv[0]);
|
---|
| 88 | argv[0] = new char[length];
|
---|
[9ad4d6] | 89 | strncpy(argv[0],_argv[0], length);
|
---|
[15e197] | 90 | app = new QApplication(argc,argv);
|
---|
| 91 | } else {
|
---|
| 92 | app = new QApplication(_argc,_argv);
|
---|
| 93 | }
|
---|
[ea94a8] | 94 | }
|
---|
| 95 |
|
---|
[4cf323d] | 96 | QtUIFactory::~QtUIFactory()
|
---|
[ea94a8] | 97 | {
|
---|
[15e197] | 98 | if (testlauncher_thread != NULL) {
|
---|
| 99 | // notify testlauncher_thread thread that we wish to terminate
|
---|
| 100 | testlauncher_thread->interrupt();
|
---|
| 101 | // wait till it ends
|
---|
| 102 | testlauncher_thread->join();
|
---|
| 103 | // and remove
|
---|
| 104 | delete testlauncher_thread;
|
---|
| 105 | }
|
---|
| 106 | // free fake command line argument arrays
|
---|
| 107 | delete[] argv[0];
|
---|
| 108 | delete[] argv;
|
---|
[ea94a8] | 109 | }
|
---|
| 110 |
|
---|
[163110] | 111 | Dialog* QtUIFactory::makeDialog(const std::string &_title) {
|
---|
| 112 | return new QtDialog(_title);
|
---|
[ea94a8] | 113 | }
|
---|
| 114 |
|
---|
[4cf323d] | 115 | MainWindow* QtUIFactory::makeMainWindow() {
|
---|
[28864c] | 116 | MainWindow * const mainwindow = new QtMainWindow(app);
|
---|
| 117 | UIFactory::doneInitializing = true;
|
---|
| 118 | return mainwindow;
|
---|
[ea94a8] | 119 | }
|
---|
[82b71a] | 120 |
|
---|
[15e197] | 121 | QtUIFactory::description::description(int _argc, char **_argv) :
|
---|
| 122 | UIFactory::factoryDescription("Qt4"),
|
---|
| 123 | argc(_argc),
|
---|
| 124 | argv(_argv)
|
---|
[82b71a] | 125 | {}
|
---|
| 126 |
|
---|
[4cf323d] | 127 | QtUIFactory::description::~description()
|
---|
[82b71a] | 128 | {}
|
---|
| 129 |
|
---|
[4cf323d] | 130 | UIFactory* QtUIFactory::description::makeFactory(){
|
---|
[15e197] | 131 | return new QtUIFactory(argc, argv);
|
---|
[82b71a] | 132 | }
|
---|
[3054689] | 133 |
|
---|
| 134 | bool QtUIFactory::isTestMode(const char *_argument)
|
---|
| 135 | {
|
---|
| 136 | return (strncmp(_argument,"--test", 6) == 0);
|
---|
| 137 | }
|
---|
[15e197] | 138 |
|
---|
[08d042] | 139 | void QtUIFactory::testrun(const std::vector<std::string> _scripts, const bool _singleLineStepping) const
|
---|
[15e197] | 140 | {
|
---|
| 141 | std::cout << "TESTLAUNCHER: Waiting for GUI to set up" << std::endl;
|
---|
[28864c] | 142 | while (!UIFactory::isDoneInitializing())
|
---|
| 143 | testlauncher_sleep(boost::posix_time::seconds(.1));
|
---|
[15e197] | 144 |
|
---|
| 145 | std::vector<std::string>::const_iterator scriptiter = _scripts.begin();
|
---|
| 146 | do {
|
---|
| 147 | // then launch script
|
---|
[08d042] | 148 | std::cout << "TESTLAUNCHER: Launching script " << *scriptiter
|
---|
| 149 | << (_singleLineStepping ? " line by line." : ".") <<std::endl;
|
---|
[dbfb03] | 150 | #ifdef HAVE_PYTHON
|
---|
[08d042] | 151 | if (_singleLineStepping) {
|
---|
| 152 | boost::filesystem::path scriptfilename(*scriptiter);
|
---|
| 153 | ASSERT( boost::filesystem::exists(scriptfilename),
|
---|
| 154 | "QtUIFactory::testrun() - given testmode script file "
|
---|
| 155 | +toString(scriptfilename.string())+" does not exist.");
|
---|
| 156 | std::ifstream scriptfile(scriptfilename.string().c_str());
|
---|
| 157 | std::string scriptfile_line;
|
---|
| 158 | const std::string testmode("testmode, line nr.");
|
---|
| 159 | unsigned int line_nr = 0;
|
---|
| 160 | while(std::getline(scriptfile, scriptfile_line)) {
|
---|
| 161 | std::string scriptname = testmode+toString(++line_nr);
|
---|
| 162 | executePythonScript(scriptfile_line, scriptname);
|
---|
[883ea0] | 163 | do {
|
---|
| 164 | testlauncher_sleep(boost::posix_time::seconds(.1));
|
---|
| 165 | } while (!MoleCuilder::ActionQueue::getInstance().isIdle());
|
---|
[08d042] | 166 | }
|
---|
| 167 | } else {
|
---|
| 168 | executePythonScriptFile(*scriptiter);
|
---|
| 169 | }
|
---|
[dbfb03] | 170 | #else
|
---|
| 171 | std::cerr << "Python support not compiled in, cannot execute gui test scripts.\n";
|
---|
| 172 | #endif
|
---|
[15e197] | 173 | ++scriptiter;
|
---|
| 174 |
|
---|
| 175 | std::cout << "TESTLAUNCHER: Sleeping after script" << std::endl;
|
---|
| 176 | testlauncher_sleep(boost::posix_time::seconds(1.5));
|
---|
| 177 |
|
---|
| 178 | } while ((scriptiter != _scripts.end()) && (!testlauncher_Interrupted));
|
---|
| 179 |
|
---|
| 180 | // send quit signal
|
---|
| 181 | std::cout << "TESTLAUNCHER: Quitting" << std::endl;
|
---|
[c13b91] | 182 | app->quit(); // exit flag here is not relevant, end of main() sets the return code
|
---|
[15e197] | 183 | }
|
---|
| 184 |
|
---|
| 185 | void QtUIFactory::testlauncher_sleep(const boost::posix_time::time_duration& _period) const
|
---|
| 186 | {
|
---|
| 187 | try {
|
---|
| 188 | // first sleep for four seconds
|
---|
| 189 | #if BOOST_VERSION < 105000
|
---|
| 190 | testlauncher_thread->sleep(boost::get_system_time() + _period);
|
---|
| 191 | #else
|
---|
| 192 | boost::this_thread::sleep_for(boost::chrono::seconds(4));
|
---|
| 193 | #endif
|
---|
| 194 | } catch(boost::thread_interrupted &e) {
|
---|
| 195 | LOG(2, "INFO: testlauncher thread has received stop signal.");
|
---|
| 196 | }
|
---|
| 197 | }
|
---|