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
KoOdtFrameReportCheckBox(OROCheckBox * primitive)38 KoOdtFrameReportCheckBox::KoOdtFrameReportCheckBox(OROCheckBox *primitive)
39 : KoOdtFrameReportPrimitive(primitive)
40 {
41 }
42
~KoOdtFrameReportCheckBox()43 KoOdtFrameReportCheckBox::~KoOdtFrameReportCheckBox()
44 {
45 }
46
checkBox() const47 OROCheckBox *KoOdtFrameReportCheckBox::checkBox() const
48 {
49 return static_cast<OROCheckBox*>(m_primitive);
50 }
51
createStyle(KoGenStyles * coll)52 void 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
createBody(KoXmlWriter * bodyWriter) const77 void 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
imageName() const99 QString KoOdtFrameReportCheckBox::imageName() const
100 {
101 return QString("Checkbox_%1.png").arg(m_uid);
102 }
103
saveData(KoStore * store,KoXmlWriter * manifestWriter) const104 bool 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;
122 QImage image(rc.size().toSize(), QImage::Format_ARGB32);
123 image.fill(0);
124 painter.begin(&image);
125 painter.setBackgroundMode(Qt::OpaqueMode);
126 painter.setRenderHint(QPainter::Antialiasing);
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;
173 const QString mimetype(db.mimeTypeForFile(name, QMimeDatabase::MatchExtension).name());
174 manifestWriter->addManifestEntry(name, mimetype);
175 //kreportDebug() << "manifest:" << mimetype;
176 }
177 bool cl = store->close();
178 //kreportDebug()<<ok<<cl;
179 return ok && cl;
180 }
181