-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagewindow.cpp
185 lines (156 loc) · 5.65 KB
/
imagewindow.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
#include <queue>
#include "imagewindow.h"
ImageWindow::ImageWindow(QWidget *parent) : QMainWindow(parent) {
scene = new ImageScene;
view = new QGraphicsView(scene);
view->setRenderHint(QPainter::Antialiasing);
setCentralWidget(view);
view->setMouseTracking(true);
setStatusBar(new QStatusBar);
zoomSlider = new QSlider(Qt::Horizontal, statusBar());
zoomSlider->setFixedWidth(200);
zoomSlider->setFocusPolicy(Qt::NoFocus);
zoomSlider->setRange(1, 200);
zoomSlider->setTickPosition(QSlider::TicksBothSides);
zoomSlider->setTickInterval(80);
statusBar()->addWidget(zoomSlider);
zoomScaleLabel = new QLabel;
statusBar()->addWidget(zoomScaleLabel);
connect(zoomSlider, &QSlider::sliderMoved, [&](int value) {
double actualScale = value <= 100
? value / 100.0
: (value - 100) / 25.0 + 1;
this->setSlider(actualScale);
view->resetMatrix();
view->scale(scale, scale);
});
setSlider(1.0);
}
ImageWindow::~ImageWindow() {
delete view;
delete scene;
delete zoomSlider;
delete zoomScaleLabel;
}
void ImageWindow::setSlider(double scale) {
scale = utils::clamp(scale, 0.01, 5.0);
this->scale = scale;
auto percentage = static_cast<int>(scale * 100);
zoomScaleLabel->setText(QStringLiteral("%1%").arg(percentage));
int sliderValue = percentage <= 100 ? percentage : 100 + (percentage - 100) / 4;
if (zoomSlider->value() != sliderValue)
zoomSlider->setValue(sliderValue);
if (view->matrix().m11() != scale) {
view->resetMatrix();
view->scale(scale, scale);
}
}
bool ImageWindow::loadFile(const QString &filePath) {
QImageReader reader(filePath);
reader.setAutoTransform(true);
const QImage image = reader.read();
if (image.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1: %2").arg(QDir::toNativeSeparators(filePath), reader.errorString()));
return false;
}
originalImage = QPixmap::fromImage(image);
scene->setPixmap(originalImage);
imageSize = image.size();
setWindowFilePath(QFileInfo(filePath).canonicalFilePath());
return true;
}
bool ImageWindow::event(QEvent *event) {
if (event->type() == QEvent::MouseButtonPress) {
/*
if (dynamic_cast<QMouseEvent *>(event)->button() == Qt::RightButton) {
auto *image = getSelectedImage();
scene->addItem(new QGraphicsPixmapItem(*image));
}
*/
}
// if (event->type() == QEvent::Gesture)
// return gestureEvent(dynamic_cast<QGestureEvent *>(event));
if (event->type() == QEvent::NativeGesture)
return nativeGestureEvent(dynamic_cast<QNativeGestureEvent *>(event));
inGesture = false;
return QMainWindow::event(event);
}
bool ImageWindow::gestureEvent(QGestureEvent *event) {
if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
auto *pinch = dynamic_cast<QPinchGesture *>(gesture);
qDebug() << pinch->centerPoint() << pinch->hotSpot() << pinch->scaleFactor();
} else {
inGesture = false;
}
return true;
}
bool ImageWindow::nativeGestureEvent(QNativeGestureEvent *event) {
if (event->gestureType() == Qt::ZoomNativeGesture) {
if (!inGesture) {
inGesture = true;
scaleBeforeGesture = scale;
cumulativeScale = 0.0;
}
cumulativeScale += event->value();
auto pos = event->pos();
auto targetScenePos = view->mapToScene(pos);
double newScale = scaleBeforeGesture * (1.0 + cumulativeScale);
setSlider(newScale);
if (scale == newScale) { // scale is not clamped
view->resetTransform();
view->scale(scale, scale);
view->centerOn(targetScenePos);
auto deltaViewportPos = pos - QPointF(view->viewport()->width() / 2.0, view->viewport()->height() / 2.0);
auto viewportCenter = view->mapFromScene(targetScenePos) - deltaViewportPos;
view->centerOn(view->mapToScene(viewportCenter.toPoint())); // still buggy, but fuck it
}
} else {
inGesture = false;
}
return true;
}
const QString ImageWindow::currentFile() const {
return windowFilePath();
}
const QString ImageWindow::currentFileName() const {
return QFileInfo(windowFilePath()).fileName();
}
void ImageWindow::showWithSizeHint(QSize parentSize) {
if (imageSize.height() > parentSize.height() || imageSize.width() > parentSize.width()) {
view->fitInView(QRect(QPoint(0, 0), imageSize), Qt::KeepAspectRatio);
setSlider(view->matrix().m11());
showMaximized();
} else {
resize(imageSize);
show();
}
}
const bool ImageWindow::hasSelection() const {
return scene->getSelection() != nullptr;
}
const bool ImageWindow::hasPastedPixmaps() const {
return scene->getPastedPixmaps().length() > 0;
}
QPixmap ImageWindow::getSelectedImage() {
return scene->getSelectedImage();
}
void ImageWindow::pastePixmap(const QPixmap &pixmap) {
scene->pastePixmap(pixmap);
}
void ImageWindow::poissonFusion() {
scene->poissonFusion();
}
void ImageWindow::smartFill() {
scene->smartFill();
}
bool ImageWindow::saveFile() {
QImageWriter writer(windowFilePath());
if (!writer.write(scene->getImage())) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot write %1: %2").arg(QDir::toNativeSeparators(windowFilePath())),
writer.errorString());
return false;
}
return true;
}