TinkerCell Core 1.0
TinkerCell's Core library providing all basic functionalities
UndoCommands.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 
00003 Copyright (c) 2008 Deepak Chandran
00004 Contact: Deepak Chandran (dchandran1@gmail.com)
00005 See COPYRIGHT.TXT
00006 
00007 This file contains a collection of commands that perform simple operations that can be redone and undone.
00008 
00009 ****************************************************************************/
00010 
00011 #ifndef TINKERCELL_BASICUNDOCOMMANDS_H
00012 #define TINKERCELL_BASICUNDOCOMMANDS_H
00013 
00014 #include <stdlib.h>
00015 #include <QtGui>
00016 #include <QMdiArea>
00017 #include <QMdiSubWindow>
00018 #include <QString>
00019 #include <QFileDialog>
00020 #include <QtDebug>
00021 #include <QGraphicsItem>
00022 #include <QGraphicsItemGroup>
00023 #include <QGraphicsScene>
00024 #include <QGraphicsView>
00025 #include <QAction>
00026 #include <QMenu>
00027 #include <QFile>
00028 #include <QHBoxLayout>
00029 #include <QMainWindow>
00030 #include <QHash>
00031 #include <QUndoCommand>
00032 #include <QGraphicsItemAnimation>
00033 #include <QPrinter>
00034 
00035 #include "ItemFamily.h"
00036 #include "ItemHandle.h"
00037 #include "DataTable.h"
00038 #include "NodeGraphicsItem.h"
00039 #include "ConnectionGraphicsItem.h"
00040 
00041 namespace Tinkercell
00042 {
00043         class NetworkHandle;
00044         class GraphicsScene;
00045         class TextEditor;
00046         class RenameCommand;
00047 
00050         class TINKERCELLCOREEXPORT InsertHandlesCommand : public QUndoCommand
00051         {
00052         public:
00058                 InsertHandlesCommand(TextEditor *, const QList<ItemHandle*> &, bool checkNames=true);
00064                 InsertHandlesCommand(TextEditor *, ItemHandle*, bool checkNames=true);
00066                 ~InsertHandlesCommand();
00068                 void redo();
00070                 void undo();
00071 
00072         private:
00074                 QList<ItemHandle*> items;
00076                 QList<ItemHandle*> parentHandles;
00078                 TextEditor * textEditor;
00080                 NetworkHandle * network;
00082                 RenameCommand * renameCommand;
00084                 bool checkNames;
00085         };
00086 
00089         class TINKERCELLCOREEXPORT RemoveHandlesCommand : public QUndoCommand
00090         {
00091         public:
00097                 RemoveHandlesCommand(TextEditor *, const QList<ItemHandle*> & , bool updateDataFields=true);
00103                 RemoveHandlesCommand(TextEditor *, ItemHandle*, bool updateDataFields=true);
00105                 void redo();
00107                 void undo();
00108 
00109         private:
00111                 Change2DataCommand<qreal,QString> * changeDataCommand;
00113                 QList<ItemHandle*> items;
00115                 QList<ItemHandle*> parentHandles;
00117                 TextEditor * textEditor;
00119                 NetworkHandle * network;
00121                 bool updateData;
00122         };
00123 
00126         class TINKERCELLCOREEXPORT MoveCommand : public QUndoCommand
00127         {
00128         public:
00134                 MoveCommand(GraphicsScene * scene, QGraphicsItem * item, const QPointF& distance);
00140                 MoveCommand(GraphicsScene * scene, const QList<QGraphicsItem*>& items, const QPointF& distance);
00146                 MoveCommand(GraphicsScene * scene, const QList<QGraphicsItem*>& items, const QList<QPointF>& distance);
00148                 void redo();
00150                 void undo();
00154                 void static refreshAllConnectionIn(const QList<QGraphicsItem*>&);
00155         private:
00157                 GraphicsScene * graphicsScene;
00159                 QList<QGraphicsItem*> graphicsItems;
00161                 QList<QPointF> change;
00162         };
00165         class TINKERCELLCOREEXPORT InsertGraphicsCommand : public QUndoCommand
00166         {
00167         public:
00174                 InsertGraphicsCommand(const QString& name, GraphicsScene * scene, QGraphicsItem * item, bool checkNames=true);
00181                 InsertGraphicsCommand(const QString& name, GraphicsScene * scene, const QList<QGraphicsItem*>& items, bool checkNames=true);
00183                 void redo();
00185                 void undo();
00187                 virtual ~InsertGraphicsCommand();
00188         private:
00190                 GraphicsScene * graphicsScene;
00192                 NetworkHandle * network;
00194                 QList<QGraphicsItem*> graphicsItems;
00196                 QList<QGraphicsItem*> parentGraphicsItems;
00198                 QList<ItemHandle*> handles;
00200                 QList<ItemHandle*> parentHandles;
00202                 RenameCommand * renameCommand;
00204                 bool checkNames;
00205         };
00208         class TINKERCELLCOREEXPORT RemoveGraphicsCommand : public QUndoCommand
00209         {
00210         public:
00217                 RemoveGraphicsCommand(const QString& name, QGraphicsItem * item, bool updataDataFields=true);
00224                 RemoveGraphicsCommand(const QString& name, const QList<QGraphicsItem*>& items, bool updateDataFields=true);
00226                 void redo();
00228                 void undo();
00229         private:
00231                 Change2DataCommand<qreal,QString> * changeDataCommand;
00233                 QList<GraphicsScene*> graphicsScenes;
00235                 QList<QGraphicsItem*> graphicsItems;
00237                 QList<QGraphicsItem*> itemParents;
00239                 QList<ItemHandle*> itemHandles;
00241                 QList<ItemHandle*> parentHandles;
00243                 QList< ItemHandle* > affectedHandles;
00244                 /* \param bool update data of other items where removed items might occur (default=true)*/
00245                 bool updateData;
00246         };
00247 
00248 
00251         class TINKERCELLCOREEXPORT ChangeBrushCommand : public QUndoCommand
00252         {
00253         public:
00260                 ChangeBrushCommand(const QString& name, QGraphicsItem * item, const QBrush& to);
00267                 ChangeBrushCommand(const QString& name, const QList<QGraphicsItem*>& items, const QList<QBrush>& to);
00268                 void redo();
00269                 void undo();
00270         private:
00271                 QList<QGraphicsItem*> graphicsItems;
00272                 QList<QBrush> oldBrush, newBrush;
00273         };
00274 
00277         class TINKERCELLCOREEXPORT ChangePenCommand : public QUndoCommand
00278         {
00279         public:
00286                 ChangePenCommand(const QString& name, QGraphicsItem * item, const QPen& to);
00293                 ChangePenCommand(const QString& name, const QList<QGraphicsItem*>& items, const QList<QPen>& to);
00294                 void redo();
00295                 void undo();
00296         private:
00297                 QList<QGraphicsItem*> graphicsItems;
00298                 QList<QPen> oldPen, newPen;
00299         };
00300 
00303         class TINKERCELLCOREEXPORT ChangeBrushAndPenCommand : public QUndoCommand
00304         {
00305         public:
00313                 ChangeBrushAndPenCommand(const QString& name, QGraphicsItem * item, const QBrush& brush, const QPen& pen);
00321                 ChangeBrushAndPenCommand(const QString& name, const QList<QGraphicsItem*>& items, const QList<QBrush>& brushes, const QList<QPen>& pens);
00322                 ~ChangeBrushAndPenCommand();
00323                 void redo();
00324                 void undo();
00325         private:
00326                 ChangeBrushCommand * changeBrushCommand;
00327                 ChangePenCommand * changePenCommand;
00328         };
00329 
00332         class TINKERCELLCOREEXPORT ChangeZCommand : public QUndoCommand
00333         {
00334         public:
00341                 ChangeZCommand(const QString& name, QGraphicsScene * scene, QGraphicsItem * item, qreal to);
00348                 ChangeZCommand(const QString& name, QGraphicsScene * scene, const QList<QGraphicsItem*>& items, const QList<qreal>& to);
00349                 void redo();
00350                 void undo();
00351         private:
00352                 QGraphicsScene * graphicsScene;
00353                 QList<QGraphicsItem*> graphicsItems;
00354                 QList<qreal> oldZ, newZ;
00355         };
00356 
00359         class TINKERCELLCOREEXPORT TransformCommand : public QUndoCommand
00360         {
00361         public:
00371                 TransformCommand(const QString& name, QGraphicsScene * scene, QGraphicsItem * item,
00372                         const QPointF& sizechange,
00373                         qreal anglechange,
00374                         bool VFlip, bool HFlip);
00384                 TransformCommand(const QString& name, QGraphicsScene * scene, const QList<QGraphicsItem *>& items,
00385                         const QList<QPointF>& sizechange,
00386                         const QList<qreal>& anglechange,
00387                         const QList<bool>& VFlip, const QList<bool>& HFlip);
00388                 void redo();
00389                 void undo();
00390         private:
00391                 QGraphicsScene * graphicsScene;
00392                 QList<QGraphicsItem*> graphicsItems;
00393                 QList<QPointF> sizeFactor;
00394                 QList<qreal> angleChange;
00395                 QList<bool> vFlip, hFlip;
00396         };
00397 
00400         class TINKERCELLCOREEXPORT ChangeParentCommand : public QUndoCommand
00401         {
00402         public:
00409                 ChangeParentCommand(const QString& name, QGraphicsScene * scene, QGraphicsItem * item, QGraphicsItem * newParent);
00416                 ChangeParentCommand(const QString& name, QGraphicsScene * scene, const QList<QGraphicsItem*>& items, const QList<QGraphicsItem*>& newParents);
00417                 void redo();
00418                 void undo();
00419         private:
00420                 QList<QGraphicsItem*> graphicsItems;
00421                 QList<QGraphicsItem*> oldParents;
00422                 QList<QGraphicsItem*> newParents;
00423                 QGraphicsScene * scene;
00424         };
00425 
00428         class TINKERCELLCOREEXPORT RenameCommand : public QUndoCommand
00429         {
00430         public:
00431                 virtual ~RenameCommand();
00440                 RenameCommand(const QString& name, NetworkHandle *, const QList<ItemHandle*>& allItems, const QString& oldname, const QString& newname, bool forceUnique=true);
00448                 RenameCommand(const QString& name, NetworkHandle *, const QString& oldname, const QString& newname, bool forceUnique=true);
00457                 RenameCommand(const QString& name, NetworkHandle *,  const QList<ItemHandle*>& allItems, const QList<QString>& oldname, const QList<QString>& newname, bool forceUnique=true);
00465                 RenameCommand(const QString& name, NetworkHandle *,  const QList<QString>& oldname, const QList<QString>& newname, bool forceUnique=true);
00473                 RenameCommand(const QString& name, NetworkHandle * , ItemHandle * itemHandle, const QString& newname, bool forceUnique=true);
00482                 RenameCommand(const QString& name, NetworkHandle *,  const QList<ItemHandle*>& allItems, ItemHandle * item, const QString& newname, bool forceUnique=true);
00490                 RenameCommand(const QString& name, NetworkHandle * , const QList<ItemHandle*>& itemhandles, const QList<QString>& newnames, bool forceUnique=true);
00499                 RenameCommand(const QString& name, NetworkHandle *, const QList<ItemHandle*>& allItems, const QList<ItemHandle*>& itemhandles, const QList<QString>& newnames, bool forceUnique=true);
00500                 void redo();
00501                 void undo();
00502                 static void findReplaceAllHandleData(QList<ItemHandle*>& allItems,const QString& oldName,const QString& newName);
00503                 static void substituteString(QString& targetValue, const QString& oldName,const QString& newName);
00504         private:
00505                 QList<ItemHandle*> allhandles;
00506                 QList<ItemHandle*> handles;
00507                 QList<QString> oldNames;
00508                 QList<QString> newNames;
00509                 QList< QPair<ItemHandle *, QString> > newItemNames;
00510                 QList< QPair<ItemHandle *, QString> > oldItemNames;
00511                 QList< QPair<TextGraphicsItem *, QString> > newTextItemsNames;
00512                 QList< QPair<TextGraphicsItem *, QString> > oldTextItemsNames;
00513                 Change2DataCommand<qreal,QString> * changeDataCommand;
00514                 NetworkHandle * network;
00515                 bool makeUnique;
00516 
00517                 static void addColumns(DataTable<double>& dat, int k, const QString&);
00518         };
00519 
00522         class TINKERCELLCOREEXPORT CompositeCommand : public QUndoCommand
00523         {
00524         public:
00530                 CompositeCommand(const QString&, const QList<QUndoCommand*>&, const QList<QUndoCommand*>& noClear = QList<QUndoCommand*>());
00537                 CompositeCommand(const QString&, QUndoCommand*, QUndoCommand*, bool deleteCommands = true);
00539                 ~CompositeCommand();
00541                 void redo();
00543                 void undo();
00545                 QList<QUndoCommand*> commands;
00547                 QList<QUndoCommand*> doNotDelete;
00548         };
00549 
00552         class TINKERCELLCOREEXPORT ReverseUndoCommand : public QUndoCommand
00553         {
00554         public:
00560                 ReverseUndoCommand(const QString&, QUndoCommand*, bool deleteCommand = true);
00561                 ~ReverseUndoCommand();
00562                 void redo();
00563                 void undo();
00564                 QUndoCommand* command;
00565                 bool deleteCommand;
00566         };
00567 
00570         class TINKERCELLCOREEXPORT ReplaceNodeGraphicsCommand : public QUndoCommand
00571         {
00572         public:
00579                 ReplaceNodeGraphicsCommand(const QString&,NodeGraphicsItem*,const QString&,bool transform=true);
00586                 ReplaceNodeGraphicsCommand(const QString&,const QList<NodeGraphicsItem*>&,const QList<QString>&,bool transform=true);
00587                 void undo();
00588                 void redo();
00589                 ~ReplaceNodeGraphicsCommand();
00590         private:
00591                 QList<NodeGraphicsItem*> targetNodes;
00592                 QList<NodeGraphicsItem> oldNodes, newNodes;
00593                 QList< QGraphicsItem* > itemsToDelete;
00594                 void loadFromFile(NodeGraphicsItem*,const QString&);
00595                 bool transform;
00596         };
00597 
00600         class TINKERCELLCOREEXPORT AssignHandleCommand : public QUndoCommand
00601         {
00602         public:
00603                 AssignHandleCommand(const QString& text, QGraphicsItem* item, ItemHandle* handle);
00604                 AssignHandleCommand(const QString& text, const QList<QGraphicsItem*>& items, ItemHandle* handle);
00605                 AssignHandleCommand(const QString& text, const QList<QGraphicsItem*>& items, QList<ItemHandle*>& handles);
00606                 void redo();
00607                 void undo();
00608                 ~AssignHandleCommand();
00609                 QList<QGraphicsItem*> graphicsItems;
00610                 QList<ItemHandle*> oldHandles;
00611                 QList<ItemHandle*> newHandles;
00612         private:
00613                 QList< QPair< QGraphicsItem*, ItemHandle*> > oldItemHandles;
00614                 QList< QPair< QGraphicsItem*, ItemHandle*> > newItemHandles;
00615         };
00616 
00617         class TINKERCELLCOREEXPORT SetHandleFamilyCommand;
00620         class TINKERCELLCOREEXPORT MergeHandlesCommand : public QUndoCommand
00621         {
00622         public:
00623                 MergeHandlesCommand(const QString& text, NetworkHandle * , const QList<ItemHandle*>& handles);
00624                 MergeHandlesCommand(const QString& text, const QList<ItemHandle*>& allHandles, const QList<ItemHandle*>& handles);
00625                 void redo();
00626                 void undo();
00627                 ~MergeHandlesCommand();
00628                 QList<ItemHandle*> oldHandles;
00629                 ItemHandle* newHandle;
00630         private:
00631                 void init(NetworkHandle * , const QList<ItemHandle*>& , const QList<ItemHandle*>& );
00632                 QHash< ItemHandle*, QList<QGraphicsItem*> > oldGraphicsItems;
00633                 QHash< ItemHandle*, QList<ItemHandle*> > oldChildren;
00634                 QHash< ItemHandle*, ItemHandle* > oldParents;
00635                 QList< ItemHandle* > allChildren;
00636                 QList<QGraphicsItem*> allGraphicsItems;
00637                 RenameCommand * renameCommand;
00638                 SetHandleFamilyCommand * setFamilyCommand;
00639                 Change2DataCommand<qreal,QString> * changeDataCommand;
00640         };
00641 
00645         class TINKERCELLCOREEXPORT SetParentHandleCommand : public QUndoCommand
00646         {
00647         public:
00649                 SetParentHandleCommand(const QString& name, NetworkHandle * , ItemHandle * child, ItemHandle * parent);
00651                 SetParentHandleCommand(const QString& name, NetworkHandle * , const QList<ItemHandle*>& children, ItemHandle * parent);
00653                 SetParentHandleCommand(const QString& name, NetworkHandle * , const QList<ItemHandle*>& children, const QList<ItemHandle*>& parents);
00655                 ~SetParentHandleCommand();
00657                 void redo();
00659                 void undo();
00660         private:
00662                 QList<ItemHandle*> children;
00664                 QList<ItemHandle*> newParents;
00666                 QList<ItemHandle*> oldParents;
00667                 NetworkHandle * net;
00668                 RenameCommand * renameCommand;
00669                 
00670                 friend class NetworkHandle;
00671         };
00672         
00677         class TINKERCELLCOREEXPORT SetGraphicsSceneVisibilityCommand : public QUndoCommand
00678         {
00679         public:
00681                 SetGraphicsSceneVisibilityCommand(const QString& name, const QList<QGraphicsItem*>&, const QList<bool>&);
00683                 SetGraphicsSceneVisibilityCommand(const QString& name, QGraphicsItem*, bool);
00685                 SetGraphicsSceneVisibilityCommand(const QString& name, const QList<QGraphicsItem*>&, bool);
00687                 void redo();
00689                 void undo();
00690         private:
00691                 QList<QGraphicsItem*> items;
00692                 QList<bool> before;
00693         };
00694         
00699         class TINKERCELLCOREEXPORT SetHandleFamilyCommand : public QUndoCommand
00700         {
00701         public:
00703                 SetHandleFamilyCommand(const QString& name, const QList<ItemHandle*>&, const QList<ItemFamily*>&);
00705                 SetHandleFamilyCommand(const QString& name, ItemHandle*, ItemFamily*);
00707                 void redo();
00709                 void undo();
00710         private:
00711                 QList<ItemHandle*> handles;
00712                 QList<ItemFamily*> oldFamily, newFamily;
00713                 
00714                 friend class NetworkHandle;
00715         };
00716         
00719         class TINKERCELLCOREEXPORT AddControlPointCommand : public QUndoCommand
00720         {
00721         public:
00727                 AddControlPointCommand(const QString& name, GraphicsScene * scene, ConnectionGraphicsItem::ControlPoint * item );
00733                 AddControlPointCommand(const QString& name, GraphicsScene * scene, QList<ConnectionGraphicsItem::ControlPoint *> items);
00735                 virtual ~AddControlPointCommand();
00739                 void redo();
00743                 void undo();    
00745                 GraphicsScene * graphicsScene;
00747                 QList<ConnectionGraphicsItem::ControlPoint*> graphicsItems;
00749                 QList<int> listK1, listK2;
00750         private:
00752                 bool undone;
00753         };
00754 
00757         class TINKERCELLCOREEXPORT RemoveControlPointCommand : public QUndoCommand
00758         {
00759         public:
00765                 RemoveControlPointCommand(const QString& name, GraphicsScene * scene, 
00766                         ConnectionGraphicsItem::ControlPoint * item);
00772                 RemoveControlPointCommand(const QString& name, GraphicsScene * scene, 
00773                         QList<ConnectionGraphicsItem::ControlPoint *> items);
00777                 void redo();
00781                 void undo();    
00783                 QList<ConnectionGraphicsItem::ControlPoint*> graphicsItems;
00785                 GraphicsScene * graphicsScene;  
00787                 QList<int> listK1, listK2;
00788         };
00789 
00792         class TINKERCELLCOREEXPORT AddCurveSegmentCommand : public QUndoCommand
00793         {
00794         public:
00800                 AddCurveSegmentCommand(const QString& name, GraphicsScene * scene, ConnectionGraphicsItem* connection,
00801                         ConnectionGraphicsItem::CurveSegment& item );
00807                 AddCurveSegmentCommand(const QString& name, GraphicsScene * scene, ConnectionGraphicsItem* connection,
00808                         QList<ConnectionGraphicsItem::CurveSegment> items);
00810                 virtual ~AddCurveSegmentCommand();
00814                 void redo();
00818                 void undo();    
00820                 GraphicsScene * graphicsScene;
00822                 ConnectionGraphicsItem* connectionItem;
00824                 QList<ConnectionGraphicsItem::CurveSegment> curveSegments;
00826                 QList<int> listK1;
00827         private:
00829                 bool undone;
00830         };
00831 
00834         class TINKERCELLCOREEXPORT RemoveCurveSegmentCommand : public QUndoCommand
00835         {
00836         public:
00842                 RemoveCurveSegmentCommand(const QString& name, GraphicsScene * scene,
00843                         ConnectionGraphicsItem::ControlPoint * item);
00849                 RemoveCurveSegmentCommand(const QString& name, GraphicsScene * scene, ConnectionGraphicsItem* connection,
00850                         QList<ConnectionGraphicsItem::ControlPoint *> items);
00851 
00855                 void redo();
00859                 void undo();    
00861                 QList<ConnectionGraphicsItem::CurveSegment> curveSegments;
00863                 GraphicsScene * graphicsScene;  
00865                 ConnectionGraphicsItem* connectionItem;
00867                 QList<QGraphicsItem*> parentsAtStart, parentsAtEnd;
00868         };
00869         
00872         class TINKERCELLCOREEXPORT ReplaceConnectedNodeCommand : public QUndoCommand
00873         {
00874         public:
00881                 ReplaceConnectedNodeCommand(const QString& name, ConnectionGraphicsItem *, NodeGraphicsItem * oldNode, NodeGraphicsItem * newNode);
00882                 void redo();
00883                 void undo();
00884         private:
00885                 ConnectionGraphicsItem* connection;
00886                 NodeGraphicsItem* oldNode;
00887                 NodeGraphicsItem* newNode;
00888         };
00889         
00892         class TINKERCELLCOREEXPORT LineTypeChanged : public QUndoCommand
00893         {
00894         public:
00895                 QList<ConnectionGraphicsItem*> list;
00896                 bool straight;
00897                 void undo();
00898                 void redo();
00899         };
00900         
00903         class TINKERCELLCOREEXPORT ChangeArrowHeadDistance : public QUndoCommand
00904         {
00905         public:
00906                 QList<ConnectionGraphicsItem*> list;
00907                 QList<qreal> dists;
00908                 void undo();
00909                 void redo();
00910         };
00911 
00912 
00913 }
00914 
00915 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines