KReport

KReportDesignerItemLine.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 2001-2007 by OpenMFG, LLC (info@openmfg.com)
3 * Copyright (C) 2007-2008 by Adam Pigg (adam@piggz.co.uk)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "KReportDesignerItemLine.h"
20#include "KReportDesignerItemBase.h"
21#include "KReportDesigner.h"
22#include "KReportDesignerSectionScene.h"
23#include "KReportUtils.h"
24#include "KReportLineStyle.h"
25
26#include <QDomDocument>
27#include <QPainter>
28#include <QGraphicsScene>
29#include <QGraphicsSceneMouseEvent>
30
31//
32// class ReportEntityLine
33//
34void KReportDesignerItemLine::init(QGraphicsScene* s, KReportDesigner *r)
35{
36 setPos(0, 0);
37 setUnit(r->pageUnit());
38
39 nameProperty()->setValue(r->suggestEntityName(typeName()));
40
42
45
46 if (s)
47 s->addItem(this);
48
49 setZValue(z());
50}
51
52KReportDesignerItemLine::KReportDesignerItemLine(KReportDesigner * d, QGraphicsScene * scene, const QPointF &pos)
54{
55 init(scene, d);
56 setLineScene(QLineF(pos, QPointF(20,20)+pos));
57
58}
59
60KReportDesignerItemLine::KReportDesignerItemLine(KReportDesigner * d, QGraphicsScene * scene, const QPointF &startPos, const QPointF &endPos)
62{
63 init(scene, d);
64 setLineScene(QLineF(startPos, endPos));
65}
66
67KReportDesignerItemLine::KReportDesignerItemLine(const QDomNode & entity, KReportDesigner * d, QGraphicsScene * scene)
68 : KReportItemLine(entity), KReportDesignerItemBase(d, this)
69{
70 init(scene, d);
71 QPointF s = scenePosition(startPosition());
72 QPointF e = scenePosition(endPosition());
73
74 setLine ( s.x(), s.y(), e.x(), e.y() );
75}
76
77KReportDesignerItemLine::~KReportDesignerItemLine()
78{
79}
80
81KReportDesignerItemLine* KReportDesignerItemLine::clone()
82{
85 QDomNode n;
86 buildXML(&d, &e);
87 n = e.firstChild();
88 return new KReportDesignerItemLine(n, designer(), nullptr);
89}
90
91void KReportDesignerItemLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
92 QWidget *widget)
93{
94 Q_UNUSED(option);
95 Q_UNUSED(widget);
96
98 QPen p = painter->pen();
99 painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
100 painter->drawLine(line());
101 if (isSelected()) {
102
103 // draw a selected border for visual purposes
104 painter->setPen(QPen(QColor(128, 128, 255), 0, Qt::DotLine));
105 QPointF pt = line().p1();
106 painter->fillRect(pt.x(), pt.y() - 2, 5, 5, QColor(128, 128, 255));
107 pt = line().p2();
108 painter->fillRect(pt.x() - 4, pt.y() - 2, 5, 5, QColor(128, 128, 255));
109
110 painter->setPen(p);
111 }
112}
113
114void KReportDesignerItemLine::buildXML(QDomDocument *doc, QDomElement *parent)
115{
116 QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
117
118 // properties
119 addPropertyAsAttribute(&entity, nameProperty());
120 entity.setAttribute(QLatin1String("report:z-index"), zValue());
121 KReportUtils::setAttribute(&entity, QLatin1String("svg:x1"), startPosition().x());
122 KReportUtils::setAttribute(&entity, QLatin1String("svg:y1"), startPosition().y());
123 KReportUtils::setAttribute(&entity, QLatin1String("svg:x2"), endPosition().x());
124 KReportUtils::setAttribute(&entity, QLatin1String("svg:y2"), endPosition().y());
125
126 buildXMLLineStyle(doc, &entity, lineStyle());
127
128 parent->appendChild(entity);
129}
130
131void KReportDesignerItemLine::propertyChanged(KPropertySet &s, KProperty &p)
132{
133 Q_UNUSED(s);
134
135 if (p.name() == "startposition" || p.name() == "endposition") {
136 QPointF s = scenePosition(startPosition());
137 QPointF e = scenePosition(endPosition());
138
139 setLine ( s.x(), s.y(), e.x(), e.y() );
140 }
141 else if (p.name() == "name") {
142 //For some reason p.oldValue returns an empty string
143 if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
144 p.setValue(oldName());
145 } else {
146 setOldName(p.value().toString());
147 }
148 }
149 if (designer())
150 designer()->setModified(true);
151
152 update();
153}
154
155void KReportDesignerItemLine::mousePressEvent(QGraphicsSceneMouseEvent * event)
156{
157 designer()->changeSet(propertySet());
158 setSelected(true);
160}
161
162QVariant KReportDesignerItemLine::itemChange(GraphicsItemChange change, const QVariant &value)
163{
164 return QGraphicsItem::itemChange(change, value);
165}
166
167void KReportDesignerItemLine::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
168{
170}
171
172void KReportDesignerItemLine::mouseMoveEvent(QGraphicsSceneMouseEvent * event)
173{
174 int x;
175 int y;
176
177 KReportDesignerSectionScene *section = qobject_cast<KReportDesignerSectionScene*>(scene());
178 if (!section) {
179 return;
180 }
181
182 QPointF p = section->gridPoint(event->scenePos());
183 //kreportDebug() << p;
184 x = p.x();
185 y = p.y();
186
187 if (x < 0) x = 0;
188 if (y < 0) y = 0;
189 if (x > scene()->width()) x = scene()->width();
190 if (y > scene()->height()) y = scene()->height();
191
192 switch (m_grabAction) {
193 case 1:
194 setStartPosition(positionFromScene(QPointF(x,y)));
195 break;
196 case 2:
197 setEndPosition(positionFromScene(QPointF(x,y)));
198 break;
199 default:
200 QPointF d = mapToItem(this, section->gridPoint(event->scenePos())) - mapToItem(this, section->gridPoint(event->lastScenePos()));
201
202 if (((line().p1() + d).x() >= 0) &&
203 ((line().p2() + d).x() >= 0) &&
204 ((line().p1() + d).y() >= 0) &&
205 ((line().p2() + d).y() >= 0) &&
206 ((line().p1() + d).x() <= scene()->width()) &&
207 ((line().p2() + d).x() <= scene()->width()) &&
208 ((line().p1() + d).y() <= scene()->height()) &&
209 ((line().p2() + d).y() <= scene()->height()))
210 setLineScene(QLineF(line().p1() + d, line().p2() + d));
211 break;
212 }
213}
214
215int KReportDesignerItemLine::grabHandle(const QPointF &pos)
216{
217 if (QRectF(line().p1().x(), line().p1().y() - 2, 5, 5).contains(pos)) {
218 // we are over point 1
219 return 1;
220 } else if (QRectF(line().p2().x() - 4, line().p2().y() - 2, 5, 5).contains(pos)) {
221 // we are over point 2
222 return 2;
223 } else {
224 return 0;
225 }
226
227}
228
229void KReportDesignerItemLine::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
230{
231 if (isSelected()) {
232 m_grabAction = grabHandle(event->pos());
233 switch (m_grabAction) {
234 case 1: //Point 1
236 break;
237 case 2: //Point 2
239 break;
240 default:
241 unsetCursor();
242 }
243 }
244}
245
246void KReportDesignerItemLine::setLineScene(const QLineF &line)
247{
248 setStartPosition(positionFromScene(line.p1()));
249 setEndPosition(positionFromScene(line.p2()));
250 setLine(line);
251}
252
253void KReportDesignerItemLine::move(const QPointF& m)
254{
255 QPointF original = scenePosition(position());
256 original += m;
257
259}
QVariant value() const
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QByteArray name() const
Base class for report items used within the designer GUI.
The ReportDesigner is the main widget for designing a report.
KReportUnit pageUnit() const
Return the current unit assigned to the report.
QString suggestEntityName(const QString &name) const
Return a unique name that can be used by the entity.
void setModified(bool modified)
Sets the modified status, defaulting to true for modified.
void changeSet(KPropertySet *set)
Sets the property set for the currently selected item.
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
QPointF position() const
Return the position in points.
void setPosition(const QPointF &ptPos)
Sets position for the element.
qreal z() const
Return the z-value in points.
static QPointF positionFromScene(const QPointF &pos)
Helper function mapping from screen units to points, pos is in pixels.
QCA_EXPORT void init()
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
QDomNode firstChild() const const
bool isSelected() const const
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
QPainterPath mapToItem(const QGraphicsItem *item, const QPainterPath &path) const const
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
QPointF pos() const const
QGraphicsScene * scene() const const
void setAcceptHoverEvents(bool enabled)
void setCursor(const QCursor &cursor)
void setFlags(GraphicsItemFlags flags)
void setPos(const QPointF &pos)
void setSelected(bool selected)
void setZValue(qreal z)
void unsetCursor()
void update(const QRectF &rect)
qreal x() const const
qreal y() const const
qreal zValue() const const
virtual bool contains(const QPointF &point) const const override
QLineF line() const const
void setLine(const QLineF &line)
void setPen(const QPen &pen)
void addItem(QGraphicsItem *item)
qreal height() const const
qreal width() const const
QPointF p1() const const
QPointF p2() const const
virtual bool event(QEvent *e)
QObject * parent() const const
T qobject_cast(QObject *object)
void drawLine(const QLine &line)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
const QPen & pen() const const
void setPen(Qt::PenStyle style)
void setRenderHint(RenderHint hint, bool on)
qreal x() const const
qreal y() const const
SizeAllCursor
PenStyle
int toInt(bool *ok) const const
QString toString() const const
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.