Developer Documentation
zoomButton.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#include <QPixmap>
46#include <QGraphicsItem>
47#include <QGraphicsSceneMouseEvent>
48
50
51#include "zoomButton.hh"
52#include "graphicsScene.hh"
53
54//== NAMESPACES ===============================================================
55namespace VSI {
56
57//=============================================================================
58//
59// CLASS VSI::ZoomButton - IMPLEMENTATION
60//
61//=============================================================================
62
64ZoomButton::ZoomButton (GraphicsScene *_scene, QGraphicsItem *_parent, Type _type) :
65 QGraphicsPixmapItem (_parent),
66 scene_ (_scene),
67 type_ (_type)
68{
69
70 // Load icon depending on type
71 switch (type_)
72 {
73 case In:
74 setPixmap (OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-in.png");
75 break;
76 case Out:
77 setPixmap (OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"zoom-out.png");
78 break;
79 }
80
81 setOpacity (0.4);
82
83 setAcceptHoverEvents (true);
84
85 connect (&timer_, SIGNAL (timeout ()), this, SLOT (timeout ()));
86}
87
88//------------------------------------------------------------------------------
89
92{
93}
94
95//------------------------------------------------------------------------------
96
97// make the widget opaque if the mouse is over it
98void ZoomButton::hoverEnterEvent (QGraphicsSceneHoverEvent * /*_event*/)
99{
100 setOpacity (1.0);
101}
102
103//------------------------------------------------------------------------------
104
105// make the widget transparent if the mouse leaves it
106void ZoomButton::hoverLeaveEvent (QGraphicsSceneHoverEvent * /*_event*/)
107{
108 setOpacity (0.4);
109}
110
111//------------------------------------------------------------------------------
112
113// zoom the scene on mouse press
114void ZoomButton::mousePressEvent (QGraphicsSceneMouseEvent *_event)
115{
116 _event->accept ();
117
118 switch (type_)
119 {
120 case In:
121 scene_->scaleElements (1.25);
122 break;
123 case Out:
124 scene_->scaleElements (0.8);
125 break;
126 }
127
128 QRectF rect = scene_->sceneRect ();
129 pos_ = QPointF (rect.x () + (rect.width () / 2.0), rect.y () + (rect.height () / 2.0));
130
131 // start timer for zooming during the mouse is pressed
132 timer_.start (500);
133}
134
135//------------------------------------------------------------------------------
136
137// stop zooming on release
138void ZoomButton::mouseReleaseEvent (QGraphicsSceneMouseEvent *_event)
139{
140 _event->accept ();
141 timer_.stop ();
142}
143
144//------------------------------------------------------------------------------
145
146// zoom on mouse wheel
147void ZoomButton::wheelEvent (QGraphicsSceneWheelEvent *_event)
148{
149 _event->accept ();
150
151 qreal delta;
152
153 if (_event->delta() > 0)
154 delta = 1.25;
155 else
156 delta = 0.8;
157
158 scene_->scaleElements (delta);
159}
160
161//------------------------------------------------------------------------------
162
163// zoom the scene on timeout
164void ZoomButton::timeout ()
165{
166 switch (type_)
167 {
168 case In:
169 scene_->scaleElements (1.25, pos_);
170 break;
171 case Out:
172 scene_->scaleElements (0.8, pos_);
173 break;
174 }
175}
176
177//------------------------------------------------------------------------------
178
180void ZoomButton::activate (QPointF _pos)
181{
182 pos_ = _pos;
183 setOpacity (1.0);
184 if (!timer_.isActive ())
185 timer_.start (500);
186}
187
188//------------------------------------------------------------------------------
189
192{
193 setOpacity (0.4);
194 timer_.stop ();
195}
196
197//------------------------------------------------------------------------------
198
200void ZoomButton::setGeometry (const QRectF &_rect)
201{
202 QGraphicsPixmapItem::setPos (_rect.topLeft ());
203 QGraphicsLayoutItem::setGeometry (_rect);
204}
205
206//------------------------------------------------------------------------------
207
208// size information for layouting
209QSizeF ZoomButton::sizeHint (Qt::SizeHint _which, const QSizeF &/*_constraint*/) const
210{
211 QSizeF sh;
212 switch (_which) {
213 case Qt::MinimumSize:
214 case Qt::PreferredSize:
215 case Qt::MaximumSize:
216 sh = QSizeF (pixmap ().width (), pixmap ().height ());
217 break;
218 default:
219 break;
220 }
221
222 return sh;
223}
224
225//------------------------------------------------------------------------------
226}
void scaleElements(qreal _delta)
Scale all elements with scaling center in the center of the scene.
~ZoomButton()
Destructor.
Definition: zoomButton.cc:91
void deactivate()
Stop the timer.
Definition: zoomButton.cc:191
void activate(QPointF _pos)
Activates the timer for zoom with center at _pos (will be called if an element is moved above)
Definition: zoomButton.cc:180
ZoomButton(GraphicsScene *_scene, QGraphicsItem *_parent, Type _type)
Constructor.
Definition: zoomButton.cc:64
Type
Zoom type.
Definition: zoomButton.hh:69
virtual void setGeometry(const QRectF &_rect)
Sets the geometry.
Definition: zoomButton.cc:200