00001 //# TBAction.h: Edit actions that can be done, undone, and redone. 00002 //# Copyright (C) 2005 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# $Id: $ 00027 #ifndef TBACTION_H_ 00028 #define TBACTION_H_ 00029 00030 #include <casaqt/QtBrowser/TBConstants.h> 00031 00032 #include <casa/BasicSL/String.h> 00033 00034 #include <vector> 00035 00036 namespace casa { 00037 00038 //# Forward Declarations 00039 class TBTableTabs; 00040 class TBViewArray; 00041 class TBData; 00042 00043 // <summary> 00044 // Abstract parent class of any action that can be performed. 00045 // <summary> 00046 // 00047 // <synopsis> 00048 // Any editing change to the underlying table should be encapsulated in a 00049 // TBAction. A TBAction subclass needs to be able to perform the designated 00050 // action as well as knowing how to undo it. The specific implementation of 00051 // performing the action is left up to the subclasses. 00052 // </synopsis> 00053 00054 class TBAction { 00055 public: 00056 // Constructor that takes a pointer to the origin object. The origin is 00057 // used to, for example, remove actions associated with objects that have 00058 // been closed or otherwise made unavailable. 00059 TBAction(void* origin); 00060 00061 virtual ~TBAction(); 00062 00063 00064 // Returns a pointer to the origin object. 00065 void* getOrigin(); 00066 00067 00068 // doAction() must be implemented by any subclass. This method performs 00069 // the action, updating both the backend and the GUI as necessary. 00070 virtual Result doAction() = 0; 00071 00072 // undoAction() must be implemented by any subclass. This method undoes 00073 // the action, updating both the backend and the GUI as necessary. 00074 virtual Result undoAction() = 0; 00075 00076 // name() must be implemented by any subclass. This method returns the 00077 // name of the action. The name should be human-readable and does not have 00078 // to be any specific format. 00079 virtual String name() = 0; 00080 00081 // isAssociatedWith() must be implemented by any subclass. This method 00082 // returns true if this action is associated with the given origin object, 00083 // false otherwise. If an action is associated with an object that is 00084 // then closed (like a table or an array) then the action may be removed 00085 // from the performed/undone queues. 00086 virtual bool isAssociatedWith(void* origin) = 0; 00087 00088 protected: 00089 // Pointer to origin object. 00090 void* origin; 00091 }; 00092 00093 // <summary> 00094 // Contains an list of performed actions and a list of undone actions. 00095 // <summary> 00096 // 00097 // <synopsis> 00098 // A TBActionList keep track of performed and undone actions and provides 00099 // methods to add and move actions between the two lists. The lists have a 00100 // maximum length defined by TBConstants::MAX_ACTION_BUFFER; once this limit 00101 // has been reached, old actions are discarded. 00102 // </synopsis> 00103 00104 class TBActionList { 00105 public: 00106 // Default Constructor to initialize the empty lists. 00107 TBActionList(); 00108 00109 ~TBActionList(); 00110 00111 00112 // Returns true if the performed list is empty, false otherwise. 00113 bool isEmpty(); 00114 00115 // Returns true if the undone list is empty, false otherwise. 00116 bool undoneIsEmpty(); 00117 00118 // Returns the size of the performed list. 00119 int size(); 00120 00121 // Returns the size of the undone list. 00122 int undoneSize(); 00123 00124 // Returns the name() value of the last performed action, or blank if 00125 // there is none. 00126 String lastActionName(); 00127 00128 // Returns the name() value of the last undone action, or blank if there 00129 // is none. 00130 String lastUndoneActionName(); 00131 00132 // Returns the performed action at the designated index, or NULL if the 00133 // index is invalid. 00134 TBAction* at(unsigned int i); 00135 00136 // Returns the undone action at the designated index, or NULL if the index 00137 // is invalid. 00138 TBAction* undoneAt(unsigned int i); 00139 00140 00141 // Adds the given TBAction to the performed list and calls the action's 00142 // doAction() method. 00143 Result doAction(TBAction* action); 00144 00145 // Moves the latest performed action to the undone list and calls the 00146 // action's undoAction() method. 00147 Result undoAction(); 00148 00149 // Moves the latest undone action to the performed list and calls the 00150 // action's doAction() method. 00151 Result redoAction(); 00152 00153 // Removes the given action from the performed list, but does not delete 00154 // it. Returns true if the remove was successful, false otherwise. 00155 bool remove(TBAction* a); 00156 00157 // Removes the given action from the undone list, but does not delete it. 00158 // Returns true if the remove was successful, false otherwise. 00159 bool removeUndone(TBAction* a); 00160 00161 private: 00162 // Performed actions list 00163 std::vector<TBAction*> actions; 00164 00165 // Undone actions list 00166 std::vector<TBAction*> undone; 00167 }; 00168 00169 /* Specific Actions */ 00170 00171 // <summary> 00172 // TBAction for when non-array data in the table is edited. 00173 // <summary> 00174 // 00175 // <synopsis> 00176 // A TBEditDataAction keeps track of the table, row, column, new value, and old 00177 // value. When this action is performed, the underlying table is updated to 00178 // the new value at the given row and column; when this action is undone, the 00179 // underlying table is updated to the old value. 00180 // </synopsis> 00181 00182 class TBEditDataAction : public TBAction { 00183 public: 00184 TBEditDataAction(TBTableTabs* tt, int row, int col, TBData* newVal); 00185 00186 virtual ~TBEditDataAction(); 00187 00188 00189 // Implements TBAction::doAction(). 00190 // Updates the table by calling TBTable::editData() followed by a 00191 // TBDataTab::setData() if the edit is successful. Returns the result 00192 // of the TBTable::editData() call. 00193 Result doAction(); 00194 00195 // Implements TBAction::undoAction(). 00196 // Updates the table by calling TBTable::editData() followed by a 00197 // TBDataTab::setData() if the edit is successful. Returns the result 00198 // of the TBTable::editData() call. 00199 Result undoAction(); 00200 00201 // Implements TBAction::name(). 00202 // Returns "edit [table name]([row],[col])". 00203 String name(); 00204 00205 // Implements TBAction::isAssociatedWith(). 00206 // Returns true if o is equal to the TBTableTabs object given in the 00207 // constructor, false otherwise. 00208 bool isAssociatedWith(void* o); 00209 00210 private: 00211 // Origin table. 00212 TBTableTabs* tt; 00213 00214 // Row of the edit data. 00215 int row; 00216 00217 // Column of the edit data. 00218 int col; 00219 00220 // New value. 00221 TBData* newVal; 00222 00223 // Old value. 00224 TBData* oldVal; 00225 00226 // This action's name. 00227 String actionName; 00228 00229 00230 // Updates the underlying table with the given value. 00231 Result update(TBData* val); 00232 }; 00233 00234 // <summary> 00235 // TBAction for when array data in the table is edited. 00236 // <summary> 00237 // 00238 // <synopsis> 00239 // A TBEditArrayDataAction keeps track of the table, row, column, array 00240 // coordinates, new value, and old value. When this action is performed, the 00241 // underlying table is updated to the new value at the given row, column, and 00242 // coordinates; when this action is undone, the underlying table is updated to 00243 // the old value. 00244 // </synopsis> 00245 00246 class TBEditArrayDataAction : public TBAction { 00247 public: 00248 TBEditArrayDataAction(TBTableTabs* tt, TBViewArray* array, int row, 00249 int col, std::vector<int> coord, TBData* newVal); 00250 00251 virtual ~TBEditArrayDataAction(); 00252 00253 00254 // Implements TBAction::doAction(). 00255 // Updates the table by calling TBTable::editArrayData() followed by a 00256 // TBViewArray::setDataAt() if the edit is successful. Returns the result 00257 // of the TBTable::editArrayData(). 00258 Result doAction(); 00259 00260 // Implements TBAction::undoAction(). 00261 // Updates the table by calling TBTable::editArrayData() followed by a 00262 // TBViewArray::setDataAt() if the edit is successful. Returns the result 00263 // of the TBTable::editArrayData(). 00264 Result undoAction(); 00265 00266 // Implements TBAction::name(). 00267 // Returns "edit [table name]([row],[col])[coords]". 00268 String name(); 00269 00270 // Implements TBAction::isAssociatedWith(). 00271 // Returns true if o is the TBTableTabs object or the TBViewArray object 00272 // given in the constructor, false otherwise. 00273 bool isAssociatedWith(void* o); 00274 00275 private: 00276 // Origin table. 00277 TBTableTabs* tt; 00278 00279 // Row of the edit data. 00280 int row; 00281 00282 // Column of the edit data. 00283 int col; 00284 00285 // Array coordinates of the edit data. 00286 std::vector<int> coords; 00287 00288 // New value. 00289 TBData* newVal; 00290 00291 // Old value. 00292 TBData* oldVal; 00293 00294 // This action's name. 00295 String actionName; 00296 00297 // Indicates whether the array is one-dimensional or not. 00298 bool oneDim; 00299 00300 00301 // Updates the underlying table with the given value. 00302 Result update(TBData* val); 00303 }; 00304 00305 } 00306 00307 #endif /* TBACTION_H_ */