source: trunk/demos/undo/document.cpp@ 282

Last change on this file since 282 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 11.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qevent.h>
43#include <QPainter>
44#include <QTextStream>
45#include <QUndoStack>
46#include "document.h"
47#include "commands.h"
48
49static const int resizeHandleWidth = 6;
50
51/******************************************************************************
52** Shape
53*/
54
55const QSize Shape::minSize(80, 50);
56
57Shape::Shape(Type type, const QColor &color, const QRect &rect)
58 : m_type(type), m_rect(rect), m_color(color)
59{
60}
61
62Shape::Type Shape::type() const
63{
64 return m_type;
65}
66
67QRect Shape::rect() const
68{
69 return m_rect;
70}
71
72QColor Shape::color() const
73{
74 return m_color;
75}
76
77QString Shape::name() const
78{
79 return m_name;
80}
81
82QRect Shape::resizeHandle() const
83{
84 QPoint br = m_rect.bottomRight();
85 return QRect(br - QPoint(resizeHandleWidth, resizeHandleWidth), br);
86}
87
88QString Shape::typeToString(Type type)
89{
90 QString result;
91
92 switch (type) {
93 case Rectangle:
94 result = QLatin1String("Rectangle");
95 break;
96 case Circle:
97 result = QLatin1String("Circle");
98 break;
99 case Triangle:
100 result = QLatin1String("Triangle");
101 break;
102 }
103
104 return result;
105}
106
107Shape::Type Shape::stringToType(const QString &s, bool *ok)
108{
109 if (ok != 0)
110 *ok = true;
111
112 if (s == QLatin1String("Rectangle"))
113 return Rectangle;
114 if (s == QLatin1String("Circle"))
115 return Circle;
116 if (s == QLatin1String("Triangle"))
117 return Triangle;
118
119 if (ok != 0)
120 *ok = false;
121 return Rectangle;
122}
123
124/******************************************************************************
125** Document
126*/
127
128Document::Document(QWidget *parent)
129 : QWidget(parent), m_currentIndex(-1), m_mousePressIndex(-1), m_resizeHandlePressed(false)
130{
131 m_undoStack = new QUndoStack(this);
132
133 setAutoFillBackground(true);
134 setBackgroundRole(QPalette::Base);
135
136 QPalette pal = palette();
137 pal.setBrush(QPalette::Base, QPixmap(":/icons/background.png"));
138 pal.setColor(QPalette::HighlightedText, Qt::red);
139 setPalette(pal);
140}
141
142QString Document::addShape(const Shape &shape)
143{
144 QString name = Shape::typeToString(shape.type());
145 name = uniqueName(name);
146
147 m_shapeList.append(shape);
148 m_shapeList[m_shapeList.count() - 1].m_name = name;
149 setCurrentShape(m_shapeList.count() - 1);
150
151 return name;
152}
153
154void Document::deleteShape(const QString &shapeName)
155{
156 int index = indexOf(shapeName);
157 if (index == -1)
158 return;
159
160 update(m_shapeList.at(index).rect());
161
162 m_shapeList.removeAt(index);
163
164 if (index <= m_currentIndex) {
165 m_currentIndex = -1;
166 if (index == m_shapeList.count())
167 --index;
168 setCurrentShape(index);
169 }
170}
171
172Shape Document::shape(const QString &shapeName) const
173{
174 int index = indexOf(shapeName);
175 if (index == -1)
176 return Shape();
177 return m_shapeList.at(index);
178}
179
180void Document::setShapeRect(const QString &shapeName, const QRect &rect)
181{
182 int index = indexOf(shapeName);
183 if (index == -1)
184 return;
185
186 Shape &shape = m_shapeList[index];
187
188 update(shape.rect());
189 update(rect);
190
191 shape.m_rect = rect;
192}
193
194void Document::setShapeColor(const QString &shapeName, const QColor &color)
195{
196
197 int index = indexOf(shapeName);
198 if (index == -1)
199 return;
200
201 Shape &shape = m_shapeList[index];
202 shape.m_color = color;
203
204 update(shape.rect());
205}
206