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 
31 namespace 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
armarx::Graphics_view_zoom::set_modifiers
void set_modifiers(Qt::KeyboardModifiers modifiers)
Definition: GraphicsViewZoom.cpp:64
armarx::ItemZoomer::boundingRect
QRectF boundingRect() const override
Definition: GraphicsViewZoom.cpp:225
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:855
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:65
option
#define option(type, fn)
armarx::Graphics_view_zoom::getView
QGraphicsView * getView() const
Definition: GraphicsViewZoom.cpp:76
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:70
armarx::ItemZoomer
Definition: GraphicsViewZoom.h:88
armarx::ItemZoomer::ItemZoomer
ItemZoomer(QGraphicsView *view, const QGraphicsItem *item, int time, QGraphicsItem *parent=0)
Definition: GraphicsViewZoom.cpp:140
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::ItemZoomer::~ItemZoomer
~ItemZoomer() override
Definition: GraphicsViewZoom.cpp:172
armarx::ItemZoomer::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: GraphicsViewZoom.cpp:231