~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/gui/image/qpaintengine_pic.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the painting module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "private/qpaintengine_p.h"
 
30
#include "private/qpainter_p.h"
 
31
#include "private/qpicture_p.h"
 
32
 
 
33
#include "qbuffer.h"
 
34
#include "qbytearray.h"
 
35
#include "qdatastream.h"
 
36
#include "qpaintengine_pic_p.h"
 
37
#include "qpicture.h"
 
38
#include "qpolygon.h"
 
39
#include "qrect.h"
 
40
 
 
41
// #define QT_PICTURE_DEBUG
 
42
#include <qdebug.h>
 
43
 
 
44
class QPicturePaintEnginePrivate : public QPaintEnginePrivate
 
45
{
 
46
    Q_DECLARE_PUBLIC(QPicturePaintEngine)
 
47
public:
 
48
    QDataStream s;
 
49
    QPainter *pt;
 
50
    QPicturePrivate *pic_d;
 
51
};
 
52
 
 
53
QPicturePaintEngine::QPicturePaintEngine()
 
54
    : QPaintEngine(*(new QPicturePaintEnginePrivate), AllFeatures)
 
55
{
 
56
    Q_D(QPicturePaintEngine);
 
57
    d->pt = 0;
 
58
}
 
59
 
 
60
QPicturePaintEngine::QPicturePaintEngine(QPaintEnginePrivate &dptr)
 
61
    : QPaintEngine(dptr, AllFeatures)
 
62
{
 
63
    Q_D(QPicturePaintEngine);
 
64
    d->pt = 0;
 
65
}
 
66
 
 
67
QPicturePaintEngine::~QPicturePaintEngine()
 
68
{
 
69
}
 
70
 
 
71
bool QPicturePaintEngine::begin(QPaintDevice *pd)
 
72
{
 
73
    Q_D(QPicturePaintEngine);
 
74
#ifdef QT_PICTURE_DEBUG
 
75
    qDebug() << "QPicturePaintEngine::begin()";
 
76
#endif
 
77
    Q_ASSERT(pd);
 
78
    QPicture *pic = static_cast<QPicture *>(pd);
 
79
 
 
80
    d->pdev = pd;
 
81
    d->pic_d = pic->d_func();
 
82
    Q_ASSERT(d->pic_d);
 
83
 
 
84
    d->s.setDevice(&d->pic_d->pictb);
 
85
    d->s.setVersion(d->pic_d->formatMajor);
 
86
 
 
87
    d->pic_d->pictb.open(QIODevice::WriteOnly | QIODevice::Truncate);
 
88
    d->s.writeRawData(qt_mfhdr_tag, 4);
 
89
    d->s << (quint16) 0 << (quint16) d->pic_d->formatMajor << (quint16) d->pic_d->formatMinor;
 
90
    d->s << (quint8) QPicturePrivate::PdcBegin << (quint8) sizeof(qint32);
 
91
    d->pic_d->brect = QRect();
 
92
    if (d->pic_d->formatMajor >= 4) {
 
93
        QRect r = d->pic_d->brect;
 
94
        d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
 
95
             << (qint32) r.height();
 
96
    }
 
97
    d->pic_d->trecs = 0;
 
98
    d->s << (quint32)d->pic_d->trecs; // total number of records
 
99
    d->pic_d->formatOk = false;
 
100
    setActive(true);
 
101
    return true;
 
102
}
 
103
 
 
104
bool QPicturePaintEngine::end()
 
105
{
 
106
    Q_D(QPicturePaintEngine);
 
107
#ifdef QT_PICTURE_DEBUG
 
108
    qDebug() << "QPicturePaintEngine::end()";
 
109
#endif
 
110
    d->pdev = 0;
 
111
    d->pic_d->trecs++;
 
112
    d->s << (quint8) QPicturePrivate::PdcEnd << (quint8) 0;
 
113
    int cs_start = sizeof(quint32);                // pos of checksum word
 
114
    int data_start = cs_start + sizeof(quint16);
 
115
    int brect_start = data_start + 2*sizeof(qint16) + 2*sizeof(quint8);
 
116
    int pos = d->pic_d->pictb.pos();
 
117
    d->pic_d->pictb.seek(brect_start);
 
118
    if (d->pic_d->formatMajor >= 4) { // bounding rectangle
 
119
        QRect r = d->pic_d->brect;
 
120
        d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
 
121
             << (qint32) r.height();
 
122
    }
 
123
    d->s << (quint32) d->pic_d->trecs;                        // write number of records
 
124
    d->pic_d->pictb.seek(cs_start);
 
125
    QByteArray buf = d->pic_d->pictb.buffer();
 
126
    quint16 cs = (quint16) qChecksum(buf.constData() + data_start, pos - data_start);
 
127
    d->s << cs;                                // write checksum
 
128
    d->pic_d->pictb.close();
 
129
    setActive(false);
 
130
    return true;
 
131
}
 
132
 
 
133
#define SERIALIZE_CMD(c) \
 
134
    d->pic_d->trecs++; \
 
135
    d->s << (quint8) c; \
 
136
    d->s << (quint8) 0; \
 
137
    pos = d->pic_d->pictb.pos()
 
138
 
 
139
void QPicturePaintEngine::updatePen(const QPen &pen)
 
140
{
 
141
    Q_D(QPicturePaintEngine);
 
142
#ifdef QT_PICTURE_DEBUG
 
143
    qDebug() << " -> updatePen(): width:" << pen.width() << "style:"
 
144
             << pen.style() << "color:" << pen.color();
 
145
#endif
 
146
    int pos;
 
147
    SERIALIZE_CMD(QPicturePrivate::PdcSetPen);
 
148
    d->s << pen;
 
149
    writeCmdLength(pos, QRect(), false);
 
150
}
 
151
 
 
152
void QPicturePaintEngine::updateBrush(const QBrush &brush, const QPointF &)
 
153
{
 
154
    Q_D(QPicturePaintEngine);
 
155
#ifdef QT_PICTURE_DEBUG
 
156
    qDebug() << " -> updateBrush(): style:" << brush.style();
 
157
#endif
 
158
    int pos;
 
159
    SERIALIZE_CMD(QPicturePrivate::PdcSetBrush);
 
160
    d->s << brush;
 
161
    writeCmdLength(pos, QRect(), false);
 
162
}
 
163
 
 
164
void QPicturePaintEngine::updateFont(const QFont &font)
 
165
{
 
166
    Q_D(QPicturePaintEngine);
 
167
#ifdef QT_PICTURE_DEBUG
 
168
    qDebug() << " -> updateFont(): pt sz:" << font.pointSize();
 
169
#endif
 
170
    int pos;
 
171
    SERIALIZE_CMD(QPicturePrivate::PdcSetFont);
 
172
    QFont fnt = font;
 
173
    d->s << fnt;
 
174
    writeCmdLength(pos, QRectF(), false);
 
175
}
 
176
 
 
177
void QPicturePaintEngine::updateBackground(Qt::BGMode bgMode, const QBrush &bgBrush)
 
178
{
 
179
    Q_D(QPicturePaintEngine);
 
180
#ifdef QT_PICTURE_DEBUG
 
181
    qDebug() << " -> updateBackground(): mode:" << bgMode << "style:" << bgBrush.style();
 
182
#endif
 
183
    int pos;
 
184
    SERIALIZE_CMD(QPicturePrivate::PdcSetBkColor);
 
185
    d->s << bgBrush.color();
 
186
    writeCmdLength(pos, QRect(), false);
 
187
 
 
188
    SERIALIZE_CMD(QPicturePrivate::PdcSetBkMode);
 
189
    d->s << (qint8) bgMode;
 
190
    writeCmdLength(pos, QRectF(), false);
 
191
}
 
192
 
 
193
void QPicturePaintEngine::updateMatrix(const QMatrix &matrix)
 
194
{
 
195
    Q_D(QPicturePaintEngine);
 
196
#ifdef QT_PICTURE_DEBUG
 
197
    qDebug() << " -> updateMatrix():" << matrix;
 
198
#endif
 
199
    int pos;
 
200
    SERIALIZE_CMD(QPicturePrivate::PdcSetWMatrix);
 
201
    d->s << matrix << (qint8) false;
 
202
    writeCmdLength(pos, QRectF(), false);
 
203
}
 
204
 
 
205
void QPicturePaintEngine::updateClipRegion(const QRegion &region, Qt::ClipOperation op)
 
206
{
 
207
    Q_D(QPicturePaintEngine);
 
208
#ifdef QT_PICTURE_DEBUG
 
209
    qDebug() << " -> updateClipRegion(): op:" << op
 
210
             << "bounding rect:" << region.boundingRect();
 
211
#endif
 
212
    Q_UNUSED(op);
 
213
    int pos;
 
214
    SERIALIZE_CMD(QPicturePrivate::PdcSetClipRegion);
 
215
    d->s << region << qint8(0);
 
216
    writeCmdLength(pos, QRectF(), false);
 
217
 
 
218
    SERIALIZE_CMD(QPicturePrivate::PdcSetClip);
 
219
    d->s << (qint8) !region.isEmpty();
 
220
    writeCmdLength(pos, QRectF(), false);
 
221
}
 
222
 
 
223
void QPicturePaintEngine::updateClipPath(const QPainterPath &path, Qt::ClipOperation op)
 
224
{
 
225
    Q_D(QPicturePaintEngine);
 
226
#ifdef QT_PICTURE_DEBUG
 
227
    qDebug() << " -> updateClipPath(): op:" << op
 
228
             << "bounding rect:" << path.boundingRect();
 
229
#endif
 
230
    Q_UNUSED(op);
 
231
    int pos;
 
232
 
 
233
    SERIALIZE_CMD(QPicturePrivate::PdcSetClipPath);
 
234
    d->s << path << (qint8) op;
 
235
    writeCmdLength(pos, QRectF(), false);
 
236
}
 
237
 
 
238
void QPicturePaintEngine::updateRenderHints(QPainter::RenderHints hints)
 
239
{
 
240
    Q_D(QPicturePaintEngine);
 
241
#ifdef QT_PICTURE_DEBUG
 
242
    qDebug() << " -> updateRenderHints(): " << hints;
 
243
#endif
 
244
    int pos;
 
245
    SERIALIZE_CMD(QPicturePrivate::PdcSetRenderHint);
 
246
    d->s << (quint32) hints;
 
247
    writeCmdLength(pos, QRect(), false);
 
248
}
 
249
 
 
250
void QPicturePaintEngine::writeCmdLength(int pos, const QRectF &r, bool corr)
 
251
{
 
252
    Q_D(QPicturePaintEngine);
 
253
    int newpos = d->pic_d->pictb.pos();            // new position
 
254
    int length = newpos - pos;
 
255
    QRect br = r.toRect();
 
256
 
 
257
    if (length < 255) {                         // write 8-bit length
 
258
        d->pic_d->pictb.seek(pos - 1);             // position to right index
 
259
        d->s << (quint8)length;
 
260
    } else {                                    // write 32-bit length
 
261
        d->s << (quint32)0;                    // extend the buffer
 
262
        d->pic_d->pictb.seek(pos - 1);             // position to right index
 
263
        d->s << (quint8)255;                   // indicate 32-bit length
 
264
        char *p = d->pic_d->pictb.buffer().data();
 
265
        memmove(p+pos+4, p+pos, length);        // make room for 4 byte
 
266
        d->s << (quint32)length;
 
267
        newpos += 4;
 
268
    }
 
269
    d->pic_d->pictb.seek(newpos);                  // set to new position
 
270
 
 
271
    if (br.isValid()) {
 
272
        if (corr) {                             // widen bounding rect
 
273
            int w2 = painter()->pen().width() / 2;
 
274
            br.setCoords(br.left() - w2, br.top() - w2,
 
275
                          br.right() + w2, br.bottom() + w2);
 
276
        }
 
277
#ifndef QT_NO_TRANSFORMATIONS
 
278
        br = painter()->matrix().mapRect(br);
 
279
#endif
 
280
        if (painter()->hasClipping()) {
 
281
            QRect cr = painter()->clipRegion().boundingRect();
 
282
            br &= cr;
 
283
        }
 
284
        if (br.isValid())
 
285
            d->pic_d->brect |= br;                 // merge with existing rect
 
286
    }
 
287
}
 
288
 
 
289
void QPicturePaintEngine::drawPath(const QPainterPath &path)
 
290
{
 
291
    Q_D(QPicturePaintEngine);
 
292
#ifdef QT_PICTURE_DEBUG
 
293
    qDebug() << " -> drawPath():" << path.boundingRect();
 
294
#endif
 
295
    int pos;
 
296
    SERIALIZE_CMD(QPicturePrivate::PdcDrawPath);
 
297
    d->s << path;
 
298
    writeCmdLength(pos, path.boundingRect(), true);
 
299
}
 
300
 
 
301
void QPicturePaintEngine::drawPolygon(const QPointF *points, int numPoints, PolygonDrawMode mode)
 
302
{
 
303
    Q_D(QPicturePaintEngine);
 
304
#ifdef QT_PICTURE_DEBUG
 
305
    qDebug() << " -> drawPolygon(): size=" << numPoints;
 
306
#endif
 
307
    int pos;
 
308
 
 
309
    QPolygonF polygon;
 
310
    for (int i=0; i<numPoints; ++i)
 
311
        polygon << points[i];
 
312
 
 
313
    if (mode == PolylineMode) {
 
314
        SERIALIZE_CMD(QPicturePrivate::PdcDrawPolyline);
 
315
        d->s << polygon;
 
316
    } else {
 
317
        SERIALIZE_CMD(QPicturePrivate::PdcDrawPolygon);
 
318
        d->s << polygon;
 
319
        d->s << (qint8)(mode == OddEvenMode ? 0 : 1);
 
320
    }
 
321
 
 
322
    writeCmdLength(pos, polygon.boundingRect(), true);
 
323
}
 
324
 
 
325
void QPicturePaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
 
326
{
 
327
    Q_D(QPicturePaintEngine);
 
328
#ifdef QT_PICTURE_DEBUG
 
329
    qDebug() << " -> drawPixmap():" << r;
 
330
#endif
 
331
    int pos;
 
332
    SERIALIZE_CMD(QPicturePrivate::PdcDrawPixmap);
 
333
    d->s << r << pm << sr;
 
334
    writeCmdLength(pos, r, false);
 
335
}
 
336
 
 
337
void QPicturePaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
 
338
{
 
339
    Q_D(QPicturePaintEngine);
 
340
#ifdef QT_PICTURE_DEBUG
 
341
    qDebug() << " -> drawTiledPixmap():" << r << s;
 
342
#endif
 
343
    int pos;
 
344
    SERIALIZE_CMD(QPicturePrivate::PdcDrawTiledPixmap);
 
345
    d->s << r << pixmap << s;
 
346
    writeCmdLength(pos, r, false);
 
347
}
 
348
 
 
349
void QPicturePaintEngine::drawTextItem(const QPointF &p , const QTextItem &ti)
 
350
{
 
351
    Q_D(QPicturePaintEngine);
 
352
#ifdef QT_PICTURE_DEBUG
 
353
    qDebug() << " -> drawTextItem():" << p << QString(ti.chars, ti.num_chars);
 
354
#endif
 
355
    int pos;
 
356
    SERIALIZE_CMD(QPicturePrivate::PdcDrawText2);
 
357
    d->s << p << ti.text();
 
358
    writeCmdLength(pos, QRectF(p, QSizeF(1,1)), true);
 
359
}
 
360
 
 
361
void QPicturePaintEngine::updateState(const QPaintEngineState &state)
 
362
{
 
363
    QPaintEngine::DirtyFlags flags = state.state();
 
364
    if (flags & DirtyPen) updatePen(state.pen());
 
365
    if (flags & DirtyBrush) updateBrush(state.brush(), state.brushOrigin());
 
366
    if (flags & DirtyBackground) updateBackground(state.backgroundMode(), state.backgroundBrush());
 
367
    if (flags & DirtyFont) updateFont(state.font());
 
368
    if (flags & DirtyTransform) updateMatrix(state.matrix());
 
369
    if (flags & DirtyClipPath) updateClipPath(state.clipPath(), state.clipOperation());
 
370
    if (flags & DirtyClipRegion) updateClipRegion(state.clipRegion(), state.clipOperation());
 
371
    if (flags & DirtyHints) updateRenderHints(state.renderHints());
 
372
}