Warning, file /libraries/kreport/src/renderer/odtframe/KoOdtFrameReportCheckBox.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (C) 2011, 2012 by Dag Andersen (danders@get2net.dk) 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2.1 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KoOdtFrameReportCheckBox.h" 0021 #include <KoXmlWriter.h> 0022 #include <KoOdfGraphicStyles.h> 0023 #include <KoGenStyle.h> 0024 #include <KoGenStyles.h> 0025 #include <KReportUnit.h> 0026 #include <KoStore.h> 0027 #include <KoStoreDevice.h> 0028 0029 #include "KReportRenderObjects.h" 0030 0031 #include <QPainter> 0032 #include <QPen> 0033 #include <QImage> 0034 #include "kreport_debug.h" 0035 #include <QMimeDatabase> 0036 #include <QMimeType> 0037 0038 KoOdtFrameReportCheckBox::KoOdtFrameReportCheckBox(OROCheckBox *primitive) 0039 : KoOdtFrameReportPrimitive(primitive) 0040 { 0041 } 0042 0043 KoOdtFrameReportCheckBox::~KoOdtFrameReportCheckBox() 0044 { 0045 } 0046 0047 OROCheckBox *KoOdtFrameReportCheckBox::checkBox() const 0048 { 0049 return static_cast<OROCheckBox*>(m_primitive); 0050 } 0051 0052 void KoOdtFrameReportCheckBox::createStyle(KoGenStyles *coll) 0053 { 0054 KoGenStyle gs(KoGenStyle::GraphicStyle, "graphic"); 0055 gs.addProperty("draw:fill", "none"); 0056 gs.addPropertyPt("fo:margin", 0); 0057 gs.addProperty("style:horizontal-pos", "from-left"); 0058 gs.addProperty("style:horizontal-rel", "page"); 0059 gs.addProperty("style:vertical-pos", "from-top"); 0060 gs.addProperty("style:vertical-rel", "page"); 0061 gs.addProperty("style:wrap", "dynamic"); 0062 gs.addPropertyPt("style:wrap-dynamic-threshold", 0); 0063 0064 QPen pen; 0065 qreal weight = checkBox()->lineStyle().weight; 0066 if (weight < 1.0) { 0067 weight = 1.0; 0068 } 0069 pen.setWidthF(weight); 0070 pen.setColor(checkBox()->lineStyle().lineColor); 0071 pen.setStyle(checkBox()->lineStyle().style); 0072 KoOdfGraphicStyles::saveOdfStrokeStyle(gs, coll, pen); 0073 0074 m_frameStyleName = coll->insert(gs, "F"); 0075 } 0076 0077 void KoOdtFrameReportCheckBox::createBody(KoXmlWriter *bodyWriter) const 0078 { 0079 bodyWriter->startElement("draw:frame"); 0080 bodyWriter->addAttribute("draw:id", itemName()); 0081 bodyWriter->addAttribute("xml:id", itemName()); 0082 bodyWriter->addAttribute("draw:name", itemName()); 0083 bodyWriter->addAttribute("text:anchor-type", "page"); 0084 bodyWriter->addAttribute("text:anchor-page-number", pageNumber()); 0085 bodyWriter->addAttribute("draw:style-name", m_frameStyleName); 0086 0087 commonAttributes(bodyWriter); 0088 0089 bodyWriter->startElement("draw:image"); 0090 bodyWriter->addAttribute("xlink:href", "Pictures/" + imageName()); 0091 bodyWriter->addAttribute("xlink:type", "simple"); 0092 bodyWriter->addAttribute("xlink:show", "embed"); 0093 bodyWriter->addAttribute("xlink:actuate", "onLoad"); 0094 bodyWriter->endElement(); // draw:image 0095 0096 bodyWriter->endElement(); // draw:frame 0097 } 0098 0099 QString KoOdtFrameReportCheckBox::imageName() const 0100 { 0101 return QString("Checkbox_%1.png").arg(m_uid); 0102 } 0103 0104 bool KoOdtFrameReportCheckBox::saveData(KoStore* store, KoXmlWriter* manifestWriter) const 0105 { 0106 QString name = "Pictures/" + imageName(); 0107 if (!store->open(name)) { 0108 return false; 0109 } 0110 OROCheckBox * chk = checkBox(); 0111 QSizeF sz = chk->size(); 0112 QPen fpen; // frame pen 0113 if (chk->lineStyle().style == Qt::NoPen || chk->lineStyle().weight <= 0) { 0114 fpen = QPen(Qt::lightGray); 0115 } else { 0116 fpen = QPen(chk->lineStyle().lineColor, chk->lineStyle().weight, chk->lineStyle().style); 0117 } 0118 QPointF ps(fpen.widthF(), fpen.widthF()); 0119 QRectF rc = QRectF(0, 0, sz.width() + (ps.x()*2), sz.height() + (ps.y()*2)); 0120 0121 QPainter painter; 0122 QImage image(rc.size().toSize(), QImage::Format_ARGB32); 0123 image.fill(0); 0124 painter.begin(&image); 0125 painter.setBackgroundMode(Qt::OpaqueMode); 0126 painter.setRenderHint(QPainter::Antialiasing); 0127 0128 qreal ox = sz.width() / 5; 0129 qreal oy = sz.height() / 5; 0130 0131 //Checkbox Style 0132 if (chk->checkType() == "Cross") { 0133 painter.drawRoundedRect(rc.adjusted(ps.x(), ps.y(), -ps.x(), -ps.y()), sz.width() / 10 , sz.height() / 10); 0134 0135 if (chk->value()) { 0136 QPen lp; 0137 lp.setColor(chk->foregroundColor()); 0138 lp.setWidth(ox > oy ? oy : ox); 0139 painter.setPen(lp); 0140 QRectF r = rc.adjusted(ox + ps.x(), oy + ps.y(), -(ox + ps.x()), -(oy + ps.y())); 0141 painter.drawLine(r.topLeft(), r.bottomRight()); 0142 painter.drawLine(r.bottomLeft(), r.topRight()); 0143 } 0144 } else if (chk->checkType() == "Dot") { 0145 //Radio Style 0146 painter.drawEllipse(rc); 0147 0148 if (chk->value()) { 0149 QBrush lb(chk->foregroundColor()); 0150 painter.setBrush(lb); 0151 painter.setPen(Qt::NoPen); 0152 painter.drawEllipse(rc.center(), sz.width() / 2 - ox, sz.height() / 2 - oy); 0153 } 0154 } else { 0155 //Tickbox Style 0156 painter.drawRoundedRect(rc.adjusted(ps.x(), ps.y(), -ps.x(), -ps.y()), sz.width() / 10 , sz.height() / 10); 0157 0158 if (chk->value()) { 0159 QPen lp; 0160 lp.setColor(chk->foregroundColor()); 0161 lp.setWidth(ox > oy ? oy : ox); 0162 painter.setPen(lp); 0163 painter.drawLine(QPointF(ox, sz.height() / 2) + ps, QPointF(sz.width() / 2, sz.height() - oy) + ps); 0164 painter.drawLine(QPointF(sz.width() / 2, sz.height() - oy) + ps, QPointF(sz.width() - ox, oy) + ps); 0165 } 0166 } 0167 painter.end(); 0168 0169 KoStoreDevice device(store); 0170 bool ok = image.save(&device, "PNG"); 0171 if (ok) { 0172 QMimeDatabase db; 0173 const QString mimetype(db.mimeTypeForFile(name, QMimeDatabase::MatchExtension).name()); 0174 manifestWriter->addManifestEntry(name, mimetype); 0175 //kreportDebug() << "manifest:" << mimetype; 0176 } 0177 bool cl = store->close(); 0178 //kreportDebug()<<ok<<cl; 0179 return ok && cl; 0180 }