KReport

KReportDesignerItemCheckBox.cpp
1 /* This file is part of the KDE project
2  * Copyright (C) 2009-2010 by Adam Pigg ([email protected])
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "KReportDesignerItemCheckBox.h"
19 #include "KReportDesignerItemRectBase.h"
20 #include "KReportDesigner.h"
21 #include "KReportLineStyle.h"
22 
23 #include <QDomDocument>
24 #include <QPainter>
25 #include <QGraphicsScene>
26 #include <QGraphicsSceneMouseEvent>
27 
28 //
29 // class ReportEntityCheck
30 //
31 
32 void KReportDesignerItemCheckBox::init(QGraphicsScene *scene)
33 {
34  if (scene)
35  scene->addItem(this);
36 
37  connect(propertySet(), SIGNAL(propertyChanged(KPropertySet&,KProperty&)),
38  this, SLOT(slotPropertyChanged(KPropertySet&,KProperty&)));
39 
40  setZValue(z());
41 }
42 
43 // methods (constructors)
44 KReportDesignerItemCheckBox::KReportDesignerItemCheckBox(KReportDesigner* d, QGraphicsScene * scene, const QPointF &pos)
46 {
47  Q_UNUSED(pos);
48  init(scene);
49  setSceneRect(properRect(*d, KREPORT_ITEM_CHECK_DEFAULT_WIDTH, KREPORT_ITEM_CHECK_DEFAULT_HEIGHT));
50  nameProperty()->setValue(designer()->suggestEntityName(typeName()));
51 }
52 
53 KReportDesignerItemCheckBox::KReportDesignerItemCheckBox(const QDomNode & element, KReportDesigner * d, QGraphicsScene * s)
54  : KReportItemCheckBox(element), KReportDesignerItemRectBase(d, this)
55 {
56  init(s);
57  setSceneRect(KReportItemBase::scenePosition(item()->position()), KReportItemBase::sceneSize(item()->size()));
58 }
59 
60 KReportDesignerItemCheckBox* KReportDesignerItemCheckBox::clone()
61 {
62  QDomDocument d;
63  QDomElement e = d.createElement(QLatin1String("clone"));
64  QDomNode n;
65  buildXML(&d, &e);
66  n = e.firstChild();
67  return new KReportDesignerItemCheckBox(n, designer(), nullptr);
68 }
69 
70 // methods (deconstructor)
71 KReportDesignerItemCheckBox::~KReportDesignerItemCheckBox()
72 {}
73 
74 void KReportDesignerItemCheckBox::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
75 {
76  Q_UNUSED(option);
77  Q_UNUSED(widget);
78 
79  // store any values we plan on changing so we can restore them
80  QFont f = painter->font();
81  QPen p = painter->pen();
82  QBrush b = painter->brush();
83 
86 
87  painter->setPen(m_foregroundColor->value().value<QColor>());
88 
89  if ((Qt::PenStyle)m_lineStyle->value().toInt() == Qt::NoPen || m_lineWeight->value().toInt() <= 0) {
90  painter->setPen(QPen(Qt::lightGray));
91  } else {
92  painter->setPen(QPen(m_lineColor->value().value<QColor>(), m_lineWeight->value().toInt(), (Qt::PenStyle)m_lineStyle->value().toInt()));
93  }
94 
95  QSizeF sceneSize = this->sceneSize(size());
96  qreal ox = sceneSize.width() / 5;
97  qreal oy = sceneSize.height() / 5;
98 
99  //Checkbox Style
100  if (m_checkStyle->value().toString() == QLatin1String("Cross")) {
101  painter->drawRoundedRect(QGraphicsRectItem::rect(), sceneSize.width() / 10 , sceneSize.height() / 10);
102 
103  QPen lp;
104  lp.setColor(m_foregroundColor->value().value<QColor>());
105  lp.setWidth(ox > oy ? oy : ox);
106  painter->setPen(lp);
107  painter->drawLine(ox, oy, sceneSize.width() - ox, sceneSize.height() - oy);
108  painter->drawLine(ox, sceneSize.height() - oy, sceneSize.width() - ox, oy);
109  } else if (m_checkStyle->value().toString() == QLatin1String("Dot")) {
110  //Radio Style
112 
113  QBrush lb(m_foregroundColor->value().value<QColor>());
114  painter->setBrush(lb);
115  painter->setPen(Qt::NoPen);
116  painter->drawEllipse(rect().center(), sceneSize.width() / 2 - ox, sceneSize.height() / 2 - oy);
117  } else {
118  //Tickbox Style
119  painter->drawRoundedRect(QGraphicsRectItem::rect(), sceneSize.width() / 10 , sceneSize.height() / 10);
120 
121  QPen lp;
122  lp.setColor(m_foregroundColor->value().value<QColor>());
123  lp.setWidth(ox > oy ? oy : ox);
124  painter->setPen(lp);
125  painter->drawLine(ox, sceneSize.height() / 2, sceneSize.width() / 2, sceneSize.height() - oy);
126  painter->drawLine(sceneSize.width() / 2, sceneSize.height() - oy, sceneSize.width() - ox, oy);
127 
128  }
129 
131  painter->setPen(m_foregroundColor->value().value<QColor>());
132 
133  // restore an values before we started just in case
134  painter->setFont(f);
135  painter->setPen(p);
136  painter->setBrush(b);
137 
138  drawHandles(painter);
139 }
140 
141 void KReportDesignerItemCheckBox::buildXML(QDomDocument *doc, QDomElement *parent)
142 {
143  //kreportpluginDebug();
144  QDomElement entity = doc->createElement(QLatin1String("report:") + typeName());
145 
146  //properties
147  addPropertyAsAttribute(&entity, nameProperty());
148  addPropertyAsAttribute(&entity, dataSourceProperty());
149  entity.setAttribute(QLatin1String("fo:foreground-color"), m_foregroundColor->value().toString());
150  addPropertyAsAttribute(&entity, m_checkStyle);
151  addPropertyAsAttribute(&entity, m_staticValue);
152 
153  // bounding rect
154  buildXMLRect(doc, &entity, this);
155 
156  //Line Style
157  buildXMLLineStyle(doc, &entity, lineStyle());
158 
159  parent->appendChild(entity);
160 }
161 
162 void KReportDesignerItemCheckBox::slotPropertyChanged(KPropertySet &s, KProperty &p)
163 {
164  Q_UNUSED(s)
165 
166  if (p.name() == "name") {
167  //For some reason p.oldValue returns an empty string
168  if (!designer()->isEntityNameUnique(p.value().toString(), this)) {
169  p.setValue(oldName());
170  } else {
171  setOldName(p.value().toString());
172  }
173  }
174 
175  KReportDesignerItemRectBase::propertyChanged(s, p);
176  if (designer()) designer()->setModified(true);
177 }
QByteArray name() const
qreal height() const const
void setPen(const QColor &color)
void setBackgroundMode(Qt::BGMode mode)
QDomNode firstChild() const const
void addItem(QGraphicsItem *item)
void drawEllipse(const QRectF &rectangle)
QCA_EXPORT void init()
OpaqueMode
const QPen & pen() const const
QDomElement createElement(const QString &tagName)
void setAttribute(const QString &name, const QString &value)
void setColor(const QColor &color)
static QPointF scenePosition(const QPointF &ptPos)
Helper function mapping to screen units (pixels), ptPos is in points.
void drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
static QSizeF sceneSize(const QSizeF &ptSize)
Helper function mapping to screen units (pixels), ptSize is in points.
int value() const const
void setWidth(int width)
const QBrush & brush() const const
void setBrush(const QBrush &brush)
bool setValue(const QVariant &value, ValueOptions options=ValueOptions())
QDomNode appendChild(const QDomNode &newChild)
QRectF rect() const const
Base class for rectangular report items used within the designer GUI.
void drawLine(const QLineF &line)
const QFont & font() const const
QVariant value() const
QTextStream & center(QTextStream &stream)
The ReportDesigner is the main widget for designing a report.
void setRenderHint(QPainter::RenderHint hint, bool on)
PenStyle
void setFont(const QFont &font)
lightGray
QString toString() const const
qreal width() 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.