| 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 QGEOTILEFETCHER_TEST_H |
| 30 | #define QGEOTILEFETCHER_TEST_H |
| 31 | |
| 32 | #include <QtLocation/private/qgeotiledmapreply_p.h> |
| 33 | #include <QtLocation/private/qgeotilefetcher_p.h> |
| 34 | #include <QtLocation/private/qgeotilespec_p.h> |
| 35 | #include <QtLocation/private/qgeomappingmanagerengine_p.h> |
| 36 | |
| 37 | #include <QLocale> |
| 38 | #include <QPainter> |
| 39 | #include <QPixmap> |
| 40 | #include <QByteArray> |
| 41 | #include <QBuffer> |
| 42 | #include <QTimer> |
| 43 | #include <QDebug> |
| 44 | #include <QTimerEvent> |
| 45 | #include <QVariant> |
| 46 | |
| 47 | QT_USE_NAMESPACE |
| 48 | |
| 49 | class TiledMapReplyTest :public QGeoTiledMapReply |
| 50 | { |
| 51 | Q_OBJECT |
| 52 | public: |
| 53 | TiledMapReplyTest(const QGeoTileSpec &spec, QObject *parent=0): QGeoTiledMapReply (spec, parent) {} |
| 54 | void callSetError ( Error error, const QString & errorString ) {setError(error, errorString);} |
| 55 | void callSetFinished ( bool finished ) { setFinished(finished);} |
| 56 | void callSetCached(bool cached) { setFinished(cached);} |
| 57 | void callSetMapImageData(const QByteArray &data) { setMapImageData(data); } |
| 58 | void callSetMapImageFormat(const QString &format) { setMapImageFormat(format); } |
| 59 | void abort() { emit aborted(); } |
| 60 | |
| 61 | Q_SIGNALS: |
| 62 | void aborted(); |
| 63 | }; |
| 64 | |
| 65 | class QGeoTileFetcherTest: public QGeoTileFetcher |
| 66 | { |
| 67 | Q_OBJECT |
| 68 | public: |
| 69 | QGeoTileFetcherTest(QGeoMappingManagerEngine *parent) |
| 70 | : QGeoTileFetcher(parent), finishRequestImmediately_(false), errorCode_(QGeoTiledMapReply::NoError) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | bool init() |
| 75 | { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | QGeoTiledMapReply* getTileImage(const QGeoTileSpec &spec) |
| 80 | { |
| 81 | TiledMapReplyTest* mappingReply = new TiledMapReplyTest(spec, this); |
| 82 | |
| 83 | QImage im(256, 256, QImage::Format_RGB888); |
| 84 | im.fill(color: QColor("lightgray" )); |
| 85 | QRectF rect; |
| 86 | QString text("X: " + QString::number(spec.x()) + "\nY: " + QString::number(spec.y()) + "\nZ: " + QString::number(spec.zoom())); |
| 87 | rect.setWidth(250); |
| 88 | rect.setHeight(250); |
| 89 | rect.setLeft(3); |
| 90 | rect.setTop(3); |
| 91 | QPainter painter; |
| 92 | QPen pen(QColor("firebrick" )); |
| 93 | painter.begin(&im); |
| 94 | painter.setPen(pen); |
| 95 | painter.setFont( QFont("Times" , 35, 10, false)); |
| 96 | painter.drawText(r: rect, text); |
| 97 | // different border color for vertically and horizontally adjacent frames |
| 98 | if ((spec.x() + spec.y()) % 2 == 0) |
| 99 | pen.setColor(QColor("yellow" )); |
| 100 | pen.setWidth(5); |
| 101 | painter.setPen(pen); |
| 102 | painter.drawRect(x: 0,y: 0,w: 255,h: 255); |
| 103 | painter.end(); |
| 104 | QPixmap pm = QPixmap::fromImage(image: im); |
| 105 | QByteArray bytes; |
| 106 | QBuffer buffer(&bytes); |
| 107 | buffer.open(openMode: QIODevice::WriteOnly); |
| 108 | pm.save(device: &buffer, format: "PNG" ); |
| 109 | |
| 110 | mappingReply->callSetMapImageData(data: bytes); |
| 111 | mappingReply->callSetMapImageFormat(format: "png" ); |
| 112 | |
| 113 | if (finishRequestImmediately_) { |
| 114 | updateRequest(mappingReply); |
| 115 | return mappingReply; |
| 116 | } else { |
| 117 | if (m_queue.isEmpty()) |
| 118 | timer_.start(msec: 500, obj: this); |
| 119 | m_queue.append(t: mappingReply); |
| 120 | } |
| 121 | |
| 122 | return mappingReply; |
| 123 | } |
| 124 | |
| 125 | void setFinishRequestImmediately(bool enabled) |
| 126 | { |
| 127 | finishRequestImmediately_ = enabled; |
| 128 | } |
| 129 | |
| 130 | void setTileSize(QSize tileSize) |
| 131 | { |
| 132 | tileSize_ = tileSize; |
| 133 | } |
| 134 | |
| 135 | public Q_SLOTS: |
| 136 | void requestAborted() |
| 137 | { |
| 138 | timer_.stop(); |
| 139 | errorString_.clear(); |
| 140 | errorCode_ = QGeoTiledMapReply::NoError; |
| 141 | } |
| 142 | Q_SIGNALS: |
| 143 | void tileFetched(const QGeoTileSpec&); |
| 144 | |
| 145 | protected: |
| 146 | void updateRequest(TiledMapReplyTest* mappingReply) |
| 147 | { |
| 148 | if (errorCode_) { |
| 149 | mappingReply->callSetError(error: errorCode_, errorString: errorString_); |
| 150 | emit tileError(spec: mappingReply->tileSpec(), errorString: errorString_); |
| 151 | } else { |
| 152 | mappingReply->callSetError(error: QGeoTiledMapReply::NoError, errorString: "no error" ); |
| 153 | mappingReply->callSetFinished(finished: true); |
| 154 | emit tileFetched(mappingReply->tileSpec()); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void timerEvent(QTimerEvent *event) |
| 159 | { |
| 160 | if (event->timerId() != timer_.timerId()) { |
| 161 | QGeoTileFetcher::timerEvent(event); |
| 162 | return; |
| 163 | } |
| 164 | updateRequest(mappingReply: m_queue.takeFirst()); |
| 165 | if (m_queue.isEmpty()) { |
| 166 | timer_.stop(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | bool finishRequestImmediately_; |
| 172 | QBasicTimer timer_; |
| 173 | QGeoTiledMapReply::Error errorCode_; |
| 174 | QString errorString_; |
| 175 | QSize tileSize_; |
| 176 | QList<TiledMapReplyTest*> m_queue; |
| 177 | }; |
| 178 | |
| 179 | #endif |
| 180 | |