KReport

KReportDesignerItemLine.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2001-2007 by OpenMFG, LLC ([email protected])
3  * Copyright (C) 2007-2008 by Adam Pigg ([email protected])
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 //
34 void KReportDesignerItemLine::init(QGraphicsScene* s, KReportDesigner *r)
35 {
36  setPos(0, 0);
37  setUnit(r->pageUnit());
38 
39  nameProperty()->setValue(r->suggestEntityName(typeName()));
40 
41  setFlags(ItemIsSelectable | ItemIsMovable | ItemSendsGeometryChanges);
42 
43  setPen(QPen(Qt::black, 5));
44  setAcceptHoverEvents(true);
45 
46  if (s)
47  s->addItem(this);
48 
49  setZValue(z());
50 }
51 
52 KReportDesignerItemLine::KReportDesignerItemLine(KReportDesigner * d, QGraphicsScene * scene, const QPointF &pos)
53  : KReportDesignerItemBase(d, this)
54 {
55  init(scene, d);
56  setLineScene(QLineF(pos, QPointF(20,20)+pos));
57 
58 }
59 
60 KReportDesignerItemLine::KReportDesignerItemLine(KReportDesigner * d, QGraphicsScene * scene, const QPointF &startPos, const QPointF &endPos)
61  : KReportDesignerItemBase(d, this)
62 {
63  init(scene, d);
64  setLineScene(QLineF(startPos, endPos));
65 }
66 
67 KReportDesignerItemLine::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 
77 KReportDesignerItemLine::~KReportDesignerItemLine()
78 {
79 }
80 
81 KReportDesignerItemLine* KReportDesignerItemLine::clone()
82 {
83  QDomDocument d;
84  QDomElement e = d.createElement(QLatin1String("clone"));
85  QDomNode n;
86  buildXML(&d, &e);
87  n = e.firstChild();
88  return new KReportDesignerItemLine(n, designer(), nullptr);
89 }
90 
91 void KReportDesignerItemLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
92  QWidget *widget)
93 {
94  Q_UNUSED(option);
95  Q_UNUSED(widget);
96 
97  painter->setRenderHint(QPainter::Antialiasing, true);
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 
114 void 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 
131 void 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 
155 void KReportDesignerItemLine::mousePressEvent(QGraphicsSceneMouseEvent * event)
156 {
157  designer()->changeSet(propertySet());
158  setSelected(true);
160 }
161 
162 QVariant KReportDesignerItemLine::itemChange(GraphicsItemChange change, const QVariant &value)
163 {
164  return QGraphicsItem::itemChange(change, value);
165 }
166 
167 void KReportDesignerItemLine::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
168 {
170 }
171 
172 void 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 
215 int 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 
229 void KReportDesignerItemLine::hoverMoveEvent(QGraphicsSceneHoverEvent * event)
230 {
231  if (isSelected()) {
232  m_grabAction = grabHandle(event->pos());
233  switch (m_grabAction) {
234  case 1: //Point 1
235  setCursor(Qt::SizeAllCursor);
236  break;
237  case 2: //Point 2
238  setCursor(Qt::SizeAllCursor);
239  break;
240  default:
241  unsetCursor();
242  }
243  }
244 }
245 
246 void KReportDesignerItemLine::setLineScene(const QLineF &line)
247 {
248  setStartPosition(positionFromScene(line.p1()));
249  setEndPosition(positionFromScene(line.p2()));
250  setLine(line);
251 }
252 
253 void KReportDesignerItemLine::move(const QPointF& m)
254 {
255  QPointF original = scenePosition(position());
256  original += m;
257 
258  setPosition(positionFromScene(original));
259 }
QByteArray name() const
qreal width() const const
virtual QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
void setPen(const QColor &color)
QDomNode firstChild() const const
void addItem(QGraphicsItem *item)
QCA_EXPORT void init()
const QPen & pen() const const
QDomElement createElement(const QString &tagName)
KCRASH_EXPORT void setFlags(KCrash::CrashFlags flags)
void fillRect(const QRectF &rectangle, const QBrush &brush)
KReportUnit pageUnit() const
Return the current unit assigned to the report.
void setAttribute(const QString &name, const QString &value)
SizeAllCursor
int value() const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QString suggestEntityName(const QString &name) const
Return a unique name that can be used by the entity.
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
qreal x() const const
qreal y() const const
QDomNode appendChild(const QDomNode &newChild)
void update(Part *part, const QByteArray &data, qint64 dataSize)
void drawLine(const QLineF &line)
QVariant value() const
The ReportDesigner is the main widget for designing a report.
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
qreal height() const const
void setRenderHint(QPainter::RenderHint hint, bool on)
PenStyle
QString toString() const const
QPointF p1() const const
QPointF p2() const const
Base class for report items used within the designer GUI.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 04:17:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.