File indexing completed on 2024-04-21 04:41:49
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 "KReportItemBase.h" 0019 0020 #include "KReportUtils.h" 0021 #include "KReportUtils_p.h" 0022 0023 #include <KPropertySet> 0024 #include <KPropertyListData> 0025 #include <QApplication> 0026 #include <QDomElement> 0027 0028 class Q_DECL_HIDDEN KReportItemBase::Private 0029 { 0030 public: 0031 Private(); 0032 ~Private(); 0033 0034 void setUnit(const KReportUnit& u, bool force) 0035 { 0036 if (!force && unit == u) { 0037 return; 0038 } 0039 const QSignalBlocker blocker(set); 0040 KReportUnit oldunit = unit; 0041 unit = u; 0042 // convert values 0043 0044 if (positionProperty) { 0045 positionProperty->setValue(KReportUnit::convertFromUnitToUnit( 0046 positionProperty->value().toPointF(), oldunit, u), 0047 KProperty::ValueOption::IgnoreOld); 0048 0049 positionProperty->setOption("suffix", u.symbol()); 0050 0051 } 0052 if (sizeProperty) { 0053 sizeProperty->setValue( 0054 KReportUnit::convertFromUnitToUnit(sizeProperty->value().toSizeF(), oldunit, u), 0055 KProperty::ValueOption::IgnoreOld); 0056 0057 sizeProperty->setOption("suffix", u.symbol()); 0058 } 0059 0060 } 0061 0062 KPropertySet *set; 0063 KProperty *nameProperty; 0064 KProperty *sizeProperty; 0065 KProperty *positionProperty; 0066 KProperty *dataSourceProperty = nullptr; 0067 QString oldName; 0068 qreal z = 0; 0069 KReportUnit unit; 0070 }; 0071 0072 KReportItemBase::Private::Private() 0073 { 0074 set = new KPropertySet(); 0075 nameProperty = new KProperty("name", QString(), tr("Name"), tr("Object Name")); 0076 nameProperty->setValueSyncPolicy(KProperty::ValueSyncPolicy::FocusOut); 0077 0078 positionProperty = new KProperty("position", QPointF(), QCoreApplication::translate("ItemPosition", "Position")); 0079 sizeProperty = new KProperty("size", QSizeF(), QCoreApplication::translate("ItemSize", "Size")); 0080 setUnit(DEFAULT_UNIT, true); 0081 0082 set->addProperty(nameProperty); 0083 set->addProperty(positionProperty); 0084 set->addProperty(sizeProperty); 0085 } 0086 0087 KReportItemBase::Private::~Private() 0088 { 0089 delete set; 0090 } 0091 0092 0093 KReportItemBase::KReportItemBase() : d(new Private()) 0094 { 0095 connect(propertySet(), &KPropertySet::propertyChanged, 0096 this, &KReportItemBase::propertyChanged); 0097 0098 connect(propertySet(), &KPropertySet::aboutToDeleteProperty, this, &KReportItemBase::aboutToDeleteProperty); 0099 } 0100 0101 KReportItemBase::~KReportItemBase() 0102 { 0103 delete d; 0104 } 0105 0106 void KReportItemBase::createDataSourceProperty() 0107 { 0108 if (d->dataSourceProperty) { 0109 return; 0110 } 0111 d->dataSourceProperty 0112 = new KProperty("item-data-source", new KPropertyListData, QVariant(), tr("Data Source")); 0113 d->dataSourceProperty->setOption("extraValueAllowed", true); 0114 d->set->addProperty(d->dataSourceProperty); 0115 } 0116 0117 bool KReportItemBase::parseReportTextStyleData(const QDomElement & elemSource, KReportTextStyleData *ts) 0118 { 0119 return KReportUtils::parseReportTextStyleData(elemSource, ts); 0120 } 0121 0122 bool KReportItemBase::parseReportLineStyleData(const QDomElement & elemSource, KReportLineStyle *ls) 0123 { 0124 return KReportUtils::parseReportLineStyleData(elemSource, ls); 0125 } 0126 0127 0128 bool KReportItemBase::parseReportRect(const QDomElement & elemSource) 0129 { 0130 const QRectF r(KReportUtils::readRectAttributes(elemSource, DEFAULT_ELEMENT_RECT_PT)); 0131 setPosition(r.topLeft()); 0132 setSize(r.size()); 0133 return true; 0134 } 0135 0136 KReportUnit KReportItemBase::unit() const 0137 { 0138 return d->unit; 0139 } 0140 0141 void KReportItemBase::setUnit(const KReportUnit& u) 0142 { 0143 d->setUnit(u, false); 0144 } 0145 0146 int KReportItemBase::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset, 0147 const QVariant &data, KReportScriptHandler* script) 0148 { 0149 Q_UNUSED(page) 0150 Q_UNUSED(section) 0151 Q_UNUSED(offset) 0152 Q_UNUSED(data) 0153 Q_UNUSED(script) 0154 return 0; 0155 } 0156 0157 int KReportItemBase::renderReportData(OROPage *page, OROSection *section, const QPointF &offset, 0158 KReportDataSource *dataSource, KReportScriptHandler* script) 0159 { 0160 Q_UNUSED(page) 0161 Q_UNUSED(section) 0162 Q_UNUSED(offset) 0163 Q_UNUSED(dataSource) 0164 Q_UNUSED(script) 0165 return 0; 0166 } 0167 0168 QString KReportItemBase::itemDataSource() const 0169 { 0170 return d->dataSourceProperty ? d->dataSourceProperty->value().toString() : QString(); 0171 } 0172 0173 void KReportItemBase::setItemDataSource(const QString& source) 0174 { 0175 if (d->dataSourceProperty && d->dataSourceProperty->value() != source) { 0176 d->dataSourceProperty->setValue(source); 0177 } 0178 } 0179 0180 KPropertySet* KReportItemBase::propertySet() 0181 { 0182 return d->set; 0183 } 0184 0185 bool KReportItemBase::supportsSubQuery() const 0186 { 0187 return false; 0188 } 0189 0190 QString KReportItemBase::entityName() const 0191 { 0192 return d->nameProperty->value().toString(); 0193 } 0194 0195 void KReportItemBase::setEntityName(const QString& n) 0196 { 0197 d->nameProperty->setValue(n); 0198 } 0199 0200 KProperty* KReportItemBase::nameProperty() 0201 { 0202 return d->nameProperty; 0203 } 0204 0205 QString KReportItemBase::oldName() const 0206 { 0207 return d->oldName; 0208 } 0209 0210 void KReportItemBase::setOldName(const QString& old) 0211 { 0212 d->oldName = old; 0213 } 0214 0215 KProperty* KReportItemBase::dataSourceProperty() 0216 { 0217 return d->dataSourceProperty; 0218 } 0219 0220 QPointF KReportItemBase::position() const 0221 { 0222 return d->unit.convertToPoint(d->positionProperty->value().toPointF()); 0223 } 0224 0225 QSizeF KReportItemBase::size() const 0226 { 0227 return d->unit.convertToPoint(d->sizeProperty->value().toSizeF()); 0228 } 0229 0230 const KPropertySet * KReportItemBase::propertySet() const 0231 { 0232 return d->set; 0233 } 0234 0235 QPointF KReportItemBase::scenePosition(const QPointF &ptPos) 0236 { 0237 const qreal x = POINT_TO_INCH(ptPos.x()) * KReportPrivate::dpiX(); 0238 const qreal y = POINT_TO_INCH(ptPos.y()) * KReportPrivate::dpiY(); 0239 return QPointF(x, y); 0240 } 0241 0242 QSizeF KReportItemBase::sceneSize(const QSizeF &ptSize) 0243 { 0244 const qreal w = POINT_TO_INCH(ptSize.width()) * KReportPrivate::dpiX(); 0245 const qreal h = POINT_TO_INCH(ptSize.height()) * KReportPrivate::dpiY(); 0246 return QSizeF(w, h); 0247 } 0248 0249 qreal KReportItemBase::z() const 0250 { 0251 return d->z; 0252 } 0253 0254 void KReportItemBase::setZ(qreal z) 0255 { 0256 d->z = z; 0257 } 0258 0259 void KReportItemBase::setPosition(const QPointF& ptPos) 0260 { 0261 d->positionProperty->setValue(d->unit.convertFromPoint(ptPos)); 0262 } 0263 0264 void KReportItemBase::setSize(const QSizeF &ptSize) 0265 { 0266 d->sizeProperty->setValue(d->unit.convertFromPoint(ptSize)); 0267 } 0268 0269 QPointF KReportItemBase::positionFromScene(const QPointF& pos) 0270 { 0271 const qreal x = INCH_TO_POINT(pos.x() / KReportPrivate::dpiX()); 0272 const qreal y = INCH_TO_POINT(pos.y() / KReportPrivate::dpiY()); 0273 return QPointF(x, y); 0274 } 0275 0276 QSizeF KReportItemBase::sizeFromScene(const QSizeF& size) 0277 { 0278 qreal w = INCH_TO_POINT(size.width() / KReportPrivate::dpiX()); 0279 qreal h = INCH_TO_POINT(size.height() / KReportPrivate::dpiY()); 0280 return QSizeF(w, h); 0281 } 0282 0283 void KReportItemBase::propertyChanged(KPropertySet& s, KProperty& p) 0284 { 0285 Q_UNUSED(s) 0286 Q_UNUSED(p) 0287 } 0288 0289 void KReportItemBase::aboutToDeleteProperty(KPropertySet& set, KProperty& property) 0290 { 0291 Q_UNUSED(set) 0292 if (property.name() == "size") { 0293 d->sizeProperty = nullptr; 0294 } 0295 else if (property.name() == "position") { 0296 d->positionProperty = nullptr; 0297 } 0298 }