demos/undo/commands.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the demonstration applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "commands.h"
       
    43 
       
    44 static const int setShapeRectCommandId = 1;
       
    45 static const int setShapeColorCommandId = 2;
       
    46 
       
    47 /******************************************************************************
       
    48 ** AddShapeCommand
       
    49 */
       
    50 
       
    51 AddShapeCommand::AddShapeCommand(Document *doc, const Shape &shape, QUndoCommand *parent)
       
    52     : QUndoCommand(parent)
       
    53 {
       
    54     m_doc = doc;
       
    55     m_shape = shape;
       
    56 }
       
    57 
       
    58 void AddShapeCommand::undo()
       
    59 {
       
    60     m_doc->deleteShape(m_shapeName);
       
    61 }
       
    62 
       
    63 void AddShapeCommand::redo()
       
    64 {
       
    65     // A shape only gets a name when it is inserted into a document
       
    66     m_shapeName = m_doc->addShape(m_shape);
       
    67     setText(QObject::tr("Add %1").arg(m_shapeName));
       
    68 }
       
    69 
       
    70 /******************************************************************************
       
    71 ** RemoveShapeCommand
       
    72 */
       
    73 
       
    74 RemoveShapeCommand::RemoveShapeCommand(Document *doc, const QString &shapeName,
       
    75                                         QUndoCommand *parent)
       
    76     : QUndoCommand(parent)
       
    77 {
       
    78     setText(QObject::tr("Remove %1").arg(shapeName));
       
    79     m_doc = doc;
       
    80     m_shape = doc->shape(shapeName);
       
    81     m_shapeName = shapeName;
       
    82 }
       
    83 
       
    84 void RemoveShapeCommand::undo()
       
    85 {
       
    86     m_shapeName = m_doc->addShape(m_shape);
       
    87 }
       
    88 
       
    89 void RemoveShapeCommand::redo()
       
    90 {
       
    91     m_doc->deleteShape(m_shapeName);
       
    92 }
       
    93 
       
    94 /******************************************************************************
       
    95 ** SetShapeColorCommand
       
    96 */
       
    97 
       
    98 SetShapeColorCommand::SetShapeColorCommand(Document *doc, const QString &shapeName,
       
    99                                             const QColor &color, QUndoCommand *parent)
       
   100     : QUndoCommand(parent)
       
   101 {
       
   102     setText(QObject::tr("Set %1's color").arg(shapeName));
       
   103 
       
   104     m_doc = doc;
       
   105     m_shapeName = shapeName;
       
   106     m_oldColor = doc->shape(shapeName).color();
       
   107     m_newColor = color;
       
   108 }
       
   109 
       
   110 void SetShapeColorCommand::undo()
       
   111 {
       
   112     m_doc->setShapeColor(m_shapeName, m_oldColor);
       
   113 }
       
   114 
       
   115 void SetShapeColorCommand::redo()
       
   116 {
       
   117     m_doc->setShapeColor(m_shapeName, m_newColor);
       
   118 }
       
   119 
       
   120 bool SetShapeColorCommand::mergeWith(const QUndoCommand *command)
       
   121 {
       
   122     if (command->id() != setShapeColorCommandId)
       
   123         return false;
       
   124 
       
   125     const SetShapeColorCommand *other = static_cast<const SetShapeColorCommand*>(command);
       
   126     if (m_shapeName != other->m_shapeName)
       
   127         return false;
       
   128 
       
   129     m_newColor = other->m_newColor;
       
   130     return true;
       
   131 }
       
   132 
       
   133 int SetShapeColorCommand::id() const
       
   134 {
       
   135     return setShapeColorCommandId;
       
   136 }
       
   137 
       
   138 /******************************************************************************
       
   139 ** SetShapeRectCommand
       
   140 */
       
   141 
       
   142 SetShapeRectCommand::SetShapeRectCommand(Document *doc, const QString &shapeName,
       
   143                                             const QRect &rect, QUndoCommand *parent)
       
   144     : QUndoCommand(parent)
       
   145 {
       
   146     setText(QObject::tr("Change %1's geometry").arg(shapeName));
       
   147 
       
   148     m_doc = doc;
       
   149     m_shapeName = shapeName;
       
   150     m_oldRect = doc->shape(shapeName).rect();
       
   151     m_newRect = rect;
       
   152 }
       
   153 
       
   154 void SetShapeRectCommand::undo()
       
   155 {
       
   156     m_doc->setShapeRect(m_shapeName, m_oldRect);
       
   157 }
       
   158 
       
   159 void SetShapeRectCommand::redo()
       
   160 {
       
   161     m_doc->setShapeRect(m_shapeName, m_newRect);
       
   162 }
       
   163 
       
   164 bool SetShapeRectCommand::mergeWith(const QUndoCommand *command)
       
   165 {
       
   166     if (command->id() != setShapeRectCommandId)
       
   167         return false;
       
   168 
       
   169     const SetShapeRectCommand *other = static_cast<const SetShapeRectCommand*>(command);
       
   170     if (m_shapeName != other->m_shapeName)
       
   171         return false;
       
   172 
       
   173     m_newRect = other->m_newRect;
       
   174     return true;
       
   175 }
       
   176 
       
   177 int SetShapeRectCommand::id() const
       
   178 {
       
   179     return setShapeRectCommandId;
       
   180 }