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

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007-2008 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 "KReportItemLabel.h"
0019 #include "KReportRenderObjects.h"
0020 #include "KReportUtils.h"
0021 #include "kreportplugin_debug.h"
0022 
0023 #include <KPropertySet>
0024 #include <KPropertyListData>
0025 
0026 #include <QFontDatabase>
0027 #include <QPalette>
0028 #include <QDomNodeList>
0029 
0030 KReportItemLabel::KReportItemLabel()
0031 {
0032     createProperties();
0033 }
0034 
0035 KReportItemLabel::KReportItemLabel(const QDomNode & element)
0036     : KReportItemLabel()
0037 {
0038     nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
0039     m_text->setValue(element.toElement().attribute(QLatin1String("report:caption")));
0040     setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
0041     m_horizontalAlignment->setValue(element.toElement().attribute(QLatin1String("report:horizontal-align")));
0042     m_verticalAlignment->setValue(element.toElement().attribute(QLatin1String("report:vertical-align")));
0043 
0044     parseReportRect(element.toElement());
0045 
0046     QDomNodeList nl = element.childNodes();
0047     QString n;
0048     QDomNode node;
0049     for (int i = 0; i < nl.count(); i++) {
0050         node = nl.item(i);
0051         n = node.nodeName();
0052 
0053         if (n == QLatin1String("report:text-style")) {
0054             KReportTextStyleData ts;
0055             if (parseReportTextStyleData(node.toElement(), &ts)) {
0056                 m_backgroundColor->setValue(ts.backgroundColor);
0057                 m_foregroundColor->setValue(ts.foregroundColor);
0058                 m_backgroundOpacity->setValue(ts.backgroundOpacity);
0059                 m_font->setValue(ts.font);
0060 
0061             }
0062         } else if (n == QLatin1String("report:line-style")) {
0063             KReportLineStyle ls;
0064             if (parseReportLineStyleData(node.toElement(), &ls)) {
0065                 m_lineWeight->setValue(ls.weight());
0066                 m_lineColor->setValue(ls.color());
0067                 m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
0068             }
0069         } else {
0070             kreportpluginWarning() << "while parsing label element encountered unknown element: " << n;
0071         }
0072     }
0073 }
0074 
0075 KReportItemLabel::~KReportItemLabel()
0076 {
0077 }
0078 
0079 QString KReportItemLabel::text() const
0080 {
0081     return m_text->value().toString();
0082 }
0083 
0084 void KReportItemLabel::setText(const QString& t)
0085 {
0086     m_text->setValue(t);
0087 }
0088 
0089 void KReportItemLabel::createProperties()
0090 {
0091     m_text = new KProperty("caption", QLatin1String("Label"), tr("Caption"));
0092     KPropertyListData *listData = new KPropertyListData(
0093         { QLatin1String("left"), QLatin1String("center"), QLatin1String("right") },
0094         QVariantList{ tr("Left"), tr("Center"), tr("Right") });
0095     m_horizontalAlignment = new KProperty("horizontal-align", listData, QLatin1String("left"),
0096                                           tr("Horizontal Alignment"));
0097 
0098     listData = new KPropertyListData(
0099         { QLatin1String("top"), QLatin1String("center"), QLatin1String("bottom") },
0100         QVariantList{ tr("Top"), tr("Center"), tr("Bottom") });
0101     m_verticalAlignment = new KProperty("vertical-align", listData, QLatin1String("center"),
0102                                         tr("Vertical Alignment"));
0103 
0104     m_font = new KProperty("font", QFontDatabase::systemFont(QFontDatabase::GeneralFont), tr("Font"), tr("Font"));
0105 
0106     m_backgroundColor = new KProperty("background-color", QColor(Qt::white), tr("Background Color"));
0107     m_foregroundColor = new KProperty("foreground-color", QColor(Qt::black), tr("Foreground Color"));
0108 
0109     m_backgroundOpacity = new KProperty("background-opacity", QVariant(0), tr("Background Opacity"));
0110     m_backgroundOpacity->setOption("max", 100);
0111     m_backgroundOpacity->setOption("min", 0);
0112     m_backgroundOpacity->setOption("suffix", QLatin1String("%"));
0113 
0114     m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
0115     m_lineWeight->setOption("step", 1.0);
0116     m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
0117     m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::NoPen), tr("Line Style"), QString(), KProperty::LineStyle);
0118 
0119     propertySet()->addProperty(m_text);
0120     propertySet()->addProperty(m_horizontalAlignment);
0121     propertySet()->addProperty(m_verticalAlignment);
0122     propertySet()->addProperty(m_font);
0123     propertySet()->addProperty(m_backgroundColor);
0124     propertySet()->addProperty(m_foregroundColor);
0125     propertySet()->addProperty(m_backgroundOpacity);
0126     propertySet()->addProperty(m_lineWeight);
0127     propertySet()->addProperty(m_lineColor);
0128     propertySet()->addProperty(m_lineStyle);
0129 }
0130 
0131 Qt::Alignment KReportItemLabel::textFlags() const
0132 {
0133     Qt::Alignment align;
0134     QString t;
0135     t = m_horizontalAlignment->value().toString();
0136     if (t == QLatin1String("center"))
0137         align = Qt::AlignHCenter;
0138     else if (t == QLatin1String("right"))
0139         align = Qt::AlignRight;
0140     else
0141         align = Qt::AlignLeft;
0142 
0143     t = m_verticalAlignment->value().toString();
0144     if (t == QLatin1String("center"))
0145         align |= Qt::AlignVCenter;
0146     else if (t == QLatin1String("bottom"))
0147         align |= Qt::AlignBottom;
0148     else
0149         align |= Qt::AlignTop;
0150 
0151     return align;
0152 }
0153 
0154 KReportTextStyleData KReportItemLabel::textStyle() const
0155 {
0156     KReportTextStyleData d;
0157     d.backgroundColor = m_backgroundColor->value().value<QColor>();
0158     d.foregroundColor = m_foregroundColor->value().value<QColor>();
0159     d.font = m_font->value().value<QFont>();
0160     d.backgroundOpacity = m_backgroundOpacity->value().toInt();
0161     return d;
0162 }
0163 
0164 KReportLineStyle KReportItemLabel::lineStyle() const
0165 {
0166     KReportLineStyle ls;
0167     ls.setWeight(m_lineWeight->value().toReal());
0168     ls.setColor(m_lineColor->value().value<QColor>());
0169     ls.setPenStyle((Qt::PenStyle)m_lineStyle->value().toInt());
0170     return ls;
0171 }
0172 
0173 // RTTI
0174 QString KReportItemLabel::typeName() const
0175 {
0176     return QLatin1String("label");
0177 }
0178 
0179 int KReportItemLabel::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0180                                         const QVariant &data, KReportScriptHandler *script)
0181 {
0182     Q_UNUSED(data)
0183     Q_UNUSED(script)
0184 
0185     OROTextBox * tb = new OROTextBox();
0186     tb->setPosition(scenePosition(position()) + offset);
0187     tb->setSize(sceneSize(size()));
0188     tb->setFont(font());
0189     tb->setText(text());
0190     tb->setFlags(textFlags());
0191     tb->setTextStyle(textStyle());
0192     tb->setLineStyle(lineStyle());
0193 
0194     if (page) {
0195         page->insertPrimitive(tb);
0196     }
0197 
0198     if (section) {
0199         OROPrimitive *clone = tb->clone();
0200         clone->setPosition(scenePosition(position()));
0201         section->addPrimitive(clone);
0202     }
0203 
0204     if (!page) {
0205         delete tb;
0206     }
0207 
0208     return 0; //Item doesn't stretch the section height
0209 }
0210