-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageViewer.cpp
More file actions
1162 lines (997 loc) · 34.1 KB
/
Copy pathImageViewer.cpp
File metadata and controls
1162 lines (997 loc) · 34.1 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ImageViewer.cpp
#include "ImageViewer.h"
#include "qnamespace.h"
#include <QApplication>
#include <QCryptographicHash>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QGestureEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QPinchGesture>
#include <QPixmap>
#include <QPushButton>
#include <QScreen>
#include <QScroller>
#include <QThread>
#include <QTimer>
#include <QTransform>
#include <QtConcurrent>
// WebP 支持
#include <webp/decode.h>
#include <webp/demux.h>
ImageViewer::ImageViewer(const QString &path, QWidget *parent)
: QWidget(parent), currentPath(path) {
setWindowTitle(QFileInfo(path).fileName());
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_DeleteOnClose);
// 检查是否是 GIF 文件
isGif = path.toLower().endsWith(".gif");
// 检查是否是 WebP 文件
isWebp = path.toLower().endsWith(".webp");
if (isGif) {
// 初始化 GIF 动画
setupGifAnimation();
} else if (isWebp) {
// 加载 WebP 图片
loadWebpImage();
} else {
// 加载普通图片
originalPixmap = QPixmap(currentPath);
rotatedPixmap = originalPixmap;
}
if (originalPixmap.isNull() && !isGif) {
QTimer::singleShot(1500, this, &ImageViewer::close);
}
// 缩放按钮
zoomInButton = new QPushButton("+", this);
zoomOutButton = new QPushButton("-", this);
zoomInButton->setVisible(false);
zoomOutButton->setVisible(false);
zoomInButton->setStyleSheet(buttonStyle());
zoomOutButton->setStyleSheet(buttonStyle());
zoomInButton->setFixedSize(40, 32);
zoomOutButton->setFixedSize(40, 32);
connect(zoomInButton, &QPushButton::clicked, this, &ImageViewer::onZoomIn);
connect(zoomOutButton, &QPushButton::clicked, this, &ImageViewer::onZoomOut);
connect(&zoomButtonHideTimer, &QTimer::timeout, this,
&ImageViewer::hideZoomButtons);
rotateButton = new QPushButton("↻", this);
rotateButton->setVisible(false);
rotateButton->setStyleSheet(buttonStyle());
rotateButton->setFixedSize(40, 32);
connect(rotateButton, &QPushButton::clicked, this, [this]() {
rotateImage();
updateImageDisplay();
showZoomButtons(); // 重新计时按钮隐藏
});
// 缩略图按钮
thumbnailButton = new QPushButton("▣", this);
thumbnailButton->setVisible(false);
thumbnailButton->setStyleSheet(buttonStyle());
thumbnailButton->setFixedSize(40, 32);
connect(thumbnailButton, &QPushButton::clicked, this, [this]() {
if (thumbnailsVisible) {
hideThumbnailMenu();
} else {
showThumbnailMenu();
}
});
closeButton = new QPushButton("✕", this);
closeButton->setVisible(false);
closeButton->setStyleSheet(buttonStyle());
closeButton->setFixedSize(40, 32);
connect(closeButton, &QPushButton::clicked, this, &ImageViewer::close);
// 初始化缩略图相关
scanImageFiles();
initThumbnailView();
// 启动后台加载缩略图
thumbnailWatcher = new QFutureWatcher<void>(this);
connect(thumbnailWatcher, &QFutureWatcher<void>::finished,
[]() { qDebug() << "Thumbnail loading completed"; });
loadThumbnailsInBackground();
grabGesture(Qt::PinchGesture);
}
ImageViewer::~ImageViewer() {
// 停止 GIF 动画(如果有)
if (gifMovie) {
gifMovie->stop();
delete gifMovie;
}
// 停止 WebP 动画(如果有)
if (webpAnimationTimer) {
webpAnimationTimer->stop();
delete webpAnimationTimer;
}
}
QString ImageViewer::buttonStyle() const {
return R"(
QPushButton {
color: white;
font-size: 22px;
background: rgba(0,0,0,120);
border-radius: 16px;
padding: 4px 12px;
}
QPushButton:hover {
background: rgba(0,0,0,180);
}
)";
}
void ImageViewer::showEvent(QShowEvent *event) {
QWidget::showEvent(event);
showFullScreen();
updateImageDisplay(); // 会自动调整 scaleFactor
}
void ImageViewer::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
updateImageDisplay();
// 调整缩略图区域大小(如果可见)
if (thumbnailsVisible) {
int menuHeight = 120;
thumbnailScrollArea->setGeometry(0, height() - menuHeight, width(),
menuHeight);
}
}
void ImageViewer::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
if (isGif) {
if (currentGifFrame.isNull()) {
painter.fillRect(rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, "无法加载 GIF");
return;
}
// 使用GIF当前帧
QSizeF scaledSize = currentGifFrame.size() * scaleFactor;
QSizeF viewSize(width(), height());
QPointF center(width() / 2.0, height() / 2.0);
QPointF topLeft =
center - QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
QSizeF imgSize = scaledSize;
QPointF minOffset(qMin(0.0, (viewSize.width() - imgSize.width()) / 2.0),
qMin(0.0, (viewSize.height() - imgSize.height()) / 2.0));
QPointF maxOffset(qMax(0.0, (imgSize.width() - viewSize.width()) / 2.0),
qMax(0.0, (imgSize.height() - viewSize.height()) / 2.0));
offset.setX(qBound(minOffset.x(), offset.x(), maxOffset.x()));
offset.setY(qBound(minOffset.y(), offset.y(), maxOffset.y()));
topLeft = center -
QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
painter.fillRect(rect(), Qt::black);
painter.drawPixmap(
QRectF(topLeft, scaledSize), currentGifFrame,
QRectF(0, 0, currentGifFrame.width(), currentGifFrame.height()));
} else if (isWebp && isAnimatedWebp) {
if (currentWebpFramePixmap.isNull()) {
painter.fillRect(rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, "无法加载 WebP 动画");
return;
}
// 使用 WebP 当前帧
QSizeF scaledSize = currentWebpFramePixmap.size() * scaleFactor;
QSizeF viewSize(width(), height());
QPointF center(width() / 2.0, height() / 2.0);
QPointF topLeft =
center - QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
QSizeF imgSize = scaledSize;
QPointF minOffset(qMin(0.0, (viewSize.width() - imgSize.width()) / 2.0),
qMin(0.0, (viewSize.height() - imgSize.height()) / 2.0));
QPointF maxOffset(qMax(0.0, (imgSize.width() - viewSize.width()) / 2.0),
qMax(0.0, (imgSize.height() - viewSize.height()) / 2.0));
offset.setX(qBound(minOffset.x(), offset.x(), maxOffset.x()));
offset.setY(qBound(minOffset.y(), offset.y(), maxOffset.y()));
topLeft = center -
QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
painter.fillRect(rect(), Qt::black);
painter.drawPixmap(QRectF(topLeft, scaledSize), currentWebpFramePixmap,
QRectF(0, 0, currentWebpFramePixmap.width(),
currentWebpFramePixmap.height()));
} else {
if (rotatedPixmap.isNull()) {
painter.fillRect(rect(), Qt::black);
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, "无法加载图片");
return;
}
QSizeF scaledSize = rotatedPixmap.size() * scaleFactor;
QSizeF viewSize(width(), height());
QPointF center(width() / 2.0, height() / 2.0);
QPointF topLeft =
center - QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
QSizeF imgSize = scaledSize;
QPointF minOffset(qMin(0.0, (viewSize.width() - imgSize.width()) / 2.0),
qMin(0.0, (viewSize.height() - imgSize.height()) / 2.0));
QPointF maxOffset(qMax(0.0, (imgSize.width() - viewSize.width()) / 2.0),
qMax(0.0, (imgSize.height() - viewSize.height()) / 2.0));
offset.setX(qBound(minOffset.x(), offset.x(), maxOffset.x()));
offset.setY(qBound(minOffset.y(), offset.y(), maxOffset.y()));
topLeft = center -
QPointF(scaledSize.width() / 2.0, scaledSize.height() / 2.0) +
offset;
painter.fillRect(rect(), Qt::black);
painter.drawPixmap(
QRectF(topLeft, scaledSize), rotatedPixmap,
QRectF(0, 0, rotatedPixmap.width(), rotatedPixmap.height()));
}
int margin = 12;
int totalWidth = zoomInButton->width() + zoomOutButton->width() +
rotateButton->width() + thumbnailButton->width() +
closeButton->width() + 4 * margin;
int baseX = (width() - totalWidth) / 2;
zoomInButton->move(baseX, margin);
zoomOutButton->move(baseX + zoomInButton->width() + margin, margin);
rotateButton->move(baseX + zoomInButton->width() + zoomOutButton->width() +
2 * margin,
margin);
thumbnailButton->move(baseX + zoomInButton->width() + zoomOutButton->width() +
rotateButton->width() + 3 * margin,
margin);
closeButton->move(baseX + zoomInButton->width() + zoomOutButton->width() +
rotateButton->width() + thumbnailButton->width() +
4 * margin,
margin);
}
void ImageViewer::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
lastMousePos = event->pos();
dragging = true;
showZoomButtons();
}
}
void ImageViewer::mouseMoveEvent(QMouseEvent *event) {
if (dragging) {
QPointF delta = event->pos() - lastMousePos;
offset += delta;
lastMousePos = event->pos();
update();
}
}
void ImageViewer::mouseReleaseEvent(QMouseEvent *event) {
Q_UNUSED(event)
if (dragging) {
dragging = false;
showZoomButtons();
}
}
void ImageViewer::mouseDoubleClickEvent(QMouseEvent *event) {
Q_UNUSED(event);
// 双击切换缩略图菜单显示状态
if (thumbnailsVisible) {
hideThumbnailMenu();
} else {
showThumbnailMenu();
}
}
bool ImageViewer::event(QEvent *event) {
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent *>(event));
return QWidget::event(event);
}
bool ImageViewer::gestureEvent(QGestureEvent *event) {
if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
scaleFactor *= pinch->scaleFactor();
scaleFactor = qBound(0.05, scaleFactor, 5.0);
update();
showZoomButtons();
return true;
}
return false;
}
void ImageViewer::onZoomIn() {
scaleFactor *= 1.25;
if (scaleFactor > 5.0)
scaleFactor = 5.0;
update();
showZoomButtons();
}
void ImageViewer::onZoomOut() {
double minScale = 0.2;
if (isGif) {
if (!currentGifFrame.isNull()) {
double scaleW = double(width()) / currentGifFrame.width();
double scaleH = double(height()) / currentGifFrame.height();
minScale = qMin(scaleW, scaleH);
minScale = qMin(minScale, 1.0);
minScale = qMax(minScale, 0.05);
}
} else if (isWebp && isAnimatedWebp) {
if (!currentWebpFramePixmap.isNull()) {
double scaleW = double(width()) / currentWebpFramePixmap.width();
double scaleH = double(height()) / currentWebpFramePixmap.height();
minScale = qMin(scaleW, scaleH);
minScale = qMin(minScale, 1.0);
minScale = qMax(minScale, 0.05);
}
} else if (!rotatedPixmap.isNull()) {
double scaleW = double(width()) / rotatedPixmap.width();
double scaleH = double(height()) / rotatedPixmap.height();
minScale = qMin(scaleW, scaleH);
minScale = qMin(minScale, 1.0);
minScale = qMax(minScale, 0.05);
}
scaleFactor /= 1.25;
if (scaleFactor < minScale)
scaleFactor = minScale;
update();
showZoomButtons();
}
void ImageViewer::showZoomButtons() {
zoomInButton->setVisible(true);
zoomOutButton->setVisible(true);
rotateButton->setVisible(true);
thumbnailButton->setVisible(true);
closeButton->setVisible(true);
zoomButtonHideTimer.start(1800);
}
void ImageViewer::hideZoomButtons() {
zoomInButton->setVisible(false);
zoomOutButton->setVisible(false);
rotateButton->setVisible(false);
thumbnailButton->setVisible(false);
closeButton->setVisible(false);
}
void ImageViewer::updateImageDisplay() {
if (isGif) {
if (!currentGifFrame.isNull()) {
double scaleW = double(width()) / currentGifFrame.width();
double scaleH = double(height()) / currentGifFrame.height();
double fitScale = qMin(scaleW, scaleH);
fitScale = qMin(fitScale, 1.0);
fitScale = qMax(fitScale, 0.05);
scaleFactor = fitScale;
}
} else if (isWebp && isAnimatedWebp) {
if (!currentWebpFramePixmap.isNull()) {
double scaleW = double(width()) / currentWebpFramePixmap.width();
double scaleH = double(height()) / currentWebpFramePixmap.height();
double fitScale = qMin(scaleW, scaleH);
fitScale = qMin(fitScale, 1.0);
fitScale = qMax(fitScale, 0.05);
scaleFactor = fitScale;
}
} else if (!rotatedPixmap.isNull()) {
double scaleW = double(width()) / rotatedPixmap.width();
double scaleH = double(height()) / rotatedPixmap.height();
double fitScale = qMin(scaleW, scaleH);
fitScale = qMin(fitScale, 1.0);
fitScale = qMax(fitScale, 0.05);
scaleFactor = fitScale;
}
update();
}
void ImageViewer::rotateImage() {
rotationAngle = (rotationAngle + 90) % 360;
QTransform trans;
trans.rotate(rotationAngle);
if (isGif) {
// GIF 不支持旋转
return;
} else {
rotatedPixmap = originalPixmap.transformed(trans, Qt::SmoothTransformation);
}
offset = QPointF(0, 0);
}
// 缩略图相关实现
void ImageViewer::scanImageFiles() {
QFileInfo currentFile(currentPath);
QDir dir(currentFile.dir());
// 支持的图片格式
QStringList filters;
filters << "*.jpg" << "*.jpeg" << "*.png" << "*.bmp" << "*.gif" << "*.webp"
<< "*.JPG" << "*.JPEG" << "*.PNG" << "*.BMP" << "*.GIF" << "*.WEBP";
imageFiles = dir.entryInfoList(filters, QDir::Files, QDir::Name);
// 找到当前图片索引
for (int i = 0; i < imageFiles.size(); ++i) {
if (imageFiles[i].absoluteFilePath() == currentFile.absoluteFilePath()) {
currentImageIndex = i;
break;
}
}
}
void ImageViewer::initThumbnailView() {
// 创建滚动区域
thumbnailScrollArea = new QScrollArea(this);
thumbnailScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
thumbnailScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
thumbnailScrollArea->setStyleSheet("background-color: rgba(0,0,0,200);");
// 创建列表控件
thumbnailList = new QListWidget(this);
thumbnailList->setViewMode(QListWidget::IconMode);
thumbnailList->setIconSize(QSize(120, 90));
thumbnailList->setResizeMode(QListWidget::Adjust);
thumbnailList->setGridSize(QSize(130, 100));
thumbnailList->setSpacing(10);
// 优化缩略图列表和滚动条样式,与主窗口风格一致
thumbnailList->setStyleSheet(
"QListWidget { background-color: transparent; border: none; }"
"QListWidget::item { border: 2px solid transparent; border-radius: 4px; }"
"QListWidget::item:selected { border: 2px solid #4CAF50; }"
"QScrollBar:vertical { width: 22px; background: transparent; margin: 3px "
"0 3px 0; border-radius: 4px; }"
"QScrollBar::handle:vertical { background: #666666; min-height: 20px; "
"border-radius: 4px; }"
"QScrollBar::handle:vertical:hover { background: #888888; }"
"QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical { height: "
"0px; }"
"QScrollBar:horizontal { height: 0px; }");
// 设置滚动属性
thumbnailList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
thumbnailList->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
// 启用触摸滚动,与主窗口保持一致
QScroller::grabGesture(thumbnailList->viewport(), QScroller::TouchGesture);
// 确保滚动条可以正常工作
thumbnailScrollArea->setWidget(thumbnailList);
thumbnailScrollArea->setWidgetResizable(true);
// 禁用拖动功能但保持滚动功能
thumbnailList->setDragEnabled(false);
thumbnailList->setMovement(QListWidget::Static);
connect(thumbnailList, &QListWidget::itemClicked, this,
&ImageViewer::onThumbnailClicked);
// 默认隐藏
thumbnailScrollArea->setVisible(false);
}
QPixmap ImageViewer::generateThumbnail(const QString &path) {
// 特殊处理 GIF 文件
if (path.toLower().endsWith(".gif")) {
QMovie movie(path);
if (movie.isValid()) {
// 获取 GIF 的第一帧作为缩略图
QPixmap frame = movie.currentPixmap();
if (!frame.isNull()) {
return frame.scaled(120, 90, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
}
}
// 处理 WebP 文件
if (path.toLower().endsWith(".webp")) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) {
return QPixmap();
}
QByteArray data = file.readAll();
file.close();
if (data.isEmpty()) {
return QPixmap();
}
// 检查是否是动画 WebP
WebPData webp_data;
webp_data.bytes = reinterpret_cast<const uint8_t *>(data.constData());
webp_data.size = data.size();
WebPDemuxer *demux = WebPDemux(&webp_data);
QPixmap result;
if (demux) {
// 动画 WebP,获取第一帧
WebPIterator iter;
if (WebPDemuxGetFrame(demux, 1, &iter)) {
int width, height;
uint8_t *decoded_data = WebPDecodeRGBA(
iter.fragment.bytes, iter.fragment.size, &width, &height);
if (decoded_data) {
QImage image(decoded_data, width, height, QImage::Format_RGBA8888);
result = QPixmap::fromImage(image.copy());
WebPFree(decoded_data);
}
}
WebPDemuxReleaseIterator(&iter);
WebPDemuxDelete(demux);
} else {
// 静态 WebP
int width, height;
uint8_t *decoded_data =
WebPDecodeRGBA(reinterpret_cast<const uint8_t *>(data.constData()),
data.size(), &width, &height);
if (decoded_data) {
QImage image(decoded_data, width, height, QImage::Format_RGBA8888);
result = QPixmap::fromImage(image.copy());
WebPFree(decoded_data);
}
}
if (!result.isNull()) {
return result.scaled(120, 90, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
}
// 处理普通图片
QPixmap pixmap(path);
if (pixmap.isNull())
return QPixmap();
return pixmap.scaled(120, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
void ImageViewer::loadThumbnailsInBackground() {
thumbnailList->clear();
// 获取当前图片目录,并在该目录下创建缩略图缓存文件夹
QFileInfo currentFileInfo(currentPath);
QString currentDirPath = currentFileInfo.absolutePath();
QString cacheDirPath = currentDirPath + "/.thumbnails";
QDir cacheDir(cacheDirPath);
if (!cacheDir.exists()) {
cacheDir.mkpath(".");
}
// 先添加占位项
for (const auto &file : imageFiles) {
auto item = new QListWidgetItem(QIcon(), "");
item->setData(Qt::UserRole, file.absoluteFilePath());
thumbnailList->addItem(item);
// 标记当前图片
if (file.absoluteFilePath() == currentPath) {
thumbnailList->setCurrentItem(item);
}
}
// 后台线程生成缩略图
auto future = QtConcurrent::run([this, cacheDirPath]() {
// 优化性能:批量处理缩略图
QList<QPair<QString, QPixmap>> batchResults;
const int batchSize = 10; // 每批处理 10 个
for (int i = 0; i < imageFiles.size(); ++i) {
const QString path = imageFiles[i].absoluteFilePath();
// 生成缓存文件名(使用文件名 +
// 文件大小作为哈希基础,确保文件修改后缓存失效)
QFileInfo fileInfo(path);
QString hashBase = fileInfo.fileName() + QString::number(fileInfo.size());
QString fileHash = QString(
QCryptographicHash::hash(hashBase.toUtf8(), QCryptographicHash::Md5)
.toHex());
QString cacheFilePath = cacheDirPath + "/" + fileHash + ".png";
QPixmap thumb;
// 检查本地缓存文件是否存在
QFileInfo cacheFileInfo(cacheFilePath);
if (cacheFileInfo.exists()) {
// 从缓存文件加载缩略图
thumb.load(cacheFilePath);
// 添加到内存缓存
if (!thumb.isNull()) {
thumbnailMutex.lock();
int sizeInBytes = thumb.width() * thumb.height() * thumb.depth() / 8;
thumbnailCache.insert(path, new QPixmap(thumb), sizeInBytes);
thumbnailMutex.unlock();
batchResults.append(qMakePair(path, thumb));
// 批量处理
if (batchResults.size() >= batchSize) {
processBatchThumbnails(batchResults);
batchResults.clear();
}
continue;
}
}
// 检查内存缓存
if (thumbnailCache.contains(path)) {
batchResults.append(qMakePair(path, *thumbnailCache[path]));
// 批量处理
if (batchResults.size() >= batchSize) {
processBatchThumbnails(batchResults);
batchResults.clear();
}
continue;
}
// 生成缩略图
thumb = generateThumbnail(path);
if (!thumb.isNull()) {
// 保存到本地缓存
thumb.save(cacheFilePath, "PNG");
// 添加到内存缓存
thumbnailMutex.lock();
int sizeInBytes = thumb.width() * thumb.height() * thumb.depth() / 8;
thumbnailCache.insert(path, new QPixmap(thumb), sizeInBytes);
thumbnailMutex.unlock();
batchResults.append(qMakePair(path, thumb));
// 批量处理
if (batchResults.size() >= batchSize) {
processBatchThumbnails(batchResults);
batchResults.clear();
}
}
// 每处理 15 个休息一下,避免 UI 卡顿
if (i % 15 == 0) {
QThread::msleep(15);
}
}
// 处理剩余的缩略图
if (!batchResults.isEmpty()) {
processBatchThumbnails(batchResults);
}
});
thumbnailWatcher->setFuture(future);
}
void ImageViewer::onThumbnailLoaded(const QString &path,
const QPixmap &pixmap) {
for (int i = 0; i < thumbnailList->count(); ++i) {
QListWidgetItem *item = thumbnailList->item(i);
if (item->data(Qt::UserRole).toString() == path) {
item->setIcon(QIcon(pixmap));
// 强制更新布局以确保网格对齐
thumbnailList->update();
// 使用公共方法触发布局更新
thumbnailList->setGridSize(thumbnailList->gridSize());
break;
}
}
}
void ImageViewer::onThumbnailClicked(QListWidgetItem *item) {
QString newPath = item->data(Qt::UserRole).toString();
if (newPath == currentPath)
return;
// 找到新图片索引
for (int i = 0; i < imageFiles.size(); ++i) {
if (imageFiles[i].absoluteFilePath() == newPath) {
currentImageIndex = i;
break;
}
}
currentPath = newPath;
setWindowTitle(QFileInfo(newPath).fileName());
// 检查是否是 GIF 文件
bool newIsGif = newPath.toLower().endsWith(".gif");
// 检查是否是 WebP 文件
bool newIsWebp = newPath.toLower().endsWith(".webp");
// 如果从 GIF 切换到普通图片或反之,需要重置相关状态
if (isGif != newIsGif) {
// 停止当前 GIF 动画(如果是)
if (isGif && gifMovie) {
gifMovie->stop();
delete gifMovie;
gifMovie = nullptr;
}
isGif = newIsGif;
if (isGif) {
// 初始化 GIF 动画
setupGifAnimation();
} else if (newIsWebp) {
// 加载 WebP 图片
isWebp = true;
loadWebpImage();
} else {
// 加载普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
} else if (isGif) {
// 切换到新的 GIF
setupGifAnimation();
} else if (isWebp != newIsWebp) {
// 停止当前 WebP 动画(如果是)
if (isWebp && webpAnimationTimer) {
webpAnimationTimer->stop();
delete webpAnimationTimer;
webpAnimationTimer = nullptr;
}
isWebp = newIsWebp;
if (isWebp) {
// 加载 WebP 图片
loadWebpImage();
} else {
// 加载普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
} else if (isWebp) {
// 切换到新的 WebP
loadWebpImage();
} else {
// 切换到新的普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
offset = QPointF(0, 0);
updateImageDisplay();
hideThumbnailMenu();
}
void ImageViewer::showThumbnailMenu() {
if (imageFiles.size() <= 1)
return; // 只有一张图时不显示
int menuHeight = 120;
thumbnailScrollArea->setGeometry(0, height() - menuHeight, width(),
menuHeight);
thumbnailScrollArea->setVisible(true);
thumbnailsVisible = true;
// 确保缩略图列表布局已更新
thumbnailList->updateGeometry();
thumbnailList->update();
// 强制重新计算布局以确保网格对齐
thumbnailList->setGridSize(
thumbnailList->gridSize()); // 使用公共方法触发布局更新
// 滚动到当前选中项
if (thumbnailList->currentItem()) {
// 使用 QTimer 确保在布局更新后滚动
QTimer::singleShot(50, this, [this]() { // 增加延时确保布局完全更新
if (thumbnailList->currentItem()) {
thumbnailList->scrollToItem(thumbnailList->currentItem(),
QAbstractItemView::PositionAtCenter);
}
});
}
}
void ImageViewer::hideThumbnailMenu() {
thumbnailScrollArea->setVisible(false);
thumbnailsVisible = false;
}
bool ImageViewer::eventFilter(QObject *obj, QEvent *event) {
if (obj == thumbnailList) {
// 处理触摸事件
if (event->type() == QEvent::TouchBegin) {
showZoomButtons(); // 显示操作按钮
return true;
}
}
return QWidget::eventFilter(obj, event);
}
void ImageViewer::switchToImage(int index) {
if (index >= 0 && index < imageFiles.size()) {
currentImageIndex = index;
currentPath = imageFiles[index].absoluteFilePath();
setWindowTitle(QFileInfo(currentPath).fileName());
// 检查是否是 GIF 文件
bool newIsGif = currentPath.toLower().endsWith(".gif");
// 检查是否是 WebP 文件
bool newIsWebp = currentPath.toLower().endsWith(".webp");
// 如果从 GIF 切换到普通图片或反之,需要重置相关状态
if (isGif != newIsGif) {
// 停止当前 GIF 动画(如果是)
if (isGif && gifMovie) {
gifMovie->stop();
delete gifMovie;
gifMovie = nullptr;
}
isGif = newIsGif;
if (isGif) {
// 初始化 GIF 动画
setupGifAnimation();
} else if (newIsWebp) {
// 加载 WebP 图片
isWebp = true;
loadWebpImage();
} else {
// 加载普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
} else if (isGif) {
// 切换到新的 GIF
setupGifAnimation();
} else if (isWebp != newIsWebp) {
// 停止当前 WebP 动画(如果是)
if (isWebp && webpAnimationTimer) {
webpAnimationTimer->stop();
delete webpAnimationTimer;
webpAnimationTimer = nullptr;
}
isWebp = newIsWebp;
if (isWebp) {
// 加载 WebP 图片
loadWebpImage();
} else {
// 加载普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
} else if (isWebp) {
// 切换到新的 WebP
loadWebpImage();
} else {
// 切换到新的普通图片
originalPixmap = QPixmap(currentPath);
rotationAngle = 0;
rotatedPixmap = originalPixmap;
}
offset = QPointF(0, 0);
updateImageDisplay();
}
}
// 处理批量缩略图更新
void ImageViewer::processBatchThumbnails(
const QList<QPair<QString, QPixmap>> &batch) {
QMetaObject::invokeMethod(
this,
[this, batch]() {
for (const auto &pair : batch) {
onThumbnailLoaded(pair.first, pair.second);
}
},
Qt::QueuedConnection);
}
// 设置 GIF 动画
void ImageViewer::setupGifAnimation() {
// 停止并删除旧的 GIF 动画(如果有)
if (gifMovie) {
gifMovie->stop();
delete gifMovie;
gifMovie = nullptr;
}
// 创建新的 GIF 动画
gifMovie = new QMovie(currentPath);
if (!gifMovie->isValid()) {
delete gifMovie;
gifMovie = nullptr;
return;
}
// 连接帧更新信号
connect(gifMovie, &QMovie::frameChanged, this, &ImageViewer::updateGifFrame);
// 获取第一帧
currentGifFrame = gifMovie->currentPixmap();
// 开始播放 GIF
gifMovie->start();
}
// 更新 GIF 帧
void ImageViewer::updateGifFrame() {
if (gifMovie && gifMovie->isValid()) {
currentGifFrame = gifMovie->currentPixmap();
update(); // 触发重绘
}
}
// 加载 WebP 图片
void ImageViewer::loadWebpImage() {
// 先停止并删除旧的动画计时器(如果有)
if (webpAnimationTimer) {
webpAnimationTimer->stop();
delete webpAnimationTimer;
webpAnimationTimer = nullptr;
}
// 重置动画标志
isAnimatedWebp = false;
currentWebpFrame = 0;
currentLoopCount = 0;
QFile file(currentPath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "无法打开 WebP 文件:" << currentPath;
return;
}
QByteArray data = file.readAll();
file.close();
if (data.isEmpty()) {
qWarning() << "WebP 文件为空:" << currentPath;
return;
}