| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * builder_init.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Dec 15, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <iomanip>
 | 
|---|
| 23 | #include <iostream>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 26 | #include "Actions/ActionExceptions.hpp"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include "builder_init.hpp"
 | 
|---|
| 29 | #include "cleanUp.hpp"
 | 
|---|
| 30 | 
 | 
|---|
| 31 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "Parser/ChangeTracker.hpp"
 | 
|---|
| 36 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 39 | #include "UIElements/CommandLineUI/CommandLineParser.hpp"
 | 
|---|
| 40 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
 | 
|---|
| 41 | #ifdef USE_GUI_QT
 | 
|---|
| 42 | #include "UIElements/Qt4/QtUIFactory.hpp"
 | 
|---|
| 43 | #endif
 | 
|---|
| 44 | #include "UIElements/TextUI/TextUIFactory.hpp"
 | 
|---|
| 45 | #include "UIElements/MainWindow.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include "version.h"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | #include "World.hpp"
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /** Print some initial information output the program.
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | void ProgramHeader()
 | 
|---|
| 56 | {
 | 
|---|
| 57 |   // print version check and copyright notice
 | 
|---|
| 58 |   cout << MOLECUILDERVERSION << endl;
 | 
|---|
| 59 |   cout << "MoleCuilder comes with ABSOLUTELY NO WARRANTY; for details type" << endl;
 | 
|---|
| 60 |   cout << "`molecuilder --warranty'." << endl;
 | 
|---|
| 61 |   cout << "`MoleCuilder - to create and alter molecular systems." << endl;
 | 
|---|
| 62 |   cout << "Copyright (C) 2010  University Bonn. All rights reserved." << endl;
 | 
|---|
| 63 |   cout << endl;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | /** General stuff to intialize before UI.
 | 
|---|
| 67 |  *
 | 
|---|
| 68 |  */
 | 
|---|
| 69 | void initGeneral()
 | 
|---|
| 70 | {
 | 
|---|
| 71 |   // while we are non interactive, we want to abort from asserts
 | 
|---|
| 72 |   ASSERT_DO(Assert::Abort);
 | 
|---|
| 73 |   ASSERT_HOOK(dumpMemory);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   ProgramHeader();
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   setVerbosity(1);
 | 
|---|
| 78 |   // need to init the history before any action is created
 | 
|---|
| 79 |   MoleCuilder::ActionHistory::init();
 | 
|---|
| 80 | 
 | 
|---|
| 81 |   // from this moment on, we need to be sure to deeinitialize in the correct order
 | 
|---|
| 82 |   // this is handled by the cleanup function
 | 
|---|
| 83 |   atexit(cleanUp);
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | /** Initialize specific UIFactory.
 | 
|---|
| 87 |  *
 | 
|---|
| 88 |  * @param argc argument count
 | 
|---|
| 89 |  * @param argv argument array
 | 
|---|
| 90 |  */
 | 
|---|
| 91 | void initUI(int argc, char **argv)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   std::string BondGraphFileName("\n");
 | 
|---|
| 94 |   // Parse command line options and if present create respective UI
 | 
|---|
| 95 |   // construct bond graph
 | 
|---|
| 96 |   if (boost::filesystem::exists(BondGraphFileName)) {
 | 
|---|
| 97 |     std::ifstream input(BondGraphFileName.c_str());
 | 
|---|
| 98 |     if ((input.good()) && (World::getInstance().getBondGraph()->LoadBondLengthTable(input))) {
 | 
|---|
| 99 |       LOG(0, "Bond length table loaded successfully.");
 | 
|---|
| 100 |     } else {
 | 
|---|
| 101 |       ELOG(1, "Bond length table loading failed.");
 | 
|---|
| 102 |     }
 | 
|---|
| 103 |     input.close();
 | 
|---|
| 104 |   }
 | 
|---|
| 105 |   // handle remaining arguments by CommandLineParser
 | 
|---|
| 106 |   if (argc>1) {
 | 
|---|
| 107 |     LOG(0, "Setting UI to CommandLine.");
 | 
|---|
| 108 |     CommandLineParser::getInstance().InitializeCommandArguments();
 | 
|---|
| 109 |     CommandLineParser::getInstance().Run(argc,argv);
 | 
|---|
| 110 |     UIFactory::registerFactory(new CommandLineUIFactory::description());
 | 
|---|
| 111 |     UIFactory::makeUserInterface("CommandLine");
 | 
|---|
| 112 |   } else {
 | 
|---|
| 113 |     // In the interactive mode, we can leave the user the choice in case of error
 | 
|---|
| 114 |     ASSERT_DO(Assert::Ask);
 | 
|---|
| 115 |     #ifdef USE_GUI_QT
 | 
|---|
| 116 |       LOG(0, "Setting UI to Qt4.");
 | 
|---|
| 117 |       UIFactory::registerFactory(new QtUIFactory::description());
 | 
|---|
| 118 |       UIFactory::makeUserInterface("Qt4");
 | 
|---|
| 119 |     #else
 | 
|---|
| 120 |       LOG(0, "Setting UI to Text.");
 | 
|---|
| 121 |       cout << MOLECUILDERVERSION << endl;
 | 
|---|
| 122 |       UIFactory::registerFactory(new TextUIFactory::description());
 | 
|---|
| 123 |       UIFactory::makeUserInterface("Text");
 | 
|---|
| 124 |     #endif
 | 
|---|
| 125 |   }
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | /** Create MainWindow and displays.
 | 
|---|
| 129 |  * I.e. here all the Actions are parsed and done.
 | 
|---|
| 130 |  */
 | 
|---|
| 131 | void doUI()
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   MainWindow *mainWindow = UIFactory::getInstance().makeMainWindow();
 | 
|---|
| 134 |   try {
 | 
|---|
| 135 |     mainWindow->display();
 | 
|---|
| 136 |   } catch(ActionFailureException &e) {
 | 
|---|
| 137 |     std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
 | 
|---|
| 138 |     World::getInstance().setExitFlag(5);
 | 
|---|
| 139 |   }
 | 
|---|
| 140 |   delete mainWindow;
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|