1 /*******************************************************************************************************
2  DkBaseViewPort.h
3  Created on:	03.07.2013
4 
5  nomacs is a fast and small image viewer with the capability of synchronizing multiple instances
6 
7  Copyright (C) 2011-2013 Markus Diem <markus@nomacs.org>
8  Copyright (C) 2011-2013 Stefan Fiel <stefan@nomacs.org>
9  Copyright (C) 2011-2013 Florian Kleber <florian@nomacs.org>
10 
11  This file is part of nomacs.
12 
13  nomacs is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  nomacs is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program.  If not, see <http://www.gnu.org/licenses/>.
25 
26  *******************************************************************************************************/
27 
28 #pragma once
29 
30 #pragma warning(push, 0)	// no warnings from includes - begin
31 #include <QGraphicsView>
32 #pragma warning(pop)	// no warnings from includes - end
33 
34 #pragma warning(disable: 4251)	// TODO: remove
35 
36 #include "DkImageStorage.h"
37 #include "DkSettings.h"
38 
39 #ifndef DllCoreExport
40 #ifdef DK_CORE_DLL_EXPORT
41 #define DllCoreExport Q_DECL_EXPORT
42 #elif DK_DLL_IMPORT
43 #define DllCoreExport Q_DECL_IMPORT
44 #else
45 #define DllCoreExport Q_DECL_IMPORT
46 #endif
47 #endif
48 
49 // Qt defines
50 class QGestureEvent;
51 class QShortcut;
52 class QSvgRenderer;
53 class QSettings;
54 
55 namespace nmc {
56 
57 class DllCoreExport DkBaseViewPort : public QGraphicsView {
58 	Q_OBJECT
59 
60 public:
61 
62 	enum swipes{
63 		no_swipe = 0, // dummy for now
64 		next_image,
65 		prev_image,
66 		open_thumbs,
67 		close_thumbs,
68 		open_metadata,
69 		close_metadata,
70 
71 		swipes_end
72 	};
73 
74 	DkBaseViewPort(QWidget *parent = 0);
75 	virtual ~DkBaseViewPort();
76 
77 	void zoomConstraints(double minZoom = 0.01, double maxZoom = 100.0);
78 	virtual void zoom(double factor = 0.5, const QPointF& center = QPointF(-1,-1), bool force = false);
79 	virtual void zoomLeveled(double factor = 0.5, const QPointF& center = QPointF(-1, -1));
80 
81 	void setForceFastRendering(bool fastRendering = true) {
82 		mForceFastRendering = fastRendering;
83 	};
84 
85 	/**
86 	 * Returns the scale factor for 100%.
87 	 * Note this factor is only valid for the current image.
88 	 * @return float
89 	 **/
get100Factor()90 	float get100Factor() {
91 
92 		updateImageMatrix();
93 		return 1.0f/(float)mImgMatrix.m11();
94 	};
95 
setPanControl(QPointF panControl)96 	void setPanControl(QPointF panControl) {
97 		mPanControl = panControl;
98 	};
99 
getWorldMatrix()100 	virtual QTransform getWorldMatrix() {
101 		return mWorldMatrix;
102 	};
getMainGeometry()103 	virtual QRect getMainGeometry() {
104 		return geometry();
105 	};
106 
107 	QImage getCurrentImageRegion();
108 
getImageStorage()109 	virtual DkImageStorage* getImageStorage() {
110 		return &mImgStorage;
111 	};
112 
113 #ifdef WITH_OPENCV
114 	virtual void setImage(cv::Mat newImg);
115 #endif
116 
117 	virtual QImage getImage() const;
118 	virtual QSize getImageSize() const;
119 	virtual QRectF getImageViewRect() const;
120 	virtual bool imageInside() const;
121 
122 signals:
123 	void newImageSignal(QImage* img) const;
124 	void keyReleaseSignal(QKeyEvent* event) const;	// make key presses available
125 	void imageUpdated() const;	// triggers on zoom/pan
126 
127 public slots:
128 	virtual void togglePattern(bool show);
129 	virtual void panLeft();
130 	virtual void panRight();
131 	virtual void panUp();
132 	virtual void panDown();
133 	virtual void moveView(const QPointF&);
134 	virtual void zoomIn();
135 	virtual void zoomOut();
136 	virtual void resetView();
137 	virtual void fullView();
138 	virtual void resizeEvent(QResizeEvent* event) override;
139 	virtual void stopBlockZooming();
140 	virtual void setBackgroundBrush(const QBrush &brush);
141 	void scrollVertically(int val);
142 	void scrollHorizontally(int val);
143 
144 	virtual bool unloadImage(bool fileChange = true);
145 	virtual void setImage(QImage newImg);
146 	void hideCursor();
147 
148 protected:
149 	virtual bool event(QEvent *event) override;
150 	virtual void keyPressEvent(QKeyEvent *event) override;
151 	virtual void keyReleaseEvent(QKeyEvent *event) override;
152 	virtual void mousePressEvent(QMouseEvent *event) override;
153 	virtual void mouseReleaseEvent(QMouseEvent *event) override;
154 	virtual void mouseMoveEvent(QMouseEvent *event) override;
155 	virtual void wheelEvent(QWheelEvent *event) override;
156 	virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
157 	virtual void contextMenuEvent(QContextMenuEvent *event) override;
158 	virtual void paintEvent(QPaintEvent* event) override;
159 
160 	virtual bool gestureEvent(QGestureEvent* event);
161 
162 	QVector<QShortcut*> mShortcuts;		// TODO: add to actionManager
163 
164 	Qt::KeyboardModifier mAltMod;		// it makes sense to switch these modifiers on linux (alt + mouse moves windows there)
165 	Qt::KeyboardModifier mCtrlMod;
166 
167 	DkImageStorage mImgStorage;
168 	QSharedPointer<QMovie> mMovie;
169 	QSharedPointer<QSvgRenderer> mSvg;
170 	QBrush mPattern;
171 
172 	QTransform mImgMatrix;
173 	QTransform mWorldMatrix;
174 	QRectF mImgViewRect;
175 	QRectF mViewportRect;
176 	QRectF mImgRect;
177 	QTimer* mHideCursorTimer;
178 
179 	QPointF mPanControl;	// controls how far we can pan outside an image
180 	QPointF mPosGrab;
181 	double mMinZoom = 0.01;
182 	double mMaxZoom = 100;
183 
184 	// TODO: test if gestures are fully supported in Qt5 then remove this
185 	float mLastZoom;
186 	float mStartZoom;
187 	int mSwipeGesture;
188 
189 	bool mForceFastRendering = false;
190 	bool mBlockZooming = false;
191 	QTimer* mZoomTimer;
192 
193 	// functions
194 	virtual void draw(QPainter & painter, double opacity = 1.0);
195 	virtual void drawPattern(QPainter & painter) const;
196 	virtual void updateImageMatrix();
197 	virtual QTransform getScaledImageMatrix() const;
198 	virtual QTransform getScaledImageMatrix(const QSize& size) const;
199 	virtual void controlImagePosition(float lb = -1, float ub = -1);
200 	virtual void centerImage();
201 	virtual void changeCursor();
202 	void zoomToPoint(double factor, const QPointF& pos, QTransform& matrix) const;
203 
204 };
205 
206 }
207