Developer Documentation

QtGraphicsButton.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46 //=============================================================================
47 //
48 // CLASS QtGraphicsButton - IMPLEMENTATION
49 //
50 //=============================================================================
51
52 //== INCLUDES =================================================================
53
54 #include <QPainter>
55 #include <QGraphicsSceneMouseEvent>
56
57 #include "QtGraphicsButton.hh"
58
59 //== IMPLEMENTATION ==========================================================
60
61 QtGraphicsButton::QtGraphicsButton (const QImage &_image, QGraphicsItem *_parent, int _width, int _height) :
62 QGraphicsItem (_parent),
63 checkable_(false),
64 checked_(false),
65 pressed_(false),
66 over_(false),
67 width_(_width),
68 height_(_height)
69 {
70 if (width_ <= 0)
71 width_ = _image.width ();
72 if (height_ <= 0)
73 height_ = _image.height ();
74
75 // scale image to button dimensions
76 QImage i = _image.scaled (width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat (QImage::Format_ARGB32);
77
78 checkedPix_ = QPixmap::fromImage (i);
79
80 // generate black/white and transparent images for the other button states
81 QImage normal (width_, height_, QImage::Format_ARGB32);
82 QImage over (width_, height_, QImage::Format_ARGB32);
83
84 for (int x = 0; x < width_; x++)
85 for (int y = 0; y < height_; y++)
86 {
87 QRgb pix = i.pixel (x, y);
88 over.setPixel (x, y, qRgba (qRed (pix), qGreen (pix), qBlue (pix), qAlpha (pix) * 0.5));
89 normal.setPixel (x, y, qRgba (qGray (pix), qGray (pix), qGray (pix), qAlpha (pix) * 0.3));
90 }
91 overPix_ = QPixmap::fromImage (over);
92 normalPix_ = QPixmap::fromImage (normal);
93
94 setAcceptHoverEvents (true);
95 }
96
97 //-----------------------------------------------------------------------------
98
99 QRectF QtGraphicsButton::boundingRect () const
100 {
101 return QRectF (QPointF(0, 0), QSizeF (width_, height_));
102 }
103
104 //-----------------------------------------------------------------------------
105
106 void QtGraphicsButton::paint (QPainter *_painter, const QStyleOptionGraphicsItem *, QWidget *)
107 {
108 _painter->setClipping (false);
109 if (pressed_ || checked_)
110 _painter->drawPixmap (0, 0, checkedPix_);
111 else if (over_)
112 _painter->drawPixmap (0, 0, overPix_);
113 else
114 _painter->drawPixmap (0, 0, normalPix_);
115 }
116
117 //-----------------------------------------------------------------------------
118
119 void QtGraphicsButton::setCheckable (bool _value)
120 {
121 checkable_ = _value;
122 }
123
124 //-----------------------------------------------------------------------------
125
126 void QtGraphicsButton::setChecked (bool _value)
127 {
128 checked_ = _value;
129 }
130
131 //-----------------------------------------------------------------------------
132
133 bool QtGraphicsButton::isChecked () const
134 {
135 return checked_;
136 }
137
138 //-----------------------------------------------------------------------------
139
140 void QtGraphicsButton::hoverEnterEvent (QGraphicsSceneHoverEvent *)
141 {
142 over_ = true;
143 update ();
144 }
145
146 //-----------------------------------------------------------------------------
147
148 void QtGraphicsButton::hoverLeaveEvent (QGraphicsSceneHoverEvent *)
149 {
150 over_ = false;
151 update ();
152 }
153
154 //-----------------------------------------------------------------------------
155
156 void QtGraphicsButton::mousePressEvent ( QGraphicsSceneMouseEvent *_event)
157 {
158 _event->accept ();
159 pressed_ = true;
160 if (checkable_)
161 checked_ = !checked_;
162 update ();
163
164 emit pressed ();
165 }
166
167 //-----------------------------------------------------------------------------
168
169 void QtGraphicsButton::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
170 {
171 _event->accept ();
172 pressed_ = false;
173 update ();
174 }
175
176 //-----------------------------------------------------------------------------
177
178 QVariant QtGraphicsButton::itemChange (GraphicsItemChange _change, const QVariant &_value)
179 {
180 // reset state if button was hidden
181 if (_change == QGraphicsItem::ItemVisibleHasChanged)
182 {
183 pressed_ = false;
184 over_ = false;
185 }
186 return QGraphicsItem::itemChange (_change, _value);
187 }
188
189 //=============================================================================
QtGraphicsButton::isChecked
bool isChecked() const
returns button checked state
Definition: QtGraphicsButton.cc:133
QtGraphicsButton::checkable_
bool checkable_
button state
Definition: QtGraphicsButton.hh:114
QtGraphicsButton::boundingRect
virtual QRectF boundingRect() const
returns the bounding rect
Definition: QtGraphicsButton.cc:99
QtGraphicsButton::setChecked
void setChecked(bool _value)
sets button checked state
Definition: QtGraphicsButton.cc:126
QtGraphicsButton::width_
int width_
button size
Definition: QtGraphicsButton.hh:120
QtGraphicsButton::QtGraphicsButton
QtGraphicsButton(const QImage &_image, QGraphicsItem *_parent=0, int _width=-1, int _height=-1)
Definition: QtGraphicsButton.cc:61
QtGraphicsButton::paint
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
paints the button
Definition: QtGraphicsButton.cc:106
QtGraphicsButton::normalPix_
QPixmap normalPix_
pixmaps for different button states
Definition: QtGraphicsButton.hh:124
QtGraphicsButton::hoverEnterEvent
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *_event)
event tracking
Definition: QtGraphicsButton.cc:140
QtGraphicsButton::setCheckable
void setCheckable(bool _value)
makes the button checkable
Definition: QtGraphicsButton.cc:119
QtGraphicsButton::pressed
void pressed()
signals a button press