-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphicsview.cpp
322 lines (275 loc) · 8.75 KB
/
graphicsview.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
#include "graphicsview.h"
#include "graphicsscene.h"
#include <QMouseEvent>
#include <QDebug>
#include <QScrollBar>
#include <QMimeData>
#include <QImageReader>
GraphicsView::GraphicsView(QWidget *parent)
: QGraphicsView (parent)
{
// 设置拖拽为手形拖拽
setDragMode(QGraphicsView::ScrollHandDrag);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// 鼠标下方点作为改变视图大小的锚定点
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setStyleSheet("background-color: rgba(0, 0, 0, 220);"
"border-radius: 3px;");
setAcceptDrops(true);
setCheckerboardEnabled(false);
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &GraphicsView::viewportRectChanged);
}
void GraphicsView::showFileFromUrl(const QUrl &url, bool doRequestGallery)
{
emit navigatorViewRequired(false, 0);
QString filePath(url.toLocalFile());
if (filePath.endsWith(".svg")) {
showSvg(filePath);
} else if (filePath.endsWith(".gif")) {
showGif(filePath);
} else {
QImageReader imageReader(filePath);
imageReader.setAutoTransform(true);
imageReader.setDecideFormatFromContent(true);
if (imageReader.format().isEmpty()) {
showText(tr("File not is a valid image"));
} else {
showImage(QPixmap::fromImageReader(&imageReader));
}
}
if (doRequestGallery) {
emit requestGallery(filePath);
}
}
void GraphicsView::showImage(const QPixmap &pixmap)
{
resetTransform();
scene()->showImage(pixmap);
checkAndDoFitInView();
}
void GraphicsView::showImage(const QImage &image)
{
resetTransform();
scene()->showImage(QPixmap::fromImage(image));
checkAndDoFitInView();
}
void GraphicsView::showText(const QString &text)
{
resetTransform();
scene()->showText(text);
checkAndDoFitInView();
}
void GraphicsView::showSvg(const QString &filepath)
{
resetTransform();
scene()->showSvg(filepath);
checkAndDoFitInView();
}
void GraphicsView::showGif(const QString &filepath)
{
resetTransform();
scene()->showGif(filepath);
checkAndDoFitInView();
}
GraphicsScene *GraphicsView::scene() const
{
return qobject_cast<GraphicsScene*>(QGraphicsView::scene());
}
void GraphicsView::setScene(GraphicsScene *scene)
{
return QGraphicsView::setScene(scene);
}
qreal GraphicsView::scaleFactor() const
{
int angle = static_cast<int>(m_rotateAngle);
if (angle == 0 || angle == 180) {
return qAbs(transform().m11());
} else {
return qAbs(transform().m12());
}
}
void GraphicsView::resetTransform()
{
m_rotateAngle = 0;
QGraphicsView::resetTransform();
}
void GraphicsView::zoomView(qreal scaleFactor)
{
m_enableFitInView = false;
scale(scaleFactor, scaleFactor);
applyTransformationModeByScaleFactor();
emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), m_rotateAngle);
}
void GraphicsView::resetScale()
{
resetWithScaleAndRotate(1, m_rotateAngle);
emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), m_rotateAngle);
}
void GraphicsView::rotateView(qreal rotateAngle)
{
m_rotateAngle += rotateAngle;
m_rotateAngle = static_cast<int>(m_rotateAngle) % 360;
resetWithScaleAndRotate(1, m_rotateAngle);
}
void GraphicsView::fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRadioMode)
{
QGraphicsView::fitInView(rect, aspectRadioMode);
applyTransformationModeByScaleFactor();
}
void GraphicsView::checkAndDoFitInView(bool markItOnAnyway)
{
if (!isThingSmallerThanWindowWith(transform())) {
m_enableFitInView = true;
// 保证图像适应窗口
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
if (markItOnAnyway) {
m_enableFitInView = true;
}
}
void GraphicsView::toggleCheckerboard()
{
setCheckerboardEnabled(!m_checkerboardEnabled);
}
void GraphicsView::mousePressEvent(QMouseEvent *event)
{
if (shouldIgnoreMousePressMoveEvent(event)) {
event->ignore();
// 在这里返回,否则如果我们设置一个 QGraphicsView::ScrollHandDrag 拖动模式,QMouseEvent 事件透明度将不起作用。
return;
}
return QGraphicsView::mousePressEvent(event);
}
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (shouldIgnoreMousePressMoveEvent(event)) {
event->ignore();
}
return QGraphicsView::mouseMoveEvent(event);
}
void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
QGraphicsItem *item = itemAt(event->pos());
if (!item) {
event->ignore();
}
return QGraphicsView::mouseReleaseEvent(event);
}
void GraphicsView::wheelEvent(QWheelEvent *event)
{
event->ignore();
}
void GraphicsView::resizeEvent(QResizeEvent *event)
{
if (m_enableFitInView) {
QTransform tf;
tf.rotate(m_rotateAngle);
if (isThingSmallerThanWindowWith(QTransform(tf)) && scaleFactor() >= 1) {
// 当用户放大窗口时候,什么都不做
} else {
// 缩小窗口时候,仍旧保持图像适应窗口大小
fitInView(sceneRect(), Qt::KeepAspectRatio);
}
} else {
// 是否显示缩略图
emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), m_rotateAngle);
}
return QGraphicsView::resizeEvent(event);
}
void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
qDebug() << event->mimeData() << "Drag Enter Event"
<< event->mimeData()->hasUrls() << event->mimeData()->hasImage()
<< event->mimeData()->formats() << event->mimeData()->hasFormat("text/uri-list");
return QGraphicsView::dragEnterEvent(event);
}
void GraphicsView::dragMoveEvent(QDragMoveEvent *event)
{
// 默认情况下,如果光标下没有 QGraphicsItem,QGraphicsView/Scene 将忽略该动作。(无法进行拖拽)
// 我们实际上并不关心并且希望保持拖动事件原样,所以这里什么都不做。
Q_UNUSED(event);
}
void GraphicsView::dropEvent(QDropEvent *event)
{
event->acceptProposedAction();
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls()) {
const QList<QUrl> &urls = mimeData->urls();
if (urls.isEmpty()) {
showText(tr("File url list is empty"));
} else {
showFileFromUrl(urls.first(), true);
}
} else if (mimeData->hasImage()) {
qDebug() << Q_FUNC_INFO << "this is image";
QImage img = qvariant_cast<QImage>(mimeData->imageData());
QPixmap pixmap = QPixmap::fromImage(img);
if (pixmap.isNull()) {
showText(tr("Image data is invalid"));
} else {
showImage(pixmap);
}
} else if (mimeData->hasText()) {
showText(mimeData->text());
} else {
showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first()));
}
}
bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const
{
return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize())
== rect().size();
}
bool GraphicsView::shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const
{
// 只是鼠标划过时候
if (event->buttons() == Qt::NoButton) {
return true;
}
if (isThingSmallerThanWindowWith(transform())) {
return true;
}
QGraphicsItem *item = itemAt(event->pos());
if (!item) {
return true;
}
return false;
}
void GraphicsView::setCheckerboardEnabled(bool enabled)
{
m_checkerboardEnabled = enabled;
if (m_checkerboardEnabled) {
QPixmap tilePixmap(0x20, 0x20);
tilePixmap.fill(QColor(30, 30, 30, 170));
QPainter tilePainter(&tilePixmap);
QColor color(45, 45, 45, 170);
tilePainter.fillRect(0, 0, 0x10, 0x10, color);
tilePainter.fillRect(0x10, 0x10, 0x10, 0x10, color);
tilePainter.end();
setBackgroundBrush(tilePixmap);
} else {
setBackgroundBrush(Qt::transparent);
}
}
void GraphicsView::applyTransformationModeByScaleFactor()
{
if (this->scaleFactor() < 1) {
scene()->trySetTransformationMode(Qt::SmoothTransformation);
} else {
scene()->trySetTransformationMode(Qt::FastTransformation);
}
}
void GraphicsView::resetWithScaleAndRotate(qreal scaleFactor, qreal rotateAngle)
{
QGraphicsView::resetTransform();
scale(scaleFactor, scaleFactor);
rotate(rotateAngle);
}