KReport

KReportDesignerSectionScene.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 "KReportDesignerSectionScene.h"
20 #include "KReportDesignerItemRectBase.h"
21 #include "KReportDesigner.h"
22 #include "KReportLabelSizeInfo.h"
23 #include "KReportUtils_p.h"
24 #include "kreport_debug.h"
25 
26 #include <QPainter>
27 #include <QApplication>
28 #include <QGraphicsItem>
29 #include <QGraphicsSceneMouseEvent>
30 
31 KReportDesignerSectionScene::KReportDesignerSectionScene(qreal w, qreal h, KReportDesigner *rd)
32  : QGraphicsScene(0, 0, w, h, rd), m_rd(rd)
33 {
34  m_dpiX = KReportPrivate::dpiX();
35  m_dpiY = KReportPrivate::dpiY();
36 
37  if (m_unit.type() != m_rd->pageUnit().type()) {
38  m_unit = m_rd->pageUnit();
39  if (m_unit.type() == KReportUnit::Type::Cicero ||
40  m_unit.type() == KReportUnit::Type::Pica ||
41  m_unit.type() == KReportUnit::Type::Millimeter) {
42  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX;
43  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY;
44  } else if (m_unit.type() == KReportUnit::Type::Point) {
45  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX;
46  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY;
47  } else {
48  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiX;
49  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiY;
50  }
51  }
52 }
53 KReportDesignerSectionScene::~KReportDesignerSectionScene()
54 {
55  // Qt should be handling everything for us
56 }
57 
58 void KReportDesignerSectionScene::drawBackground(QPainter* painter, const QRectF & clip)
59 {
60  //Draw the default background colour
61  QGraphicsScene::drawBackground(painter, clip);
62  painter->setRenderHint(QPainter::Antialiasing, false);
63 
64  if (m_rd->propertySet()->property("grid-visible").value().toBool()) {
65  m_unit = m_rd->pageUnit();
66  if (m_unit.type() == KReportUnit::Type::Cicero ||
67  m_unit.type() == KReportUnit::Type::Pica ||
68  m_unit.type() == KReportUnit::Type::Millimeter) {
69  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX;
70  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY;
71  } else if (m_unit.type() == KReportUnit::Type::Point) {
72  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX;
73  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY;
74  } else {
75  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiX;
76  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiY;
77  }
78 
79  int minorSteps = m_rd->propertySet()->property("grid-divisions").value().toInt();
80  m_pixelIncrementX = (m_majorX / minorSteps);
81  m_pixelIncrementY = (m_majorY / minorSteps);
82 
83  QPen pen = painter->pen();
84  painter->setPen(Qt::lightGray);
85 
86  //kreportDebug() << "dpix" << KReportPrivate::dpiX() << "dpiy" << KReportPrivate::dpiY() << "mayorx:" << majorx << "majory" << majory << "pix:" << pixel_incrementx << "piy:" << pixel_incrementy;
87 
88  QVector<QLine> lines;
89  QVector<QPoint> points;
90 
91  if (m_pixelIncrementX > 2) { // do not bother painting points if increments are so small
92  int wpoints = qRound(sceneRect().width() / m_pixelIncrementX);
93  int hpoints = qRound(sceneRect().height() / m_pixelIncrementY);
94  for (int i = 0; i < wpoints; ++i) {
95  for (int j = 0; j < hpoints; ++j) {
96  //if (clip.contains(i * pixel_incrementx, j * pixel_incrementy)){
97  if (i % minorSteps == 0 && j % minorSteps == 0) {
98  lines << QLine(QPoint(i * m_pixelIncrementX, j * m_pixelIncrementY), QPoint(i * m_pixelIncrementX, j * m_pixelIncrementY + m_majorX));
99  //painter->drawLine();
100  lines << QLine(QPoint(i * m_pixelIncrementX, j * m_pixelIncrementY), QPoint(i * m_pixelIncrementX + m_majorY, j * m_pixelIncrementY));
101  //painter->drawLine();
102  } else {
103  points << QPoint(i * m_pixelIncrementX, j * m_pixelIncrementY);
104  //painter->drawPoint();
105  }
106  //}
107  }
108  }
109  painter->drawPoints(points);
110  painter->drawLines(lines);
111 
112  }
113 
114  pen.setWidth(1);
115  //update ( clip );
116  }
117 }
118 
119 void KReportDesignerSectionScene::mousePressEvent(QGraphicsSceneMouseEvent * e)
120 {
121  // clear the selection if Shift Key has not been pressed and it's a left mouse button and
122  // if the right mouse button has been pressed over an item which is not part of selected items
123  if (((e->modifiers() & Qt::ShiftModifier) == 0 && e->button() == Qt::LeftButton) ||
124  (!(selectedItems().contains(itemAt(e->scenePos(), QTransform()))) && e->button() == Qt::RightButton))
125  clearSelection();
126 
127  //This will be caught by the section to display its properties, if an item is under the cursor then they will display their properties
128  QGraphicsItem* itemUnderCursor = itemAt(e->scenePos(), QTransform());
129  emit clicked();
130 
131  KReportDesignerItemRectBase *rectUnderCursor = qgraphicsitem_cast< KReportDesignerItemRectBase* >(itemUnderCursor);
132  if (itemUnderCursor && !rectUnderCursor) {
133  rectUnderCursor = qgraphicsitem_cast< KReportDesignerItemRectBase* >(itemUnderCursor->parentItem());
134  }
135  exitInlineEditingModeInItems(rectUnderCursor);
136 
138 }
139 
140 void KReportDesignerSectionScene::contextMenuEvent(QGraphicsSceneContextMenuEvent * e)
141 {
142  m_rd->sectionContextMenuEvent(this, e);
143 }
144 
145 QPointF KReportDesignerSectionScene::gridPoint(const QPointF& p)
146 {
147  if (!m_rd->propertySet()->property("grid-snap").value().toBool()) {
148  return p;
149  }
150 
151  if (m_unit.type() != m_rd->pageUnit().type()) {
152  m_unit = m_rd->pageUnit();
153  if (m_unit.type() == KReportUnit::Type::Cicero ||
154  m_unit.type() == KReportUnit::Type::Pica ||
155  m_unit.type() == KReportUnit::Type::Millimeter) {
156  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiX;
157  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(10)) * m_dpiY;
158  } else if (m_unit.type() == KReportUnit::Type::Point) {
159  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiX;
160  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(100)) * m_dpiY;
161  } else {
162  m_majorX = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiX;
163  m_majorY = POINT_TO_INCH(m_unit.fromUserValue(1)) * m_dpiY;
164  }
165  }
166  int minorSteps = m_rd->propertySet()->property("grid-divisions").value().toInt();
167  m_pixelIncrementX = (m_majorX / minorSteps);
168  m_pixelIncrementY = (m_majorY / minorSteps);
169 
170  return QPointF(qRound((p.x() / m_pixelIncrementX)) * m_pixelIncrementX, qRound((p.y() / m_pixelIncrementY)) * m_pixelIncrementY);
171 }
172 
173 void KReportDesignerSectionScene::focusOutEvent(QFocusEvent * focusEvent)
174 {
175  exitInlineEditingModeInItems(nullptr);
176 
177  emit lostFocus();
178  QGraphicsScene::focusOutEvent(focusEvent);
179 }
180 
181 qreal KReportDesignerSectionScene::lowestZValue()
182 {
183  qreal z;
184  qreal zz;
185  z = 0;
186  QGraphicsItemList list = items();
187  for (QGraphicsItemList::iterator it = list.begin(); it != list.end(); ++it) {
188  zz = (*it)->zValue();
189  if (zz < z) {
190  z = zz;
191  }
192 
193  }
194  return z;
195 }
196 
197 qreal KReportDesignerSectionScene::highestZValue()
198 {
199  qreal z;
200  qreal zz;
201  z = 0;
202  QGraphicsItemList list = items();
203  for (QGraphicsItemList::iterator it = list.begin(); it != list.end(); ++it) {
204  zz = (*it)->zValue();
205  if (zz > z) {
206  z = zz;
207  }
208  }
209  return z;
210 }
211 
212 void KReportDesignerSectionScene::lowerSelected()
213 {
214  QGraphicsItemList list = selectedItems();
216  it != list.end(); ++it) {
217  (*it)->setZValue(lowestZValue() - 1);
218  }
219 }
220 
221 void KReportDesignerSectionScene::raiseSelected()
222 {
223  QGraphicsItemList list = selectedItems();
225  it != list.end(); ++it) {
226  (*it)->setZValue(highestZValue() + 1);
227  }
228 }
229 
230 QGraphicsItemList KReportDesignerSectionScene::itemsOrdered() const
231 {
233  QGraphicsItemList list = items();
234  for (QGraphicsItemList::iterator it = list.begin(); it != list.end(); ++it) {
235  for (QGraphicsItemList::iterator rit = r.begin(); rit != r.end(); ++rit) {
236 
237  }
238  }
239 
240  return r;
241 }
242 
243 void KReportDesignerSectionScene::exitInlineEditingModeInItems(KReportDesignerItemRectBase *rectUnderCursor)
244 {
245  foreach(QGraphicsItem *it, items()) {
246  KReportDesignerItemRectBase *itm = qgraphicsitem_cast< KReportDesignerItemRectBase* >(it);
247  if (itm && itm != rectUnderCursor) {
248  itm->exitInlineEditingMode();
249  }
250  }
251 }
QGraphicsItem * parentItem() const const
void setPen(const QColor &color)
virtual void focusOutEvent(QFocusEvent *focusEvent)
const QPen & pen() const const
virtual void drawBackground(QPainter *painter, const QRectF &rect)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
LeftButton
Qt::MouseButton button() const const
@ Point
Postscript point, 1/72th of an Inco.
void setWidth(int width)
virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
qreal x() const const
qreal y() const const
void drawPoints(const QPointF *points, int pointCount)
Base class for rectangular report items used within the designer GUI.
QList::iterator begin()
The ReportDesigner is the main widget for designing a report.
void setRenderHint(QPainter::RenderHint hint, bool on)
QList::iterator end()
void drawLines(const QLineF *lines, int lineCount)
ShiftModifier
Qt::KeyboardModifiers modifiers() const const
lightGray
QPointF scenePos() const const
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.