2
* OpenRPT report writer and rendering engine
3
* Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
4
* Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
20
#include "KoReportDesignerItemRectBase.h"
21
#include "reportsceneview.h"
22
#include <koproperty/Set.h>
23
#include <koproperty/Property.h>
24
#include <koproperty/EditorView.h>
25
#include <QGraphicsSceneMouseEvent>
26
#include "KoReportDesigner.h"
31
#include "reportscene.h"
34
KoReportDesignerItemRectBase::KoReportDesignerItemRectBase(KoReportDesigner *r)
35
: QGraphicsRectItem(), KoReportDesignerItemBase(r)
37
m_dpiX = KoDpi::dpiX();
38
m_dpiY = KoDpi::dpiY();
43
setAcceptsHoverEvents(true);
45
#if QT_VERSION >= 0x040600
46
setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
48
setFlags(ItemIsSelectable | ItemIsMovable);
52
void KoReportDesignerItemRectBase::init(KRPos* p, KRSize* s, KoProperty::Set* se)
59
KoReportDesignerItemRectBase::~KoReportDesignerItemRectBase()
63
QRectF KoReportDesignerItemRectBase::sceneRect()
65
return QRectF(m_ppos->toScene(), m_psize->toScene());
68
QRectF KoReportDesignerItemRectBase::pointRect()
70
if (m_ppos && m_psize)
71
return QRectF(m_ppos->toPoint(), m_psize->toPoint());
73
return QRectF(0, 0, 0, 0);
76
void KoReportDesignerItemRectBase::setSceneRect(const QPointF& topLeft, const QSizeF& size, UpdatePropertyFlag update)
78
setSceneRect(QRectF(topLeft, size), update);
81
void KoReportDesignerItemRectBase::setSceneRect(const QRectF& rect, UpdatePropertyFlag update)
83
QGraphicsRectItem::setPos(rect.x(), rect.y());
84
setRect(0, 0, rect.width(), rect.height());
85
if (update == UpdateProperty) {
86
m_ppos->setScenePos(QPointF(rect.x(), rect.y()));
87
m_psize->setSceneSize(QSizeF(rect.width(), rect.height()));
92
void KoReportDesignerItemRectBase::mousePressEvent(QGraphicsSceneMouseEvent * event)
94
//Update and show properties
95
m_ppos->setScenePos(QPointF(sceneRect().x(), sceneRect().y()));
96
m_reportDesigner->changeSet(m_pset);
100
QGraphicsItem::mousePressEvent(event);
103
void KoReportDesignerItemRectBase::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
105
//Keep the size and position in sync
106
m_ppos->setScenePos(pos());
107
m_psize->setSceneSize(QSizeF(rect().width(), rect().height()));
109
QGraphicsItem::mouseReleaseEvent(event);
112
void KoReportDesignerItemRectBase::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
114
//kDebug() << m_grabAction;
118
QPointF p = dynamic_cast<ReportScene*>(scene())->gridPoint(event->scenePos());
119
w = p.x() - scenePos().x();
120
h = p.y() - scenePos().y();
122
//TODO use an enum for the directions
124
switch (m_grabAction) {
126
if (sceneRect().y() - p.y() + rect().height() > 0 && sceneRect().x() - p.x() + rect().width() >= 0)
127
setSceneRect(QPointF(p.x(), p.y()), QSizeF(sceneRect().x() - p.x() + rect().width(), sceneRect().y() - p.y() + rect().height()));
130
if (sceneRect().y() - p.y() + rect().height() >= 0)
131
setSceneRect(QPointF(sceneRect().x(), p.y()), QSizeF(rect().width(), sceneRect().y() - p.y() + rect().height()));
134
if (sceneRect().y() - p.y() + rect().height() >= 0 && w >= 0)
135
setSceneRect(QPointF(sceneRect().x(), p.y()), QSizeF(w, sceneRect().y() - p.y() + rect().height()));
139
setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF(w, (rect().height())));
142
if (h >= 0 && w >= 0)
143
setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF(w, h));
147
setSceneRect(QPointF(sceneRect().x(), sceneRect().y()), QSizeF((rect().width()), h));
150
if (sceneRect().x() - p.x() + rect().width() >= 0 && h >= 0)
151
setSceneRect(QPointF(p.x(), sceneRect().y()), QSizeF(sceneRect().x() - p.x() + rect().width(), h));
155
if (sceneRect().x() - p.x() + rect().width() >= 0)
156
setSceneRect(QPointF(p.x(), sceneRect().y()), QSizeF(sceneRect().x() - p.x() + rect().width(), rect().height()));
159
QGraphicsItem::mouseMoveEvent(event);
163
void KoReportDesignerItemRectBase::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
168
m_grabAction = grabHandle(event->pos());
169
switch (m_grabAction) {
171
setCursor(Qt::SizeFDiagCursor);
174
setCursor(Qt::SizeVerCursor);
177
setCursor(Qt::SizeBDiagCursor);
180
setCursor(Qt::SizeHorCursor);
183
setCursor(Qt::SizeFDiagCursor);
186
setCursor(Qt::SizeVerCursor);
189
setCursor(Qt::SizeBDiagCursor);
192
setCursor(Qt::SizeHorCursor);
198
//kDebug() << m_grabAction;
201
void KoReportDesignerItemRectBase::drawHandles(QPainter *painter)
204
// draw a selected border for visual purposes
205
painter->setPen(QPen(QColor(128, 128, 255), 0, Qt::DotLine));
207
painter->drawRect(rect());
209
const QRectF r = rect();
210
double halfW = (r.width() / 2);
211
double halfH = (r.height() / 2);
212
QPointF center = r.center();
214
center += QPointF(0.75,0.75);
216
painter->fillRect(center.x() - halfW, center.y() - halfH , 5, 5, QColor(128, 128, 255));
217
painter->fillRect(center.x() - 2, center.y() - halfH , 5, 5, QColor(128, 128, 255));
218
painter->fillRect(center.x() + halfW - 4, center.y() - halfH, 5, 5, QColor(128, 128, 255));
220
painter->fillRect(center.x() + (halfW - 4), center.y() - 2, 5, 5, QColor(128, 128, 255));
222
painter->fillRect(center.x() + halfW - 4 , center.y() + halfH - 4 , 5, 5, QColor(128, 128, 255));
223
painter->fillRect(center.x() - 2, center.y() + halfH - 4, 5, 5, QColor(128, 128, 255));
224
painter->fillRect(center.x() - halfW, center.y() + halfH - 4 , 5, 5, QColor(128, 128, 255));
226
painter->fillRect(center.x() - halfW, center.y() - 2, 5, 5, QColor(128, 128, 255));
236
int KoReportDesignerItemRectBase::grabHandle(QPointF pos)
238
QRectF r = boundingRect();
239
int halfW = (int)(r.width() / 2);
240
int halfH = (int)(r.height() / 2);
241
QPointF center = r.center();
243
if (QRectF(center.x() - (halfW), center.y() - (halfH), 5, 5).contains(pos)) {
244
// we are over the top-left handle
246
} else if (QRectF(center.x() - 2, center.y() - (halfH), 5, 5).contains(pos)) {
249
} else if (QRectF(center.x() + (halfW - 4), center.y() - (halfH), 5, 5).contains(pos)) {
252
} else if (QRectF(center.x() + (halfW - 4), center.y() - 2, 5, 5).contains(pos)) {
255
} else if (QRectF(center.x() + (halfW - 4), center.y() + (halfH - 4), 5, 5).contains(pos)) {
258
} else if (QRectF(center.x() - 2, center.y() + (halfH - 4), 5, 5).contains(pos)) {
261
} else if (QRectF(center.x() - (halfW), center.y() + (halfH - 4), 5, 5).contains(pos)) {
264
} else if (QRectF(center.x() - (halfW), center.y() - 2, 5, 5).contains(pos)) {
271
QVariant KoReportDesignerItemRectBase::itemChange(GraphicsItemChange change, const QVariant &value)
273
if (change == ItemPositionChange && scene()) {
274
QPointF newPos = value.toPointF();
276
newPos = dynamic_cast<ReportScene*>(scene())->gridPoint(newPos);
279
else if (newPos.x() > (scene()->width() - rect().width()))
280
newPos.setX(scene()->width() - rect().width());
284
else if (newPos.y() > (scene()->height() - rect().height()))
285
newPos.setY(scene()->height() - rect().height());
288
} else if (change == ItemPositionHasChanged && scene()) {
289
m_ppos->setScenePos(value.toPointF());
290
} else if (change == ItemSceneHasChanged && scene() && m_psize) {
291
QPointF newPos = pos();
293
newPos = dynamic_cast<ReportScene*>(scene())->gridPoint(newPos);
296
else if (newPos.x() > (scene()->width() - rect().width()))
297
newPos.setX(scene()->width() - rect().width());
301
else if (newPos.y() > (scene()->height() - rect().height()))
302
newPos.setY(scene()->height() - rect().height());
304
setSceneRect(newPos, m_psize->toScene());
307
return QGraphicsItem::itemChange(change, value);
310
void KoReportDesignerItemRectBase::propertyChanged(const KoProperty::Set &s, const KoProperty::Property &p)
314
if (p.name() == "Position") {
315
m_ppos->setUnitPos(p.value().toPointF(), KRPos::DontUpdateProperty);
316
} else if (p.name() == "Size") {
317
m_psize->setUnitSize(p.value().toSizeF(), KRPos::DontUpdateProperty);
320
setSceneRect(m_ppos->toScene(), m_psize->toScene(), DontUpdateProperty);
323
void KoReportDesignerItemRectBase::move(const QPointF& m)