CommandHistory.cpp

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

Generated on 25 Apr 2018 for Hugintrunk by  doxygen 1.4.7