File: paintbuffermodel.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (525 lines) | stat: -rw-r--r-- 21,686 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
  paintbuffermodel.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2012 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include <config-gammaray.h>
#include "paintbuffermodel.h"

#include <core/enumutil.h>
#include <core/varianthandler.h>

#include <common/metatypedeclarations.h>
#include <common/paintbuffermodelroles.h>

#include <algorithm>
#include <limits>
#include <vector>

#include <private/qvectorpath_p.h>
#include <private/qpainterpath_p.h>

using namespace GammaRay;

static const char *cmd_argument_names[] = {
    "position", // DrawText
    "text",
    "font",
    "position", // DrawImagePos
    "image",
    "target", // DrawImageRect
    "image",
    "source",
    "position", // DrawPixmapPos
    "pixmap",
    "target", // DrawPixmapRect
    "pixmap",
    "source",
    "rectangle", // DrawTiledPixmap
    "pixmap",
    "position",
};

struct cmd_t
{
    QPaintBufferPrivate::Command cmd;
    const char *name;
    int argumentCount;
    int argumentName;
};

#define CMD(cmd) QPaintBufferPrivate::Cmd_##cmd, #cmd

static const cmd_t cmdTypes[] = {
    { CMD(Save), 0, 0 },
    { CMD(Restore), 0, 0 },
    { CMD(SetBrush), 0, 0 },
    { CMD(SetBrushOrigin), 0, 0 },
    { CMD(SetClipEnabled), 0, 0 },
    { CMD(SetCompositionMode), 0, 0 },
    { CMD(SetOpacity), 0, 0 },
    { CMD(SetPen), 0, 0 },
    { CMD(SetRenderHints), 0, 0 },
    { CMD(SetTransform), 0, 0 },
    { CMD(SetBackgroundMode), 0, 0 },
    { CMD(ClipPath), 0, 0 },
    { CMD(ClipRect), 0, 0 },
    { CMD(ClipRegion), 0, 0 },
    { CMD(ClipVectorPath), 0, 0 },
    { CMD(DrawVectorPath), 0, 0 },
    { CMD(FillVectorPath), 0, 0 },
    { CMD(StrokeVectorPath), 0, 0 },
    { CMD(DrawConvexPolygonF), 0, 0 },
    { CMD(DrawConvexPolygonI), 0, 0 },
    { CMD(DrawEllipseF), 0, 0 },
    { CMD(DrawEllipseI), 0, 0 },
    { CMD(DrawLineF), 0, 0 },
    { CMD(DrawLineI), 0, 0 },
    { CMD(DrawPath), 0, 0 },
    { CMD(DrawPointsF), 0, 0 },
    { CMD(DrawPointsI), 0, 0 },
    { CMD(DrawPolygonF), 0, 0 },
    { CMD(DrawPolygonI), 0, 0 },
    { CMD(DrawPolylineF), 0, 0 },
    { CMD(DrawPolylineI), 0, 0 },
    { CMD(DrawRectF), 0, 0 },
    { CMD(DrawRectI), 0, 0 },
    { CMD(FillRectBrush), 0, 0 },
    { CMD(FillRectColor), 0, 0 },
    { CMD(DrawText), 3, 0 },
    { CMD(DrawTextItem), 0, 0 },
    { CMD(DrawImagePos), 2, 3 },
    { CMD(DrawImageRect), 3, 5 },
    { CMD(DrawPixmapPos), 2, 8 },
    { CMD(DrawPixmapRect), 3, 10 },
    { CMD(DrawTiledPixmap), 3, 13 },
    { CMD(SystemStateChanged), 0, 0 },
    { CMD(Translate), 0, 0 },
    { CMD(DrawStaticText), 0, 0 }
};

#undef CMD

static const int TopLevelId = std::numeric_limits<int>::max();

PaintBufferModel::PaintBufferModel(QObject *parent)
    : QAbstractItemModel(parent)
    , m_privateBuffer(nullptr)
    , m_maxCost(0.0)
{
}

void PaintBufferModel::setPaintBuffer(const PaintBuffer &buffer)
{
    beginResetModel();
    m_buffer = buffer;
    m_privateBuffer = buffer.data();
    m_costs.clear();
    m_maxCost = 0.0;
    endResetModel();
}

PaintBuffer PaintBufferModel::buffer() const
{
    return m_buffer;
}

void PaintBufferModel::setCosts(const QVector<double> &costs)
{
    m_costs = costs;
    if (rowCount() > 0 && !m_costs.isEmpty()) {
        m_maxCost = *std::max_element(m_costs.constBegin(), m_costs.constEnd());
        emit dataChanged(index(0, 2, QModelIndex()), index(rowCount() - 1, 2, QModelIndex()));
    }
}

template<typename T, typename Data>
static QString geometryListToString(const Data *data, int offset, int size)
{
    QStringList l;
    l.reserve(size);
    auto *elem = reinterpret_cast<const T *>(data + offset);
    for (int i = 0; i < size; ++i)
        l.push_back(VariantHandler::displayString(*elem++));
    return l.join(QLatin1String("; "));
}

static QString vectorPathToString(QPaintBufferPrivate *data, const QPaintBufferCommand &cmd)
{
    QVectorPath path(
        data->floats.constData() + cmd.offset, cmd.size,
        cmd.offset2 & 0x80000000 ? nullptr : reinterpret_cast<const QPainterPath::ElementType *>(data->ints.constData() + cmd.offset2 + 1),
        *(data->ints.constData() + (cmd.offset2 & 0x7FFFFFFF)));
    if (path.isEmpty())
        return PaintBufferModel::tr("<empty>");
    return PaintBufferModel::tr("control rect: %1, elements: %2").arg(VariantHandler::displayString(path.controlPointRect()), QString::number(path.elementCount()));
}

QVariant PaintBufferModel::argumentAt(const QPaintBufferCommand &cmd, int index) const
{
    switch (cmd.id) {
    // single argument commands
    case QPaintBufferPrivate::Cmd_SetBrush:
    case QPaintBufferPrivate::Cmd_SetPen:
        return m_privateBuffer->variants.at(cmd.offset);
    case QPaintBufferPrivate::Cmd_FillRectBrush:
        return m_privateBuffer->variants.at(cmd.extra);

    // multi argument commands
    case QPaintBufferPrivate::Cmd_DrawText:
        switch (index) {
        case 0:
            return QPointF(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1));
        case 1:
            return m_privateBuffer->variants.at(cmd.offset).value<QList<QVariant>>().at(1);
        case 2:
            return m_privateBuffer->variants.at(cmd.offset).value<QList<QVariant>>().at(0);
        }
        break;
    case QPaintBufferPrivate::Cmd_DrawImagePos:
    case QPaintBufferPrivate::Cmd_DrawPixmapPos:
        switch (index) {
        case 0:
            return QPointF(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1));
        case 1:
            return m_privateBuffer->variants.at(cmd.offset);
        }
        break;
    case QPaintBufferPrivate::Cmd_DrawImageRect:
    case QPaintBufferPrivate::Cmd_DrawPixmapRect:
        switch (index) {
        case 0:
            return QRectF(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1),
                          m_privateBuffer->floats.at(cmd.extra + 2), m_privateBuffer->floats.at(cmd.extra + 3));
        case 1:
            return m_privateBuffer->variants.at(cmd.offset);
        case 2:
            return QRectF(m_privateBuffer->floats.at(cmd.extra + 4), m_privateBuffer->floats.at(cmd.extra + 5),
                          m_privateBuffer->floats.at(cmd.extra + 6), m_privateBuffer->floats.at(cmd.extra + 7));
        }
        break;
    case QPaintBufferPrivate::Cmd_DrawTiledPixmap:
        switch (index) {
        case 0:
            return QRectF(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1),
                          m_privateBuffer->floats.at(cmd.extra + 2), m_privateBuffer->floats.at(cmd.extra + 3));
        case 1:
            return m_privateBuffer->variants.at(cmd.offset);
        case 2:
            return QPointF(m_privateBuffer->floats.at(cmd.extra + 4), m_privateBuffer->floats.at(cmd.extra + 5));
        }
        break;
    default:
        break;
    }
    return QVariant();
}

QString PaintBufferModel::argumentDisplayString(const QPaintBufferCommand &cmd) const
{
    switch (cmd.id) {
    case QPaintBufferPrivate::Cmd_Save:
    case QPaintBufferPrivate::Cmd_Restore:
        break;

    case QPaintBufferPrivate::Cmd_SetBrush:
    case QPaintBufferPrivate::Cmd_SetBrushOrigin:
    case QPaintBufferPrivate::Cmd_SetClipEnabled:
        return VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_SetCompositionMode:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<QPainter::CompositionMode>(cmd.extra)));
    case QPaintBufferPrivate::Cmd_SetOpacity:
        return QString::number(m_privateBuffer->variants.at(cmd.offset).toDouble());
    case QPaintBufferPrivate::Cmd_SetPen:
        return VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));

    case QPaintBufferPrivate::Cmd_SetRenderHints:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<QPainter::RenderHints>(cmd.extra)));
    case QPaintBufferPrivate::Cmd_SetTransform:
        return VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_SetBackgroundMode:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<Qt::BGMode>(cmd.extra)));

    case QPaintBufferPrivate::Cmd_ClipRect:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<Qt::ClipOperation>(cmd.extra)))
            + QLatin1String(": ")
            + VariantHandler::displayString(QRect(
                QPoint(m_privateBuffer->ints.at(cmd.offset), m_privateBuffer->ints.at(cmd.offset + 1)),
                QPoint(m_privateBuffer->ints.at(cmd.offset + 2), m_privateBuffer->ints.at(cmd.offset + 3))));
    case QPaintBufferPrivate::Cmd_ClipPath:
    case QPaintBufferPrivate::Cmd_ClipRegion:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<Qt::ClipOperation>(cmd.extra)))
            + QLatin1String(": ")
            + VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_ClipVectorPath:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<Qt::ClipOperation>(cmd.extra)))
            + QLatin1String(": ")
            + vectorPathToString(m_privateBuffer, cmd);

    case QPaintBufferPrivate::Cmd_DrawVectorPath:
        return vectorPathToString(m_privateBuffer, cmd);
    case QPaintBufferPrivate::Cmd_FillVectorPath:
        return tr("%1, brush: %2").arg(vectorPathToString(m_privateBuffer, cmd), VariantHandler::displayString(m_privateBuffer->variants.at(cmd.extra)));
    case QPaintBufferPrivate::Cmd_StrokeVectorPath:
        return tr("%1, pen: %2").arg(vectorPathToString(m_privateBuffer, cmd), VariantHandler::displayString(m_privateBuffer->variants.at(cmd.extra)));

    case QPaintBufferPrivate::Cmd_DrawEllipseF:
        return VariantHandler::displayString(*reinterpret_cast<const QRectF *>(m_privateBuffer->floats.constData() + cmd.offset));
    case QPaintBufferPrivate::Cmd_DrawEllipseI:
        return VariantHandler::displayString(*reinterpret_cast<const QRect *>(m_privateBuffer->ints.constData() + cmd.offset));
    case QPaintBufferPrivate::Cmd_DrawLineF:
        return geometryListToString<QLineF>(m_privateBuffer->floats.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawLineI:
        return geometryListToString<QLine>(m_privateBuffer->ints.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawPath:
        return VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_DrawPolygonF:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<QPaintEngine::PolygonDrawMode>(cmd.extra)))
            + QLatin1String(": ") + geometryListToString<QPointF>(m_privateBuffer->floats.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawPolygonI:
        return EnumUtil::enumToString(QVariant::fromValue(static_cast<QPaintEngine::PolygonDrawMode>(cmd.extra)))
            + QLatin1String(": ") + geometryListToString<QPoint>(m_privateBuffer->ints.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawPointsF:
    case QPaintBufferPrivate::Cmd_DrawConvexPolygonF:
    case QPaintBufferPrivate::Cmd_DrawPolylineF:
        return geometryListToString<QPointF>(m_privateBuffer->floats.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawPointsI:
    case QPaintBufferPrivate::Cmd_DrawConvexPolygonI:
    case QPaintBufferPrivate::Cmd_DrawPolylineI:
        return geometryListToString<QPoint>(m_privateBuffer->ints.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawRectF:
        return geometryListToString<QRectF>(m_privateBuffer->floats.constData(), cmd.offset, cmd.size);
    case QPaintBufferPrivate::Cmd_DrawRectI:
        return geometryListToString<QRect>(m_privateBuffer->ints.constData(), cmd.offset, cmd.size);

    case QPaintBufferPrivate::Cmd_FillRectBrush:
    case QPaintBufferPrivate::Cmd_FillRectColor:
        return VariantHandler::displayString(*reinterpret_cast<const QRectF *>(m_privateBuffer->floats.constData() + cmd.offset))
            + QLatin1String(" - ") + VariantHandler::displayString(m_privateBuffer->variants.at(cmd.extra));

    case QPaintBufferPrivate::Cmd_DrawText:
        return tr("position: %1, text: \"%2\", font: %3").arg(VariantHandler::displayString(argumentAt(cmd, 0)), VariantHandler::displayString(argumentAt(cmd, 1)), VariantHandler::displayString(argumentAt(cmd, 2)));
    case QPaintBufferPrivate::Cmd_DrawTextItem: {
        QPointF pos(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1));
        auto textItem = reinterpret_cast<QTextItemIntCopy *>(m_privateBuffer->variants.at(cmd.offset).value<void *>());
        return tr("position: %1, text: \"%2\", font: %3").arg(VariantHandler::displayString(pos), (*textItem)().text(), VariantHandler::displayString((*textItem)().font()));
    }

    case QPaintBufferPrivate::Cmd_DrawImagePos:
    case QPaintBufferPrivate::Cmd_DrawPixmapPos:
        return VariantHandler::displayString(argumentAt(cmd, 0));
    case QPaintBufferPrivate::Cmd_DrawImageRect:
    case QPaintBufferPrivate::Cmd_DrawPixmapRect:
        return tr("%1 source: %2").arg(VariantHandler::displayString(argumentAt(cmd, 0)), VariantHandler::displayString(argumentAt(cmd, 2)));
    case QPaintBufferPrivate::Cmd_DrawTiledPixmap:
        return tr("%1 offset: %2").arg(VariantHandler::displayString(argumentAt(cmd, 0)), VariantHandler::displayString(argumentAt(cmd, 2)));

    case QPaintBufferPrivate::Cmd_SystemStateChanged:
        return VariantHandler::displayString(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_Translate:
        return VariantHandler::displayString(QPointF(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1)));
    case QPaintBufferPrivate::Cmd_DrawStaticText: {
        const auto variants = m_privateBuffer->variants.at(cmd.offset).value<QVariantList>();
        return tr("glyphs: %1, font: %2").arg(QString::number((variants.size() - 1) / 2), VariantHandler::displayString(variants.at(0)));
    }
    }
    return QString();
}

QVariant PaintBufferModel::argumentDecoration(const QPaintBufferCommand &cmd) const
{
    switch (cmd.id) {
    case QPaintBufferPrivate::Cmd_SetBrush:
    case QPaintBufferPrivate::Cmd_SetPen:
    case QPaintBufferPrivate::Cmd_DrawPixmapRect:
    case QPaintBufferPrivate::Cmd_DrawPixmapPos:
    case QPaintBufferPrivate::Cmd_DrawTiledPixmap:
    case QPaintBufferPrivate::Cmd_DrawImageRect:
    case QPaintBufferPrivate::Cmd_DrawImagePos:
        return VariantHandler::decoration(m_privateBuffer->variants.at(cmd.offset));
    case QPaintBufferPrivate::Cmd_FillRectBrush:
    case QPaintBufferPrivate::Cmd_FillVectorPath:
    case QPaintBufferPrivate::Cmd_StrokeVectorPath:
    case QPaintBufferPrivate::Cmd_FillRectColor:
        return VariantHandler::decoration(m_privateBuffer->variants.at(cmd.extra));
    }
    return QVariant();
}

QVariant PaintBufferModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || !m_privateBuffer)
        return QVariant();

    if (!index.parent().isValid()) {
        const auto cmd = m_privateBuffer->commands.at(index.row());
        switch (role) {
        case Qt::DisplayRole:
            if (index.column() == 0)
                return cmdTypes[cmd.id].name;
            else if (index.column() == 1)
                return argumentDisplayString(cmd);
            else if (index.column() == 2 && m_costs.size() > index.row())
                return m_costs.at(index.row());
            break;
        case Qt::DecorationRole:
            if (index.column() == 1)
                return argumentDecoration(cmd);
            break;
        case Qt::EditRole:
            // for fancy matrix rendering in the client
            if (index.column() == 1 && cmd.id == QPaintBufferPrivate::Cmd_SetTransform)
                return m_privateBuffer->variants.at(cmd.offset);
            break;
        case PaintBufferModelRoles::ValueRole:
            return argumentAt(cmd, 0);
        case PaintBufferModelRoles::ClipPathRole:
            return QVariant::fromValue(clipPath(index.row()));
        case PaintBufferModelRoles::MaxCostRole:
            if (index.column() == 2 && index.row() == 0)
                return m_maxCost;
            break;
        case PaintBufferModelRoles::ObjectIdRole:
            return QVariant::fromValue(m_buffer.origin(index.row()));
        }
    } else {
        const auto cmd = m_privateBuffer->commands.at(index.internalId());
        switch (role) {
        case Qt::DisplayRole:
            if (index.column() == 0)
                return cmd_argument_names[cmdTypes[cmd.id].argumentName + index.row()];
            else if (index.column() == 1)
                return VariantHandler::displayString(argumentAt(cmd, index.row()));
            break;
        case PaintBufferModelRoles::ValueRole:
            return argumentAt(cmd, index.row());
        }
    }

    return QVariant();
}

QMap<int, QVariant> PaintBufferModel::itemData(const QModelIndex &index) const
{
    QMap<int, QVariant> d = QAbstractItemModel::itemData(index);
    d.insert(PaintBufferModelRoles::MaxCostRole, data(index, PaintBufferModelRoles::MaxCostRole));
    d.insert(PaintBufferModelRoles::ObjectIdRole, data(index, PaintBufferModelRoles::ObjectIdRole));
    return d;
}

int PaintBufferModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 3;
}

int PaintBufferModel::rowCount(const QModelIndex &parent) const
{
    if (!m_privateBuffer || parent.column() > 0)
        return 0;
    if (parent.isValid()) {
        const auto cmd = m_privateBuffer->commands.at(parent.row());
        return cmdTypes[cmd.id].argumentCount;
    }
    return m_privateBuffer->commands.size();
}

QModelIndex PaintBufferModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid())
        return createIndex(row, column, TopLevelId);
    return createIndex(row, column, parent.row());
}

QModelIndex PaintBufferModel::parent(const QModelIndex &child) const
{
    if (!child.isValid() || child.internalId() == TopLevelId)
        return {};
    return createIndex(child.internalId(), 0, TopLevelId);
}

QPainterPath PaintBufferModel::clipPath(int row) const
{
    QPainterPath clip;
    QTransform t;
    std::vector<QPainterPath> clipStack;
    std::vector<QTransform> transformStack;

    for (int i = 0; i <= row; ++i) {
        const auto cmd = m_privateBuffer->commands.at(i);

        QPainterPath p;
        Qt::ClipOperation op = Qt::NoClip;
        switch (cmd.id) {
        case QPaintBufferPrivate::Cmd_Save:
            clipStack.push_back(clip);
            transformStack.push_back(t);
            continue;
        case QPaintBufferPrivate::Cmd_Restore:
            if (clipStack.empty() || transformStack.empty())
                return QPainterPath();
            clip = clipStack.back();
            clipStack.pop_back();
            t = transformStack.back();
            transformStack.pop_back();
            continue;
        case QPaintBufferPrivate::Cmd_SetTransform:
            t = m_privateBuffer->variants.at(cmd.offset).value<QTransform>();
            continue;
        case QPaintBufferPrivate::Cmd_Translate:
            t.translate(m_privateBuffer->floats.at(cmd.extra), m_privateBuffer->floats.at(cmd.extra + 1));
            continue;
        case QPaintBufferPrivate::Cmd_ClipRect:
            p.addRect(QRect(QPoint(m_privateBuffer->ints.at(cmd.offset), m_privateBuffer->ints.at(cmd.offset + 1)),
                            QPoint(m_privateBuffer->ints.at(cmd.offset + 2), m_privateBuffer->ints.at(cmd.offset + 3))));
            p = t.map(p);
            op = static_cast<Qt::ClipOperation>(cmd.extra);
            break;
        case QPaintBufferPrivate::Cmd_ClipRegion:
            p.addRegion(m_privateBuffer->variants.at(cmd.offset).value<QRegion>());
            p = t.map(p);
            op = static_cast<Qt::ClipOperation>(cmd.extra);
            break;
        case QPaintBufferPrivate::Cmd_ClipPath:
            p = m_privateBuffer->variants.at(cmd.offset).value<QPainterPath>();
            p = t.map(p);
            op = static_cast<Qt::ClipOperation>(cmd.extra);
            break;
        case QPaintBufferPrivate::Cmd_ClipVectorPath:
            p = QVectorPath(m_privateBuffer->floats.constData() + cmd.offset, cmd.size,
                            cmd.offset2 & 0x80000000 ? nullptr : reinterpret_cast<const QPainterPath::ElementType *>(m_privateBuffer->ints.constData() + cmd.offset2 + 1),
                            *(m_privateBuffer->ints.constData() + (cmd.offset2 & 0x7FFFFFFF)))
                    .convertToPainterPath();
            p = t.map(p);
            op = static_cast<Qt::ClipOperation>(cmd.extra);
            break;
        case QPaintBufferPrivate::Cmd_SystemStateChanged:
            p.addRegion(m_privateBuffer->variants.at(cmd.offset).value<QRegion>());
            op = Qt::ReplaceClip;
            break;
        default:
            continue;
        }

        switch (op) {
        case Qt::NoClip:
            clip = QPainterPath();
            break;
        case Qt::ReplaceClip:
            clip = p;
            break;
        case Qt::IntersectClip:
            clip = clip.intersected(p);
            break;
        }
    }
    return clip;
}