GraphicsViewZoom.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <QGraphicsObject>
27#include <QGraphicsView>
28#include <QObject>
29#include <QTimeLine>
30
31namespace armarx
32{
33
34
35 /*!
36 * This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
37 * remains motionless while it's possible.
38 *
39 * Note that it becomes not possible when the scene's
40 * size is not large enough comparing to the viewport size. QGraphicsView centers the picture
41 * when it's smaller than the view. And QGraphicsView's scrolls boundaries don't allow to
42 * put any picture point at any viewport position.
43 *
44 * When the user starts scrolling, this class remembers original scene position and
45 * keeps it until scrolling is completed. It's better than getting original scene position at
46 * each scrolling step because that approach leads to position errors due to before-mentioned
47 * positioning restrictions.
48 *
49 * When zommed using scroll, this class emits zoomed() signal.
50 *
51 * Usage:
52 *
53 * new Graphics_view_zoom(view);
54 *
55 * The object will be deleted automatically when the view is deleted.
56 *
57 * You can set keyboard modifiers used for zooming using set_modified(). Zooming will be
58 * performed only on exact match of modifiers combination. The default modifier is Ctrl.
59 *
60 * You can change zoom velocity by calling set_zoom_factor_base().
61 * Zoom coefficient is calculated as zoom_factor_base^angle_delta
62 * (see QWheelEvent::angleDelta).
63 * The default zoom factor base is 1.0015.
64 */
65 class Graphics_view_zoom : public QObject
66 {
67 Q_OBJECT
68 public:
69 Graphics_view_zoom(QGraphicsView* view);
70 void gentle_zoom(double factor);
71 void set_modifiers(Qt::KeyboardModifiers modifiers);
72 void set_zoom_factor_base(double value);
73
74 QGraphicsView* getView() const;
75
76 private:
77 QGraphicsView* _view;
78 Qt::KeyboardModifiers _modifiers;
79 double _zoom_factor_base;
80 QPointF target_scene_pos, target_viewport_pos;
81 bool eventFilter(QObject* object, QEvent* event) override;
82
83 signals:
84 void zoomed();
85 void moved();
86 };
87
88 class ItemZoomer : public QGraphicsObject
89 {
90 Q_OBJECT
91 public:
92 explicit ItemZoomer(QGraphicsView* view,
93 const QGraphicsItem* item,
94 int time,
95 QGraphicsItem* parent = 0);
96 ~ItemZoomer() override;
97 private slots:
98 void setHeight(int height);
99 void setWidth(int width);
100 void setTimeLineBRFinished();
101 void setTimeLineLTFinished();
102 void deleteSelf();
103 signals:
104 void zoomed();
105
106 private:
107 QGraphicsView* mView;
108 const QGraphicsItem* mItem;
109 QTimeLine* lt;
110 QTimeLine* br;
111 bool brFinished, ltFinished;
112 QPointF curLT, curBR;
113 QPointF ltStepSize;
114 QPointF brStepSize;
115 QRectF start;
116 void update(void);
117
118 // QGraphicsItem interface
119 public:
120 QRectF boundingRect() const override;
121 void
122 paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
123 };
124
125
126} // namespace armarx
#define option(type, fn)
void set_zoom_factor_base(double value)
Graphics_view_zoom(QGraphicsView *view)
void set_modifiers(Qt::KeyboardModifiers modifiers)
void gentle_zoom(double factor)
QGraphicsView * getView() const
ItemZoomer(QGraphicsView *view, const QGraphicsItem *item, int time, QGraphicsItem *parent=0)
QRectF boundingRect() const override
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
This file offers overloads of toIce() and fromIce() functions for STL container types.