|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
5 ** |
|
6 ** This file is part of the examples of the Qt Toolkit. |
|
7 ** |
|
8 ** $QT_BEGIN_LICENSE:LGPL$ |
|
9 ** No Commercial Usage |
|
10 ** This file contains pre-release code and may not be distributed. |
|
11 ** You may use this file in accordance with the terms and conditions |
|
12 ** contained in the either Technology Preview License Agreement or the |
|
13 ** Beta Release License Agreement. |
|
14 ** |
|
15 ** GNU Lesser General Public License Usage |
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
17 ** General Public License version 2.1 as published by the Free Software |
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
19 ** packaging of this file. Please review the following information to |
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
22 ** |
|
23 ** In addition, as a special exception, Nokia gives you certain |
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL |
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
|
26 ** package. |
|
27 ** |
|
28 ** GNU General Public License Usage |
|
29 ** Alternatively, this file may be used under the terms of the GNU |
|
30 ** General Public License version 3.0 as published by the Free Software |
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the |
|
32 ** packaging of this file. Please review the following information to |
|
33 ** ensure the GNU General Public License version 3.0 requirements will be |
|
34 ** met: http://www.gnu.org/copyleft/gpl.html. |
|
35 ** |
|
36 ** If you are unsure which license is appropriate for your use, please |
|
37 ** contact the sales department at http://qt.nokia.com/contact. |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 #include "button.h" |
|
44 #include "theme.h" |
|
45 |
|
46 static const int MinTextWidthAsChars = 8; |
|
47 |
|
48 class ButtonPrivate { |
|
49 Q_DECLARE_PUBLIC(Button) |
|
50 |
|
51 public: |
|
52 |
|
53 ButtonPrivate(Button *button) |
|
54 : down(false) |
|
55 , q_ptr(button) |
|
56 { |
|
57 textItem = new QGraphicsSimpleTextItem(q_ptr); |
|
58 } |
|
59 |
|
60 QGraphicsSimpleTextItem *textItem; |
|
61 bool down; |
|
62 Button *q_ptr; |
|
63 }; |
|
64 |
|
65 Button::Button(const QString &text, QGraphicsItem *parent, QSizeF minimumSize) |
|
66 : QGraphicsWidget(parent) |
|
67 , d_ptr(new ButtonPrivate(this)), m_background(), m_selected(false) |
|
68 { |
|
69 Q_D(Button); |
|
70 setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); |
|
71 //setCacheMode(QGraphicsItem::ItemCoordinateCache); |
|
72 if(minimumSize.isValid()) |
|
73 setMinimumSize(minimumSize); |
|
74 setContentsMargins(0, 0, 0, 0); |
|
75 d->textItem->setText(text); |
|
76 prepareGeometryChange(); |
|
77 |
|
78 m_font = Theme::p()->font(Theme::MenuItem); |
|
79 d->textItem->setFont(m_font); |
|
80 connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange())); |
|
81 } |
|
82 |
|
83 Button::~Button() |
|
84 { |
|
85 delete d_ptr; |
|
86 } |
|
87 |
|
88 bool Button::isDown() |
|
89 { |
|
90 Q_D(Button); |
|
91 |
|
92 return d->down; |
|
93 } |
|
94 |
|
95 void Button::setText(const QString &text) |
|
96 { |
|
97 Q_D(Button); |
|
98 d->textItem->setText(text); |
|
99 update(); |
|
100 } |
|
101 |
|
102 QString Button::text() |
|
103 { |
|
104 Q_D(Button); |
|
105 return d->textItem->text(); |
|
106 } |
|
107 |
|
108 void Button::paint(QPainter *painter, |
|
109 const QStyleOptionGraphicsItem *option, |
|
110 QWidget *widget) |
|
111 { |
|
112 Q_UNUSED(widget); |
|
113 Q_UNUSED(option); |
|
114 |
|
115 if(!m_background.isNull()) |
|
116 painter->drawPixmap(QPoint(), m_background); |
|
117 if(m_selected) { |
|
118 painter->setBrush(Qt::black); |
|
119 painter->setOpacity(0.2); |
|
120 painter->drawRect(boundingRect().toRect()); |
|
121 } |
|
122 } |
|
123 |
|
124 QSizeF Button::sizeHint(Qt::SizeHint which, |
|
125 const QSizeF &constraint) const |
|
126 { |
|
127 Q_D(const Button); |
|
128 |
|
129 switch (which) |
|
130 { |
|
131 case Qt::MinimumSize: |
|
132 { |
|
133 QFontMetricsF fm(d->textItem->font()); |
|
134 return QSizeF(MinTextWidthAsChars * fm.maxWidth(), fm.height()); |
|
135 } |
|
136 case Qt::PreferredSize: |
|
137 { |
|
138 QFontMetricsF fm(d->textItem->font()); |
|
139 return QSizeF(fm.width(d->textItem->text()), fm.height()); |
|
140 } |
|
141 default: |
|
142 return QGraphicsWidget::sizeHint(which, constraint); |
|
143 } |
|
144 } |
|
145 |
|
146 void Button::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
147 { |
|
148 Q_D(Button); |
|
149 |
|
150 if (event->button() != Qt::LeftButton || |
|
151 !sceneBoundingRect().contains(event->scenePos())) |
|
152 return; |
|
153 |
|
154 d->down = true; |
|
155 |
|
156 prepareGeometryChange(); |
|
157 emit pressed(); |
|
158 |
|
159 } |
|
160 |
|
161 void Button::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
162 { |
|
163 Q_D(Button); |
|
164 |
|
165 if (!d->down || event->button() != Qt::LeftButton) |
|
166 return; |
|
167 |
|
168 d->down = false; |
|
169 |
|
170 prepareGeometryChange(); |
|
171 |
|
172 emit released(); |
|
173 |
|
174 if (sceneBoundingRect().contains(event->scenePos())) |
|
175 emit clicked(); |
|
176 } |
|
177 |
|
178 void Button::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
179 { |
|
180 Q_UNUSED(event); |
|
181 } |
|
182 |
|
183 void Button::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
184 { |
|
185 Q_D(Button); |
|
186 QGraphicsWidget::resizeEvent(event); |
|
187 |
|
188 QRectF rect = d->textItem->boundingRect(); |
|
189 QRectF buttonrect = this->boundingRect(); |
|
190 d->textItem->setPos((buttonrect.width() - rect.width())/2, (buttonrect.height() - rect.height())/2 ); |
|
191 |
|
192 QSize currentSize = buttonrect.size().toSize(); |
|
193 if( m_background.size() != currentSize && (currentSize.width() > 0 && currentSize.height() > 0) ) { |
|
194 m_background = Theme::p()->pixmap("status_field_middle.svg", buttonrect.size().toSize()); |
|
195 } |
|
196 } |
|
197 |
|
198 void Button::setBackground(QPixmap& background) |
|
199 { |
|
200 m_background = background; |
|
201 } |
|
202 |
|
203 void Button::themeChange() |
|
204 { |
|
205 Q_D(Button); |
|
206 |
|
207 m_font = Theme::p()->font(Theme::MenuItem); |
|
208 d->textItem->setFont(m_font); |
|
209 } |