-
Notifications
You must be signed in to change notification settings - Fork 48
/
imageviewer.h
135 lines (109 loc) · 4.16 KB
/
imageviewer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/****************************************************************************
* EZ Viewer
* Copyright (C) 2012 huangezhao. CHINA.
* Contact: huangezhao ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#ifndef IMAGEVIEWER_H
#define IMAGEVIEWER_H
#include <QWidget>
class VelocityTracker;
class ImageViewer : public QWidget
{
Q_OBJECT
public:
ImageViewer(QWidget *parent = 0);
// use repaint()
void loadImage(const QImage &im,
const QString &msg_if_no_image = QString::null);
// use update(), no change member like scale, shift
void updatePixmap(const QImage &image);
bool hasPicture() const { return !image.isNull(); }
bool noPicture() const { return image.isNull(); }
qreal currentScale() const { return scale; }
public slots:
void changeScaleMode(int mode);
void changeAlignMode(int mode);
void changeAntialiasMode(int mode);
// if color is invalid, means disabled custom background color.
void changeBgColor(const QColor &color);
void scrollContent(int deltaX, int deltaY);
void scrollContent(const QPoint &delta) { scrollContent(delta.x(), delta.y()); }
void zoomIn(double factor); // pivot is the center of this widget.
void zoomIn(double factor, const QPoint &pivot);
void rotateLeft() { rotatePixmap(-90); }
void rotateRight() { rotatePixmap(90); }
void mirrorHorizontal() { mirrored(MirrorHorizontal); }
void mirrorVertical() { mirrored(MirrorVertical); }
void copyToClipboard();
protected slots:
void paintEvent(QPaintEvent *e);
void resizeEvent ( QResizeEvent * event );
void mouseMoveEvent ( QMouseEvent * event );
void mousePressEvent ( QMouseEvent * event );
void mouseReleaseEvent ( QMouseEvent * event );
private:
bool scaleLargeThanWidget();
void updateImageArea()
{ update(QRect((topLeft + shift).toPoint(), image.size()*scale)); }
/*! init the value of topLeft and scale, according to the size of image
* no use update(), no impact the value of rotate or mirrorH/mirrorV.
*/
void layoutImage(); // determine the visible rect of the image.
void calcScaleRatio();
/*! updateShift() needs the value of topLeft,
* so the order of these two functions below is important.
*/
void calcTopLeft(); // no use update()
void calcShift(); // no use update()
void changeCursor(Qt::CursorShape shape); // for updateCursor()
void updateCursor();
void updateShift(); // use update()
void rotatePixmap(int degree); // use update()
enum MirrorMode {
MirrorHorizontal = 0,
MirrorVertical
};
void mirrored(MirrorMode mode);
private:
QImage image;
QString errStr; // msg to show if image is null.
int scaleMode;
int alignMode;
int antialiasMode;
QColor bgColor;
QPointF topLeft; //
qreal scale;
qreal scaleMin; //
int rotate;
bool mirrorH;
bool mirrorV;
QPointF shift; //
bool hasUserZoom;
bool leftMousePressed; // for updateCursor()
VelocityTracker *velocityTracker;
};
inline void ImageViewer::calcTopLeft()
{
topLeft.setX((rect().width() - image.width() * scale) / qreal(2));
topLeft.setY((rect().height() - image.height() * scale ) / qreal(2));
}
inline void ImageViewer::changeCursor(Qt::CursorShape shape)
{
if (cursor().shape() != shape)
setCursor(shape);
}
#endif