| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * VerboseAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 9, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "Actions/CmdAction/VerboseAction.hpp"
 | 
|---|
| 23 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 24 | #include "Helpers/Log.hpp"
 | 
|---|
| 25 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | #include <iostream>
 | 
|---|
| 28 | #include <string>
 | 
|---|
| 29 | 
 | 
|---|
| 30 | using namespace std;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include "UIElements/UIFactory.hpp"
 | 
|---|
| 33 | #include "UIElements/Dialog.hpp"
 | 
|---|
| 34 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| 35 | 
 | 
|---|
| 36 | // memento to remember the state when undoing
 | 
|---|
| 37 | 
 | 
|---|
| 38 | class CommandLineVerboseState : public ActionState {
 | 
|---|
| 39 | public:
 | 
|---|
| 40 |   CommandLineVerboseState(const int _oldverbosity, const int _newverbosity) :
 | 
|---|
| 41 |     oldverbosity(_oldverbosity),
 | 
|---|
| 42 |     newverbosity(_newverbosity)
 | 
|---|
| 43 |   {}
 | 
|---|
| 44 |   int oldverbosity;
 | 
|---|
| 45 |   int newverbosity;
 | 
|---|
| 46 | };
 | 
|---|
| 47 | 
 | 
|---|
| 48 | 
 | 
|---|
| 49 | const char CommandLineVerboseAction::NAME[] = "verbose";
 | 
|---|
| 50 | 
 | 
|---|
| 51 | CommandLineVerboseAction::CommandLineVerboseAction() :
 | 
|---|
| 52 |   Action(NAME)
 | 
|---|
| 53 | {}
 | 
|---|
| 54 | 
 | 
|---|
| 55 | CommandLineVerboseAction::~CommandLineVerboseAction()
 | 
|---|
| 56 | {}
 | 
|---|
| 57 | 
 | 
|---|
| 58 | void CommandVerbose(int verbosity) {
 | 
|---|
| 59 |   ValueStorage::getInstance().setCurrentValue(CommandLineVerboseAction::NAME, verbosity);
 | 
|---|
| 60 |   ActionRegistry::getInstance().getActionByName(CommandLineVerboseAction::NAME)->call(Action::NonInteractive);
 | 
|---|
| 61 | };
 | 
|---|
| 62 | 
 | 
|---|
| 63 | Dialog* CommandLineVerboseAction::fillDialog(Dialog *dialog) {
 | 
|---|
| 64 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   dialog->queryInt(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   return dialog;
 | 
|---|
| 69 | }
 | 
|---|
| 70 | 
 | 
|---|
| 71 | Action::state_ptr CommandLineVerboseAction::performCall() {
 | 
|---|
| 72 |   int oldverbosity = getVerbosity();
 | 
|---|
| 73 |   int newverbosity = 2;
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   ValueStorage::getInstance().queryCurrentValue(NAME, newverbosity);
 | 
|---|
| 76 | 
 | 
|---|
| 77 |   if (oldverbosity != newverbosity) {
 | 
|---|
| 78 |     CommandLineVerboseState *UndoState = new CommandLineVerboseState(oldverbosity, newverbosity);
 | 
|---|
| 79 |     setVerbosity(newverbosity);
 | 
|---|
| 80 |     DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << oldverbosity << " to " << newverbosity << "." << endl);
 | 
|---|
| 81 |     return Action::state_ptr(UndoState);
 | 
|---|
| 82 |   } else {
 | 
|---|
| 83 |     DoLog(0) && (Log() << Verbose(0) << "Verbosity remains unchanged at " << oldverbosity << "." << endl);
 | 
|---|
| 84 |     return Action::failure;
 | 
|---|
| 85 |   }
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | Action::state_ptr CommandLineVerboseAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 89 |   CommandLineVerboseState *state = assert_cast<CommandLineVerboseState*>(_state.get());
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << state->newverbosity << " to " << state->oldverbosity << "." << endl);
 | 
|---|
| 92 |   setVerbosity(state->oldverbosity);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   return Action::state_ptr(_state);
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | Action::state_ptr CommandLineVerboseAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 98 |   CommandLineVerboseState *state = assert_cast<CommandLineVerboseState*>(_state.get());
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   DoLog(0) && (Log() << Verbose(0) << "Setting verbosity from " << state->oldverbosity << " to " << state->newverbosity << "." << endl);
 | 
|---|
| 101 |   setVerbosity(state->newverbosity);
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   return Action::state_ptr(_state);
 | 
|---|
| 104 | }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | bool CommandLineVerboseAction::canUndo() {
 | 
|---|
| 107 |   return true;
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | bool CommandLineVerboseAction::shouldUndo() {
 | 
|---|
| 111 |   return true;
 | 
|---|
| 112 | }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | const string CommandLineVerboseAction::getName() {
 | 
|---|
| 115 |   return NAME;
 | 
|---|
| 116 | }
 | 
|---|