File indexing completed on 2024-05-19 04:44:31

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2009-2010 by Adam Pigg (adam@piggz.co.uk)
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser 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  * Lesser General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Lesser General Public
0015  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #include "KReportItemCheck.h"
0019 #include "KReportRenderObjects.h"
0020 #include "KReportUtils.h"
0021 #include "KReportUtils_p.h"
0022 #include "kreportplugin_debug.h"
0023 #ifdef KREPORT_SCRIPTING
0024 #include "KReportScriptHandler.h"
0025 #endif
0026 
0027 #include <KPropertyListData>
0028 #include <KPropertySet>
0029 
0030 #include <QPalette>
0031 #include <QDomNodeList>
0032 
0033 KReportItemCheckBox::KReportItemCheckBox()
0034 {
0035     createProperties();
0036 }
0037 
0038 KReportItemCheckBox::KReportItemCheckBox(const QDomNode &element)
0039     : KReportItemCheckBox()
0040 {
0041     nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
0042     setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
0043     setZ(KReportUtils::readZAttribute(element.toElement()));
0044     m_foregroundColor->setValue(QColor(element.toElement().attribute(QLatin1String("fo:foreground-color"))));
0045     m_checkStyle->setValue(element.toElement().attribute(QLatin1String("report:check-style")));
0046     m_staticValue->setValue(QVariant(element.toElement().attribute(QLatin1String("report:value"))).toBool());
0047 
0048     parseReportRect(element.toElement());
0049 
0050     QDomNodeList nl = element.childNodes();
0051     QString n;
0052     QDomNode node;
0053     for (int i = 0; i < nl.count(); i++) {
0054         node = nl.item(i);
0055         n = node.nodeName();
0056 
0057         if (n == QLatin1String("report:line-style")) {
0058             KReportLineStyle ls;
0059             if (parseReportLineStyleData(node.toElement(), &ls)) {
0060                 m_lineWeight->setValue(ls.weight());
0061                 m_lineColor->setValue(ls.color());
0062                 m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
0063             }
0064         } else {
0065             kreportpluginWarning() << "while parsing check element encountered unknown element: " << n;
0066         }
0067     }
0068 
0069 }
0070 
0071 KReportItemCheckBox::~KReportItemCheckBox()
0072 {
0073 }
0074 
0075 void KReportItemCheckBox::createProperties()
0076 {
0077     KPropertyListData *listData = new KPropertyListData(
0078         QVariantList{ QLatin1String("Cross"), QLatin1String("Tick"), QLatin1String("Dot") },
0079         QVariantList{ tr("Cross"), tr("Tick"), tr("Dot") });
0080     m_checkStyle = new KProperty("check-style", listData, QLatin1String("Cross"), tr("Style"));
0081 
0082     createDataSourceProperty();
0083 
0084     m_foregroundColor = new KProperty("foreground-color", QColor(Qt::black), tr("Foreground Color"));
0085 
0086     m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
0087     m_lineWeight->setOption("step", 1.0);
0088     m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
0089     m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::SolidLine), tr("Line Style"), QString(), KProperty::LineStyle);
0090     m_staticValue = new KProperty("value", QVariant(false), tr("Value"), tr("Value used if not bound to a field"));
0091 
0092     propertySet()->addProperty(m_staticValue);
0093     propertySet()->addProperty(m_checkStyle);
0094     propertySet()->addProperty(m_foregroundColor);
0095     propertySet()->addProperty(m_lineWeight);
0096     propertySet()->addProperty(m_lineColor);
0097     propertySet()->addProperty(m_lineStyle);
0098 }
0099 
0100 KReportLineStyle KReportItemCheckBox::lineStyle()
0101 {
0102     KReportLineStyle ls;
0103     ls.setWeight(m_lineWeight->value().toReal());
0104     ls.setColor(m_lineColor->value().value<QColor>());
0105     ls.setPenStyle((Qt::PenStyle)m_lineStyle->value().toInt());
0106     return ls;
0107 }
0108 
0109 // RTTI
0110 QString KReportItemCheckBox::typeName() const
0111 {
0112     return QLatin1String("check");
0113 }
0114 
0115 int KReportItemCheckBox::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0116                                         const QVariant &data, KReportScriptHandler *script)
0117 {
0118     OROCheckBox *chk = new OROCheckBox();
0119 
0120     chk->setPosition(scenePosition(position()) + offset);
0121     chk->setSize(sceneSize(size()));
0122 
0123     chk->setLineStyle(lineStyle());
0124     chk->setForegroundColor(m_foregroundColor->value().value<QColor>());
0125     if (m_checkStyle->value().toString() == QLatin1String("Cross")) {
0126         chk->setCheckType(OROCheckBox::Type::Cross);
0127     } else if (m_checkStyle->value().toString() == QLatin1String("Dot")) {
0128         chk->setCheckType(OROCheckBox::Type::Dot);
0129     } else {
0130         chk->setCheckType(OROCheckBox::Type::Tick);
0131     }
0132 
0133     QString str;
0134     bool v = false;
0135     QString cs = itemDataSource();
0136 
0137     //kreportpluginDebug() << "ControlSource:" << cs;
0138     if (!cs.isEmpty()) {
0139 #ifdef KREPORT_SCRIPTING
0140         if (cs.left(1) == QLatin1String("=") && script) {
0141             str = script->evaluate(cs.mid(1)).toString();
0142         } else
0143 #else
0144         Q_UNUSED(script);
0145 #endif
0146         {
0147             str = data.toString();
0148         }
0149 
0150         str = str.toLower();
0151 
0152         //kreportpluginDebug() << "Check Value:" << str;
0153         if (str == QLatin1String("t") || str == QLatin1String("y") || str == QLatin1String("true") || str == QLatin1String("1"))
0154             v = true;
0155 
0156     } else {
0157         v = value();
0158     }
0159 
0160     chk->setValue(v);
0161 
0162     if (page) {
0163         page->insertPrimitive(chk);
0164     }
0165 
0166     if (section) {
0167         OROCheckBox *chk2 = dynamic_cast<OROCheckBox*>(chk->clone());
0168         if (chk2) {
0169             chk2->setPosition(scenePosition(position()));
0170             section->addPrimitive(chk2);
0171         }
0172     }
0173 
0174     if (!page) {
0175         delete chk;
0176     }
0177 
0178     return 0; //Item doesn't stretch the section height
0179 }
0180 
0181 bool KReportItemCheckBox::value() const
0182 {
0183     return m_staticValue->value().toBool();
0184 }
0185 
0186 void KReportItemCheckBox::setValue(bool v)
0187 {
0188     m_staticValue->setValue(v);
0189 }