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

« back to all changes in this revision

Viewing changes to examples/tools/qtdemo/displayshape.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) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes 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 <math.h>
 
30
#include <QtGui>
 
31
 
 
32
#include "displayshape.h"
 
33
 
 
34
DisplayShape::DisplayShape(const QPointF &position, const QSizeF &maxSize)
 
35
    : pos(position), maxSize(maxSize), interactive(false)
 
36
{
 
37
}
 
38
 
 
39
bool DisplayShape::animate()
 
40
{
 
41
    if (!targetPos.isNull()) {
 
42
        QLineF displacement(pos, targetPos);
 
43
        QPointF newPosition = displacement.pointAt(0.25);
 
44
        if (displacement.length() <= 1.0) {
 
45
            pos = targetPos;
 
46
            targetPos = QPointF();
 
47
        } else {
 
48
            pos = newPosition;
 
49
        }
 
50
        return true;
 
51
    }
 
52
 
 
53
    return false;
 
54
}
 
55
 
 
56
bool DisplayShape::contains(const QString &key) const
 
57
{
 
58
    return meta.contains(key);
 
59
}
 
60
 
 
61
bool DisplayShape::isInteractive() const
 
62
{
 
63
    return interactive;
 
64
}
 
65
 
 
66
QVariant DisplayShape::metaData(const QString &key) const
 
67
{
 
68
    return meta.value(key);
 
69
}
 
70
 
 
71
void DisplayShape::paint(QPainter *painter) const
 
72
{
 
73
    painter->save();
 
74
    painter->drawImage(pos, image);
 
75
    painter->restore();
 
76
}
 
77
 
 
78
QPointF DisplayShape::position() const
 
79
{
 
80
    return pos;
 
81
}
 
82
 
 
83
QRectF DisplayShape::rect() const
 
84
{
 
85
    return QRectF(pos, image.size());
 
86
}
 
87
 
 
88
void DisplayShape::removeMetaData(const QString &key)
 
89
{
 
90
    meta.remove(key);
 
91
}
 
92
 
 
93
void DisplayShape::setInteractive(bool enable)
 
94
{
 
95
    interactive = enable;
 
96
}
 
97
 
 
98
void DisplayShape::setMetaData(const QString &key, const QVariant &value)
 
99
{
 
100
    meta[key] = value;
 
101
}
 
102
 
 
103
void DisplayShape::setPosition(const QPointF &position)
 
104
{
 
105
    pos = position;
 
106
}
 
107
 
 
108
void DisplayShape::setTarget(const QPointF &position)
 
109
{
 
110
    targetPos = position;
 
111
}
 
112
 
 
113
QSizeF DisplayShape::size() const
 
114
{
 
115
    return maxSize;
 
116
}
 
117
 
 
118
QPointF DisplayShape::target() const
 
119
{
 
120
    return targetPos;
 
121
}
 
122
 
 
123
PanelShape::PanelShape(const QPainterPath &path, const QBrush &normal,
 
124
                       const QBrush &highlighted, const QPen &pen,
 
125
                       const QPointF &position, const QSizeF &maxSize)
 
126
    : DisplayShape(position, maxSize), highlightedBrush(highlighted),
 
127
      normalBrush(normal), path(path), pen(pen)
 
128
{
 
129
    brush = normalBrush;
 
130
}
 
131
 
 
132
bool PanelShape::animate()
 
133
{
 
134
    bool updated = false;
 
135
 
 
136
    if (!meta.contains("destroy")) {
 
137
        if (meta.contains("fade")) {
 
138
            QColor penColor = pen.color();
 
139
            QColor brushColor = brush.color();
 
140
            int penAlpha = penColor.alpha();
 
141
            int brushAlpha = brushColor.alpha();
 
142
 
 
143
            penAlpha = qBound(meta.value("fade minimum").toInt(),
 
144
                              penAlpha + meta.value("fade").toInt(), 255);
 
145
            brushAlpha = qBound(meta.value("fade minimum").toInt(),
 
146
                              brushAlpha + meta.value("fade").toInt(), 255);
 
147
 
 
148
            penColor.setAlpha(qBound(0, penAlpha, 255));
 
149
            brushColor.setAlpha(qBound(0, brushAlpha, 255));
 
150
            pen.setColor(penColor);
 
151
            brush.setColor(brushColor);
 
152
 
 
153
            if (penAlpha == 0 && brushAlpha == 0) {
 
154
                meta["destroy"] = true;
 
155
                meta.remove("fade");
 
156
            } else if (penAlpha == 255 && brushAlpha == 255)
 
157
                meta.remove("fade");
 
158
 
 
159
            updated = true;
 
160
        } else if (meta.contains("highlight")) {
 
161
            qreal scale = meta.value("highlight scale").toDouble();
 
162
            QColor color = brush.color();
 
163
 
 
164
            if (meta.value("highlight").toBool())
 
165
                scale = qBound(0.0, scale + 0.5, 1.0);
 
166
            else
 
167
                scale = qBound(0.0, scale - 0.2, 1.0);
 
168
 
 
169
            if (scale == 0.0) {
 
170
                brush = normalBrush;
 
171
                meta.remove("highlight");
 
172
                meta.remove("highlight scale");
 
173
            } else {
 
174
                meta["highlight scale"] = scale;
 
175
 
 
176
                if (scale == 1.0)
 
177
                    brush = highlightedBrush;
 
178
                else {
 
179
                    QColor normal = normalBrush.color();
 
180
                    QColor highlighted = highlightedBrush.color();
 
181
 
 
182
                    color.setRedF((1.0-scale) * normal.redF()
 
183
                                  + scale*highlighted.redF());
 
184
                    color.setGreenF((1.0-scale) * normal.greenF()
 
185
                                    + scale*highlighted.greenF());
 
186
                    color.setBlueF((1.0-scale) * normal.blueF()
 
187
                                   + scale*highlighted.blueF());
 
188
                    brush.setColor(color);
 
189
                }
 
190
            }
 
191
            updated = true;
 
192
        }
 
193
    }
 
194
 
 
195
    return DisplayShape::animate() || updated;
 
196
}
 
197
 
 
198
void PanelShape::paint(QPainter *painter) const
 
199
{
 
200
    painter->save();
 
201
    painter->setRenderHint(QPainter::Antialiasing);
 
202
    painter->setBrush(brush);
 
203
    painter->setPen(pen);
 
204
    painter->translate(pos);
 
205
    painter->drawPath(path);
 
206
    painter->restore();
 
207
}
 
208
 
 
209
QRectF PanelShape::rect() const
 
210
{
 
211
    return QRectF(pos + path.boundingRect().topLeft(),
 
212
                  path.boundingRect().size());
 
213
}
 
214
 
 
215
TitleShape::TitleShape(const QString &text, const QFont &f,
 
216
                       const QPen &pen, const QPointF &position,
 
217
                       const QSizeF &maxSize, Qt::Alignment alignment)
 
218
    : DisplayShape(position, maxSize), font(f), text(text), pen(pen),
 
219
      alignment(alignment)
 
220
{
 
221
    QFontMetrics fm(font);
 
222
    textRect = fm.boundingRect(QRect(QPoint(0, 0), maxSize.toSize()),
 
223
        alignment, text);
 
224
 
 
225
    qreal textWidth = qMax(fm.width(text), textRect.width());
 
226
    qreal textHeight = qMax(fm.height(), textRect.height());
 
227
 
 
228
    qreal scale = qMin(maxSize.width()/textWidth,
 
229
                       maxSize.height()/textHeight);
 
230
 
 
231
    font.setPointSizeF(font.pointSizeF() * scale);
 
232
    fm = QFontMetrics(font);
 
233
    textRect = fm.boundingRect(QRect(QPoint(0, 0), maxSize.toSize()),
 
234
        alignment, text);
 
235
}
 
236
 
 
237
bool TitleShape::animate()
 
238
{
 
239
    bool updated = false;
 
240
 
 
241
    if (!meta.contains("destroy")) {
 
242
        if (meta.contains("fade")) {
 
243
            QColor penColor = pen.color();
 
244
            int penAlpha = penColor.alpha();
 
245
 
 
246
            penAlpha = qBound(meta.value("fade minimum").toInt(),
 
247
                              penAlpha + meta.value("fade").toInt(), 255);
 
248
 
 
249
            penColor.setAlpha(penAlpha);
 
250
            pen.setColor(penColor);
 
251
 
 
252
            if (penAlpha == 0) {
 
253
                meta["destroy"] = true;
 
254
                meta.remove("fade");
 
255
            } else if (penAlpha == 255)
 
256
                meta.remove("fade");
 
257
 
 
258
            updated = true;
 
259
        }
 
260
    }
 
261
 
 
262
    return DisplayShape::animate() || updated;
 
263
}
 
264
 
 
265
void TitleShape::paint(QPainter *painter) const
 
266
{
 
267
    QRectF rect(textRect); // 2
 
268
    rect.translate(pos); // 2
 
269
    painter->save();
 
270
    painter->setRenderHint(QPainter::TextAntialiasing);
 
271
    painter->setPen(pen);
 
272
    painter->setFont(font);
 
273
    //painter->drawText(QRectF(pos, maxSize), alignment, text); // 1
 
274
    painter->drawText(rect, Qt::AlignLeft | Qt::AlignTop, text); // 2
 
275
    painter->restore();
 
276
}
 
277
 
 
278
QRectF TitleShape::rect() const
 
279
{
 
280
    QRectF rect(textRect);
 
281
    return rect.translated(pos);
 
282
}
 
283
 
 
284
ImageShape::ImageShape(const QImage &original, const QPointF &position,
 
285
                       const QSizeF &maxSize, int alpha,
 
286
                       Qt::Alignment alignment)
 
287
    : DisplayShape(position, maxSize), alpha(alpha), alignment(alignment)
 
288
{
 
289
    source = original.convertToFormat(QImage::Format_ARGB32_Premultiplied);
 
290
    scale = qMin(qMin(maxSize.width()/source.width(),
 
291
                      maxSize.height()/source.height()), 1.0);
 
292
    image = QImage(int(ceil(scale * source.width())),
 
293
                   int(ceil(scale * source.height())),
 
294
                   QImage::Format_ARGB32_Premultiplied);
 
295
 
 
296
    offset = QPointF(0.0, 0.0);
 
297
 
 
298
    if (alignment & Qt::AlignHCenter)
 
299
        offset.setX((maxSize.width() - image.width())/2);
 
300
    else if (alignment & Qt::AlignRight)
 
301
        offset.setX(maxSize.width() - image.width());
 
302
 
 
303
    if (alignment & Qt::AlignVCenter)
 
304
        offset.setY((maxSize.height() - image.height())/2);
 
305
    else if (alignment & Qt::AlignBottom)
 
306
        offset.setY(maxSize.height() - image.height());
 
307
 
 
308
    redraw();
 
309
}
 
310
 
 
311
void ImageShape::redraw()
 
312
{
 
313
    image.fill(qRgba(alpha, alpha, alpha, alpha));
 
314
 
 
315
    QPainter painter;
 
316
    painter.begin(&image);
 
317
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
 
318
    painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
 
319
    painter.scale(scale, scale);
 
320
    painter.drawImage(0, 0, source);
 
321
    painter.end();
 
322
}
 
323
 
 
324
void ImageShape::paint(QPainter *painter) const
 
325
{
 
326
    painter->drawImage(pos + offset, image);
 
327
}
 
328
 
 
329
QRectF ImageShape::rect() const
 
330
{
 
331
    return QRectF(pos, maxSize);
 
332
}
 
333
 
 
334
bool ImageShape::animate()
 
335
{
 
336
    bool updated = false;
 
337
 
 
338
    if (!meta.contains("destroy")) {
 
339
        if (meta.contains("fade")) {
 
340
            alpha = qBound(meta.value("fade minimum").toInt(),
 
341
                           alpha + meta.value("fade").toInt(), 255);
 
342
            redraw();
 
343
 
 
344
            if (alpha == 0) {
 
345
                meta["destroy"] = true;
 
346
                meta.remove("fade");
 
347
            } else if (alpha == 255)
 
348
                meta.remove("fade");
 
349
 
 
350
            updated = true;
 
351
        }
 
352
    }
 
353
 
 
354
    return DisplayShape::animate() || updated;
 
355
}
 
356
 
 
357
DocumentShape::DocumentShape(const QString &text, const QFont &f,
 
358
                       const QPen &pen, const QPointF &position,
 
359
                       const QSizeF &maxSize)
 
360
    : DisplayShape(position, maxSize), font(f), pen(pen)
 
361
{
 
362
    paragraphs = text.split("\n", QString::SkipEmptyParts);
 
363
 
 
364
    QFontMetrics fm(font);
 
365
    qreal scale = qMax(maxSize.height()/(fm.lineSpacing()*20), 1.0);
 
366
    font.setPointSizeF(font.pointSizeF() * scale);
 
367
 
 
368
    qreal oldHeight = maxSize.height();
 
369
    qreal height = formatText();
 
370
    if (height > oldHeight) {
 
371
        font.setPointSizeF(font.pointSizeF() * oldHeight/height);
 
372
        formatText();
 
373
    }
 
374
}
 
375
 
 
376
DocumentShape::~DocumentShape()
 
377
{
 
378
    qDeleteAll(layouts);
 
379
    layouts.clear();
 
380
}
 
381
 
 
382
bool DocumentShape::animate()
 
383
{
 
384
    bool updated = false;
 
385
 
 
386
    if (!meta.contains("destroy")) {
 
387
        if (meta.contains("fade")) {
 
388
            QColor penColor = pen.color();
 
389
            int penAlpha = penColor.alpha();
 
390
 
 
391
            penAlpha = qBound(meta.value("fade minimum").toInt(),
 
392
                              penAlpha + meta.value("fade").toInt(), 255);
 
393
 
 
394
            penColor.setAlpha(penAlpha);
 
395
            pen.setColor(penColor);
 
396
 
 
397
            if (penAlpha == 0) {
 
398
                meta["destroy"] = true;
 
399
                meta.remove("fade");
 
400
            } else if (penAlpha == 255)
 
401
                meta.remove("fade");
 
402
 
 
403
            updated = true;
 
404
        }
 
405
    }
 
406
 
 
407
    return DisplayShape::animate() || updated;
 
408
}
 
409
 
 
410
qreal DocumentShape::formatText()
 
411
{
 
412
    qDeleteAll(layouts);
 
413
    layouts.clear();
 
414
 
 
415
    QFontMetrics fm(font);
 
416
    qreal lineHeight = fm.height();
 
417
    qreal y = 0.0;
 
418
    qreal leftMargin = 0.0;
 
419
    qreal rightMargin = maxSize.width();
 
420
 
 
421
    foreach (QString paragraph, paragraphs) {
 
422
 
 
423
        QTextLayout *textLayout = new QTextLayout(paragraph, font);
 
424
        textLayout->beginLayout();
 
425
 
 
426
        while (true) {
 
427
            QTextLine line = textLayout->createLine();
 
428
            if (!line.isValid())
 
429
                break;
 
430
 
 
431
            line.setLineWidth(rightMargin - leftMargin);
 
432
            line.setPosition(QPointF(leftMargin, y));
 
433
            y += line.height();
 
434
        }
 
435
 
 
436
        textLayout->endLayout();
 
437
        layouts.append(textLayout);
 
438
 
 
439
        y += lineHeight;
 
440
    }
 
441
 
 
442
    maxSize.setHeight(y);
 
443
    return y;
 
444
}
 
445
 
 
446
void DocumentShape::paint(QPainter *painter) const
 
447
{
 
448
    painter->save();
 
449
    painter->setRenderHint(QPainter::TextAntialiasing);
 
450
    painter->setPen(pen);
 
451
    painter->setFont(font);
 
452
    foreach (QTextLayout *layout, layouts)
 
453
        layout->draw(painter, pos);
 
454
    painter->restore();
 
455
}
 
456
 
 
457
QRectF DocumentShape::rect() const
 
458
{
 
459
    return QRectF(pos, maxSize);
 
460
}