-
Notifications
You must be signed in to change notification settings - Fork 0
/
tab_page.cpp
175 lines (154 loc) · 4.69 KB
/
tab_page.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
#include <QtCore/QDebug>
#include <QMessageBox>
#include <QtConcurrentRun>
#include <QMovie>
#include <QDir>
#include <QScrollBar>
#include "tab_page.h"
const QString TabPage::SAVE_PREFIX(tr("Resized-"));
void print_exif(const Exiv2::ExifData exif)
{
Exiv2::ExifData::const_iterator end = exif.end();
for (Exiv2::ExifData::const_iterator i=exif.begin(); i!=end; ++i) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left
<< i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right
<< std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< (tn ? tn : "Unknown") << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< i->count() << " "
<< std::dec << i->value()
<< "\n";
}
}
TabPage::TabPage(const QString& fullpath, QWidget* parent):
QWidget(parent), displayScaleFactor(1.0), fullpath(fullpath)
{
ui.setupUi(this);
QMovie* loadingMovie = new QMovie(":/image/images/loading.gif");
ui.label->setMovie(loadingMovie);
loadingMovie->start();
ui.label->setBackgroundRole(QPalette::Base);
ui.scrollArea->setBackgroundRole(QPalette::Dark);
connect(&loadWatcher, SIGNAL(finished()), this, SLOT(displayImage()));
connect(this, SIGNAL(loadFinish()), this, SLOT(displayImage()));
}
QString TabPage::getFullpath() const
{
return fullpath;
}
void TabPage::displayScale(double scale)
{
if ((displayScaleFactor > 3.0 && scale > 1.0)
|| (displayScaleFactor < 0.1 && scale < 1.0)) {
return;
}
//ui.scrollArea->setWidgetResizable(false);
Q_ASSERT(ui.label->pixmap() != nullptr);
displayScaleFactor *= scale;
auto scaledSize = displayScaleFactor * ui.label->pixmap()->size();
//ui.scrollArea->resize(scaledSize);
ui.label->resize(scaledSize);
// ensure the alignment of label is correct
ui.label->setMinimumSize(scaledSize);
ui.label->setMaximumSize(scaledSize);
adjustScrollBar(ui.scrollArea->horizontalScrollBar(), scale);
adjustScrollBar(ui.scrollArea->verticalScrollBar(), scale);
qDebug() << "Scaled" << displayScaleFactor;
}
void TabPage::adjustScrollBar(QScrollBar* scrollbar, double scale)
{
scrollbar->setValue(scale * scrollbar->value()
+ ((scale-1) * scrollbar->pageStep()/2));
}
void TabPage::zoomIn()
{
displayScale(1.25);
}
void TabPage::zoomOut()
{
displayScale(0.8);
}
int TabPage::getHeight() const
{
return image.height();
}
int TabPage::getWidth() const
{
return image.width();
}
void TabPage::save(int height, int width, const QString& outdir, int quality)
{
if (image.isNull()) {
doLoadImage(fullpath);
emit loadFinish();
}
if (image.isNull()) {
qDebug() << fullpath << "Not saved";
return;
}
auto scaledImg = image.scaled(width, height,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
auto path = QDir(outdir).absoluteFilePath(
SAVE_PREFIX + QFileInfo(fullpath).baseName()+".jpg");
scaledImg.save(path, 0, quality);
qDebug() << "Save done" << path;
// write exif data
Exiv2::Image::AutoPtr exivimg =
Exiv2::ImageFactory::open(path.toLocal8Bit().constData());
if (exivimg.get() != nullptr) {
exivimg->setExifData(exif_data);
exivimg->writeMetadata();
}
}
bool TabPage::isLoaded(const QString& fullpath) const
{
return !image.isNull() && this->fullpath == fullpath;
}
void TabPage::loadImage(const QString& fullpath)
{
auto future = QtConcurrent::run(this,
&TabPage::doLoadImage, fullpath);
loadWatcher.setFuture(future);
}
void TabPage::loadImage()
{
if (isLoaded(fullpath)) {
return;
}
loadImage(fullpath);
}
void TabPage::doLoadImage(const QString& fullpath)
{
this->fullpath = fullpath;
image = QImage(fullpath);
if (image.isNull()) {
emit loadError(fullpath);
}
qDebug() << "Image loaded:" << fullpath;
// Read exif data
Exiv2::Image::AutoPtr exivimg =
Exiv2::ImageFactory::open(fullpath.toLocal8Bit().constData());
if (exivimg.get() != nullptr) {
exivimg->readMetadata();
exif_data = exivimg->exifData();
print_exif(exif_data);
}
}
void TabPage::displayImage()
{
if (image.isNull()) {
return;
}
ui.label->setPixmap(QPixmap::fromImage(image));
if (image.height() > image.width() && image.height() > 900) {
auto scale = 900.0 / image.height();
displayScale(scale);
} else if (image.width() > 900) {
auto scale = 900.0 / image.width();
displayScale(scale);
}
}