| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 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/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * QtUIFactory.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Jan 14, 2010
 | 
|---|
| 27 |  *      Author: crueger
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include <cassert>
 | 
|---|
| 36 | #include <iostream>
 | 
|---|
| 37 | #include <fstream>
 | 
|---|
| 38 | #include <string.h>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <boost/filesystem/path.hpp>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include <Qt/qapplication.h>
 | 
|---|
| 43 | 
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #include "UIElements/Qt4/QtUIFactory.hpp"
 | 
|---|
| 46 | #include "UIElements/Qt4/QtMainWindow.hpp"
 | 
|---|
| 47 | #include "UIElements/Qt4/QtDialog.hpp"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | // boost::python uses placement new which is incompatible with MemDebug.
 | 
|---|
| 50 | #ifdef HAVE_PYTHON
 | 
|---|
| 51 | #include "Python/PythonScripting.hpp"
 | 
|---|
| 52 | #endif
 | 
|---|
| 53 | 
 | 
|---|
| 54 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 55 | 
 | 
|---|
| 56 | #include "Helpers/defs.hpp"
 | 
|---|
| 57 | 
 | 
|---|
| 58 | using namespace std;
 | 
|---|
| 59 | 
 | 
|---|
| 60 | QtUIFactory::QtUIFactory(int _argc, char **_argv) :
 | 
|---|
| 61 |     argc(1),
 | 
|---|
| 62 |     argv(new char*[1]),
 | 
|---|
| 63 |     testlauncher_Interrupted(false),
 | 
|---|
| 64 |     testlauncher_thread(NULL)
 | 
|---|
| 65 | {
 | 
|---|
| 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 | 
 | 
|---|
| 75 |     // check for line-by-line execution
 | 
|---|
| 76 |     const bool SingleLineStepping = (strncmp(&_argv[1][6], "single", 6) == 0);
 | 
|---|
| 77 | 
 | 
|---|
| 78 |     // prepare an extra thread
 | 
|---|
| 79 |     std::cout << "TESTLAUNCHER: Preparing " << std::endl;
 | 
|---|
| 80 |     testlauncher_thread = new boost::thread(
 | 
|---|
| 81 |         boost::bind(&QtUIFactory::testrun, this, scripts, SingleLineStepping));
 | 
|---|
| 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 |   }
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | QtUIFactory::~QtUIFactory()
 | 
|---|
| 96 | {
 | 
|---|
| 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;
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | Dialog* QtUIFactory::makeDialog(const std::string &_title) {
 | 
|---|
| 111 |   return new QtDialog(_title);
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | MainWindow* QtUIFactory::makeMainWindow() {
 | 
|---|
| 115 |   return new QtMainWindow(app);
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | QtUIFactory::description::description(int _argc, char **_argv) :
 | 
|---|
| 119 |     UIFactory::factoryDescription("Qt4"),
 | 
|---|
| 120 |     argc(_argc),
 | 
|---|
| 121 |     argv(_argv)
 | 
|---|
| 122 | {}
 | 
|---|
| 123 | 
 | 
|---|
| 124 | QtUIFactory::description::~description()
 | 
|---|
| 125 | {}
 | 
|---|
| 126 | 
 | 
|---|
| 127 | UIFactory* QtUIFactory::description::makeFactory(){
 | 
|---|
| 128 |   return new QtUIFactory(argc, argv);
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | bool QtUIFactory::isTestMode(const char *_argument)
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   return (strncmp(_argument,"--test", 6) == 0);
 | 
|---|
| 134 | }
 | 
|---|
| 135 | 
 | 
|---|
| 136 | void QtUIFactory::testrun(const std::vector<std::string> _scripts, const bool _singleLineStepping) const
 | 
|---|
| 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
 | 
|---|
| 144 |     std::cout << "TESTLAUNCHER: Launching script " << *scriptiter
 | 
|---|
| 145 |         << (_singleLineStepping ? " line by line." : ".") <<std::endl;
 | 
|---|
| 146 | #ifdef HAVE_PYTHON
 | 
|---|
| 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);
 | 
|---|
| 159 |         testlauncher_sleep(boost::posix_time::milliseconds(100));
 | 
|---|
| 160 |       }
 | 
|---|
| 161 |     } else {
 | 
|---|
| 162 |       executePythonScriptFile(*scriptiter);
 | 
|---|
| 163 |     }
 | 
|---|
| 164 | #else
 | 
|---|
| 165 |     std::cerr << "Python support not compiled in, cannot execute gui test scripts.\n";
 | 
|---|
| 166 | #endif
 | 
|---|
| 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;
 | 
|---|
| 176 |   app->quit(); // exit flag here is not relevant, end of main() sets the return code
 | 
|---|
| 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 | }
 | 
|---|