1
/****************************************************************************
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
32
#include "displayshape.h"
34
DisplayShape::DisplayShape(const QPointF &position, const QSizeF &maxSize)
35
: pos(position), maxSize(maxSize), interactive(false)
39
bool DisplayShape::animate()
41
if (!targetPos.isNull()) {
42
QLineF displacement(pos, targetPos);
43
QPointF newPosition = displacement.pointAt(0.25);
44
if (displacement.length() <= 1.0) {
46
targetPos = QPointF();
56
bool DisplayShape::contains(const QString &key) const
58
return meta.contains(key);
61
bool DisplayShape::isInteractive() const
66
QVariant DisplayShape::metaData(const QString &key) const
68
return meta.value(key);
71
void DisplayShape::paint(QPainter *painter) const
74
painter->drawImage(pos, image);
78
QPointF DisplayShape::position() const
83
QRectF DisplayShape::rect() const
85
return QRectF(pos, image.size());
88
void DisplayShape::removeMetaData(const QString &key)
93
void DisplayShape::setInteractive(bool enable)
98
void DisplayShape::setMetaData(const QString &key, const QVariant &value)
103
void DisplayShape::setPosition(const QPointF &position)
108
void DisplayShape::setTarget(const QPointF &position)
110
targetPos = position;
113
QSizeF DisplayShape::size() const
118
QPointF DisplayShape::target() const
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)
132
bool PanelShape::animate()
134
bool updated = false;
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();
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);
148
penColor.setAlpha(qBound(0, penAlpha, 255));
149
brushColor.setAlpha(qBound(0, brushAlpha, 255));
150
pen.setColor(penColor);
151
brush.setColor(brushColor);
153
if (penAlpha == 0 && brushAlpha == 0) {
154
meta["destroy"] = true;
156
} else if (penAlpha == 255 && brushAlpha == 255)
160
} else if (meta.contains("highlight")) {
161
qreal scale = meta.value("highlight scale").toDouble();
162
QColor color = brush.color();
164
if (meta.value("highlight").toBool())
165
scale = qBound(0.0, scale + 0.5, 1.0);
167
scale = qBound(0.0, scale - 0.2, 1.0);
171
meta.remove("highlight");
172
meta.remove("highlight scale");
174
meta["highlight scale"] = scale;
177
brush = highlightedBrush;
179
QColor normal = normalBrush.color();
180
QColor highlighted = highlightedBrush.color();
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);
195
return DisplayShape::animate() || updated;
198
void PanelShape::paint(QPainter *painter) const
201
painter->setRenderHint(QPainter::Antialiasing);
202
painter->setBrush(brush);
203
painter->setPen(pen);
204
painter->translate(pos);
205
painter->drawPath(path);
209
QRectF PanelShape::rect() const
211
return QRectF(pos + path.boundingRect().topLeft(),
212
path.boundingRect().size());
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),
221
QFontMetrics fm(font);
222
textRect = fm.boundingRect(QRect(QPoint(0, 0), maxSize.toSize()),
225
qreal textWidth = qMax(fm.width(text), textRect.width());
226
qreal textHeight = qMax(fm.height(), textRect.height());
228
qreal scale = qMin(maxSize.width()/textWidth,
229
maxSize.height()/textHeight);
231
font.setPointSizeF(font.pointSizeF() * scale);
232
fm = QFontMetrics(font);
233
textRect = fm.boundingRect(QRect(QPoint(0, 0), maxSize.toSize()),
237
bool TitleShape::animate()
239
bool updated = false;
241
if (!meta.contains("destroy")) {
242
if (meta.contains("fade")) {
243
QColor penColor = pen.color();
244
int penAlpha = penColor.alpha();
246
penAlpha = qBound(meta.value("fade minimum").toInt(),
247
penAlpha + meta.value("fade").toInt(), 255);
249
penColor.setAlpha(penAlpha);
250
pen.setColor(penColor);
253
meta["destroy"] = true;
255
} else if (penAlpha == 255)
262
return DisplayShape::animate() || updated;
265
void TitleShape::paint(QPainter *painter) const
267
QRectF rect(textRect); // 2
268
rect.translate(pos); // 2
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
278
QRectF TitleShape::rect() const
280
QRectF rect(textRect);
281
return rect.translated(pos);
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)
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);
296
offset = QPointF(0.0, 0.0);
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());
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());
311
void ImageShape::redraw()
313
image.fill(qRgba(alpha, alpha, alpha, alpha));
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);
324
void ImageShape::paint(QPainter *painter) const
326
painter->drawImage(pos + offset, image);
329
QRectF ImageShape::rect() const
331
return QRectF(pos, maxSize);
334
bool ImageShape::animate()
336
bool updated = false;
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);
345
meta["destroy"] = true;
347
} else if (alpha == 255)
354
return DisplayShape::animate() || updated;
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)
362
paragraphs = text.split("\n", QString::SkipEmptyParts);
364
QFontMetrics fm(font);
365
qreal scale = qMax(maxSize.height()/(fm.lineSpacing()*20), 1.0);
366
font.setPointSizeF(font.pointSizeF() * scale);
368
qreal oldHeight = maxSize.height();
369
qreal height = formatText();
370
if (height > oldHeight) {
371
font.setPointSizeF(font.pointSizeF() * oldHeight/height);
376
DocumentShape::~DocumentShape()
382
bool DocumentShape::animate()
384
bool updated = false;
386
if (!meta.contains("destroy")) {
387
if (meta.contains("fade")) {
388
QColor penColor = pen.color();
389
int penAlpha = penColor.alpha();
391
penAlpha = qBound(meta.value("fade minimum").toInt(),
392
penAlpha + meta.value("fade").toInt(), 255);
394
penColor.setAlpha(penAlpha);
395
pen.setColor(penColor);
398
meta["destroy"] = true;
400
} else if (penAlpha == 255)
407
return DisplayShape::animate() || updated;
410
qreal DocumentShape::formatText()
415
QFontMetrics fm(font);
416
qreal lineHeight = fm.height();
418
qreal leftMargin = 0.0;
419
qreal rightMargin = maxSize.width();
421
foreach (QString paragraph, paragraphs) {
423
QTextLayout *textLayout = new QTextLayout(paragraph, font);
424
textLayout->beginLayout();
427
QTextLine line = textLayout->createLine();
431
line.setLineWidth(rightMargin - leftMargin);
432
line.setPosition(QPointF(leftMargin, y));
436
textLayout->endLayout();
437
layouts.append(textLayout);
442
maxSize.setHeight(y);
446
void DocumentShape::paint(QPainter *painter) const
449
painter->setRenderHint(QPainter::TextAntialiasing);
450
painter->setPen(pen);
451
painter->setFont(font);
452
foreach (QTextLayout *layout, layouts)
453
layout->draw(painter, pos);
457
QRectF DocumentShape::rect() const
459
return QRectF(pos, maxSize);