| 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 | * Dialog.cpp | 
|---|
| 25 | * | 
|---|
| 26 | *  Created on: Jan 5, 2010 | 
|---|
| 27 | *      Author: crueger | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | // include config.h | 
|---|
| 31 | #ifdef HAVE_CONFIG_H | 
|---|
| 32 | #include <config.h> | 
|---|
| 33 | #endif | 
|---|
| 34 |  | 
|---|
| 35 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 36 |  | 
|---|
| 37 | #include "Dialog.hpp" | 
|---|
| 38 |  | 
|---|
| 39 | #include "CodePatterns/Log.hpp" | 
|---|
| 40 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 41 |  | 
|---|
| 42 | #include "Parameters/ParameterExceptions.hpp" | 
|---|
| 43 |  | 
|---|
| 44 | class Atom; | 
|---|
| 45 | class element; | 
|---|
| 46 | class RealSpaceMatrix; | 
|---|
| 47 | class molecule; | 
|---|
| 48 | class Vector; | 
|---|
| 49 |  | 
|---|
| 50 | using namespace std; | 
|---|
| 51 |  | 
|---|
| 52 | Dialog::Dialog() | 
|---|
| 53 | { | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | Dialog::~Dialog() | 
|---|
| 57 | { | 
|---|
| 58 | list<Query*>::iterator iter; | 
|---|
| 59 | for(iter=queries.begin();iter!=queries.end();iter++){ | 
|---|
| 60 | delete (*iter); | 
|---|
| 61 | } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | void Dialog::registerQuery(Query *query){ | 
|---|
| 65 | queries.push_back(query); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | bool Dialog::display(){ | 
|---|
| 69 | handleAll(); | 
|---|
| 70 | if(checkAll()){ | 
|---|
| 71 | setAll(); | 
|---|
| 72 | return true; | 
|---|
| 73 | } | 
|---|
| 74 | else{ | 
|---|
| 75 | return false; | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void Dialog::handleAll(){ | 
|---|
| 80 | list<Query*>::iterator iter; | 
|---|
| 81 | for(iter=queries.begin(); iter!=queries.end(); iter++){ | 
|---|
| 82 | try{ | 
|---|
| 83 | (*iter)->handle(); | 
|---|
| 84 | }catch(...){ | 
|---|
| 85 | // if any query fails (is canceled), we can end the handling process | 
|---|
| 86 | ELOG(1, "The following query failed: " << (**iter).getTitle() << "."); | 
|---|
| 87 | break; | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | bool Dialog::checkAll(){ | 
|---|
| 93 | list<Query*>::iterator iter; | 
|---|
| 94 | bool retval = true; | 
|---|
| 95 | for(iter=queries.begin(); iter!=queries.end(); iter++){ | 
|---|
| 96 | retval &= (*iter)->isValid(); | 
|---|
| 97 | // if any query fails (is canceled), we can end the handling process | 
|---|
| 98 | if(!retval) { | 
|---|
| 99 | ELOG(1, "The following query failed: " << (**iter).getTitle() << "."); | 
|---|
| 100 | break; | 
|---|
| 101 | } | 
|---|
| 102 | } | 
|---|
| 103 | return retval; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void Dialog::setAll(){ | 
|---|
| 107 | list<Query*>::iterator iter; | 
|---|
| 108 | for(iter=queries.begin(); iter!=queries.end(); iter++) { | 
|---|
| 109 | try { | 
|---|
| 110 | (*iter)->setResult(); | 
|---|
| 111 | } catch (ParameterException &e) { | 
|---|
| 112 | if( const std::string *name=boost::get_error_info<ParameterName>(e) ) | 
|---|
| 113 | ELOG(1, "The following parameter value is not valid: " << *name << "."); | 
|---|
| 114 | break; | 
|---|
| 115 | } | 
|---|
| 116 | } | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | bool Dialog::hasQueries(){ | 
|---|
| 120 | return queries.size(); | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /*template <> void Dialog::query<Dialog::EmptyType>(Parameter<Dialog::EmptyType> ¶m, const char *token, std::string description) | 
|---|
| 124 | { | 
|---|
| 125 | queryEmpty(param, token, description); | 
|---|
| 126 | }*/ | 
|---|
| 127 |  | 
|---|
| 128 | template <> void Dialog::query<bool>(Parameter<bool> ¶m, const char *token, std::string description) | 
|---|
| 129 | { | 
|---|
| 130 | queryBoolean(param, token, description); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | template <> void Dialog::query<int>(Parameter<int> ¶m, const char *token, std::string description) | 
|---|
| 134 | { | 
|---|
| 135 | queryInt(param, token, description); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | template <> void Dialog::query< std::vector<int> >(Parameter<std::vector<int> > ¶m, const char *token, std::string description) | 
|---|
| 139 | { | 
|---|
| 140 | queryInts(param, token, description); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | template <> void Dialog::query<unsigned int>(Parameter<unsigned int> ¶m, const char *token, std::string description) | 
|---|
| 144 | { | 
|---|
| 145 | queryUnsignedInt(param, token, description); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | template <> void Dialog::query< std::vector<unsigned int> >(Parameter<std::vector<unsigned int> > ¶m, const char *token, std::string description) | 
|---|
| 149 | { | 
|---|
| 150 | queryUnsignedInts(param, token, description); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | template <> void Dialog::query<double>(Parameter<double> ¶m, const char *token, std::string description) | 
|---|
| 154 | { | 
|---|
| 155 | queryDouble(param, token, description); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | template <> void Dialog::query< std::vector<double> >(Parameter<std::vector<double> > ¶m, const char *token, std::string description) | 
|---|
| 159 | { | 
|---|
| 160 | queryDoubles(param, token, description); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | template <> void Dialog::query<std::string>(Parameter<std::string> ¶m, const char *token, std::string description) | 
|---|
| 164 | { | 
|---|
| 165 | queryString(param, token, description); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | template <> void Dialog::query< std::vector<std::string> >(Parameter<std::vector<std::string> > ¶m, const char *token, std::string description) | 
|---|
| 169 | { | 
|---|
| 170 | queryStrings(param, token, description); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | template <> void Dialog::query<const atom *>(Parameter<const atom *> ¶m, const char *token, std::string description) | 
|---|
| 174 | { | 
|---|
| 175 | queryAtom(param, token, description); | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | template <> void Dialog::query< std::vector<const atom *> >(Parameter<std::vector<const atom *> > ¶m, const char *token, std::string description) | 
|---|
| 179 | { | 
|---|
| 180 | queryAtoms(param, token, description); | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | template <> void Dialog::query<const molecule *>(Parameter<const molecule *> ¶m, const char *token, std::string description) | 
|---|
| 184 | { | 
|---|
| 185 | queryMolecule(param, token, description); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | template <> void Dialog::query< std::vector<const molecule *> >(Parameter<std::vector<const molecule *> > ¶m, const char *token, std::string description) | 
|---|
| 189 | { | 
|---|
| 190 | queryMolecules(param, token, description); | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | template <> void Dialog::query<Vector>(Parameter<Vector> ¶m, const char *token, std::string description) | 
|---|
| 194 | { | 
|---|
| 195 | queryVector(param, token, description); | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | template <> void Dialog::query< std::vector<Vector> >(Parameter<std::vector<Vector> > ¶m, const char *token, std::string description) | 
|---|
| 199 | { | 
|---|
| 200 | queryVectors(param, token, description); | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | template <> void Dialog::query<RealSpaceMatrix>(Parameter<RealSpaceMatrix> ¶m, const char *token, std::string description) | 
|---|
| 204 | { | 
|---|
| 205 | queryRealSpaceMatrix(param, token, description); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | template <> void Dialog::query<const element *>(Parameter<const element *> ¶m, const char *token, std::string description) | 
|---|
| 209 | { | 
|---|
| 210 | queryElement(param, token, description); | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | template <> void Dialog::query< std::vector<const element *> >(Parameter<std::vector<const element *> > ¶m, const char *token, std::string description) | 
|---|
| 214 | { | 
|---|
| 215 | queryElements(param, token, description); | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | template <> void Dialog::query< boost::filesystem::path >(Parameter<boost::filesystem::path> ¶m, const char *token, std::string description) | 
|---|
| 219 | { | 
|---|
| 220 | queryFile(param, token, description); | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | template <> void Dialog::query< std::vector<boost::filesystem::path> >(Parameter<std::vector< boost::filesystem::path> > ¶m, const char *token, std::string description) | 
|---|
| 224 | { | 
|---|
| 225 | queryFiles(param, token, description); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | template <> void Dialog::query< RandomNumberDistribution_Parameters >(Parameter<RandomNumberDistribution_Parameters> ¶m, const char *token, std::string description) | 
|---|
| 229 | { | 
|---|
| 230 | queryRandomNumberDistribution_Parameters(param, token, description); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | /************************** Query Infrastructure ************************/ | 
|---|
| 234 | /*       ---> shifted to folder Query                                   */ | 
|---|
| 235 | /************************************************************************/ | 
|---|