[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 |
|
---|
[e4fe8d] | 56 | #include "Helpers/defs.hpp"
|
---|
[bbbad5] | 57 |
|
---|
| 58 | using namespace std;
|
---|
[ea94a8] | 59 |
|
---|
[15e197] | 60 | QtUIFactory::QtUIFactory(int _argc, char **_argv) :
|
---|
| 61 | argc(1),
|
---|
| 62 | argv(new char*[1]),
|
---|
| 63 | testlauncher_Interrupted(false),
|
---|
| 64 | testlauncher_thread(NULL)
|
---|
[ea94a8] | 65 | {
|
---|
[15e197] | 66 |
|
---|
| 67 | // check whether we are in test mode
|
---|
| 68 | if ((_argc > 1) && (isTestMode(_argv[1]))) {
|
---|
| 69 | #ifdef HAVE_BOOST_THREAD_HPP
|
---|
| 70 | std::vector<std::string> scripts;
|
---|
| 71 | scripts.reserve(_argc-1);
|
---|
| 72 | for (int i=2;i<_argc;++i)
|
---|
| 73 | scripts.push_back(std::string(_argv[i]));
|
---|
| 74 |
|
---|
[08d042] | 75 | // check for line-by-line execution
|
---|
| 76 | const bool SingleLineStepping = (strncmp(&_argv[1][6], "single", 6) == 0);
|
---|
| 77 |
|
---|
[15e197] | 78 | // prepare an extra thread
|
---|
| 79 | std::cout << "TESTLAUNCHER: Preparing " << std::endl;
|
---|
| 80 | testlauncher_thread = new boost::thread(
|
---|
[08d042] | 81 | boost::bind(&QtUIFactory::testrun, this, scripts, SingleLineStepping));
|
---|
[15e197] | 82 | #else
|
---|
| 83 | std::cerr << "Boost::thread support missing! Cannot launch test scripts.\n";
|
---|
| 84 | #endif
|
---|
| 85 | // use fake commands to not pass test stuff
|
---|
| 86 | const int length = strlen(_argv[0]);
|
---|
| 87 | argv[0] = new char[length];
|
---|
| 88 | strncpy(_argv[0],_argv[0], length);
|
---|
| 89 | app = new QApplication(argc,argv);
|
---|
| 90 | } else {
|
---|
| 91 | app = new QApplication(_argc,_argv);
|
---|
| 92 | }
|
---|
[ea94a8] | 93 | }
|
---|
| 94 |
|
---|
[4cf323d] | 95 | QtUIFactory::~QtUIFactory()
|
---|
[ea94a8] | 96 | {
|
---|
[15e197] | 97 | if (testlauncher_thread != NULL) {
|
---|
| 98 | // notify testlauncher_thread thread that we wish to terminate
|
---|
| 99 | testlauncher_thread->interrupt();
|
---|
| 100 | // wait till it ends
|
---|
| 101 | testlauncher_thread->join();
|
---|
| 102 | // and remove
|
---|
| 103 | delete testlauncher_thread;
|
---|
| 104 | }
|
---|
| 105 | // free fake command line argument arrays
|
---|
| 106 | delete[] argv[0];
|
---|
| 107 | delete[] argv;
|
---|
[ea94a8] | 108 | }
|
---|
| 109 |
|
---|
[163110] | 110 | Dialog* QtUIFactory::makeDialog(const std::string &_title) {
|
---|
| 111 | return new QtDialog(_title);
|
---|
[ea94a8] | 112 | }
|
---|
| 113 |
|
---|
[4cf323d] | 114 | MainWindow* QtUIFactory::makeMainWindow() {
|
---|
| 115 | return new QtMainWindow(app);
|
---|
[ea94a8] | 116 | }
|
---|
[82b71a] | 117 |
|
---|
[15e197] | 118 | QtUIFactory::description::description(int _argc, char **_argv) :
|
---|
| 119 | UIFactory::factoryDescription("Qt4"),
|
---|
| 120 | argc(_argc),
|
---|
| 121 | argv(_argv)
|
---|
[82b71a] | 122 | {}
|
---|
| 123 |
|
---|
[4cf323d] | 124 | QtUIFactory::description::~description()
|
---|
[82b71a] | 125 | {}
|
---|
| 126 |
|
---|
[4cf323d] | 127 | UIFactory* QtUIFactory::description::makeFactory(){
|
---|
[15e197] | 128 | return new QtUIFactory(argc, argv);
|
---|
[82b71a] | 129 | }
|
---|
[3054689] | 130 |
|
---|
| 131 | bool QtUIFactory::isTestMode(const char *_argument)
|
---|
| 132 | {
|
---|
| 133 | return (strncmp(_argument,"--test", 6) == 0);
|
---|
| 134 | }
|
---|
[15e197] | 135 |
|
---|
[08d042] | 136 | void QtUIFactory::testrun(const std::vector<std::string> _scripts, const bool _singleLineStepping) const
|
---|
[15e197] | 137 | {
|
---|
| 138 | std::cout << "TESTLAUNCHER: Waiting for GUI to set up" << std::endl;
|
---|
| 139 | testlauncher_sleep(boost::posix_time::seconds(3));
|
---|
| 140 |
|
---|
| 141 | std::vector<std::string>::const_iterator scriptiter = _scripts.begin();
|
---|
| 142 | do {
|
---|
| 143 | // then launch script
|
---|
[08d042] | 144 | std::cout << "TESTLAUNCHER: Launching script " << *scriptiter
|
---|
| 145 | << (_singleLineStepping ? " line by line." : ".") <<std::endl;
|
---|
[dbfb03] | 146 | #ifdef HAVE_PYTHON
|
---|
[08d042] | 147 | if (_singleLineStepping) {
|
---|
| 148 | boost::filesystem::path scriptfilename(*scriptiter);
|
---|
| 149 | ASSERT( boost::filesystem::exists(scriptfilename),
|
---|
| 150 | "QtUIFactory::testrun() - given testmode script file "
|
---|
| 151 | +toString(scriptfilename.string())+" does not exist.");
|
---|
| 152 | std::ifstream scriptfile(scriptfilename.string().c_str());
|
---|
| 153 | std::string scriptfile_line;
|
---|
| 154 | const std::string testmode("testmode, line nr.");
|
---|
| 155 | unsigned int line_nr = 0;
|
---|
| 156 | while(std::getline(scriptfile, scriptfile_line)) {
|
---|
| 157 | std::string scriptname = testmode+toString(++line_nr);
|
---|
| 158 | executePythonScript(scriptfile_line, scriptname);
|
---|
[69b434] | 159 | testlauncher_sleep(boost::posix_time::milliseconds(100));
|
---|
[08d042] | 160 | }
|
---|
| 161 | } else {
|
---|
| 162 | executePythonScriptFile(*scriptiter);
|
---|
| 163 | }
|
---|
[dbfb03] | 164 | #else
|
---|
| 165 | std::cerr << "Python support not compiled in, cannot execute gui test scripts.\n";
|
---|
| 166 | #endif
|
---|
[15e197] | 167 | ++scriptiter;
|
---|
| 168 |
|
---|
| 169 | std::cout << "TESTLAUNCHER: Sleeping after script" << std::endl;
|
---|
| 170 | testlauncher_sleep(boost::posix_time::seconds(1.5));
|
---|
| 171 |
|
---|
| 172 | } while ((scriptiter != _scripts.end()) && (!testlauncher_Interrupted));
|
---|
| 173 |
|
---|
| 174 | // send quit signal
|
---|
| 175 | std::cout << "TESTLAUNCHER: Quitting" << std::endl;
|
---|
[c13b91] | 176 | app->quit(); // exit flag here is not relevant, end of main() sets the return code
|
---|
[15e197] | 177 | }
|
---|
| 178 |
|
---|
| 179 | void QtUIFactory::testlauncher_sleep(const boost::posix_time::time_duration& _period) const
|
---|
| 180 | {
|
---|
| 181 | try {
|
---|
| 182 | // first sleep for four seconds
|
---|
| 183 | #if BOOST_VERSION < 105000
|
---|
| 184 | testlauncher_thread->sleep(boost::get_system_time() + _period);
|
---|
| 185 | #else
|
---|
| 186 | boost::this_thread::sleep_for(boost::chrono::seconds(4));
|
---|
| 187 | #endif
|
---|
| 188 | } catch(boost::thread_interrupted &e) {
|
---|
| 189 | LOG(2, "INFO: testlauncher thread has received stop signal.");
|
---|
| 190 | }
|
---|
| 191 | }
|
---|