Warning, file /libraries/kreport/src/common/KReportItemLine.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) 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 "KReportItemLine.h"
0019 #include "KReportRenderObjects.h"
0020 #include "KReportUtils.h"
0021 #include "KReportUtils_p.h"
0022 #include "kreport_debug.h"
0023 
0024 #include <KPropertySet>
0025 
0026 #include <QCoreApplication>
0027 #include <QDomNode>
0028 
0029 KReportItemLine::KReportItemLine()
0030 {
0031     createProperties();
0032 }
0033 
0034 KReportItemLine::KReportItemLine(const QDomNode & element)
0035     : KReportItemLine()
0036 {
0037     nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
0038     setZ(KReportUtils::readZAttribute(element.toElement()));
0039 
0040     const QPointF s(KReportUnit::parseValue(element.toElement().attribute(
0041                         QLatin1String("svg:x1"), DEFAULT_ELEMENT_POS_STRING)),
0042                     KReportUnit::parseValue(element.toElement().attribute(
0043                         QLatin1String("svg:y1"), DEFAULT_ELEMENT_POS_STRING)));
0044     const QPointF e(KReportUnit::parseValue(element.toElement().attribute(
0045                         QLatin1String("svg:x2"), DEFAULT_ELEMENT_POS_STRING)),
0046                     KReportUnit::parseValue(element.toElement().attribute(
0047                         QLatin1String("svg:y2"),
0048                         QString::number(POINT_TO_CM(DEFAULT_ELEMENT_POS_PT.y()
0049                                                     + DEFAULT_ELEMENT_SIZE_PT.height()))
0050                             + QLatin1String("cm"))));
0051     setStartPosition(s);
0052     setEndPosition(e);
0053 
0054     QDomNodeList nl = element.childNodes();
0055     QString n;
0056     QDomNode node;
0057     for (int i = 0; i < nl.count(); i++) {
0058         node = nl.item(i);
0059         n = node.nodeName();
0060 
0061         if (n == QLatin1String("report:line-style")) {
0062             KReportLineStyle ls;
0063             if (parseReportLineStyleData(node.toElement(), &ls)) {
0064                 m_lineWeight->setValue(ls.weight());
0065                 m_lineColor->setValue(ls.color());
0066                 m_lineStyle->setValue(static_cast<int>(ls.penStyle()));
0067             }
0068         } else {
0069             kreportWarning() << "while parsing line element encountered unknown element: " << n;
0070         }
0071     }
0072 }
0073 
0074 KReportItemLine::~KReportItemLine()
0075 {
0076 }
0077 
0078 void KReportItemLine::createProperties()
0079 {
0080     m_start = new KProperty("startposition", QPointF(), QCoreApplication::translate("StartPosition", "Start Position"));
0081     m_end = new KProperty("endposition", QPointF(), QCoreApplication::translate("EndPosition", "End Position"));
0082 
0083     m_lineWeight = new KProperty("line-weight", 1.0, tr("Line Weight"));
0084     m_lineWeight->setOption("step", 1.0);
0085     m_lineColor = new KProperty("line-color", QColor(Qt::black), tr("Line Color"));
0086     m_lineStyle = new KProperty("line-style", static_cast<int>(Qt::SolidLine), tr("Line Style"),
0087                                 QString(), KProperty::LineStyle);
0088 
0089     //Remove the unused properies from KReportItemBase
0090     propertySet()->property("size").setVisible(false);
0091     propertySet()->property("position").setVisible(false);
0092 
0093     propertySet()->addProperty(m_start);
0094     propertySet()->addProperty(m_end);
0095     propertySet()->addProperty(m_lineWeight);
0096     propertySet()->addProperty(m_lineColor);
0097     propertySet()->addProperty(m_lineStyle);
0098 }
0099 
0100 KReportLineStyle KReportItemLine::lineStyle() const
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 qreal KReportItemLine::weight() const
0110 {
0111     return m_lineWeight->value().toReal();
0112 }
0113 
0114 void KReportItemLine::setWeight(qreal w)
0115 {
0116     m_lineWeight->setValue(w);
0117 }
0118 
0119 QString KReportItemLine::typeName() const
0120 {
0121     return QLatin1String("line");
0122 }
0123 
0124 int KReportItemLine::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0125                                        const QVariant &data, KReportScriptHandler *script)
0126 {
0127     Q_UNUSED(script)
0128     Q_UNUSED(data)
0129 
0130     OROLine * ln = new OROLine();
0131     QPointF s = scenePosition(startPosition());
0132     QPointF e = scenePosition(endPosition());
0133 
0134     s += offset;
0135     e += offset;
0136 
0137     ln->setStartPoint(s);
0138     ln->setEndPoint(e);
0139     ln->setLineStyle(lineStyle());
0140     if (page) page->insertPrimitive(ln);
0141 
0142     OROLine *l2 = dynamic_cast<OROLine*>(ln->clone());
0143     if (l2) {
0144         l2->setStartPoint(startPosition());
0145         l2->setEndPoint(endPosition());
0146 
0147         if (section) section->addPrimitive(l2);
0148     }
0149     return 0;
0150 }
0151 
0152 void KReportItemLine::setUnit(const KReportUnit &u)
0153 {
0154     if (unit() == u) {
0155         return;
0156     }
0157     KReportUnit oldunit = unit();
0158     KReportItemBase::setUnit(u);
0159     
0160     // convert values
0161     m_start->setValue(KReportUnit::convertFromUnitToUnit(m_start->value().toPointF(), oldunit, u),
0162                       KProperty::ValueOption::IgnoreOld);
0163     m_end->setValue(KReportUnit::convertFromUnitToUnit(m_end->value().toPointF(), oldunit, u),
0164                     KProperty::ValueOption::IgnoreOld);
0165 
0166     m_start->setOption("suffix", u.symbol());
0167     m_end->setOption("suffix", u.symbol());
0168 }
0169 
0170 QPointF KReportItemLine::startPosition() const
0171 {
0172     return unit().convertToPoint(m_start->value().toPointF());
0173 }
0174 
0175 void KReportItemLine::setStartPosition(const QPointF &ptPos)
0176 {
0177     m_start->setValue(unit().convertFromPoint(ptPos));
0178 }
0179 
0180 QPointF KReportItemLine::endPosition() const
0181 {
0182     return unit().convertToPoint(m_end->value().toPointF());
0183 }
0184 
0185 void KReportItemLine::setEndPosition(const QPointF &ptPos)
0186 {
0187     m_end->setValue(unit().convertFromPoint(ptPos));
0188 }