Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49
50
51
52 //=============================================================================
53 //
54 // CLASS QtGraphicsButton - IMPLEMENTATION
55 //
56 //=============================================================================
57
58 //== INCLUDES =================================================================
59
60 #include <QPainter>
61 #include <QGraphicsSceneMouseEvent>
62
63 #include "QtGraphicsButton.hh"
64
65 //== IMPLEMENTATION ==========================================================
66
67 QtGraphicsButton::QtGraphicsButton (const QImage &_image, QGraphicsItem *_parent, int _width, int _height) :
68 QGraphicsItem (_parent),
69 checkable_(false),
70 checked_(false),
71 pressed_(false),
72 over_(false),
73 width_(_width),
74 height_(_height)
75 {
76 if (width_ <= 0)
77 width_ = _image.width ();
78 if (height_ <= 0)
79 height_ = _image.height ();
80
81 // scale image to button dimensions
82 QImage i = _image.scaled (width_, height_, Qt::IgnoreAspectRatio, Qt::SmoothTransformation).convertToFormat (QImage::Format_ARGB32);
83
84 checkedPix_ = QPixmap::fromImage (i);
85
86 // generate black/white and transparent images for the other button states
87 QImage normal (width_, height_, QImage::Format_ARGB32);
88 QImage over (width_, height_, QImage::Format_ARGB32);
89
90 for (int x = 0; x < width_; x++)
91 for (int y = 0; y < height_; y++)
92 {
93 QRgb pix = i.pixel (x, y);
94 over.setPixel (x, y, qRgba (qRed (pix), qGreen (pix), qBlue (pix), qAlpha (pix) * 0.5));
95 normal.setPixel (x, y, qRgba (qGray (pix), qGray (pix), qGray (pix), qAlpha (pix) * 0.3));
96 }
97 overPix_ = QPixmap::fromImage (over);
98 normalPix_ = QPixmap::fromImage (normal);
99
100 setAcceptHoverEvents (true);
101 }
102
103 //-----------------------------------------------------------------------------
104
105 QRectF QtGraphicsButton::boundingRect () const
106 {
107 return QRectF (QPointF(0, 0), QSizeF (width_, height_));
108 }
109
110 //-----------------------------------------------------------------------------
111
112 void QtGraphicsButton::paint (QPainter *_painter, const QStyleOptionGraphicsItem *, QWidget *)
113 {
114 _painter->setClipping (false);
115 if (pressed_ || checked_)
116 _painter->drawPixmap (0, 0, checkedPix_);
117 else if (over_)
118 _painter->drawPixmap (0, 0, overPix_);
119 else
120 _painter->drawPixmap (0, 0, normalPix_);
121 }
122
123 //-----------------------------------------------------------------------------
124
125 void QtGraphicsButton::setCheckable (bool _value)
126 {
127 checkable_ = _value;
128 }
129
130 //-----------------------------------------------------------------------------
131
132 void QtGraphicsButton::setChecked (bool _value)
133 {
134 checked_ = _value;
135 }
136
137 //-----------------------------------------------------------------------------
138
139 bool QtGraphicsButton::isChecked () const
140 {
141 return checked_;
142 }
143
144 //-----------------------------------------------------------------------------
145
146 void QtGraphicsButton::hoverEnterEvent (QGraphicsSceneHoverEvent *)
147 {
148 over_ = true;
149 update ();
150 }
151
152 //-----------------------------------------------------------------------------
153
154 void QtGraphicsButton::hoverLeaveEvent (QGraphicsSceneHoverEvent *)
155 {
156 over_ = false;
157 update ();
158 }
159
160 //-----------------------------------------------------------------------------
161
162 void QtGraphicsButton::mousePressEvent ( QGraphicsSceneMouseEvent *_event)
163 {
164 _event->accept ();
165 pressed_ = true;
166 if (checkable_)
167 checked_ = !checked_;
168 update ();
169
170 emit pressed ();
171 }
172
173 //-----------------------------------------------------------------------------
174
175 void QtGraphicsButton::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
176 {
177 _event->accept ();
178 pressed_ = false;
179 update ();
180 }
181
182 //-----------------------------------------------------------------------------
183
184 QVariant QtGraphicsButton::itemChange (GraphicsItemChange _change, const QVariant &_value)
185 {
186 // reset state if button was hidden
187 if (_change == QGraphicsItem::ItemVisibleHasChanged)
188 {
189 pressed_ = false;
190 over_ = false;
191 }
192 return QGraphicsItem::itemChange (_change, _value);
193 }
194
195 //=============================================================================
QtGraphicsButton::isChecked
bool isChecked() const
returns button checked state
Definition: QtGraphicsButton.cc:139
QtGraphicsButton::normalPix_
QPixmap normalPix_
pixmaps for different button states
Definition: QtGraphicsButton.hh:130
QtGraphicsButton::QtGraphicsButton
QtGraphicsButton(const QImage &_image, QGraphicsItem *_parent=0, int _width=-1, int _height=-1)
Definition: QtGraphicsButton.cc:67
QtGraphicsButton::setChecked
void setChecked(bool _value)
sets button checked state
Definition: QtGraphicsButton.cc:132
QtGraphicsButton::hoverEnterEvent
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *_event)
event tracking
Definition: QtGraphicsButton.cc:146
QtGraphicsButton::boundingRect
virtual QRectF boundingRect() const
returns the bounding rect
Definition: QtGraphicsButton.cc:105
QtGraphicsButton::setCheckable
void setCheckable(bool _value)
makes the button checkable
Definition: QtGraphicsButton.cc:125
QtGraphicsButton::width_
int width_
button size
Definition: QtGraphicsButton.hh:126
QtGraphicsButton::pressed
void pressed()
signals a button press
QtGraphicsButton::paint
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
paints the button
Definition: QtGraphicsButton.cc:112
QtGraphicsButton::checkable_
bool checkable_
button state
Definition: QtGraphicsButton.hh:120