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
|
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
****************************************************************************/
#include "roundrectitem.h"
#include <QtGui/QtGui>
RoundRectItem::RoundRectItem(const QRectF &rect, const QBrush &brush, QWidget *embeddedWidget)
: QGraphicsWidget(),
brush(brush),
proxyWidget(0),
m_rect(rect)
{
if (embeddedWidget) {
proxyWidget = new QGraphicsProxyWidget(this);
proxyWidget->setFocusPolicy(Qt::StrongFocus);
proxyWidget->setWidget(embeddedWidget);
}
}
void RoundRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
const bool widgetHidden = parentItem() == 0 || qAbs(static_cast<QGraphicsWidget*>(parentItem())->yRotation()) < 90;
if (proxyWidget) {
if (widgetHidden) {
proxyWidget->hide();
} else {
if (!proxyWidget->isVisible()) {
proxyWidget->setGeometry(boundingRect().adjusted(25, 25, -25, -25));
proxyWidget->show();
proxyWidget->setFocus();
}
painter->setBrush(brush);
painter->setPen(QPen(Qt::black, 1));
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
painter->drawRoundRect(m_rect);
}
} else if (widgetHidden) {
painter->setPen(Qt::NoPen);
painter->setBrush(QColor(0, 0, 0, 64));
painter->drawRoundRect(m_rect.translated(2, 2));
QLinearGradient gradient(m_rect.topLeft(), m_rect.bottomRight());
const QColor col = brush.color();
gradient.setColorAt(0, col);
gradient.setColorAt(1, col.dark(200));
painter->setBrush(gradient);
painter->setPen(QPen(Qt::black, 1));
painter->drawRoundRect(m_rect);
if (!pix.isNull()) {
painter->scale(qreal(1.95), qreal(1.95));
painter->drawPixmap(-pix.width() / 2, -pix.height() / 2, pix);
}
}
}
QRectF RoundRectItem::boundingRect() const
{
qreal penW = qreal(.5);
qreal shadowW = 2;
return m_rect.adjusted(-penW, -penW, penW + shadowW, penW + shadowW);
}
void RoundRectItem::setPixmap(const QPixmap &pixmap)
{
pix = pixmap;
if (scene() && isVisible())
update();
}
void RoundRectItem::keyPressEvent(QKeyEvent *event)
{
if (event->isAutoRepeat() || event->key() != Qt::Key_Return) {
QGraphicsWidget::keyPressEvent(event);
return;
}
if (!proxyWidget) {
setXScale(qreal(.9));
setYScale(qreal(.9));
}
emit activated();
}
void RoundRectItem::keyReleaseEvent(QKeyEvent *event)
{
if (event->isAutoRepeat() || event->key() != Qt::Key_Return) {
QGraphicsWidget::keyReleaseEvent(event);
return;
}
if (!proxyWidget) {
setXScale(1);
setYScale(1);
}
}
|