-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimgviewer.cpp
213 lines (179 loc) · 5.18 KB
/
imgviewer.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
#include "imgviewer.h"
#include <QDebug>
#include <QMessageBox>
#include <QPrintDialog>
#include <QFileDialog>
#include <QWheelEvent>
#include <QtMath>
#include <QMatrix>
ImgViewer::ImgViewer(QWidget *parent) :
QGraphicsView(parent), m_rotateAngle(0), m_IsFitWindow(false), m_IsViewInitialized(false)
{
m_scene = new QGraphicsScene(this);
this->setScene(m_scene);
this->setBackgroundBrush(QBrush(QColor(38,38,38,255),Qt::SolidPattern));
this->setDragMode(NoDrag);
}
bool ImgViewer::loadFile(const QString &strFilePath, QString &strError)
{
// clear previous and reset
resetView();
m_image.load(strFilePath);
if(m_image.isNull()){
strError = QObject::tr("Cannot load %1.").arg(strFilePath);
return false;
}
m_fileName = strFilePath;
m_pixmap = QPixmap::fromImage(m_image);
m_pixmapItem = m_scene->addPixmap(m_pixmap); // add pixmap to scene and return pointer to pixmapItem
m_scene->setSceneRect(m_pixmap.rect()); // set scene rect to image
this->centerOn(m_pixmapItem); // ensure item is centered in the view.
// preserve fitWindow if activated
if(m_IsFitWindow)
fitWindow();
else
this->setDragMode(ScrollHandDrag);
m_IsViewInitialized = true;
return true;
}
void ImgViewer::resetView()
{
if(m_image.isNull()){
return;
}
m_scene->clear();
m_image = QImage();
m_fileName.clear();
m_rotateAngle = 0;
this->setDragMode(NoDrag);
this->resetTransform();
}
void ImgViewer::reactToFitWindowToggle(bool checked)
{
m_IsFitWindow = checked;
if(checked)
fitWindow();
else
originalSize();
}
void ImgViewer::fitWindow()
{
if(m_image.isNull())
return;
this->setDragMode(NoDrag);
this->resetTransform();
QPixmap px = m_pixmap; // scale a copy to viewsize (scaling the original results in blurred image)
px = px.scaled(QSize(this->width(),this->height()),Qt::KeepAspectRatio,Qt::SmoothTransformation);
m_pixmapItem->setPixmap(px);
m_scene->setSceneRect(px.rect());
}
void ImgViewer::originalSize()
{
if(m_image.isNull())
return;
this->setDragMode(ScrollHandDrag);
m_pixmap = m_pixmap.scaled(QSize(m_image.width(),m_image.height()),Qt::KeepAspectRatio,Qt::SmoothTransformation);
m_pixmapItem->setPixmap(m_pixmap);
m_scene->setSceneRect(m_pixmap.rect());
this->centerOn(m_pixmapItem);
}
void ImgViewer::rotateView(const int nVal)
{
if(m_image.isNull()){
return;
}
// rotate view
this->rotate(nVal);
//this->show(); // this necessary?
// update angle
m_rotateAngle += nVal; // a=a+(-90)== -90
// reset angle
if(m_rotateAngle >= 360 || m_rotateAngle <= -360){
m_rotateAngle =0;
}
}
void ImgViewer::printView()
{
if(m_image.isNull()){
return;
}
#ifndef QT_NO_PRINTER
if (QPrintDialog(&printer).exec() == QDialog::Accepted) {
QPainter painter(&printer);
painter.setRenderHint(QPainter::Antialiasing);
m_scene->render(&painter);
}
#endif
}
bool ImgViewer::saveViewToDisk(QString &strError)
{
if(m_image.isNull()){
strError = QObject::tr("Save failed.");
return false;
}
// save a copy
QImage imageCopy = m_image;
// Output file dialog
QString fileFormat = getImageFormat(m_fileName);
QString strFilePath = QFileDialog::getSaveFileName(
this,
tr("Save File"),
QDir::homePath(),
fileFormat);
// If Cancel is pressed, getSaveFileName() returns a null string.
if(strFilePath==""){
strError = QObject::tr("");
return false;
}
// ensure output path has proper extension
if(!strFilePath.endsWith(fileFormat))
strFilePath += "."+fileFormat;
// save image in modified state
if(isModified()) {
QTransform t;
t.rotate(m_rotateAngle);
imageCopy = imageCopy.transformed(t, Qt::SmoothTransformation);
}
// quality factor (-1 default, 100 max)
// note: -1 is about 4 times smaller than original, 100 is larger than original
if(!imageCopy.save(strFilePath,fileFormat.toLocal8Bit().constData(),100)){
strError = QObject::tr("Save failed.");
return false;
}
return true;
}
QString ImgViewer::getImageFormat(QString strFileName)
{
QString str = strFileName.mid(strFileName.lastIndexOf(".")+1,-1);
if(str.toLower() == "tif")
str = "tiff";
return str;
}
// preserve fitWindow state on window resize
void ImgViewer::resizeEvent(QResizeEvent *event)
{
if(!m_IsViewInitialized)
return;
if(m_IsFitWindow)
fitWindow();
else {
QGraphicsView::resizeEvent(event); // call base implementation
this->centerOn(m_pixmapItem);
}
}
// scale image on wheelEvent
void ImgViewer::wheelEvent(QWheelEvent *event)
{
// prevent zooming when fitWindow is active
if(m_IsFitWindow)
return;
this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// zoomIn factor
qreal factor = 1.15;
// zoomOut factor
if(event->delta() < 0)
factor = 1.0 / factor;
// scale the View
this->scale(factor,factor);
event->accept();
}