1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 #ifndef QGEOMAPPINGMANAGERENGINE_TEST_H
30 #define QGEOMAPPINGMANAGERENGINE_TEST_H
31 
32 #include <qgeoserviceprovider.h>
33 #include <qgeomappingmanagerengine.h>
34 #include <QLocale>
35 #include <QPainter>
36 #include <QPixmap>
37 #include <QByteArray>
38 #include <QBuffer>
39 #include <qgeotiledmapreply.h>
40 #include "qgeomaptype.h"
41 #include "qgeotilespec.h"
42 #include "qgeocameracapabilities_p.h"
43 
44 #include <QTimer>
45 #include <QDebug>
46 #include <QTimerEvent>
47 
48 QT_USE_NAMESPACE
49 
50 
51 class TiledMapReplyTest :public QGeoTiledMapReply
52 {
53     Q_OBJECT
54 public:
QGeoTiledMapReply(spec,parent)55     TiledMapReplyTest(const QGeoTileSpec &spec, QObject *parent=0): QGeoTiledMapReply (spec, parent) {}
callSetError(Error error,const QString & errorString)56     void callSetError ( Error error, const QString & errorString ) {setError(error, errorString);}
callSetFinished(bool finished)57     void callSetFinished ( bool finished ) { setFinished(finished);}
callSetCached(bool cached)58     void callSetCached(bool cached) { setFinished(cached);}
callSetMapImageData(const QByteArray & data)59     void callSetMapImageData(const QByteArray &data) { setMapImageData(data); }
callSetMapImageFormat(const QString & format)60     void callSetMapImageFormat(const QString &format) { setMapImageFormat(format); }
abort()61     void abort() { emit aborted(); }
62 
63 Q_SIGNALS:
64     void aborted();
65 };
66 
67 class QGeoMappingManagerEngineTest: public QGeoMappingManagerEngine
68 
69 {
70 Q_OBJECT
71 public:
QGeoMappingManagerEngineTest(const QMap<QString,QVariant> & parameters,QGeoServiceProvider::Error * error,QString * errorString)72     QGeoMappingManagerEngineTest(const QMap<QString, QVariant> &parameters,
73         QGeoServiceProvider::Error *error, QString *errorString) :
74         QGeoMappingManagerEngine(parameters),
75         finishRequestImmediately_(true),
76         mappingReply_(0),
77         timerId_(0),
78         errorCode_(QGeoTiledMapReply::NoError)
79     {
80         Q_UNUSED(error);
81         Q_UNUSED(errorString);
82         if (parameters.contains("finishRequestImmediately"))
83             finishRequestImmediately_ = qvariant_cast<bool>(parameters.value("finishRequestImmediately"));
84         setLocale(QLocale (QLocale::German, QLocale::Germany));
85         QGeoCameraCapabilities capabilities;
86         capabilities.setMinimumZoomLevel(0.0);
87         capabilities.setMaximumZoomLevel(20.0);
88         capabilities.setSupportsBearing(true);
89         setCameraCapabilities(capabilities);
90     }
91 
init()92     void init()
93     {
94         setTileSize(256);
95         QList<QGeoMapType> types;
96         types << QGeoMapType(QGeoMapType::StreetMap,tr("Street Map"),tr("Test Street Map"), false, 1);
97         setSupportedMapTypes(types);
98         QGeoMappingManagerEngine::init();
99     }
100 
getTileImage(const QGeoTileSpec & spec)101     QGeoTiledMapReply* getTileImage(const QGeoTileSpec &spec)
102     {
103         mappingReply_ = new TiledMapReplyTest(spec, this);
104 
105         QImage im(256, 256, QImage::Format_RGB888);
106         im.fill(QColor("lightgray"));
107         QRectF rect;
108         QString text("X: " + QString::number(spec.x()) + "\nY: " + QString::number(spec.y()) + "\nZ: " + QString::number(spec.zoom()));
109         rect.setWidth(250);
110         rect.setHeight(250);
111         rect.setLeft(3);
112         rect.setTop(3);
113         QPainter painter;
114         QPen pen(QColor("firebrick"));
115         painter.begin(&im);
116         painter.setPen(pen);
117         painter.setFont( QFont("Times", 35, 10, false));
118         painter.drawText(rect, text);
119         // different border color for vertically and horizontally adjacent frames
120         if ((spec.x() + spec.y()) % 2 == 0)
121             pen.setColor(QColor("yellow"));
122         pen.setWidth(5);
123         painter.setPen(pen);
124         painter.drawRect(0,0,255,255);
125         painter.end();
126         QPixmap pm = QPixmap::fromImage(im);
127         QByteArray bytes;
128         QBuffer buffer(&bytes);
129         buffer.open(QIODevice::WriteOnly);
130         pm.save(&buffer, "PNG");
131 
132         mappingReply_->callSetMapImageData(bytes);
133         mappingReply_->callSetMapImageFormat("png");
134         mappingReply_->callSetFinished(true);
135 
136         return static_cast<QGeoTiledMapReply*>(mappingReply_);
137     }
138 
139 public Q_SLOTS:
requestAborted()140     void requestAborted()
141     {
142         if (timerId_) {
143             killTimer(timerId_);
144             timerId_ = 0;
145         }
146         errorString_ = "";
147         errorCode_ = QGeoTiledMapReply::NoError;
148     }
149 
150 protected:
timerEvent(QTimerEvent * event)151      void timerEvent(QTimerEvent *event)
152      {
153          Q_ASSERT(timerId_ == event->timerId());
154          Q_ASSERT(mappingReply_);
155          killTimer(timerId_);
156          timerId_ = 0;
157          if (errorCode_) {
158              mappingReply_->callSetError(errorCode_, errorString_);
159              emit tileError(mappingReply_->tileSpec(), errorString_);
160         } else {
161              mappingReply_->callSetError(QGeoTiledMapReply::NoError, "no error");
162              mappingReply_->callSetFinished(true);
163          }
164          // emit finished(mappingReply_); todo tileFinished
165      }
166 
167 private:
168     bool finishRequestImmediately_;
169     TiledMapReplyTest* mappingReply_;
170     int timerId_;
171     QGeoTiledMapReply::Error errorCode_;
172     QString errorString_;
173 };
174 
175 #endif
176