-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimageview.cpp
149 lines (129 loc) · 4.42 KB
/
imageview.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
/** @file imageview.cpp
* SOubor s tridou tridy ImageView dedici ze tridy QWidget
* pro zobrazeni noveho okna s fotografii v puvodni velikosti
*/
#include "imageview.h"
#include <QDebug>
#include <QScroller>
ImageView::ImageView(QWidget* parent)
: QWidget(parent)
{
imageL = new QLabel;
scrollArea = new ScrollArea;
this->setLayout(new QHBoxLayout);
connect(scrollArea, SIGNAL(changeSize(int)), this, SLOT(changeImageSize(int)));
QScroller::grabGesture(scrollArea, QScroller::LeftMouseButtonGesture);
this->layout()->setContentsMargins(0, 0, 0, 0);
this->layout()->addWidget(scrollArea);
this->setWindowModality(Qt::ApplicationModal);
this->adjustSize();
}
ImageView::~ImageView()
{
delete img;
}
QSize ImageView::sizeHint() const
{
return QSize(1024, 768);
}
void ImageView::setImage(QString pictureName)
{
this->setWindowTitle(pictureName);
img = new QImage(pictureName);
if (img->isNull()) {
// img je null
return;
}
std::unique_ptr<Exiv2::Image> image;
bool isExif = false;
try {
image = Exiv2::ImageFactory::open(std::string(pictureName.toUtf8()));
isExif = true;
} catch (Exiv2::Error& e) {
qDebug() << "Caught Exiv2 exception 2 '" << e.what() << "'";
isExif = false;
}
if (isExif) {
image.get();
image->readMetadata();
QTransform rot;
Exiv2::ExifData& exifData = image->exifData();
if (!exifData.empty()) {
Exiv2::ExifKey key("Exif.Image.Orientation");
Exiv2::ExifData::iterator pos = exifData.findKey(key);
if (pos != exifData.end()) {
// qDebug() << exifData["Exif.Image.Orientation"].toString().data() << "iiii";
QString str = exifData["Exif.Image.Orientation"].toString().data();
switch (str.toInt()) {
case 3: // obraz otoceny o 180stupnu
*img = img->transformed(rot.rotate(180));
break;
case 6: // obraz otoceny o 90stupnu
*img = img->transformed(rot.rotate(90));
break;
case 8: // obraz otoceny o 280stupnu
*img = img->transformed(rot.rotate(280));
break;
default:
break;
}
}
}
}
this->setContentsMargins(QMargins(0, 0, 0, 0));
scrollArea->setContentsMargins(QMargins(0, 0, 0, 0));
currentSize = QSize(this->size().width() - 2, this->size().height() - 2);
imageL->setPixmap(QPixmap::fromImage((img->scaled(currentSize, Qt::KeepAspectRatio))));
scrollArea->setWidget(imageL);
scrollArea->resize(this->size());
resize(scrollArea->widget()->width() + 2, scrollArea->widget()->height() + 2);
imageL->setAlignment(Qt::AlignCenter);
}
void ImageView::resizeEvent(QResizeEvent* event)
{
event->accept();
currentSize = QSize(this->size().width() - 2, this->size().height() - 2);
imageL->setPixmap(QPixmap::fromImage((img->scaled(currentSize, Qt::KeepAspectRatio))));
scrollArea->resize(this->size());
// scrollArea->checkScrollBars();
}
void ImageView::wheelEvent(QWheelEvent* event)
{
if (event->modifiers() & Qt::ControlModifier) {
QSize s;
if (event->angleDelta().y() < 0) {
qDebug() << imageL->width() << imageL->height();
if (imageL->width() > 30 && imageL->height() > 30) {
s = QSize(imageL->width() * 0.90, imageL->height() * 0.90);
} else {
return;
}
} else {
s = QSize(imageL->width() * 1.1, imageL->height() * 1.1);
}
qDebug() << s;
imageL->setPixmap(QPixmap::fromImage((img->scaled(s, Qt::KeepAspectRatio))));
imageL->resize(s);
event->accept();
}
}
void ImageView::changeImageSize(int delta)
{
if (delta < 0) {
if (currentSize.width() > 60 && currentSize.height() > 60) {
currentSize = QSize(currentSize.width() * 0.90, currentSize.height() * 0.90);
} else {
return;
}
} else {
currentSize = QSize(currentSize.width() * 1.1, currentSize.height() * 1.1);
}
imageL->setPixmap(QPixmap::fromImage((img->scaled(currentSize, Qt::KeepAspectRatio))));
scrollArea->setWidget(imageL);
}
void ImageView::keyPressEvent(QKeyEvent* ev)
{
if (ev->key() == Qt::Key_Escape) {
this->close();
}
}