| 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(const std::string &_title)
 | 
|---|
| 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 |   setAll();
 | 
|---|
| 71 |   return true;
 | 
|---|
| 72 | //  if(checkAll()){
 | 
|---|
| 73 | //    setAll();
 | 
|---|
| 74 | //    return true;
 | 
|---|
| 75 | //  }
 | 
|---|
| 76 | //  else{
 | 
|---|
| 77 | //    return false;
 | 
|---|
| 78 | //  }
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | void Dialog::handleAll(){
 | 
|---|
| 82 |   list<Query*>::iterator iter;
 | 
|---|
| 83 |   for(iter=queries.begin(); iter!=queries.end(); iter++){
 | 
|---|
| 84 |     try{
 | 
|---|
| 85 |       (*iter)->handle();
 | 
|---|
| 86 |     }catch(...){
 | 
|---|
| 87 |     // if any query fails (is canceled), we can end the handling process
 | 
|---|
| 88 |       ELOG(1, "The following query failed: " << (**iter).getTitle() << ".");
 | 
|---|
| 89 |       break;
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |   }
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | bool Dialog::checkAll(){
 | 
|---|
| 95 |   list<Query*>::iterator iter;
 | 
|---|
| 96 |   bool retval = true;
 | 
|---|
| 97 |   for(iter=queries.begin(); iter!=queries.end(); iter++){
 | 
|---|
| 98 |     retval &= (*iter)->isValid();
 | 
|---|
| 99 |     // if any query fails (is canceled), we can end the handling process
 | 
|---|
| 100 |     if(!retval) {
 | 
|---|
| 101 |       ELOG(1, "The following query failed: " << (**iter).getTitle() << ".");
 | 
|---|
| 102 |       break;
 | 
|---|
| 103 |     }
 | 
|---|
| 104 |   }
 | 
|---|
| 105 |   return retval;
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void Dialog::setAll(){
 | 
|---|
| 109 |   list<Query*>::iterator iter;
 | 
|---|
| 110 |   for(iter=queries.begin(); iter!=queries.end(); iter++) {
 | 
|---|
| 111 | //    try {
 | 
|---|
| 112 |       (*iter)->setResult();
 | 
|---|
| 113 | //    } catch (ParameterException &e) {
 | 
|---|
| 114 | //      if( const std::string *name=boost::get_error_info<ParameterName>(e) )
 | 
|---|
| 115 | //        ELOG(1, "The following parameter value is not valid: " << *name << ".");
 | 
|---|
| 116 | //      break;
 | 
|---|
| 117 | //    }
 | 
|---|
| 118 |   }
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | bool Dialog::hasQueries(){
 | 
|---|
| 122 |   list<Query*>::iterator iter;
 | 
|---|
| 123 |   size_t nonemptyQueries = 0;
 | 
|---|
| 124 |   for(iter=queries.begin(); iter!=queries.end(); iter++) {
 | 
|---|
| 125 |     // count all queries that not EmptyQuery
 | 
|---|
| 126 |     if (dynamic_cast<Dialog::EmptyQuery *>(*iter) == NULL)
 | 
|---|
| 127 |       ++nonemptyQueries;
 | 
|---|
| 128 |   }
 | 
|---|
| 129 |   return nonemptyQueries != 0;
 | 
|---|
| 130 | }
 | 
|---|
| 131 | 
 | 
|---|
| 132 | /*template <> void Dialog::query<Dialog::EmptyType>(Parameter<Dialog::EmptyType> ¶m, const std::string title, const std::string description)
 | 
|---|
| 133 | {
 | 
|---|
| 134 |   queryEmpty(param, title, description);
 | 
|---|
| 135 | }*/
 | 
|---|
| 136 | 
 | 
|---|
| 137 | template <> void Dialog::query<bool>(Parameter<bool> ¶m, const std::string title, const std::string description)
 | 
|---|
| 138 | {
 | 
|---|
| 139 |   queryBoolean(param, title, description);
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | template <> void Dialog::query<int>(Parameter<int> ¶m, const std::string title, const std::string description)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   queryInt(param, title, description);
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | template <> void Dialog::query< std::vector<int> >(Parameter<std::vector<int> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 148 | {
 | 
|---|
| 149 |   queryInts(param, title, description);
 | 
|---|
| 150 | }
 | 
|---|
| 151 | 
 | 
|---|
| 152 | template <> void Dialog::query<unsigned int>(Parameter<unsigned int> ¶m, const std::string title, const std::string description)
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   queryUnsignedInt(param, title, description);
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | template <> void Dialog::query< std::vector<unsigned int> >(Parameter<std::vector<unsigned int> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 158 | {
 | 
|---|
| 159 |   queryUnsignedInts(param, title, description);
 | 
|---|
| 160 | }
 | 
|---|
| 161 | 
 | 
|---|
| 162 | template <> void Dialog::query<double>(Parameter<double> ¶m, const std::string title, const std::string description)
 | 
|---|
| 163 | {
 | 
|---|
| 164 |   queryDouble(param, title, description);
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | template <> void Dialog::query< std::vector<double> >(Parameter<std::vector<double> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 168 | {
 | 
|---|
| 169 |   queryDoubles(param, title, description);
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | template <> void Dialog::query<std::string>(Parameter<std::string> ¶m, const std::string title, const std::string description)
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   queryString(param, title, description);
 | 
|---|
| 175 | }
 | 
|---|
| 176 | 
 | 
|---|
| 177 | template <> void Dialog::query< std::vector<std::string> >(Parameter<std::vector<std::string> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 178 | {
 | 
|---|
| 179 |   queryStrings(param, title, description);
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | template <> void Dialog::query<const atom *>(Parameter<const atom *> ¶m, const std::string title, const std::string description)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   queryAtom(param, title, description);
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | template <> void Dialog::query< std::vector<const atom *> >(Parameter<std::vector<const atom *> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 188 | {
 | 
|---|
| 189 |   queryAtoms(param, title, description);
 | 
|---|
| 190 | }
 | 
|---|
| 191 | 
 | 
|---|
| 192 | template <> void Dialog::query<const molecule *>(Parameter<const molecule *> ¶m, const std::string title, const std::string description)
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   queryMolecule(param, title, description);
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | template <> void Dialog::query< std::vector<const molecule *> >(Parameter<std::vector<const molecule *> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 198 | {
 | 
|---|
| 199 |   queryMolecules(param, title, description);
 | 
|---|
| 200 | }
 | 
|---|
| 201 | 
 | 
|---|
| 202 | template <> void Dialog::query<Vector>(Parameter<Vector> ¶m, const std::string title, const std::string description)
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   queryVector(param, title, description);
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | template <> void Dialog::query< std::vector<Vector> >(Parameter<std::vector<Vector> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 208 | {
 | 
|---|
| 209 |   queryVectors(param, title, description);
 | 
|---|
| 210 | }
 | 
|---|
| 211 | 
 | 
|---|
| 212 | template <> void Dialog::query<RealSpaceMatrix>(Parameter<RealSpaceMatrix> ¶m, const std::string title, const std::string description)
 | 
|---|
| 213 | {
 | 
|---|
| 214 |   queryRealSpaceMatrix(param, title, description);
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | template <> void Dialog::query<const element *>(Parameter<const element *> ¶m, const std::string title, const std::string description)
 | 
|---|
| 218 | {
 | 
|---|
| 219 |   queryElement(param, title, description);
 | 
|---|
| 220 | }
 | 
|---|
| 221 | 
 | 
|---|
| 222 | template <> void Dialog::query< std::vector<const element *> >(Parameter<std::vector<const element *> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 223 | {
 | 
|---|
| 224 |   queryElements(param, title, description);
 | 
|---|
| 225 | }
 | 
|---|
| 226 | 
 | 
|---|
| 227 | template <> void Dialog::query< boost::filesystem::path >(Parameter<boost::filesystem::path> ¶m, const std::string title, const std::string description)
 | 
|---|
| 228 | {
 | 
|---|
| 229 |   queryFile(param, title, description);
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | template <> void Dialog::query< std::vector<boost::filesystem::path> >(Parameter<std::vector< boost::filesystem::path> > ¶m, const std::string title, const std::string description)
 | 
|---|
| 233 | {
 | 
|---|
| 234 |   queryFiles(param, title, description);
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | template <> void Dialog::query< RandomNumberDistribution_Parameters >(Parameter<RandomNumberDistribution_Parameters> ¶m, const std::string title, const std::string description)
 | 
|---|
| 238 | {
 | 
|---|
| 239 |   queryRandomNumberDistribution_Parameters(param, title, description);
 | 
|---|
| 240 | }
 | 
|---|
| 241 | 
 | 
|---|
| 242 | /************************** Query Infrastructure ************************/
 | 
|---|
| 243 | /*       ---> shifted to folder Query                                   */
 | 
|---|
| 244 | /************************************************************************/
 | 
|---|