File indexing completed on 2024-05-12 04:43:21

0001 /*
0002  * Copyright (C) 2007-2016 by Adam Pigg (adam@piggz.co.uk)
0003  * Copyright (C) 2011-2015 by Radoslaw Wicik (radoslaw@wicik.pl)
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 #include "KReportItemMaps.h"
0019 #include "KReportUtils.h"
0020 #include "KReportRenderObjects.h"
0021 
0022 #include <KPropertyListData>
0023 #include <KPropertySet>
0024 
0025 #include <QStringList>
0026 
0027 #include <sys/socket.h>
0028 
0029 #define myDebug() if (0) kDebug(44021)
0030 
0031 //! @todo replace with ReportItemMaps(const QDomNode &element = QDomNode())
0032 KReportItemMaps::KReportItemMaps()
0033 {
0034     createProperties();
0035 }
0036 
0037 KReportItemMaps::KReportItemMaps(const QDomNode &element)
0038     : KReportItemMaps()
0039 {
0040     nameProperty()->setValue(KReportUtils::readNameAttribute(element.toElement()));
0041     setItemDataSource(element.toElement().attribute(QLatin1String("report:item-data-source")));
0042     setZ(element.toElement().attribute(QLatin1String("report:z-index")).toDouble());
0043     m_latitudeProperty->setValue(element.toElement().attribute(QLatin1String("report:latitude")).toDouble());
0044     m_longitudeProperty->setValue(element.toElement().attribute(QLatin1String("report:longitude")).toDouble());
0045     m_zoomProperty->setValue(element.toElement().attribute(QLatin1String("report:zoom")).toInt());
0046     QString themeId(element.toElement().attribute(QLatin1String("report:theme")));
0047     themeId = themeId.isEmpty() ? m_themeManager.mapThemeIds()[0] : themeId;
0048     m_themeProperty->setValue(themeId);
0049 
0050     parseReportRect(element.toElement());
0051 }
0052 
0053 KReportItemMaps::~KReportItemMaps()
0054 {
0055 }
0056 
0057 void KReportItemMaps::createProperties()
0058 {
0059     createDataSourceProperty();
0060 
0061     m_latitudeProperty = new KProperty("latitude", 0.0, tr("Latitude"), QString(), KProperty::Double);
0062     m_latitudeProperty->setOption("min", -90);
0063     m_latitudeProperty->setOption("max", 90);
0064     m_latitudeProperty->setOption("suffix", QString::fromUtf8("°"));
0065     m_latitudeProperty->setOption("precision", 7);
0066 
0067     m_longitudeProperty = new KProperty("longitude", 0.0, tr("Longitude"), QString(), KProperty::Double);
0068     m_longitudeProperty->setOption("min", -180);
0069     m_longitudeProperty->setOption("max", 180);
0070     m_longitudeProperty->setOption("suffix", QString::fromUtf8("°"));
0071     m_longitudeProperty->setOption("precision", 7);
0072 
0073     m_zoomProperty     = new KProperty("zoom", 1000, tr("Zoom") );
0074     m_zoomProperty->setOption("min", 0);
0075     m_zoomProperty->setOption("max", 4000);
0076     m_zoomProperty->setOption("step", 100);
0077     m_zoomProperty->setOption("slider", true);
0078 
0079     QStringList mapThemIds(m_themeManager.mapThemeIds());
0080 
0081     m_themeProperty = new KProperty("theme", new KPropertyListData(mapThemIds, mapThemIds),
0082                                     QVariant(mapThemIds[1]), tr("Theme"));
0083 
0084     if (mapThemIds.contains(QLatin1String("earth/srtm/srtm.dgml"))) {
0085         m_themeProperty->setValue(QLatin1String("earth/srtm/srtm.dgml"),
0086                                   KProperty::ValueOption::IgnoreOld);
0087     }
0088 
0089     propertySet()->addProperty(m_latitudeProperty);
0090     propertySet()->addProperty(m_longitudeProperty);
0091     propertySet()->addProperty(m_zoomProperty);
0092     propertySet()->addProperty(m_themeProperty);
0093 }
0094 
0095 QString KReportItemMaps::typeName() const
0096 {
0097     return QLatin1String("maps");
0098 }
0099 
0100 int KReportItemMaps::renderSimpleData(OROPage *page, OROSection *section, const QPointF &offset,
0101                                        const QVariant &data, KReportScriptHandler *script)
0102 {
0103     Q_UNUSED(script)
0104 
0105     deserializeData(data);
0106     m_pageId = page;
0107     m_sectionId = section;
0108     m_offset = offset;
0109 
0110 
0111     m_oroPicture = new OROPicture();
0112     m_oroPicture->setPosition(scenePosition(position()) + m_offset);
0113     m_oroPicture->setSize(sceneSize(size()));
0114 
0115     if (m_pageId) {
0116         m_pageId->insertPrimitive(m_oroPicture);
0117     }
0118 
0119     if (m_sectionId) {
0120         OROPicture *i2 = dynamic_cast<OROPicture*>(m_oroPicture->clone());
0121         if (i2) {
0122             i2->setPosition(scenePosition(position()));
0123         }
0124     }
0125 
0126     m_mapRenderer.renderJob(this);
0127 
0128     return 0; //Item doesn't stretch the section height
0129 }
0130 
0131 void KReportItemMaps::deserializeData(const QVariant& serialized)
0132 {
0133     //kreportpluginDebug() << "Map data for this record is" << serialized;
0134     QStringList dataList = serialized.toString().split(QLatin1Char(';'));
0135     if (dataList.size() == 3) {
0136         m_latitude = dataList[0].toDouble();
0137         m_longtitude = dataList[1].toDouble();
0138         m_zoom = dataList[2].toInt();
0139     } else {
0140         m_latitude = m_latitudeProperty->value().toReal();
0141         m_longtitude = m_longitudeProperty->value().toReal();
0142         m_zoom = m_zoomProperty->value().toInt();
0143     }
0144 }
0145 
0146 void KReportItemMaps::renderFinished()
0147 {
0148     emit finishedRendering();
0149 }
0150 
0151 OROPicture* KReportItemMaps::oroImage()
0152 {
0153     return m_oroPicture;
0154 }
0155 
0156 qreal KReportItemMaps::longtitude() const
0157 {
0158     return m_longtitude;
0159 }
0160 
0161 qreal KReportItemMaps::latitude() const
0162 {
0163     return m_latitude;
0164 }
0165 
0166 int KReportItemMaps::zoom() const
0167 {
0168     return m_zoom;
0169 }
0170 
0171 QString KReportItemMaps::themeId() const
0172 {
0173     return m_themeProperty->value().toString();
0174 }
0175 
0176 QVariant KReportItemMaps::realItemData(const QVariant& itemData) const
0177 {
0178     double lat, lon;
0179     int zoom;
0180 
0181     QStringList dataList = itemData.toString().split(QLatin1Char(';'));
0182 
0183     if (dataList.size() == 3) {
0184         lat = dataList[0].toDouble();
0185         lon = dataList[1].toDouble();
0186         zoom = dataList[2].toInt();
0187     } else if (dataList.size() == 2) {
0188         lat = dataList[0].toDouble();
0189         lon = dataList[1].toDouble();
0190         zoom = m_zoomProperty->value().toInt();
0191     } else {
0192         lat = m_latitudeProperty->value().toReal();
0193         lon = m_longitudeProperty->value().toReal();
0194         zoom = m_zoomProperty->value().toInt();
0195     }
0196 
0197     if (m_longDataSetFromScript) {
0198         lon = m_longtitude;
0199     }
0200     if (m_latDataSetFromScript) {
0201         lat = m_latitude;
0202     }
0203     if (m_zoomDataSetFromScript) {
0204         zoom = m_zoom;
0205     }
0206     return QString(QLatin1String("%1;%2;%3")).arg(lat).arg(lon).arg(zoom);
0207 }