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 <QObject>
27 #include <QGraphicsView>
28 #include <QTimeLine>
29 #include <QGraphicsObject>
30 namespace armarx
31 {
32 
33 
34  /*!
35  * This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
36  * remains motionless while it's possible.
37  *
38  * Note that it becomes not possible when the scene's
39  * size is not large enough comparing to the viewport size. QGraphicsView centers the picture
40  * when it's smaller than the view. And QGraphicsView's scrolls boundaries don't allow to
41  * put any picture point at any viewport position.
42  *
43  * When the user starts scrolling, this class remembers original scene position and
44  * keeps it until scrolling is completed. It's better than getting original scene position at
45  * each scrolling step because that approach leads to position errors due to before-mentioned
46  * positioning restrictions.
47  *
48  * When zommed using scroll, this class emits zoomed() signal.
49  *
50  * Usage:
51  *
52  * new Graphics_view_zoom(view);
53  *
54  * The object will be deleted automatically when the view is deleted.
55  *
56  * You can set keyboard modifiers used for zooming using set_modified(). Zooming will be
57  * performed only on exact match of modifiers combination. The default modifier is Ctrl.
58  *
59  * You can change zoom velocity by calling set_zoom_factor_base().
60  * Zoom coefficient is calculated as zoom_factor_base^angle_delta
61  * (see QWheelEvent::angleDelta).
62  * The default zoom factor base is 1.0015.
63  */
64  class Graphics_view_zoom : public QObject
65  {
66  Q_OBJECT
67  public:
68  Graphics_view_zoom(QGraphicsView* view);
69  void gentle_zoom(double factor);
70  void set_modifiers(Qt::KeyboardModifiers modifiers);
71  void set_zoom_factor_base(double value);
72 
73  QGraphicsView* getView() const;
74  private:
75  QGraphicsView* _view;
76  Qt::KeyboardModifiers _modifiers;
77  double _zoom_factor_base;
78  QPointF target_scene_pos, target_viewport_pos;
79  bool eventFilter(QObject* object, QEvent* event) override;
80 
81  signals:
82  void zoomed();
83  void moved();
84  };
85 
86  class ItemZoomer : public QGraphicsObject
87  {
88  Q_OBJECT
89  public:
90  explicit ItemZoomer(QGraphicsView* view, const QGraphicsItem* item, int time, QGraphicsItem* parent = 0);
91  ~ItemZoomer() override;
92  private slots:
93  void setHeight(int height);
94  void setWidth(int width);
95  void setTimeLineBRFinished();
96  void setTimeLineLTFinished();
97  void deleteSelf();
98  signals:
99  void zoomed();
100  private:
101  QGraphicsView* mView;
102  const QGraphicsItem* mItem;
103  QTimeLine* lt;
104  QTimeLine* br;
105  bool brFinished, ltFinished;
106  QPointF curLT, curBR;
107  QPointF ltStepSize;
108  QPointF brStepSize;
109  QRectF start;
110  void update(void);
111 
112  // QGraphicsItem interface
113  public:
114  QRectF boundingRect() const override;
115  void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override;
116  };
117 
118 
119 } // namespace armarx
120 
armarx::Graphics_view_zoom::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: GraphicsViewZoom.cpp:62
armarx::ItemZoomer::boundingRect
QRectF boundingRect() const override
Definition: GraphicsViewZoom.cpp:214
armarx::Graphics_view_zoom::zoomed
void zoomed()
armarx::Graphics_view_zoom::gentle_zoom
void gentle_zoom(double factor)
Definition: GraphicsViewZoom.cpp:51
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::ItemZoomer::zoomed
void zoomed()
armarx::Graphics_view_zoom::Graphics_view_zoom
Graphics_view_zoom(QGraphicsView *view)
Definition: GraphicsViewZoom.cpp:42
armarx::Graphics_view_zoom
Definition: GraphicsViewZoom.h:64
option
#define option(type, fn)
armarx::Graphics_view_zoom::getView
QGraphicsView * getView() const
Definition: GraphicsViewZoom.cpp:73
armarx::Graphics_view_zoom::moved
void moved()
armarx::Graphics_view_zoom::set_zoom_factor_base
void set_zoom_factor_base(double value)
Definition: GraphicsViewZoom.cpp:68
armarx::ItemZoomer
Definition: GraphicsViewZoom.h:86
armarx::ItemZoomer::ItemZoomer
ItemZoomer(QGraphicsView *view, const QGraphicsItem *item, int time, QGraphicsItem *parent=0)
Definition: GraphicsViewZoom.cpp:137
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ItemZoomer::~ItemZoomer
~ItemZoomer() override
Definition: GraphicsViewZoom.cpp:167
armarx::ItemZoomer::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: GraphicsViewZoom.cpp:219