1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
44 #include "diagramitem.h"
47 MoveCommand::MoveCommand(DiagramItem *diagramItem, const QPointF &oldPos,
49 : QUndoCommand(parent)
51 myDiagramItem = diagramItem;
52 newPos = diagramItem->pos();
58 bool MoveCommand::mergeWith(const QUndoCommand *command)
60 const MoveCommand *moveCommand = static_cast<const MoveCommand *>(command);
61 DiagramItem *item = moveCommand->myDiagramItem;
63 if (myDiagramItem != item)
67 setText(QObject::tr("Move %1")
68 .arg(createCommandString(myDiagramItem, newPos)));
75 void MoveCommand::undo()
77 myDiagramItem->setPos(myOldPos);
78 myDiagramItem->scene()->update();
79 setText(QObject::tr("Move %1")
80 .arg(createCommandString(myDiagramItem, newPos)));
85 void MoveCommand::redo()
87 myDiagramItem->setPos(newPos);
88 setText(QObject::tr("Move %1")
89 .arg(createCommandString(myDiagramItem, newPos)));
94 DeleteCommand::DeleteCommand(QGraphicsScene *scene, QUndoCommand *parent)
95 : QUndoCommand(parent)
97 myGraphicsScene = scene;
98 QList<QGraphicsItem *> list = myGraphicsScene->selectedItems();
99 list.first()->setSelected(false);
100 myDiagramItem = static_cast<DiagramItem *>(list.first());
101 setText(QObject::tr("Delete %1")
102 .arg(createCommandString(myDiagramItem, myDiagramItem->pos())));
107 void DeleteCommand::undo()
109 myGraphicsScene->addItem(myDiagramItem);
110 myGraphicsScene->update();
115 void DeleteCommand::redo()
117 myGraphicsScene->removeItem(myDiagramItem);
122 AddCommand::AddCommand(DiagramItem::DiagramType addType,
123 QGraphicsScene *scene, QUndoCommand *parent)
124 : QUndoCommand(parent)
126 static int itemCount = 0;
128 myGraphicsScene = scene;
129 myDiagramItem = new DiagramItem(addType);
130 initialPosition = QPointF((itemCount * 15) % int(scene->width()),
131 (itemCount * 15) % int(scene->height()));
134 setText(QObject::tr("Add %1")
135 .arg(createCommandString(myDiagramItem, initialPosition)));
139 AddCommand::~AddCommand()
141 if (!myDiagramItem->scene())
142 delete myDiagramItem;
146 void AddCommand::undo()
148 myGraphicsScene->removeItem(myDiagramItem);
149 myGraphicsScene->update();
154 void AddCommand::redo()
156 myGraphicsScene->addItem(myDiagramItem);
157 myDiagramItem->setPos(initialPosition);
158 myGraphicsScene->clearSelection();
159 myGraphicsScene->update();
163 QString createCommandString(DiagramItem *item, const QPointF &pos)
165 return QObject::tr("%1 at (%2, %3)")
166 .arg(item->diagramType() == DiagramItem::Box ? "Box" : "Triangle")
167 .arg(pos.x()).arg(pos.y());