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 <string.h>
|
---|
38 |
|
---|
39 | #include <boost/filesystem/path.hpp>
|
---|
40 |
|
---|
41 | #include <Qt/qapplication.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | #include "UIElements/Qt4/QtUIFactory.hpp"
|
---|
45 | #include "UIElements/Qt4/QtMainWindow.hpp"
|
---|
46 | #include "UIElements/Qt4/QtDialog.hpp"
|
---|
47 |
|
---|
48 | // boost::python uses placement new which is incompatible with MemDebug.
|
---|
49 | #ifdef HAVE_PYTHON
|
---|
50 | #include "Python/PythonScripting.hpp"
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #include "CodePatterns/MemDebug.hpp"
|
---|
54 |
|
---|
55 | #include "Helpers/defs.hpp"
|
---|
56 |
|
---|
57 | using namespace std;
|
---|
58 |
|
---|
59 | QtUIFactory::QtUIFactory(int _argc, char **_argv) :
|
---|
60 | argc(1),
|
---|
61 | argv(new char*[1]),
|
---|
62 | testlauncher_Interrupted(false),
|
---|
63 | testlauncher_thread(NULL)
|
---|
64 | {
|
---|
65 |
|
---|
66 | // check whether we are in test mode
|
---|
67 | if ((_argc > 1) && (isTestMode(_argv[1]))) {
|
---|
68 | #ifdef HAVE_BOOST_THREAD_HPP
|
---|
69 | std::vector<std::string> scripts;
|
---|
70 | scripts.reserve(_argc-1);
|
---|
71 | for (int i=2;i<_argc;++i)
|
---|
72 | scripts.push_back(std::string(_argv[i]));
|
---|
73 |
|
---|
74 | // prepare an extra thread
|
---|
75 | std::cout << "TESTLAUNCHER: Preparing " << std::endl;
|
---|
76 | testlauncher_thread = new boost::thread(
|
---|
77 | boost::bind(&QtUIFactory::testrun, this, scripts));
|
---|
78 | #else
|
---|
79 | std::cerr << "Boost::thread support missing! Cannot launch test scripts.\n";
|
---|
80 | #endif
|
---|
81 | // use fake commands to not pass test stuff
|
---|
82 | const int length = strlen(_argv[0]);
|
---|
83 | argv[0] = new char[length];
|
---|
84 | strncpy(_argv[0],_argv[0], length);
|
---|
85 | app = new QApplication(argc,argv);
|
---|
86 | } else {
|
---|
87 | app = new QApplication(_argc,_argv);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | QtUIFactory::~QtUIFactory()
|
---|
92 | {
|
---|
93 | if (testlauncher_thread != NULL) {
|
---|
94 | // notify testlauncher_thread thread that we wish to terminate
|
---|
95 | testlauncher_thread->interrupt();
|
---|
96 | // wait till it ends
|
---|
97 | testlauncher_thread->join();
|
---|
98 | // and remove
|
---|
99 | delete testlauncher_thread;
|
---|
100 | }
|
---|
101 | // free fake command line argument arrays
|
---|
102 | delete[] argv[0];
|
---|
103 | delete[] argv;
|
---|
104 | }
|
---|
105 |
|
---|
106 | Dialog* QtUIFactory::makeDialog(const std::string &_title) {
|
---|
107 | return new QtDialog(_title);
|
---|
108 | }
|
---|
109 |
|
---|
110 | MainWindow* QtUIFactory::makeMainWindow() {
|
---|
111 | return new QtMainWindow(app);
|
---|
112 | }
|
---|
113 |
|
---|
114 | QtUIFactory::description::description(int _argc, char **_argv) :
|
---|
115 | UIFactory::factoryDescription("Qt4"),
|
---|
116 | argc(_argc),
|
---|
117 | argv(_argv)
|
---|
118 | {}
|
---|
119 |
|
---|
120 | QtUIFactory::description::~description()
|
---|
121 | {}
|
---|
122 |
|
---|
123 | UIFactory* QtUIFactory::description::makeFactory(){
|
---|
124 | return new QtUIFactory(argc, argv);
|
---|
125 | }
|
---|
126 |
|
---|
127 | bool QtUIFactory::isTestMode(const char *_argument)
|
---|
128 | {
|
---|
129 | return (strncmp(_argument,"--test", 6) == 0);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void QtUIFactory::testrun(const std::vector<std::string> _scripts) const
|
---|
133 | {
|
---|
134 | std::cout << "TESTLAUNCHER: Waiting for GUI to set up" << std::endl;
|
---|
135 | testlauncher_sleep(boost::posix_time::seconds(3));
|
---|
136 |
|
---|
137 | std::vector<std::string>::const_iterator scriptiter = _scripts.begin();
|
---|
138 | do {
|
---|
139 | // then launch script
|
---|
140 | std::cout << "TESTLAUNCHER: Launching script " << *scriptiter << std::endl;
|
---|
141 | boost::filesystem::path scriptfilename(*scriptiter);
|
---|
142 | ASSERT( boost::filesystem::exists(scriptfilename),
|
---|
143 | "QtUIFactory::testrun() - given testmode script file "
|
---|
144 | +toString(scriptfilename.string())+" does not exist.");
|
---|
145 | executePythonScriptFile(scriptfilename);
|
---|
146 | ++scriptiter;
|
---|
147 |
|
---|
148 | std::cout << "TESTLAUNCHER: Sleeping after script" << std::endl;
|
---|
149 | testlauncher_sleep(boost::posix_time::seconds(1.5));
|
---|
150 |
|
---|
151 | } while ((scriptiter != _scripts.end()) && (!testlauncher_Interrupted));
|
---|
152 |
|
---|
153 | // send quit signal
|
---|
154 | std::cout << "TESTLAUNCHER: Quitting" << std::endl;
|
---|
155 | app->quit();
|
---|
156 | }
|
---|
157 |
|
---|
158 | void QtUIFactory::testlauncher_sleep(const boost::posix_time::time_duration& _period) const
|
---|
159 | {
|
---|
160 | try {
|
---|
161 | // first sleep for four seconds
|
---|
162 | #if BOOST_VERSION < 105000
|
---|
163 | testlauncher_thread->sleep(boost::get_system_time() + _period);
|
---|
164 | #else
|
---|
165 | boost::this_thread::sleep_for(boost::chrono::seconds(4));
|
---|
166 | #endif
|
---|
167 | } catch(boost::thread_interrupted &e) {
|
---|
168 | LOG(2, "INFO: testlauncher thread has received stop signal.");
|
---|
169 | }
|
---|
170 | }
|
---|