| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <QTest> |
| 6 |
#include <QBuffer> |
| 7 |
|
| 8 |
#include <qpicture.h> |
| 9 |
#include <qpainter.h> |
| 10 |
#include <qimage.h> |
| 11 |
#include <qpaintengine.h> |
| 12 |
#include <qguiapplication.h> |
| 13 |
#include <qscreen.h> |
| 14 |
#include <limits.h> |
| 15 |
|
| 16 |
#ifndef QT_NO_PICTURE |
| 17 |
|
| 18 |
class tst_QPicture : public QObject |
| 19 |
{ |
| 20 |
Q_OBJECT |
| 21 |
|
| 22 |
public: |
| 23 |
tst_QPicture(); |
| 24 |
|
| 25 |
private slots: |
| 26 |
void devType(); |
| 27 |
void paintingActive(); |
| 28 |
void boundingRect(); |
| 29 |
void swap(); |
| 30 |
void serialization(); |
| 31 |
void save_restore(); |
| 32 |
void boundaryValues_data(); |
| 33 |
void boundaryValues(); |
| 34 |
}; |
| 35 |
|
| 36 |
tst_QPicture::tst_QPicture() |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
void tst_QPicture::devType() |
| 41 |
{ |
| 42 |
QPicture p; |
| 43 |
QCOMPARE( p.devType(), (int)QInternal::Picture ); |
| 44 |
} |
| 45 |
|
| 46 |
void tst_QPicture::paintingActive() |
| 47 |
{ |
| 48 |
// actually implemented in QPainter but QPicture is a good |
| 49 |
// example of an external paint device |
| 50 |
QPicture p; |
| 51 |
QVERIFY( !p.paintingActive() ); |
| 52 |
QPainter pt( &p ); |
| 53 |
QVERIFY( p.paintingActive() ); |
| 54 |
pt.end(); |
| 55 |
QVERIFY( !p.paintingActive() ); |
| 56 |
} |
| 57 |
|
| 58 |
void tst_QPicture::boundingRect() |
| 59 |
{ |
| 60 |
QPicture p1; |
| 61 |
// default value |
| 62 |
QVERIFY( !p1.boundingRect().isValid() ); |
| 63 |
|
| 64 |
QRect r1( 20, 30, 5, 15 ); |
| 65 |
p1.setBoundingRect( r1 ); |
| 66 |
QCOMPARE( p1.boundingRect(), r1 ); |
| 67 |
p1.setBoundingRect(QRect()); |
| 68 |
|
| 69 |
QPainter pt( &p1 ); |
| 70 |
pt.drawLine( 10, 20, 110, 80 ); |
| 71 |
pt.end(); |
| 72 |
|
| 73 |
// assignment and copy constructor |
| 74 |
QRect r2( 10, 20, 100, 60 ); |
| 75 |
QCOMPARE( p1.boundingRect(), r2 ); |
| 76 |
QPicture p2( p1 ); |
| 77 |
QCOMPARE( p2.boundingRect(), r2 ); |
| 78 |
QPicture p3; |
| 79 |
p3 = p1; |
| 80 |
QCOMPARE( p3.boundingRect(), r2 ); |
| 81 |
|
| 82 |
{ |
| 83 |
QPicture p4; |
| 84 |
QPainter p(&p4); |
| 85 |
p.drawLine(0, 0, 5, 0); |
| 86 |
p.drawLine(0, 0, 0, 5); |
| 87 |
p.end(); |
| 88 |
|
| 89 |
QRect r3(0, 0, 5, 5); |
| 90 |
QCOMPARE(p4.boundingRect(), r3); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void tst_QPicture::swap() |
| 95 |
{ |
| 96 |
QPicture p1, p2; |
| 97 |
QPainter(&p1).drawLine(0, 0, 5, 5); |
| 98 |
QPainter(&p2).drawLine(0, 3, 3, 0); |
| 99 |
QCOMPARE(p1.boundingRect(), QRect(0,0,5,5)); |
| 100 |
QCOMPARE(p2.boundingRect(), QRect(0,0,3,3)); |
| 101 |
p1.swap(p2); |
| 102 |
QCOMPARE(p1.boundingRect(), QRect(0,0,3,3)); |
| 103 |
QCOMPARE(p2.boundingRect(), QRect(0,0,5,5)); |
| 104 |
} |
| 105 |
|
| 106 |
Q_DECLARE_METATYPE(QDataStream::Version) |
| 107 |
Q_DECLARE_METATYPE(QPicture) |
| 108 |
|
| 109 |
void ensureSerializesCorrectly(const QPicture &picture, QDataStream::Version version) |
| 110 |
{ |
| 111 |
QDataStream stream; |
| 112 |
|
| 113 |
QBuffer buffer; |
| 114 |
buffer.open(QIODevice::WriteOnly); |
| 115 |
stream.setDevice(&buffer); |
| 116 |
stream.setVersion(version); |
| 117 |
stream << picture; |
| 118 |
buffer.close(); |
| 119 |
|
| 120 |
buffer.open(QIODevice::ReadOnly); |
| 121 |
QPicture readpicture; |
| 122 |
stream >> readpicture; |
| 123 |
QVERIFY2(memcmp(picture.data(), readpicture.data(), picture.size()) == 0, |
| 124 |
qPrintable(QString::fromLatin1("Picture data does not compare equal for QDataStream version %1").arg(version))); |
| 125 |
} |
| 126 |
|
| 127 |
class PaintEngine : public QPaintEngine |
| 128 |
{ |
| 129 |
public: |
| 130 |
PaintEngine() : QPaintEngine() {} |
| 131 |
bool begin(QPaintDevice *) override { return true; } |
| 132 |
bool end() override { return true; } |
| 133 |
void updateState(const QPaintEngineState &) override {} |
| 134 |
void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) override {} |
| 135 |
Type type() const override { return Raster; } |
| 136 |
|
| 137 |
QFont font() { return state->font(); } |
| 138 |
}; |
| 139 |
|
| 140 |
class Picture : public QPicture |
| 141 |
{ |
| 142 |
public: |
| 143 |
Picture() : QPicture() {} |
| 144 |
QPaintEngine *paintEngine() const override { return (QPaintEngine*)&mPaintEngine; } |
| 145 |
private: |
| 146 |
PaintEngine mPaintEngine; |
| 147 |
}; |
| 148 |
|
| 149 |
void tst_QPicture::serialization() |
| 150 |
{ |
| 151 |
QDataStream stream; |
| 152 |
const int thisVersion = stream.version(); |
| 153 |
|
| 154 |
for (int version = QDataStream::Qt_1_0; version <= thisVersion; ++version) { |
| 155 |
const QDataStream::Version versionEnum = static_cast<QDataStream::Version>(version); |
| 156 |
|
| 157 |
{ |
| 158 |
// streaming of null pictures |
| 159 |
ensureSerializesCorrectly(QPicture(), versionEnum); |
| 160 |
} |
| 161 |
{ |
| 162 |
// picture with a simple line, checking bitwise equality |
| 163 |
QPicture picture; |
| 164 |
QPainter painter(&picture); |
| 165 |
painter.drawLine(10, 20, 30, 40); |
| 166 |
ensureSerializesCorrectly(picture, versionEnum); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
{ |
| 171 |
// Test features that were added after Qt 4.5, as that was hard-coded as the major |
| 172 |
// version for a while, which was incorrect. In this case, we'll test font hints. |
| 173 |
QPicture picture; |
| 174 |
QPainter painter; |
| 175 |
QFont font; |
| 176 |
font.setStyleName("Blah"); |
| 177 |
font.setHintingPreference(QFont::PreferFullHinting); |
| 178 |
painter.begin(&picture); |
| 179 |
painter.setFont(font); |
| 180 |
painter.drawText(20, 20, "Hello"); |
| 181 |
painter.end(); |
| 182 |
|
| 183 |
Picture customPicture; |
| 184 |
painter.begin(&customPicture); |
| 185 |
picture.play(&painter); |
| 186 |
const QFont actualFont = ((PaintEngine*)customPicture.paintEngine())->font(); |
| 187 |
painter.end(); |
| 188 |
QCOMPARE(actualFont.styleName(), QStringLiteral("Blah")); |
| 189 |
QCOMPARE(actualFont.hintingPreference(), QFont::PreferFullHinting); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
static QRectF scaleRect(const QRectF &rect, qreal xf, qreal yf) |
| 194 |
{ |
| 195 |
return QRectF(rect.left() * xf, rect.top() * yf, rect.width() * xf, rect.height() * yf); |
| 196 |
} |
| 197 |
|
| 198 |
static void paintStuff(QPainter *p) |
| 199 |
{ |
| 200 |
const QScreen *screen = QGuiApplication::primaryScreen(); |
| 201 |
// Calculate factors from the screen resolution against QPicture's 96DPI |
| 202 |
// (enforced by Qt::AA_Use96Dpi as set by QTEST_MAIN). |
| 203 |
const qreal xf = qreal(p->device()->logicalDpiX()) / screen->logicalDotsPerInchX(); |
| 204 |
const qreal yf = qreal(p->device()->logicalDpiY()) / screen->logicalDotsPerInchY(); |
| 205 |
p->drawRect(scaleRect(QRectF(100, 100, 100, 100), xf, yf)); |
| 206 |
p->save(); |
| 207 |
p->translate(10 * xf, 10 * yf); |
| 208 |
p->restore(); |
| 209 |
p->drawRect(scaleRect(QRectF(100, 100, 100, 100), xf, yf)); |
| 210 |
} |
| 211 |
|
| 212 |
/* See task: 41469 |
| 213 |
Problem is that the state is not properly restored if the basestate of |
| 214 |
the painter is different when the picture data was created compared to |
| 215 |
the base state of the painter when it is played back. |
| 216 |
*/ |
| 217 |
void tst_QPicture::save_restore() |
| 218 |
{ |
| 219 |
QPicture pic; |
| 220 |
QPainter p; |
| 221 |
p.begin(&pic); |
| 222 |
paintStuff(&p); |
| 223 |
p.end(); |
| 224 |
|
| 225 |
QPixmap pix1(300, 300); |
| 226 |
pix1.fill(Qt::white); |
| 227 |
p.begin(&pix1); |
| 228 |
p.drawPicture(50, 50, pic); |
| 229 |
p.end(); |
| 230 |
|
| 231 |
QPixmap pix2(300, 300); |
| 232 |
pix2.fill(Qt::white); |
| 233 |
p.begin(&pix2); |
| 234 |
p.translate(50, 50); |
| 235 |
paintStuff(&p); |
| 236 |
p.end(); |
| 237 |
|
| 238 |
QVERIFY( pix1.toImage() == pix2.toImage() ); |
| 239 |
} |
| 240 |
|
| 241 |
void tst_QPicture::boundaryValues_data() |
| 242 |
{ |
| 243 |
QTest::addColumn<int>("x"); |
| 244 |
QTest::addColumn<int>("y"); |
| 245 |
QTest::newRow("max x") << INT_MAX << 50; |
| 246 |
QTest::newRow("max y") << 50 << INT_MAX; |
| 247 |
QTest::newRow("max x and y") << INT_MAX << INT_MAX; |
| 248 |
|
| 249 |
QTest::newRow("min x") << INT_MIN << 50; |
| 250 |
QTest::newRow("min y") << 50 << INT_MIN; |
| 251 |
QTest::newRow("min x and y") << INT_MIN << INT_MIN; |
| 252 |
|
| 253 |
QTest::newRow("min x, max y") << INT_MIN << INT_MAX; |
| 254 |
QTest::newRow("max x, min y") << INT_MAX << INT_MIN; |
| 255 |
} |
| 256 |
|
| 257 |
void tst_QPicture::boundaryValues() |
| 258 |
{ |
| 259 |
QPicture picture; |
| 260 |
|
| 261 |
QPainter painter; |
| 262 |
painter.begin(&picture); |
| 263 |
|
| 264 |
QFETCH(int, x); |
| 265 |
QFETCH(int, y); |
| 266 |
painter.drawPoint(QPoint(x, y)); |
| 267 |
|
| 268 |
painter.end(); |
| 269 |
} |
| 270 |
|
| 271 |
QTEST_MAIN(tst_QPicture) |
| 272 |
#include "tst_qpicture.moc" |
| 273 |
|
| 274 |
#endif // QT_NO_PICTURE |