CommandHistory.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 // standard include
00028 #include <config.h>
00029 #include "panoinc_WX.h"
00030 #include "panoinc.h"
00031 #include "hugin/CommandHistory.h"
00032 
00033 #include "hugin/config_defaults.h"
00034 
00035 
00036 CommandHistory::CommandHistory()
00037     : nextCmd(0)
00038 {
00039 }
00040 
00041 CommandHistory::~CommandHistory()
00042 {
00043     std::vector<Command*>::iterator it;
00044     for (it = commands.begin(); it != commands.end(); ++it) {
00045         delete *it;
00046     }
00047 }
00048 
00049 
00050 void CommandHistory::clear()
00051 {
00052     std::vector<Command*>::iterator it;
00053     for (it = commands.begin(); it != commands.end(); ++it) {
00054         delete *it;
00055     }
00056     commands.clear();
00057     nextCmd=0;
00058 }
00059 
00060 
00061 
00062 
00063 void CommandHistory::addCommand(Command *command, bool execute)
00064 {
00065     assert(command);
00066 
00067     if (nextCmd > commands.size()) {
00068         DEBUG_FATAL("Invalid state in Command History: nextCmd:" << nextCmd
00069                     << " size:" << commands.size());
00070     } else if (nextCmd < (commands.size())) {
00071         // case: there were redoable commands, remove them now, the
00072         // current command has invalidated them.
00073         size_t nrDelete = commands.size()  - nextCmd;
00074         for (size_t i=0; i < nrDelete; i++) {
00075             delete commands.back();
00076             commands.pop_back();
00077         }
00078     }
00079     commands.push_back(command);
00080     nextCmd++;
00081 
00082     if (execute) {
00083         // execute command
00084         command->execute();
00085     }
00086 
00087 }
00088 
00089 
00090 
00091 void CommandHistory::undo()
00092 {
00093     if (nextCmd > 0) {
00094         // undo the current command
00095         DEBUG_DEBUG("undo: " << commands[nextCmd-1]->getName());
00096         // change nextCmd before the panorama, so panorama changed listeners get
00097         // correct results from canUndo() and canRedo().
00098         nextCmd--;
00099         commands[nextCmd]->undo();
00100         
00101         // smart undo: keep undoing simple visibility toggles according to user preference
00102         bool t = (wxConfigBase::Get()->Read(wxT("smartUndo"), HUGIN_SMART_UNDO) != 0); 
00103         if(t){
00104             while ( (commands[nextCmd]->getName()=="change active images") && (nextCmd > 0) ) {
00105                 commands[nextCmd-1]->undo();
00106                 nextCmd--;
00107             }
00108         }
00109         // TODO: reestablish visibility based on preferences
00110     } else {
00111         DEBUG_ERROR("no command in undo history");
00112     }
00113 }
00114 
00115 
00116 void CommandHistory::redo()
00117 {
00118     if (nextCmd < commands.size()) {
00119         DEBUG_DEBUG("redo: " << commands[nextCmd]->getName());
00120         nextCmd++;
00121         commands[nextCmd - 1]->execute();
00122         // smart redo: keep redoing simple visibility toggles according to user preference
00123         bool t = (wxConfigBase::Get()->Read(wxT("smartUndo"), HUGIN_SMART_UNDO) != 0); 
00124         if(t){
00125             while ( (nextCmd < commands.size()) && (commands[nextCmd]->getName()=="change active images") ) {
00126                 commands[nextCmd]->execute();
00127                 nextCmd++;
00128             }
00129         }
00130         // TODO: reestablish visibility based on preferences
00131     } else {
00132         DEBUG_ERROR("no command in redo history");
00133     }
00134 }
00135 
00136 bool CommandHistory::canUndo()
00137 {
00138     return nextCmd > 0;
00139 }
00140 
00141 bool CommandHistory::canRedo()
00142 {
00143     return nextCmd < commands.size();
00144 }
00145 
00146 // ======================================================================
00147 // ======================================================================
00148 
00149 
00150 GlobalCmdHist * GlobalCmdHist::instance = 0;
00151 
00152 GlobalCmdHist::GlobalCmdHist()
00153 {
00154 
00155 }
00156 
00157 GlobalCmdHist & GlobalCmdHist::getInstance()
00158 {
00159     if (!instance) {
00160         instance = new GlobalCmdHist();
00161     }
00162     return *instance;
00163 }

Generated on Wed Jul 16 01:25:30 2014 for Hugintrunk by  doxygen 1.3.9.1