casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TBAction.h
Go to the documentation of this file.
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 #include <casa/namespace.h>
00037 using namespace std;
00038 
00039 namespace casa {
00040 
00041 //# Forward Declarations
00042 class TBTableTabs;
00043 class TBViewArray;
00044 class TBData;
00045 
00046 // <summary>
00047 // Abstract parent class of any action that can be performed.
00048 // <summary>
00049 //
00050 // <synopsis>
00051 // Any editing change to the underlying table should be encapsulated in a
00052 // TBAction.  A TBAction subclass needs to be able to perform the designated
00053 // action as well as knowing how to undo it.  The specific implementation of
00054 // performing the action is left up to the subclasses.
00055 // </synopsis>
00056 
00057 class TBAction {
00058 public:
00059     // Constructor that takes a pointer to the origin object.  The origin is
00060     // used to, for example, remove actions associated with objects that have
00061     // been closed or otherwise made unavailable.
00062     TBAction(void* origin);
00063 
00064     virtual ~TBAction();
00065 
00066     
00067     // Returns a pointer to the origin object.
00068     void* getOrigin();
00069 
00070     
00071     // doAction() must be implemented by any subclass.  This method performs
00072     // the action, updating both the backend and the GUI as necessary.
00073     virtual Result doAction() = 0;
00074 
00075     // undoAction() must be implemented by any subclass.  This method undoes
00076     // the action, updating both the backend and the GUI as necessary.
00077     virtual Result undoAction() = 0;
00078 
00079     // name() must be implemented by any subclass.  This method returns the
00080     // name of the action.  The name should be human-readable and does not have
00081     // to be any specific format.
00082     virtual String name() = 0;
00083 
00084     // isAssociatedWith() must be implemented by any subclass.  This method
00085     // returns true if this action is associated with the given origin object,
00086     // false otherwise.  If an action is associated with an object that is
00087     // then closed (like a table or an array) then the action may be removed
00088     // from the performed/undone queues.
00089     virtual bool isAssociatedWith(void* origin) = 0;
00090 
00091 protected:
00092     // Pointer to origin object.
00093     void* origin;
00094 };
00095 
00096 // <summary>
00097 // Contains an list of performed actions and a list of undone actions.
00098 // <summary>
00099 //
00100 // <synopsis>
00101 // A TBActionList keep track of performed and undone actions and provides
00102 // methods to add and move actions between the two lists.  The lists have a
00103 // maximum length defined by TBConstants::MAX_ACTION_BUFFER; once this limit
00104 // has been reached, old actions are discarded.
00105 // </synopsis>
00106 
00107 class TBActionList {
00108 public:
00109     // Default Constructor to initialize the empty lists.
00110     TBActionList();
00111 
00112     ~TBActionList();
00113 
00114     
00115     // Returns true if the performed list is empty, false otherwise.
00116     bool isEmpty();
00117 
00118     // Returns true if the undone list is empty, false otherwise.
00119     bool undoneIsEmpty();
00120 
00121     // Returns the size of the performed list.
00122     int size();
00123 
00124     // Returns the size of the undone list.
00125     int undoneSize();
00126 
00127     // Returns the name() value of the last performed action, or blank if
00128     // there is none.
00129     String lastActionName();
00130 
00131     // Returns the name() value of the last undone action, or blank if there
00132     // is none.
00133     String lastUndoneActionName();
00134 
00135     // Returns the performed action at the designated index, or NULL if the
00136     // index is invalid.
00137     TBAction* at(unsigned int i);
00138 
00139     // Returns the undone action at the designated index, or NULL if the index
00140     // is invalid.
00141     TBAction* undoneAt(unsigned int i);
00142     
00143     
00144     // Adds the given TBAction to the performed list and calls the action's
00145     // doAction() method.
00146     Result doAction(TBAction* action);
00147 
00148     // Moves the latest performed action to the undone list and calls the
00149     // action's undoAction() method.
00150     Result undoAction();
00151 
00152     // Moves the latest undone action to the performed list and calls the
00153     // action's doAction() method.
00154     Result redoAction();
00155 
00156     // Removes the given action from the performed list, but does not delete
00157     // it.  Returns true if the remove was successful, false otherwise.
00158     bool remove(TBAction* a);
00159 
00160     // Removes the given action from the undone list, but does not delete it.
00161     // Returns true if the remove was successful, false otherwise.
00162     bool removeUndone(TBAction* a);
00163     
00164 private:
00165     // Performed actions list
00166     vector<TBAction*> actions;
00167 
00168     // Undone actions list
00169     vector<TBAction*> undone;
00170 };
00171 
00172 /* Specific Actions */
00173 
00174 // <summary>
00175 // TBAction for when non-array data in the table is edited.
00176 // <summary>
00177 //
00178 // <synopsis>
00179 // A TBEditDataAction keeps track of the table, row, column, new value, and old
00180 // value.  When this action is performed, the underlying table is updated to
00181 // the new value at the given row and column; when this action is undone, the
00182 // underlying table is updated to the old value.
00183 // </synopsis>
00184 
00185 class TBEditDataAction : public TBAction {
00186 public:    
00187     TBEditDataAction(TBTableTabs* tt, int row, int col, TBData* newVal);
00188 
00189     virtual ~TBEditDataAction();
00190 
00191     
00192     // Implements TBAction::doAction().
00193     // Updates the table by calling TBTable::editData() followed by a
00194     // TBDataTab::setData() if the edit is successful.  Returns the result
00195     // of the TBTable::editData() call.
00196     Result doAction();
00197 
00198     // Implements TBAction::undoAction().
00199     // Updates the table by calling TBTable::editData() followed by a
00200     // TBDataTab::setData() if the edit is successful.  Returns the result
00201     // of the TBTable::editData() call.
00202     Result undoAction();
00203 
00204     // Implements TBAction::name().
00205     // Returns "edit [table name]([row],[col])".
00206     String name();
00207 
00208     // Implements TBAction::isAssociatedWith().
00209     // Returns true if o is equal to the TBTableTabs object given in the
00210     // constructor, false otherwise.
00211     bool isAssociatedWith(void* o);
00212     
00213 private:
00214     // Origin table.
00215     TBTableTabs* tt;
00216 
00217     // Row of the edit data.
00218     int row;
00219 
00220     // Column of the edit data.
00221     int col;
00222     
00223     // New value.
00224     TBData* newVal;
00225 
00226     // Old value.
00227     TBData* oldVal;
00228     
00229     // This action's name.
00230     String actionName;
00231 
00232     
00233     // Updates the underlying table with the given value.
00234     Result update(TBData* val);
00235 };
00236 
00237 // <summary>
00238 // TBAction for when array data in the table is edited.
00239 // <summary>
00240 //
00241 // <synopsis>
00242 // A TBEditArrayDataAction keeps track of the table, row, column, array
00243 // coordinates, new value, and old value.  When this action is performed, the
00244 // underlying table is updated to the new value at the given row, column, and
00245 // coordinates; when this action is undone, the underlying table is updated to
00246 // the old value.
00247 // </synopsis>
00248 
00249 class TBEditArrayDataAction : public TBAction {
00250 public:
00251     TBEditArrayDataAction(TBTableTabs* tt, TBViewArray* array, int row,
00252         int col, vector<int> coord, TBData* newVal);
00253 
00254     virtual ~TBEditArrayDataAction();
00255 
00256     
00257     // Implements TBAction::doAction().
00258     // Updates the table by calling TBTable::editArrayData() followed by a
00259     // TBViewArray::setDataAt() if the edit is successful.  Returns the result
00260     // of the TBTable::editArrayData().
00261     Result doAction();
00262 
00263     // Implements TBAction::undoAction().
00264     // Updates the table by calling TBTable::editArrayData() followed by a
00265     // TBViewArray::setDataAt() if the edit is successful.  Returns the result
00266     // of the TBTable::editArrayData().
00267     Result undoAction();
00268 
00269     // Implements TBAction::name().
00270     // Returns "edit [table name]([row],[col])[coords]".
00271     String name();
00272 
00273     // Implements TBAction::isAssociatedWith().
00274     // Returns true if o is the TBTableTabs object or the TBViewArray object
00275     // given in the constructor, false otherwise.
00276     bool isAssociatedWith(void* o);
00277 
00278 private:
00279     // Origin table.
00280     TBTableTabs* tt;
00281 
00282     // Row of the edit data.
00283     int row;
00284 
00285     // Column of the edit data.
00286     int col;
00287 
00288     // Array coordinates of the edit data.
00289     vector<int> coords;;
00290     
00291     // New value.
00292     TBData* newVal;
00293     
00294     // Old value.
00295     TBData* oldVal;
00296 
00297     // This action's name.
00298     String actionName;
00299 
00300     // Indicates whether the array is one-dimensional or not.
00301     bool oneDim;
00302 
00303     
00304     // Updates the underlying table with the given value.
00305     Result update(TBData* val);
00306 };
00307 
00308 }
00309 
00310 #endif /* TBACTION_H_ */