Developer Documentation
button.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//== INCLUDES =================================================================
45
46#include <QPolygonF>
47#include <QGraphicsItem>
48#include <QFontMetrics>
49#include <QPen>
50#include <QPainter>
51#include <QGraphicsSceneMouseEvent>
52
53#include "button.hh"
54
55#define BACK_OFFSET 2
56
57//== NAMESPACES ===============================================================
58namespace VSI {
59
60//=============================================================================
61//
62// CLASS VSI::Button - IMPLEMENTATION
63//
64//=============================================================================
65
67Button::Button (QGraphicsItem *_parent) :
68 Text (_parent),
69 glow_ (false)
70{
71 setBackground (true, true);
72 setAcceptHoverEvents (true);
73}
74
75//------------------------------------------------------------------------------
76
78Button::Button (const QString &_text, QGraphicsItem *_parent) :
79 Text (_text, _parent),
80 glow_ (false)
81{
82 setBackground (true, true);
83 setAcceptHoverEvents (true);
84}
85
86//------------------------------------------------------------------------------
87
90{
91}
92
93//------------------------------------------------------------------------------
94
95// glow on mouse enter
96void Button::hoverEnterEvent (QGraphicsSceneHoverEvent * /*_event*/)
97{
98 glow_ = true;
99 update ();
100
101 QPen pen = backgroundPen_;
102 pen.setWidthF (1.0);
103 pen.setColor (QColor (127, 166, 218));
105}
106
107//------------------------------------------------------------------------------
108
109// stop glowing on mouse leave
110void Button::hoverLeaveEvent (QGraphicsSceneHoverEvent * /*_event*/)
111{
112 glow_ = false;
113 update ();
114
115 Text::setBackgroundPen (backgroundPen_);
116}
117
118//------------------------------------------------------------------------------
119
120// emit pressed () on mouse press
121void Button::mousePressEvent (QGraphicsSceneMouseEvent *_event)
122{
123 QBrush brush = backgroundBrush_;
124
125 brush.setColor (brush.color ().lighter (50));
127 emit pressed ();
128 _event->accept ();
129}
130
131//------------------------------------------------------------------------------
132
133// change brush back to normal
134void Button::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
135{
136 Text::setBackgroundBrush (backgroundBrush_);
137 _event->accept ();
138}
139
140//------------------------------------------------------------------------------
141
143void Button::setBackgroundBrush(QBrush _brush)
144{
145 backgroundBrush_ = _brush;
147}
148
149//------------------------------------------------------------------------------
150
153{
154 backgroundPen_ = _pen;
156}
157
158//------------------------------------------------------------------------------
159
161void Button::paint (QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget)
162{
163 if (glow_)
164 {
165 QPainterPath path = shape ();
166 QPen curr = _painter->pen ();
167
168 QPen pen = backgroundPen_;
169 pen.setCapStyle(Qt::RoundCap);
170 pen.setJoinStyle(Qt::RoundJoin);
171
172 pen.setColor (QColor (127, 166, 218, 64));
173 pen.setWidthF (7.0);
174 _painter->setPen (pen);
175 _painter->drawPath (path);
176 pen.setColor (QColor (127, 166, 218, 128));
177 pen.setWidthF (5.0);
178 _painter->setPen (pen);
179 _painter->drawPath (path);
180 pen.setColor (QColor (127, 166, 218, 192));
181 pen.setWidthF (3.0);
182 _painter->setPen (pen);
183 _painter->drawPath (path);
184
185 _painter->setPen (curr);
186 }
187 Text::paint (_painter, _option, _widget);
188}
189
190//------------------------------------------------------------------------------
191
194{
195 QRectF rect = Text::boundingRect ();
196
197 if (rect.width () && rect.height ())
198 rect.adjust(-3, -3, 3, 3);
199
200 return rect;
201}
202
203//------------------------------------------------------------------------------
204
206void Button::setGeometry(const QRectF & _rect)
207{
208 Text::setGeometry (_rect);
209}
210
211//------------------------------------------------------------------------------
212}
QRectF boundingRect() const override
Bounding rectangle.
Definition: button.cc:193
void setGeometry(const QRectF &_rect) override
Sets the geometry.
Definition: button.cc:206
void setBackgroundPen(QPen _pen) override
Sets the background pen.
Definition: button.cc:152
void pressed()
emmited if the button gets pressed
void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0) override
Button glow painting.
Definition: button.cc:161
~Button()
Destructor.
Definition: button.cc:89
Button(QGraphicsItem *_parent=0)
Constructor.
Definition: button.cc:67
void setBackgroundBrush(QBrush _brush) override
Sets the background brush.
Definition: button.cc:143
virtual QRectF boundingRect() const
Bounding rectangle.
Definition: text.cc:342
virtual void paint(QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget=0)
Background painting.
Definition: text.cc:185
virtual void setBackgroundBrush(QBrush _brush)
Sets the background brush.
Definition: text.cc:379
virtual void setGeometry(const QRectF &_rect)
Sets the geometry.
Definition: text.cc:102
virtual void setBackgroundPen(QPen _pen)
Sets the background pen.
Definition: text.cc:388
void setBackground(bool _leftOut, bool _rightOut)
Enables background painting.
Definition: text.cc:300
QPainterPath shape() const
Returns the shape for proper repainting/event handling.
Definition: text.cc:397