KReport

KoOdtFrameReportCheckBox.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2011, 2012 by Dag Andersen (danders@get2net.dk)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library 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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "KoOdtFrameReportCheckBox.h"
21#include <KoXmlWriter.h>
22#include <KoOdfGraphicStyles.h>
23#include <KoGenStyle.h>
24#include <KoGenStyles.h>
25#include <KReportUnit.h>
26#include <KoStore.h>
27#include <KoStoreDevice.h>
28
29#include "KReportRenderObjects.h"
30
31#include <QPainter>
32#include <QPen>
33#include <QImage>
34#include "kreport_debug.h"
35#include <QMimeDatabase>
36#include <QMimeType>
37
38KoOdtFrameReportCheckBox::KoOdtFrameReportCheckBox(OROCheckBox *primitive)
39 : KoOdtFrameReportPrimitive(primitive)
40{
41}
42
43KoOdtFrameReportCheckBox::~KoOdtFrameReportCheckBox()
44{
45}
46
47OROCheckBox *KoOdtFrameReportCheckBox::checkBox() const
48{
49 return static_cast<OROCheckBox*>(m_primitive);
50}
51
52void KoOdtFrameReportCheckBox::createStyle(KoGenStyles *coll)
53{
54 KoGenStyle gs(KoGenStyle::GraphicStyle, "graphic");
55 gs.addProperty("draw:fill", "none");
56 gs.addPropertyPt("fo:margin", 0);
57 gs.addProperty("style:horizontal-pos", "from-left");
58 gs.addProperty("style:horizontal-rel", "page");
59 gs.addProperty("style:vertical-pos", "from-top");
60 gs.addProperty("style:vertical-rel", "page");
61 gs.addProperty("style:wrap", "dynamic");
62 gs.addPropertyPt("style:wrap-dynamic-threshold", 0);
63
64 QPen pen;
65 qreal weight = checkBox()->lineStyle().weight;
66 if (weight < 1.0) {
67 weight = 1.0;
68 }
69 pen.setWidthF(weight);
70 pen.setColor(checkBox()->lineStyle().lineColor);
71 pen.setStyle(checkBox()->lineStyle().style);
72 KoOdfGraphicStyles::saveOdfStrokeStyle(gs, coll, pen);
73
74 m_frameStyleName = coll->insert(gs, "F");
75}
76
77void KoOdtFrameReportCheckBox::createBody(KoXmlWriter *bodyWriter) const
78{
79 bodyWriter->startElement("draw:frame");
80 bodyWriter->addAttribute("draw:id", itemName());
81 bodyWriter->addAttribute("xml:id", itemName());
82 bodyWriter->addAttribute("draw:name", itemName());
83 bodyWriter->addAttribute("text:anchor-type", "page");
84 bodyWriter->addAttribute("text:anchor-page-number", pageNumber());
85 bodyWriter->addAttribute("draw:style-name", m_frameStyleName);
86
87 commonAttributes(bodyWriter);
88
89 bodyWriter->startElement("draw:image");
90 bodyWriter->addAttribute("xlink:href", "Pictures/" + imageName());
91 bodyWriter->addAttribute("xlink:type", "simple");
92 bodyWriter->addAttribute("xlink:show", "embed");
93 bodyWriter->addAttribute("xlink:actuate", "onLoad");
94 bodyWriter->endElement(); // draw:image
95
96 bodyWriter->endElement(); // draw:frame
97}
98
99QString KoOdtFrameReportCheckBox::imageName() const
100{
101 return QString("Checkbox_%1.png").arg(m_uid);
102}
103
104bool KoOdtFrameReportCheckBox::saveData(KoStore* store, KoXmlWriter* manifestWriter) const
105{
106 QString name = "Pictures/" + imageName();
107 if (!store->open(name)) {
108 return false;
109 }
110 OROCheckBox * chk = checkBox();
111 QSizeF sz = chk->size();
112 QPen fpen; // frame pen
113 if (chk->lineStyle().style == Qt::NoPen || chk->lineStyle().weight <= 0) {
114 fpen = QPen(Qt::lightGray);
115 } else {
116 fpen = QPen(chk->lineStyle().lineColor, chk->lineStyle().weight, chk->lineStyle().style);
117 }
118 QPointF ps(fpen.widthF(), fpen.widthF());
119 QRectF rc = QRectF(0, 0, sz.width() + (ps.x()*2), sz.height() + (ps.y()*2));
120
121 QPainter painter;
123 image.fill(0);
124 painter.begin(&image);
127
128 qreal ox = sz.width() / 5;
129 qreal oy = sz.height() / 5;
130
131 //Checkbox Style
132 if (chk->checkType() == "Cross") {
133 painter.drawRoundedRect(rc.adjusted(ps.x(), ps.y(), -ps.x(), -ps.y()), sz.width() / 10 , sz.height() / 10);
134
135 if (chk->value()) {
136 QPen lp;
137 lp.setColor(chk->foregroundColor());
138 lp.setWidth(ox > oy ? oy : ox);
139 painter.setPen(lp);
140 QRectF r = rc.adjusted(ox + ps.x(), oy + ps.y(), -(ox + ps.x()), -(oy + ps.y()));
141 painter.drawLine(r.topLeft(), r.bottomRight());
142 painter.drawLine(r.bottomLeft(), r.topRight());
143 }
144 } else if (chk->checkType() == "Dot") {
145 //Radio Style
146 painter.drawEllipse(rc);
147
148 if (chk->value()) {
149 QBrush lb(chk->foregroundColor());
150 painter.setBrush(lb);
151 painter.setPen(Qt::NoPen);
152 painter.drawEllipse(rc.center(), sz.width() / 2 - ox, sz.height() / 2 - oy);
153 }
154 } else {
155 //Tickbox Style
156 painter.drawRoundedRect(rc.adjusted(ps.x(), ps.y(), -ps.x(), -ps.y()), sz.width() / 10 , sz.height() / 10);
157
158 if (chk->value()) {
159 QPen lp;
160 lp.setColor(chk->foregroundColor());
161 lp.setWidth(ox > oy ? oy : ox);
162 painter.setPen(lp);
163 painter.drawLine(QPointF(ox, sz.height() / 2) + ps, QPointF(sz.width() / 2, sz.height() - oy) + ps);
164 painter.drawLine(QPointF(sz.width() / 2, sz.height() - oy) + ps, QPointF(sz.width() - ox, oy) + ps);
165 }
166 }
167 painter.end();
168
169 KoStoreDevice device(store);
170 bool ok = image.save(&device, "PNG");
171 if (ok) {
172 QMimeDatabase db;
174 manifestWriter->addManifestEntry(name, mimetype);
175 //kreportDebug() << "manifest:" << mimetype;
176 }
177 bool cl = store->close();
178 //kreportDebug()<<ok<<cl;
179 return ok && cl;
180}
Defines checkbox.
QString name(GameStandardAction id)
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
QMimeType mimeTypeForFile(const QFileInfo &fileInfo, MatchMode mode) const const
bool begin(QPaintDevice *device)
void drawEllipse(const QPoint &center, int rx, int ry)
void drawLine(const QLine &line)
void drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
bool end()
void setBackgroundMode(Qt::BGMode mode)
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
void setRenderHint(RenderHint hint, bool on)
void setColor(const QColor &color)
void setStyle(Qt::PenStyle style)
void setWidth(int width)
void setWidthF(qreal width)
qreal widthF() const const
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
QPointF bottomLeft() const const
QPointF bottomRight() const const
QPointF center() const const
QSizeF size() const const
QPointF topLeft() const const
QPointF topRight() const const
qreal height() const const
QSize toSize() const const
qreal width() const const
QString arg(Args &&... args) const const
QString & insert(qsizetype position, QChar ch)
OpaqueMode
lightGray
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.