-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonitemmecs.cpp
More file actions
278 lines (230 loc) · 6.84 KB
/
commonitemmecs.cpp
File metadata and controls
278 lines (230 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the demonstration applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "commonitemmecs.h"
//#include <QtGui>
#include "interfaceitemproperties.h"
#include "project.h"
#include "page.h"
//QGraphicsSceneMouseEvent
#include <QGraphicsSceneMouseEvent>
int CommonItemMECS::stepOfGrid = 25;
CommonItemMECS::CommonItemMECS(int x, int y):
m_itemFlags(Qt::AlignCenter),
m_width(200), m_height(100),
m_removed(false), m_init(false)
{
this->x = x;
this->y = y;
setZValue((x + y) % 2);
setFlags(ItemIsSelectable | ItemIsMovable);
setAcceptHoverEvents(true);
}
QRectF CommonItemMECS::boundingRect() const
{
if (m_backgroundImage.isEmpty())
return QRectF(0, 0, m_width, m_height);
else
return m_backGroundImageCache->rect();
}
QPainterPath CommonItemMECS::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
void CommonItemMECS::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
QColor fillColor = (option->state & QStyle::State_Selected) ? color.dark(150) : color;
if (option->state & QStyle::State_MouseOver)
fillColor = fillColor.light(125);
QPen oldPen = painter->pen();
QPen pen = oldPen;
int width = 0;
if (option->state & QStyle::State_Selected)
width += 2;
pen.setWidth(width);
pen.setColor(Qt::white);
QBrush b = painter->brush();
if (m_backgroundImage.isEmpty())
{
painter->setBrush(QBrush(fillColor.dark(option->state & QStyle::State_Sunken ? 120 : 100)));
painter->drawRect(boundingRect());
painter->setBrush(b);
}
else
{
QPen oldPenBorder = painter->pen();
QPen *penBorder = new QPen(Qt::red);
penBorder->setWidth(3);
penBorder->setStyle(Qt::DotLine);
painter->setPen(*penBorder);
painter->drawRect(boundingRect());
painter->drawImage(boundingRect().topLeft(), *m_backGroundImageCache);
painter->setPen(oldPenBorder);
}
painter->setPen(pen);
painter->drawText(boundingRect(), m_itemFlags, m_text);
painter->setPen(oldPen);
if (m_removed)
{
QPen oldPenBorder = painter->pen();
QPen *penBorder = new QPen(Qt::red);
penBorder->setWidth(3);
penBorder->setStyle(Qt::SolidLine);
painter->setPen(*penBorder);
painter->drawLine(boundingRect().topLeft(), boundingRect().bottomRight());
painter->drawLine(boundingRect().bottomLeft(), boundingRect().topRight());
painter->setPen(oldPenBorder);
}
return;
}
void CommonItemMECS::setWidth(qreal width)
{
m_width = width;
}
void CommonItemMECS::setHeight(qreal height)
{
m_height = height;
}
void CommonItemMECS::setText(QString newText)
{
m_text = newText;
}
void CommonItemMECS::setBackgroundImage(QString newImage)
{
m_backgroundImage = newImage;
if (m_backgroundImage.isEmpty())
return;
QString backgroundImagePath =
Project::PathToProject + m_backgroundImage;
m_backGroundImageCache = new QImage(backgroundImagePath);
}
void CommonItemMECS::setPage(QString page)
{
m_page = page;
}
qreal CommonItemMECS::getWidth()
{
return m_width;
}
qreal CommonItemMECS::getHeight()
{
return m_height;
}
QString CommonItemMECS::getText()
{
return m_text;
}
QString CommonItemMECS::getBackgroundImage()
{
return m_backgroundImage;
}
QString CommonItemMECS::getPage()
{
return m_page;
}
void CommonItemMECS::UpdatePosition()
{
setX(x);
setY(y);
setBackgroundImage(m_backgroundImage);
}
bool CommonItemMECS::isRemoved()
{
return m_removed;
}
void CommonItemMECS::Copy(CommonItemMECS *from)
{
m_width = from->getWidth();
m_height = from->getHeight();
setBackgroundImage(from->getBackgroundImage());
}
void CommonItemMECS::Parse(QString )
{
}
QString CommonItemMECS::Save()
{
QString result = QString(
"Pages = %0\n" \
"Left = %1\n" \
"Top = %2\n" \
"Font = Images/arial.ttf\n" \
"FontSize = 20\n" \
"Color = white\n").arg(((Page*)scene())->GetName()).arg(scenePos().x()).arg(scenePos().y());
return result;
}
void CommonItemMECS::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mousePressEvent(event);
if (event->button() == Qt::RightButton)
m_removed = !m_removed;
update();
}
void CommonItemMECS::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->modifiers() & Qt::ShiftModifier) {
stuff << event->pos();
update();
return;
}
QGraphicsItem::mouseMoveEvent(event);
}
int CommonItemMECS::snapToGrid(qreal position)
{
int delimiter = (int)position / stepOfGrid;
if ((position / stepOfGrid) - delimiter >= 0.5)
delimiter++;
return delimiter * stepOfGrid;
}
void CommonItemMECS::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsItem::mouseReleaseEvent(event);
setX(snapToGrid(scenePos().x()));
setY(snapToGrid(scenePos().y()));
update();
}
void CommonItemMECS::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
{
InterfaceItemProperties *properties = new InterfaceItemProperties(*this);
properties->setWindowModality(Qt::ApplicationModal);
properties->show();
}