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