forked from yuezhao/ezviewer
/
imageviewer.cpp
390 lines (327 loc) · 11 KB
/
imageviewer.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/****************************************************************************
* EZ Viewer
* Copyright (C) 2012 huangezhao. CHINA.
* Contact: huangezhao (huangezhao@gmail.com)
*
* 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
***************************************************************************/
#include <QtGui>
#include "config.h"
#include "imageviewer.h"
#include "tooltip.h"
//! paste()
// const QMimeData *mimeData = QApplication::clipboard()->mimeData();
// if (mimeData->hasImage())
// pixmap = qvariant_cast<QPixmap>(mimeData->imageData());
// update();
const int threshold = 10;
static QPoint deaccelerate(const QPoint &speed, int a = 1, int max = 64)
{
int x = qBound(-max, speed.x(), max);
int y = qBound(-max, speed.y(), max);
x = (x == 0) ? x : (x > 0) ? qMax(0, x - a) : qMin(0, x + a);
y = (y == 0) ? y : (y > 0) ? qMax(0, y - a) : qMin(0, y + a);
return QPoint(x, y);
}
ImageViewer::ImageViewer(QWidget *parent)
: QWidget(parent)
{
antialiasMode = 0;
justPressed = false;
timeStamp = QTime::currentTime();
connect(&timer, SIGNAL(timeout()), SLOT(myTimerEvent()));
setMinimumSize(Config::WindowMinSize);
}
void ImageViewer::changeAntialiasMode(int mode)
{
if(mode < 0 || mode > 2 || antialiasMode == mode) return;
antialiasMode = mode;
updateImageArea();
}
// If color is invalid, means no background color enabled.
void ImageViewer::changeBgColor(const QColor &color)
{
if(bgColor != color){
bgColor = color;
update();
}
}
void ImageViewer::updatePixmap(const QImage &im)
{
image = im;
if(rotate != 0)
image = image.transformed(QMatrix().rotate(rotate),
Qt::SmoothTransformation);
if(mirrorH || mirrorV)
image = image.mirrored(mirrorH, mirrorV);
updateImageArea();
}
void ImageViewer::loadImage(const QImage &im, const QString &msg_if_no_image)
{
image = im;
errStr = msg_if_no_image;
rotate = 0;
mirrorH = false;
mirrorV = false;
hasUserZoom = false;
initToFitWidget();
repaint(); ///
}
void ImageViewer::updateShift()
{
QRectF widgetRect(rect()); //! + SIZE_SHIFT ???
QRectF pixRect(topLeft + shift, image.size()*scale);
if(pixRect.width() <= widgetRect.width()
&& pixRect.height() <= widgetRect.height()){
shift = Config::OriginPoint;
setCursor(QCursor(Qt::ArrowCursor));
}else{
if(pixRect.width() <= widgetRect.width())
shift.setX(0);
else if(pixRect.left() > 0 && pixRect.right() > widgetRect.width())
shift.setX(shift.x() + (0 - pixRect.left()));
else if(pixRect.left() < 0 && pixRect.right() < widgetRect.width())
shift.setX(shift.x() + (widgetRect.width() - pixRect.right()));
if(pixRect.height() <= widgetRect.height())
shift.setY(0);
else if(pixRect.top() > 0 && pixRect.bottom() > widgetRect.height())
shift.setY(shift.y() + (0 - pixRect.top()));
else if(pixRect.top() < 0 && pixRect.bottom() < widgetRect.height())
shift.setY(shift.y() + (widgetRect.height() - pixRect.bottom()));
if(cursor().shape() != Qt::ClosedHandCursor)//in mouse move event
setCursor(QCursor(Qt::OpenHandCursor));
}
update();
}
void ImageViewer::initToFitWidget()//no change the value of rotate
{
//restore the cursor even if the image cannot load.
setCursor(QCursor(Qt::ArrowCursor));
if(noPicture()) return;
QSize pixSize(image.size());
//if image large than widget, will scale image to fit widget.
if(!(rect().size() - pixSize).isValid())//! SIZE_ADJUST !!!
pixSize.scale(rect().size() + Config::SizeAdjusted, Qt::KeepAspectRatio);
if(image.width() == 0)
scale = 1.0;
else
scale = qreal(pixSize.width()) / image.width();
scaleMin = qMin(Config::ScaleMin, scale);
updateTopLeft();
shift = Config::OriginPoint;
}
void ImageViewer::zoomIn(double factor)
{
if(noPicture()) return;
qreal scale_old = scale;
scale += factor;
scale = qMax(scaleMin, qMin(Config::ScaleMax, scale));
if(scale == scale_old)//scale no changed
return;
/*! topLeft must determined before shift,
* otherwise will impact updateShift()
*/
updateTopLeft();
shift /= scale_old;
shift *= scale;
updateShift();
hasUserZoom = true;
ToolTip::showText(mapToGlobal(rect().center()),
QString("<font size='7'><b>%1%</b></font>").arg(scale * 100, 0, 'g', 4),
true, 0.7, 800);
}
void ImageViewer::rotatePixmap(bool isLeft)
{
if(noPicture()) return;
rotate += (isLeft ? -90 : 90);
rotate %= 360;
QMatrix m = QMatrix().rotate(isLeft ? -90 : 90);
image = image.transformed(m, Qt::SmoothTransformation);
initToFitWidget();
update();
}
void ImageViewer::mirrored(bool horizontal, bool vertical)
{
if(noPicture()) return;
if(horizontal)
mirrorH = !mirrorH;
if(vertical)
mirrorV = !mirrorV;
image = image.mirrored(horizontal, vertical);
initToFitWidget();
update();
}
void ImageViewer::copyToClipboard()
{
QApplication::clipboard()->setImage(image);
}
void ImageViewer::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
if(bgColor.isValid())
painter.fillRect(e->rect(), bgColor);
if(noPicture() && !errStr.isEmpty()){ // case FileNoPicture:
painter.drawText(rect(), Qt::AlignCenter, errStr);
return;
}
switch(antialiasMode){
case 0:
if(scale <= 1) break;
case 1:
painter.setRenderHint(QPainter::SmoothPixmapTransform);
break;
// case 2:
default:
break;
}
painter.save();
painter.translate(topLeft + shift);
painter.scale(scale, scale);
// painter.setClipRect(e->rect());
painter.drawImage(Config::OriginPoint, image);
painter.restore(); ///
// draw scroll bar
if(/*!justPressed && */ speed != QPoint(0, 0)){
const QColor LINE_COLOR(0, 0, 0, 80);
painter.setBrush(LINE_COLOR);
painter.setPen(Qt::NoPen);
const int LINE_WIDTH = 10;
const int MARGE = 3;
const qreal RADIUS = 6.0;
int rectW = rect().width(), rectH = rect().height();
qreal scaleW = image.width() * scale; //! qreal
qreal scaleH = image.height() * scale;
if(scaleW > rectW){ // draw horizontal scroll bar
QRectF rectangle(-(topLeft + shift).x() / scaleW * rectW,
rectH - LINE_WIDTH - MARGE,
rectW / scaleW * rectW,
LINE_WIDTH);
painter.drawRoundedRect(rectangle, RADIUS, RADIUS);
}
if(scaleH > rectH){ // draw vertical scroll bar
QRectF rectangle(rectW - LINE_WIDTH - MARGE,
-(topLeft + shift).y() / scaleH * rectH,
LINE_WIDTH,
rectH / scaleH * rectH);
painter.drawRoundedRect(rectangle, RADIUS, RADIUS);
}
}
}
void ImageViewer::resizeEvent(QResizeEvent *e)
{
if(noPicture()) return;
if(hasUserZoom){
updateTopLeft();
updateShift();
}else{
initToFitWidget();
}
QWidget::resizeEvent(e);
}
void ImageViewer::wheelEvent(QWheelEvent *e)
{
if(noPicture() || !rect().contains(e->pos())) //cursor is not in widget
return;
qreal scale_old = scale;
switch(e->modifiers()){
case Qt::ShiftModifier:
zoomIn(e->delta() / qreal(2400)); //e->delta() is +120 or -120
break;
case Qt::ControlModifier:
zoomIn(e->delta() / qreal(600));
break;
default:
zoomIn(e->delta() / qreal(1200));
break;
}
QPointF distance(rect().center() - e->pos());
QPointF change(distance / scale_old * scale - distance);
shift += change; //to keep the cursor position, must after scale.
updateShift();
}
void ImageViewer::mousePressEvent ( QMouseEvent * event )
{
if(event->button() & Qt::LeftButton){
startPos = event->globalPos();
if(hasPicture() && cursor().shape() == Qt::OpenHandCursor)
setCursor(QCursor(Qt::ClosedHandCursor));
justPressed = true;
pressPos = event->pos();
if(timer.isActive()){ // auto scroll
speed = QPoint(0, 0);
timer.stop();
}
}
}
void ImageViewer::mouseMoveEvent ( QMouseEvent * event )
{
//! For mouse move events, this is all buttons that are pressed down.
if(event->buttons() & Qt::LeftButton)
myMouseMove(event);
}
void ImageViewer::mouseReleaseEvent ( QMouseEvent * event )
{
if(event->button() & Qt::LeftButton){
myMouseMove(event);
if(!justPressed && speed != QPoint(0, 0)) {
speed /= 4; //! ??
timer.start(Config::AutoScrollInterval);
}
if(cursor().shape() == Qt::ClosedHandCursor)
setCursor(QCursor(Qt::OpenHandCursor));
}
}
void ImageViewer::myMouseMove(QMouseEvent * event)
{
QPoint change = event->globalPos() - startPos;
if(event->modifiers() == Qt::ControlModifier){
emit siteChange(change);
}else{
//if widget smaller than widget, allow to move image.
//! + SIZE_ADJUST
if(hasPicture() && !(rect().size() + Config::SizeAdjusted - image.size()*scale).isValid()){
shift += change;
updateShift();
QPoint delta = event->pos() - pressPos;
if(justPressed){
if (delta.x() > threshold || delta.x() < -threshold ||
delta.y() > threshold || delta.y() < -threshold) {
timeStamp = QTime::currentTime(); //start calculate
justPressed = false; ///
this->delta = QPoint(0, 0);
pressPos = event->pos();
}
}else{
if (timeStamp.elapsed() > 100) {
timeStamp = QTime::currentTime(); //restart calculate
speed = delta - this->delta; ///
this->delta = delta;
}
}
}
}
startPos = event->globalPos(); //
}
void ImageViewer::myTimerEvent()
{
speed = deaccelerate(speed);
QPointF shiftOld = shift;
shift += speed;
updateShift();
if(shift == shiftOld)
speed = QPoint(0, 0);
if (speed == QPoint(0, 0))
timer.stop();
}