| [f5a86a] | 1 | /* | 
|---|
|  | 2 | * Dialog.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Jan 5, 2010 | 
|---|
|  | 5 | *      Author: crueger | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef DIALOG_HPP_ | 
|---|
|  | 9 | #define DIALOG_HPP_ | 
|---|
|  | 10 |  | 
|---|
| [56f73b] | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 |  | 
|---|
| [f5a86a] | 17 | #include<string> | 
|---|
|  | 18 | #include<list> | 
|---|
| [104524] | 19 | #include<vector> | 
|---|
| [f5a86a] | 20 |  | 
|---|
| [6f5dfe] | 21 | #include <boost/filesystem.hpp> | 
|---|
| [7d9416] | 22 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| [57f243] | 23 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [f10b0c] | 24 | #include "Parameters/Parameter.hpp" | 
|---|
| [33e801] | 25 | #include "Parameters/Specifics/KeyValuePair.hpp" | 
|---|
| [7cd6e7] | 26 |  | 
|---|
| [97ebf8] | 27 | class atom; | 
|---|
| [7d9416] | 28 | class RealSpaceMatrix; | 
|---|
| [97ebf8] | 29 | class element; | 
|---|
| [7aa000] | 30 | class molecule; | 
|---|
| [45f5d6] | 31 |  | 
|---|
| [de8e45] | 32 | /** Dialog is one of the two main classes of the UIFactory base class. | 
|---|
|  | 33 | * | 
|---|
| [db7cb0] | 34 | * The Dialog is meant for asking the user for information needed to perform | 
|---|
|  | 35 | * actions he desires, such as asking for a position in space or a length. | 
|---|
| [de8e45] | 36 | * | 
|---|
| [db7cb0] | 37 | * For this purpose there is the base class Query and numerous specializations | 
|---|
|  | 38 | * for each of the types to be asked. There are primitives integer, doubles and | 
|---|
|  | 39 | * string, but also advanced types such as element, molecule or Vector. There | 
|---|
|  | 40 | * is also an empty query for displaying text. | 
|---|
| [e4afb4] | 41 | * | 
|---|
|  | 42 | * Note that the templatization of Dialog::query() allows for easy implementation | 
|---|
|  | 43 | * of new types that correspond to/are derived from old ones. | 
|---|
|  | 44 | * | 
|---|
|  | 45 | * <H3>How do Dialogs operate?</H3> | 
|---|
|  | 46 | * | 
|---|
|  | 47 | * Dialogs are initiated by Action::FillDialog() function calls. Within Action::Call() | 
|---|
|  | 48 | * a dialog is created and passed on to FillDialog(), which is specialized in each | 
|---|
|  | 49 | * specific Action to ask for the specific parameter it needs. | 
|---|
|  | 50 | * | 
|---|
|  | 51 | * Dialogs are derived for each of the UI types | 
|---|
|  | 52 | *  -# CommandLineDialog | 
|---|
|  | 53 | *  -# QtDialog | 
|---|
|  | 54 | *  -# TextDialog | 
|---|
|  | 55 | * | 
|---|
|  | 56 | * This "asking" for parameters is done via the Query class.  There are four types | 
|---|
|  | 57 | * of Query types: | 
|---|
|  | 58 | *  -# Query, members in class Dialog | 
|---|
|  | 59 | *  -# CommandLineQuery, members in class CommandLineDialog | 
|---|
|  | 60 | *  -# QtQuery, members in class QtDialog | 
|---|
|  | 61 | *  -# TextQuery, members in class TextDialog | 
|---|
|  | 62 | * Each embodies a certain way of asking the user for the specific type of parameter | 
|---|
|  | 63 | * needed, e.g. a file via the TextMenu interface would be done in member functions of | 
|---|
|  | 64 | * TextDialog::FileTextQuery. | 
|---|
|  | 65 | * | 
|---|
|  | 66 | * | 
|---|
|  | 67 | * Generally, the sequence of events is as follows: | 
|---|
|  | 68 | *  -# Action::fillDialog() calls upon Dialog::query<T> for some type T, let's say T | 
|---|
|  | 69 | *     stands for double | 
|---|
|  | 70 | *  -# Dialog::query<double> call a function queryDouble() | 
|---|
|  | 71 | *  -# depending on the currently used UIFactory, the Dialog above is actually either | 
|---|
|  | 72 | *     of the three specialized types, let's say CommandLine. And we see that within | 
|---|
|  | 73 | *     CommandLineDialog we have the respective method ueryDouble() that registers | 
|---|
|  | 74 | *     a new instance of the class CommandLineDialog::DoubleCommandLineQuery. | 
|---|
|  | 75 | *  -# The Query's are first registered, as multiple parameters may be needed for | 
|---|
|  | 76 | *     a single Action and we don't want the popping up of a dialog window for each | 
|---|
|  | 77 | *     alone. Rather, we want to merge the Query's into a single Dialog. Therefore, | 
|---|
|  | 78 | *     they are first registered and then executed in sequence. This is done in | 
|---|
|  | 79 | *     Dialog::display(), i.e. when the dialog is finally shown to the user. | 
|---|
|  | 80 | *  -# Then each of the registered Query's, here our CommandLineDialog:: | 
|---|
|  | 81 | *     DoubleCommandLineQuery, constructor is called. | 
|---|
|  | 82 | *     -# This will call the constructor of its base class, where the actual value | 
|---|
|  | 83 | *        is stored and later stored into the ValueStorage class by | 
|---|
|  | 84 | *        Dialog::Query::setResult(). | 
|---|
|  | 85 | *     -# Also, e.g. for the Qt interface, the buttons, labels and so forth are | 
|---|
|  | 86 | *        created and added to the dialog. | 
|---|
|  | 87 | *  -# Now the users enters information for each UI something different happens: | 
|---|
|  | 88 | *    -# CommandLine: The actual value retrieval is done by the CommandLineParser and | 
|---|
|  | 89 | *       the boost::program_options library, the value is stored therein for the moment. | 
|---|
|  | 90 | *       (see here: http://www.boost.org/doc/libs/1_44_0/doc/html/program_options/) | 
|---|
|  | 91 | *    -# TextMenu: The value is requested via std::cin from the user. | 
|---|
|  | 92 | *    -# QtMenu: The users enters the value in a Dialog. Due to Qt necessities a | 
|---|
|  | 93 | *       Pipe class obtains the value from the Dialog with Qt's signal/slot mechanism | 
|---|
|  | 94 | *       and put it into Dialog::...Query value. | 
|---|
|  | 95 | *  -# in our case DoubleCommandLineQuery::handle() will be called. The value is | 
|---|
|  | 96 | *     retrieved and put into Dialog::DoubleQuery::tmp | 
|---|
|  | 97 | *  -# Finally, for each Query, also Dialog::DoubleQuery, setResult() is called which | 
|---|
|  | 98 | *     puts the value as a string into the ValueStorage under a given token that is | 
|---|
|  | 99 | *     associated with a type (and thereby we assure type-safety). | 
|---|
|  | 100 | * | 
|---|
|  | 101 | * <H3>Regarding derived types of types with already present queries</H3> | 
|---|
|  | 102 | * | 
|---|
|  | 103 | * Example: We have got a derived Vector class, called BoxVector, that is by any means | 
|---|
|  | 104 | * a Vector but with one difference: it is always bound to lie within the current domain. | 
|---|
|  | 105 | * With regards to type-casting it to and from a string however, nothing is different | 
|---|
|  | 106 | * between Vector and BoxVector. | 
|---|
|  | 107 | * | 
|---|
|  | 108 | * So, do we need a new Query for this? | 
|---|
|  | 109 | * No. | 
|---|
|  | 110 | * | 
|---|
|  | 111 | * We just have to do this: | 
|---|
|  | 112 | *  -# add a specialization of Dialog::query<BoxVector> where queryVector()is used. | 
|---|
|  | 113 | *     @code | 
|---|
| [f130d4] | 114 | *     template <> void Dialog::query<BoxVector>(const std::string &title, const std::string &description) { | 
|---|
|  | 115 | *        queryVector(title, description); | 
|---|
| [e4afb4] | 116 | *     } | 
|---|
|  | 117 | *     @endcode | 
|---|
|  | 118 | *  -# make sure that | 
|---|
|  | 119 | *     -# ValueStorage::setCurrentValue() the specialization for class Vector has an | 
|---|
|  | 120 | *     if case also for BoxVector and does something appropriate. | 
|---|
|  | 121 | *     -# ValueStorage::queryCurrentValue() has another specialization doing the same | 
|---|
|  | 122 | *     as for Vector but for BoxVector in its signature. | 
|---|
|  | 123 | * | 
|---|
|  | 124 | * And that's all. | 
|---|
|  | 125 | * | 
|---|
|  | 126 | * | 
|---|
|  | 127 | * <H3>Adding new queries</H3> | 
|---|
|  | 128 | * | 
|---|
|  | 129 | * Check first whether you really need a new query or whether we can derive it and re-use | 
|---|
|  | 130 | * one of the present ones. | 
|---|
|  | 131 | * | 
|---|
|  | 132 | * Due to the three present UI interfaces we have to add a specific Query for each, here | 
|---|
|  | 133 | * is a list: | 
|---|
| [37a67f] | 134 | *   -# add a token (i.e. a unique name for the query parameter) and its type to the | 
|---|
|  | 135 | *      global list in \ref GlobalListOfParameterQueries.hpp. This will via boost | 
|---|
|  | 136 | *      preprocessor magic add definitions and some intermediatr template specializations. | 
|---|
|  | 137 | *   -# add a specialization for each of the UI interfaces of Dialog::...Query class. | 
|---|
|  | 138 | *   -# add class declaration of QtDialog::...Query in \ref QtQuery.hpp | 
|---|
| [e4afb4] | 139 | *   -# add CommandLineDialog::...Query, TextDialog::...Query, QtDialog::...Query | 
|---|
| [37a67f] | 140 | *   -# TypeEnumContainer add new type to query. Make the Type name match with the token | 
|---|
| [0275ad] | 141 | *   -# CommandLineParser::AddOptionToParser() add new type to query | 
|---|
|  | 142 | *   -# CommandLineParser_valdiates.[ch]pp: If given above as a new type | 
|---|
|  | 143 | *      program_options::value, define and implement a validate() function here. | 
|---|
| [e4afb4] | 144 | * | 
|---|
| [37a67f] | 145 | * Note that the Empty..Query is always specificly present as it has not type and thus | 
|---|
|  | 146 | * does not fit into the preprocessor magic scheme (on the plus side it also serves | 
|---|
|  | 147 | * as a kind of visualization of what the preprocessor actually does). | 
|---|
|  | 148 | * | 
|---|
| [de8e45] | 149 | */ | 
|---|
| [f5a86a] | 150 | class Dialog | 
|---|
|  | 151 | { | 
|---|
|  | 152 | public: | 
|---|
| [163110] | 153 | Dialog(const std::string &_title); | 
|---|
| [f5a86a] | 154 | virtual ~Dialog(); | 
|---|
|  | 155 |  | 
|---|
| [f130d4] | 156 | template <class T> void query(Parameter<T> &, const std::string ="", const std::string = ""); | 
|---|
| [9ee38b] | 157 |  | 
|---|
| [f130d4] | 158 | virtual void queryEmpty(const std::string ="", const std::string = "")=0; | 
|---|
| [37a67f] | 159 |  | 
|---|
|  | 160 | /** With the following boost::preprocessor code we generate virtual function | 
|---|
|  | 161 | * definitions for all desired query types in the abstract class Dialog. | 
|---|
|  | 162 | */ | 
|---|
|  | 163 | #include "UIElements/GlobalListOfParameterQueries.hpp" | 
|---|
|  | 164 | #include "UIElements/Dialog_impl_pre.hpp" | 
|---|
|  | 165 |  | 
|---|
|  | 166 | // iterate over all parameter query types | 
|---|
|  | 167 | #if defined GLOBALLISTOFPARAMETERQUERIES_Token && defined GLOBALLISTOFPARAMETERQUERIES_Type | 
|---|
|  | 168 | #define SUFFIX =0 | 
|---|
|  | 169 | #define BOOST_PP_LOCAL_MACRO(n) dialog_declaration(~, n, GLOBALLISTOFPARAMETERQUERIES_Token, GLOBALLISTOFPARAMETERQUERIES_Type) | 
|---|
|  | 170 | #define BOOST_PP_LOCAL_LIMITS  (0, MAXPARAMETERTOKENS-1) | 
|---|
|  | 171 | #include BOOST_PP_LOCAL_ITERATE() | 
|---|
|  | 172 | #undef dialog_declaration | 
|---|
|  | 173 | #undef SUFFIX | 
|---|
|  | 174 | #endif | 
|---|
|  | 175 |  | 
|---|
|  | 176 | #include "Dialog_impl_undef.hpp" | 
|---|
|  | 177 | /* End of preprocessor code piece */ | 
|---|
| [f5a86a] | 178 |  | 
|---|
| [45f5d6] | 179 | virtual bool display(); | 
|---|
| [f5a86a] | 180 |  | 
|---|
| [852ea3] | 181 | virtual void handleAll(); | 
|---|
| [d3a5ea] | 182 | virtual bool checkAll(); | 
|---|
|  | 183 | virtual void setAll(); | 
|---|
|  | 184 |  | 
|---|
| [c508ef5] | 185 | virtual bool hasQueries(); | 
|---|
|  | 186 |  | 
|---|
| [7dfd07] | 187 | virtual void update(){} | 
|---|
|  | 188 |  | 
|---|
| [87d6bd] | 189 | public: | 
|---|
| [f5a86a] | 190 | // methodology for handling queries | 
|---|
|  | 191 | // all queries are stored and then performed at appropriate times | 
|---|
|  | 192 |  | 
|---|
|  | 193 | //these queries can be handled by this dialog | 
|---|
| [45f5d6] | 194 |  | 
|---|
| [db7cb0] | 195 | /** Base class for all queries. | 
|---|
|  | 196 | * | 
|---|
| [b2c302] | 197 | * A query is request to the user for a value of a specific type. | 
|---|
|  | 198 | * E.g. All \ref Action's need parameters to perform a specific function. | 
|---|
|  | 199 | * These are obtained from the user at run-time via a Query regardless | 
|---|
|  | 200 | * of the interface that the user is using. | 
|---|
| [db7cb0] | 201 | * | 
|---|
| [7ef5b9a] | 202 | * A Query just has title and description and serves as the general interface | 
|---|
|  | 203 | * to queries. TQuery is a templatization of the Query containing a protected | 
|---|
|  | 204 | * member variable of the specific type and also a Parameter<> reference of | 
|---|
|  | 205 | * the type that actually belongs to the Action that triggered/created the | 
|---|
|  | 206 | * Query. handle() is UI-specific and sets the (temporary) member variable. | 
|---|
|  | 207 | * However, isValid() in TQuery checks via the Parameter<> reference whether | 
|---|
|  | 208 | * the variable is valid with the given Validators and setResult() finally | 
|---|
|  | 209 | * set the Parameter with the temporary variable. | 
|---|
|  | 210 | * | 
|---|
|  | 211 | * For each type there is a derived class per \b UI, e.g. for the | 
|---|
|  | 212 | * \link userinterfaces-textmenu textmenu \endlink we have | 
|---|
|  | 213 | * \ref BooleanTextQuery that derives from \ref TQuery<bool>. This derived | 
|---|
|  | 214 | * class has to implement the Query::handle() function that actually sets | 
|---|
|  | 215 | * the protected member variable to something the user has entered. | 
|---|
|  | 216 | * | 
|---|
|  | 217 | * Thanks to the templated TQuery this is a as simple as it can get. The | 
|---|
|  | 218 | * handle() has to be UI-specific, hence has to be implemented once for | 
|---|
|  | 219 | * every present UI. But all other code can be used for either of them. | 
|---|
| [b2c302] | 220 | * | 
|---|
|  | 221 | * \section query-howto How to add another query? | 
|---|
| [db7cb0] | 222 | * | 
|---|
|  | 223 | * Let's say  we want to query for a type called \a Value. | 
|---|
|  | 224 | * | 
|---|
|  | 225 | * Then, we do the following: | 
|---|
| [7ef5b9a] | 226 | *  -# add a virtual function queryValue inside class Dialog. | 
|---|
|  | 227 | *  -# now, for each of the GUIs we basically have to add a sub-class for the | 
|---|
|  | 228 | *     respective Query inside the derived Dialog that implements handle(). | 
|---|
|  | 229 | *    -# QT: typically we use a onUpdate() function here attached the Qt | 
|---|
|  | 230 | *       signals and handle then just does nothing. | 
|---|
| [db7cb0] | 231 | *    -# CommandLine: nothing special, handle() imports value from \a | 
|---|
|  | 232 | *       CommandLineParser and sets the tmp variable. | 
|---|
|  | 233 | *    -# Text: nothing special, handle() queries the user and sets the tmp | 
|---|
|  | 234 | *       variable | 
|---|
|  | 235 | */ | 
|---|
| [45f5d6] | 236 | class Query { | 
|---|
| [94d131] | 237 | friend class Dialog; | 
|---|
| [45f5d6] | 238 | public: | 
|---|
| [f130d4] | 239 | Query(const std::string _title, const std::string _description = ""); | 
|---|
| [5a7243] | 240 | virtual ~Query(); | 
|---|
| [45f5d6] | 241 | virtual bool handle()=0; | 
|---|
| [852ea3] | 242 | virtual bool isValid()=0; | 
|---|
| [45f5d6] | 243 | virtual void setResult()=0; | 
|---|
|  | 244 | protected: | 
|---|
|  | 245 | const std::string getTitle() const; | 
|---|
| [a2ab15] | 246 | const std::string getDescription() const; | 
|---|
| [45f5d6] | 247 | private: | 
|---|
| [f130d4] | 248 | const std::string title;  //!< short title of the query | 
|---|
|  | 249 | const std::string description; //!< longer description for tooltips or for help | 
|---|
| [f5a86a] | 250 | }; | 
|---|
|  | 251 |  | 
|---|
| [6f5dfe] | 252 |  | 
|---|
| [1c55b8] | 253 | template<class T> | 
|---|
|  | 254 | class TQuery : public Query { | 
|---|
| [2c5765] | 255 | public: | 
|---|
| [f130d4] | 256 | TQuery(Parameter<T> &_param, const std::string title, const std::string _description = "") : | 
|---|
| [1c55b8] | 257 | Query(title, _description), param(_param) {} | 
|---|
|  | 258 | virtual ~TQuery(){} | 
|---|
| [2c5765] | 259 | virtual bool handle()=0; | 
|---|
| [852ea3] | 260 | virtual bool isValid(){ return param.isValid(temp);  } | 
|---|
|  | 261 | virtual void setResult(){ param.set(temp);  } | 
|---|
| [2c5765] | 262 | protected: | 
|---|
| [1c55b8] | 263 | T temp; | 
|---|
|  | 264 | Parameter<T> ¶m; | 
|---|
| [2c5765] | 265 | }; | 
|---|
|  | 266 |  | 
|---|
| [1c55b8] | 267 | // Empty Query is just meant for showing text, such as version, help, initial message or alike | 
|---|
|  | 268 | class EmptyQuery : public Query { | 
|---|
| [0275ad] | 269 | public: | 
|---|
| [f130d4] | 270 | EmptyQuery(const std::string title, const std::string _description = ""); | 
|---|
| [1c55b8] | 271 | virtual ~EmptyQuery(); | 
|---|
| [0275ad] | 272 | virtual bool handle()=0; | 
|---|
| [852ea3] | 273 | virtual bool isValid(){ return true;  } | 
|---|
| [0275ad] | 274 | virtual void setResult(); | 
|---|
|  | 275 | }; | 
|---|
|  | 276 |  | 
|---|
| [45f5d6] | 277 | void registerQuery(Query* query); | 
|---|
|  | 278 |  | 
|---|
|  | 279 | private: | 
|---|
|  | 280 | std::list<Query*> queries; | 
|---|
| [f5a86a] | 281 |  | 
|---|
|  | 282 | }; | 
|---|
|  | 283 |  | 
|---|
|  | 284 | #endif /* DIALOG_HPP_ */ | 
|---|