-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathviewpanel.cpp
More file actions
2550 lines (2271 loc) · 92.9 KB
/
viewpanel.cpp
File metadata and controls
2550 lines (2271 loc) · 92.9 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
// SPDX-FileCopyrightText: 2020 - 2024 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "viewpanel.h"
#include <QVBoxLayout>
#include <QShortcut>
#include <QFileInfo>
#include <QDBusInterface>
#include <QGuiApplication>
#include <QScreen>
#include <QApplication>
#include <QStringList>
#include <dshadowline.h>
#include <DDesktopServices>
#include <DMenu>
#include <DFileDialog>
#include "contents/bottomtoolbar.h"
#include "navigationwidget.h"
#include "lockwidget.h"
#include "thumbnailwidget.h"
#include "unionimage/imageutils.h"
#include "unionimage/baseutils.h"
#include "unionimage/pluginbaseutils.h"
#include "unionimage/unionimage.h"
#include "imageengine.h"
#include "widgets/printhelper.h"
#include "contents/imageinfowidget.h"
#include "widgets/extensionpanel.h"
#include "widgets/toptoolbar.h"
#include "widgets/renamedialog.h"
#include "service/ocrinterface.h"
#include "slideshow/slideshowpanel.h"
#include "service/permissionconfig.h"
#include "service/configsetter.h"
#include "service/imagedataservice.h"
#include "service/mtpfileproxy.h"
#include "unionimage/imageutils.h"
#include "service/aimodelservice.h"
#include "contents/aienhancefloatwidget.h"
const QString IMAGE_TMPPATH = QDir::homePath() + "/.config/deepin/deepin-image-viewer/";
const int BOTTOM_TOOLBAR_HEIGHT = 80; //底部工具看高
const int BOTTOM_SPACING = 5; //底部工具栏与底部边缘距离
const int RT_SPACING = 10;
const int TOP_TOOLBAR_HEIGHT = 50;
const int DELAY_HIDE_CURSOR_INTERVAL = 3000;
// 标题栏高度
const int TITLEBAR_HEIGHT = 50;
// 紧凑模式下标题栏高度
const int COMPACT_TITLEBAR_HEIGHT = 40;
using namespace imageViewerSpace;
bool compareByFileInfo(const QFileInfo &str1, const QFileInfo &str2)
{
static QCollator sortCollator;
sortCollator.setNumericMode(true);
return sortCollator.compare(str1.baseName(), str2.baseName()) < 0;
}
QString ss(const QString &text, const QString &defaultValue)
{
Q_UNUSED(text);
//采用代码中快捷键不使用配置文件快捷键
// QString str = dApp->setter->value(SHORTCUTVIEW_GROUP, text, defaultValue).toString();
QString str = defaultValue;
str.replace(" ", "");
return defaultValue;
}
/**
@return 返回当前标题栏高度,紧凑模式和普通模式下的标题栏高度不同
*/
int titleBarHeight()
{
// DTK 在 5.6.4 后提供紧凑模式接口,调整控件大小
#ifdef DTKWIDGET_CLASS_DSizeMode
if (DGuiApplicationHelper::isCompactMode()) {
return COMPACT_TITLEBAR_HEIGHT;
} else {
return TITLEBAR_HEIGHT;
}
#else
return TITLEBAR_HEIGHT;
#endif
}
LibViewPanel::LibViewPanel(AbstractTopToolbar *customToolbar, QWidget *parent)
: QFrame(parent)
, m_topToolbar(customToolbar)
{
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setContentsMargins(0, 0, 0, 0);
this->setLayout(layout);
m_stack = new DStackedWidget(this);
layout->addWidget(m_stack);
m_view = new LibImageGraphicsView(this);
m_stack->addWidget(m_view);
//m_bottomToolbar的父为主窗口,就不会出现右键菜单
m_bottomToolbar = new LibBottomToolbar(dynamic_cast<QWidget *>(this->parent()));
setContextMenuPolicy(Qt::CustomContextMenu);
initRightMenu();
initFloatingComponent();
initTopBar();
initShortcut();
initLockPanel();
initThumbnailWidget();
initConnect();
if (AIModelService::instance()->isValid()) {
// 创建按钮
createAIBtn();
}
setAcceptDrops(true);
// initExtensionPanel();
QObject::connect(m_view, &LibImageGraphicsView::currentThumbnailChanged, m_bottomToolbar, &LibBottomToolbar::onThumbnailChanged, Qt::DirectConnection);
QObject::connect(m_view, &LibImageGraphicsView::gestureRotate, this, &LibViewPanel::slotRotateImage);
//删除完了图片需要返回原状bug137195
QObject::connect(ImageEngine::instance(), &ImageEngine::sigPicCountIsNull, this, [ = ] {
if (ImgViewerType::ImgViewerTypeAlbum != LibCommonService::instance()->getImgViewerType())
{
if (window()->isFullScreen()) {
window()->showNormal();
window()->resize(m_windowSize);
window()->move(m_windowX, m_windowY);
QTimer::singleShot(50, [ = ] {
window()->move(m_windowX, m_windowY);
});
}
}
});
titleShadow= new DShadowLine(this);
titleShadow->setAttribute(Qt::WA_AlwaysStackOnTop);
QRect rect(0,m_topToolbar->geometry().bottom()+1,m_topToolbar->geometry().width(),titleShadow->sizeHint().height());
titleShadow->setGeometry(rect);
titleShadow->setVisible(true);
titleShadow->raise();
// 添加过滤处理,当窗口出现状态变更时(最大化、全屏),处理标题栏隐藏、显示
if (window()) {
window()->installEventFilter(this);
}
}
LibViewPanel::~LibViewPanel()
{
// 关闭前保存旋转状态
if (m_view) {
m_view->slotRotatePixCurrent();
}
// 析构时通知图片窗口关闭
PermissionConfig::instance()->triggerAction(PermissionConfig::TidClose, m_currentPath);
// 清空图像缓存目录
Libutils::image::clearCacheImageFolder();
if (m_bottomToolbar) {
m_bottomToolbar->deleteLater();
m_bottomToolbar = nullptr;
}
if (m_topToolbar) {
m_topToolbar->deleteLater();
m_topToolbar = nullptr;
}
}
void LibViewPanel::loadImage(const QString &path, QStringList paths)
{
qInfo() << "Loading image:" << path;
// 初始化图像缓存目录
Libutils::image::initCacheImageFolder();
QFileInfo info(path);
m_topToolbar->setMiddleContent(info.fileName());
//展示图片
// m_view->setImage(path);
// m_view->resetTransform();
// m_stack->setCurrentWidget(m_view);
//刷新工具栏,如果paths不含有path,则添加进入paths
if (!paths.contains(path)) {
paths << path;
}
m_bottomToolbar->setAllFile(path, paths);
m_currentPath = path;
//刷新收藏按钮
emit ImageEngine::instance()->sigUpdateCollectBtn();
//重置底部工具栏位置与大小
resetBottomToolbarGeometry(true);
m_dirWatcher->removePaths(m_dirWatcher->directories());
m_dirWatcher->addPath(QFileInfo(path).dir().path());
// 判断是否授权状态更新
QFileInfo targetImageInfo(PermissionConfig::instance()->targetImage());
if (info.absoluteDir() != targetImageInfo.absoluteDir()) {
if (!paths.contains(targetImageInfo.absoluteFilePath())) {
// 关闭前保存旋转状态
if (m_view) {
m_view->slotRotatePixCurrent();
}
// 不含授权文件,提示文件关闭
PermissionConfig::instance()->triggerAction(PermissionConfig::TidClose,
PermissionConfig::instance()->targetImage());
}
}
}
void LibViewPanel::initConnect()
{
//缩略图列表,单机打开图片
connect(m_bottomToolbar, &LibBottomToolbar::openImg, this, &LibViewPanel::openImg);
connect(m_view, &LibImageGraphicsView::imageChanged, this, [ = ](QString path) {
emit imageChanged(path);
// Pixmap is cache in thread, make sure the size would correct after
// cache is finish
//暂时屏蔽,这里存在疑问,放开会导致每次切换图片,1:1高亮
// m_view->autoFit();
});
//旋转信号
connect(m_bottomToolbar, &LibBottomToolbar::rotateClockwise, this, [ = ] {
this->slotRotateImage(-90);
});
connect(m_bottomToolbar, &LibBottomToolbar::rotateCounterClockwise, this, [ = ] {
this->slotRotateImage(90);
});
//适应窗口和适应图片按钮
connect(m_bottomToolbar, &LibBottomToolbar::resetTransform, this, &LibViewPanel::slotResetTransform);
//删除后需要重新布局
connect(m_bottomToolbar, &LibBottomToolbar::removed, this, [ = ] {
//重新布局
this->resetBottomToolbarGeometry(true);
}, Qt::DirectConnection);
//切换图片需要保存旋转
connect(m_bottomToolbar, &LibBottomToolbar::sigRotateSave, m_view, &LibImageGraphicsView::slotRotatePixCurrent);
//适应窗口的状态更新
connect(m_view, &LibImageGraphicsView::checkAdaptScreenBtn, m_bottomToolbar, &LibBottomToolbar::checkAdaptImageBtn);
connect(m_view, &LibImageGraphicsView::disCheckAdaptScreenBtn, m_bottomToolbar, &LibBottomToolbar::disCheckAdaptScreenBtn);
connect(m_view, &LibImageGraphicsView::checkAdaptImageBtn, m_bottomToolbar, &LibBottomToolbar::checkAdaptImageBtn);
connect(m_view, &LibImageGraphicsView::disCheckAdaptImageBtn, m_bottomToolbar, &LibBottomToolbar::disCheckAdaptImageBtn);
connect(m_bottomToolbar, &LibBottomToolbar::sigOcr, this, &LibViewPanel::slotOcrPicture);
connect(m_bottomToolbar, &LibBottomToolbar::sigLeaveBottom, this, &LibViewPanel::slotBottomMove);
connect(m_bottomToolbar, &LibBottomToolbar::sigResizeBottom, this, &LibViewPanel::slotBottomMove);
connect(m_view, &LibImageGraphicsView::sigImageOutTitleBar, this, &LibViewPanel::slotsImageOutTitleBar);
// connect(m_view, &LibImageGraphicsView::sigImageOutTitleBar, m_topToolbar, &AbstractTopToolbar::setTitleBarTransparent);
connect(m_view, &LibImageGraphicsView::sigMouseMove, this, &LibViewPanel::slotBottomMove);
connect(m_view, &LibImageGraphicsView::sigClicked, this, &LibViewPanel::slotChangeShowTopBottom);
connect(ImageEngine::instance(), &ImageEngine::sigOneImgReady, this, &LibViewPanel::slotOneImgReady, Qt::QueuedConnection);
connect(m_view, &LibImageGraphicsView::UpdateNavImg, this, [ = ]() {
m_nav->setImage(m_view->image());
m_nav->setRectInImage(m_view->visibleImageRect());
//二指放大会触发信号,导致窗口隐藏,这里下面存在问题
// //正在滑动缩略图的时候不再显示
// if (m_nav->isVisible()) {
// m_nav->setVisible(false);
// }
});
connect(m_view, &LibImageGraphicsView::sigFIleDelete, this, [ = ]() {
this->updateMenuContent();
});
//增加双击全屏和退出全屏的功能
connect(m_view, &LibImageGraphicsView::doubleClicked, this, [ = ]() {
toggleFullScreen();
});
//上一页,下一页信号连接
connect(m_view, &LibImageGraphicsView::previousRequested, this, &LibViewPanel::showPrevious);
connect(m_view, &LibImageGraphicsView::nextRequested, this, &LibViewPanel::showNext);
m_dirWatcher = new QFileSystemWatcher(this);
connect(m_dirWatcher, &QFileSystemWatcher::directoryChanged, this, &LibViewPanel::slotsDirectoryChanged);
// AI图像增强
if (AIModelService::instance()->isValid()) {
connect(AIModelService::instance(), &AIModelService::enhanceStart, this, &LibViewPanel::onEnhanceStart);
connect(AIModelService::instance(), &AIModelService::enhanceReload, this, &LibViewPanel::onEnhanceReload);
connect(AIModelService::instance(), &AIModelService::enhanceEnd, this, &LibViewPanel::onEnhanceEnd);
}
// DTK 在 5.6.4 后提供紧凑模式接口,调整控件大小
#ifdef DTKWIDGET_CLASS_DSizeMode
connect(DGuiApplicationHelper::instance(),
&DGuiApplicationHelper::sizeModeChanged,
this,
[this](DGuiApplicationHelper::SizeMode) {
m_topToolbar->resize(width(), titleBarHeight());
m_topToolbar->move(0, 0);
m_topToolbar->update();
});
#endif
// 展示的item增加时(拖拽、放大等),需要同步更新缩略图
connect(m_bottomToolbar, &LibBottomToolbar::displayItemGrowUp, this, [this](int){
loadThumbnails(m_currentPath);
});
}
void LibViewPanel::initTopBar()
{
//防止在标题栏右键菜单会触发默认的和主窗口的发生
if (m_topToolbar == nullptr) { //如果调用者没有指定有效的顶部栏,则使用内置方案
m_topToolbar = new LibTopToolbar(false, dynamic_cast<QWidget *>(this->parent()));
connect(m_topToolbar, &LibTopToolbar::sigLeaveTitle, this, &LibViewPanel::slotBottomMove);
} else {
m_topToolbar->setParent(dynamic_cast<QWidget *>(this->parent()));
}
m_topToolbar->resize(width(), titleBarHeight());
m_topToolbar->move(0, 0);
m_topToolbar->setTitleBarTransparent(false);
}
void LibViewPanel::initOcr()
{
if (!m_ocrInterface) {
m_ocrInterface = new OcrInterface("com.deepin.Ocr", "/com/deepin/Ocr", QDBusConnection::sessionBus(), this);
}
}
void LibViewPanel::initFloatingComponent()
{
initScaleLabel();
initNavigation();
}
void LibViewPanel::initScaleLabel()
{
using namespace Libutils::base;
DAnchors<DFloatingWidget> scalePerc = new DFloatingWidget(this);
scalePerc->setBlurBackgroundEnabled(true);
QHBoxLayout *layout = new QHBoxLayout(scalePerc);
scalePerc->setLayout(layout);
DLabel *label = new DLabel(this);
layout->addWidget(label);
scalePerc->setAttribute(Qt::WA_TransparentForMouseEvents);
scalePerc.setAnchor(Qt::AnchorHorizontalCenter, this, Qt::AnchorHorizontalCenter);
scalePerc.setAnchor(Qt::AnchorBottom, this, Qt::AnchorBottom);
scalePerc.setBottomMargin(75 + 14);
label->setAlignment(Qt::AlignCenter);
// scalePerc->setFixedSize(82, 48);
scalePerc->setFixedWidth(90 + 10);
scalePerc->setFixedHeight(40 + 10);
scalePerc->adjustSize();
label->setText("100%");
DFontSizeManager::instance()->bind(label, DFontSizeManager::T6);
scalePerc->hide();
QTimer *hideT = new QTimer(this);
hideT->setSingleShot(true);
connect(hideT, &QTimer::timeout, scalePerc, &DLabel::hide);
connect(m_view, &LibImageGraphicsView::scaled, this, [ = ](qreal perc) {
label->setText(QString("%1%").arg(int(perc)));
});
connect(m_view, &LibImageGraphicsView::showScaleLabel, this, [ = ]() {
scalePerc->show();
hideT->start(1000);
});
}
void LibViewPanel::initNavigation()
{
m_nav = new NavigationWidget(this);
// 导航窗口左下对齐,间距10px
m_nav.setBottomMargin(80);
m_nav.setLeftMargin(6);
m_nav.setAnchor(Qt::AnchorLeft, this, Qt::AnchorLeft);
m_nav.setAnchor(Qt::AnchorBottom, this, Qt::AnchorBottom);
connect(this, &LibViewPanel::imageChanged, this, [ = ](const QString & path) {
Q_UNUSED(path)
//BUG#93145 去除对path的判断,直接隐藏导航窗口
m_nav->setVisible(false);
m_nav->setImage(m_view->image());
//转移到中心位置
// m_bottomToolbar->thumbnailMoveCenterWidget();
});
connect(m_nav, &NavigationWidget::requestMove, [this](int x, int y) {
m_view->centerOn(x, y);
});
connect(m_view, &LibImageGraphicsView::transformChanged, this, [ = ]() {
//如果stackindex不为2,全屏会出现导航窗口
//如果是正在移动的情况,将不会出现导航栏窗口
if (m_stack->currentWidget() == m_view) {
m_nav->setVisible((! m_nav->isAlwaysHidden() && ! m_view->isWholeImageVisible()));
m_nav->setRectInImage(m_view->visibleImageRect());
} else {
m_nav->setVisible(false);
}
});
}
void LibViewPanel::initRightMenu()
{
//初始化时设置所有菜单项都显示
m_menuItemDisplaySwitch.set();
if (!m_menu) {
m_menu = new DMenu(this);
}
QShortcut *ctrlm = new QShortcut(QKeySequence("Ctrl+M"), this);
ctrlm->setContext(Qt::WindowShortcut);
connect(ctrlm, &QShortcut::activated, this, [ = ] {
this->customContextMenuRequested(cursor().pos());
});
connect(this, &LibViewPanel::customContextMenuRequested, this, [ = ] {
updateMenuContent();
m_menu->popup(QCursor::pos());
});
connect(m_menu, &DMenu::triggered, this, &LibViewPanel::onMenuItemClicked);
}
void LibViewPanel::initExtensionPanel()
{
if (!m_info) {
m_info = new LibImageInfoWidget("", "", this);
m_info->hide();
}
m_info->setImagePath(m_bottomToolbar->getCurrentItemInfo().path);
if (!m_extensionPanel) {
m_extensionPanel = new ExtensionPanel(this);
connect(m_info, &LibImageInfoWidget::extensionPanelHeight, m_extensionPanel, &ExtensionPanel::updateRectWithContent);
connect(m_view, &LibImageGraphicsView::clicked, this, [ = ] {
this->m_extensionPanel->hide();
this->m_info->show();
});
}
}
void LibViewPanel::updateMenuContent(const QString &path)
{
//判断是否为相册调用
bool isAlbum = false;
if (ImgViewerType::ImgViewerTypeAlbum == LibCommonService::instance()->getImgViewerType()) {
isAlbum = true;
}
if (!window()->isFullScreen()) {
resetBottomToolbarGeometry(true);
}
if (m_menu) {
m_menu->clear();
qDeleteAll(this->actions());
imageViewerSpace::ItemInfo ItemInfo = m_bottomToolbar->getCurrentItemInfo();
bool isPic = !ItemInfo.image.isNull();
if (!isPic) {
isPic = !m_view->image().isNull();//当前视图是否是图片
}
QString currentPath;
if (path.isEmpty()) {
currentPath = ItemInfo.path;
} else {
currentPath = path;
}
if (currentPath.isEmpty()) {
currentPath = m_currentPath;
}
// AI增强图片特殊处理,部分选项屏蔽
bool enhanceImage = AIModelService::instance()->isTemporaryFile(m_currentPath);
if (enhanceImage) {
currentPath = m_currentPath;
}
QFileInfo info(currentPath);
bool isReadable = info.isReadable(); //是否可读
//判断文件是否可写和文件目录是否可写
bool isWritable = info.isWritable() && QFileInfo(info.dir(), info.dir().path()).isWritable(); //是否可写
bool isRotatable = ImageEngine::instance()->isRotatable(currentPath);//是否可旋转
imageViewerSpace::PathType pathType; // 路径类型
// 判断是否为MTP文件,现MTP文件将使用代理文件加载
if (MtpFileProxy::instance()->contains(currentPath)) {
pathType = imageViewerSpace::PathType::PathTypeMTP;
} else {
pathType = LibUnionImage_NameSpace::getPathType(currentPath);
}
imageViewerSpace::ImageType imageType = LibUnionImage_NameSpace::getImageType(currentPath);//图片类型
auto authIns = PermissionConfig::instance();
bool isCopyable = authIns->checkAuthFlag(PermissionConfig::EnableCopy, currentPath);
//判断是否是损坏图片
setCurrentWidget(currentPath);
if (m_info) {
m_info->setImagePath(currentPath, false);
}
if (imageViewerSpace::ImageTypeDamaged == imageType) {
return;
}
DIconButton *AdaptImageButton = m_bottomToolbar->getBottomtoolbarButton(imageViewerSpace::ButtonTypeAdaptImage);
DIconButton *AdaptScreenButton = m_bottomToolbar->getBottomtoolbarButton(imageViewerSpace::ButtonTypeAdaptScreen);
//修复外部删除图片仍然能够使用适应图片和适应窗口的问题
if (isPic && isReadable) {
AdaptImageButton->setEnabled(true);
AdaptScreenButton->setEnabled(true);
} else {
AdaptImageButton->setEnabled(false);
AdaptScreenButton->setEnabled(false);
}
if (!isPic) {
AdaptScreenButton->setChecked(isPic);
AdaptImageButton->setChecked(isPic);
}
if (window()->isFullScreen()) {
appendAction(IdExitFullScreen, QObject::tr("Exit fullscreen"), ss("Fullscreen", "F11"));
} else {
appendAction(IdFullScreen, QObject::tr("Fullscreen"), ss("Fullscreen", "F11"));
}
if (authIns->isCurrentIsTargetImage() && !authIns->isUnlimitPrint()) {
appendAction(IdPrint, QObject::tr("Print") + QObject::tr("(Remaining %1 times)").arg(authIns->printCount()),
ss("Print", "Ctrl+P"), authIns->isPrintable());
} else {
appendAction(IdPrint, QObject::tr("Print"), ss("Print", "Ctrl+P"));
}
//ocr按钮,是否是动态图,todo
DIconButton *OcrButton = m_bottomToolbar->getBottomtoolbarButton(imageViewerSpace::ButtonTypeOcr);
if (OcrButton != nullptr) {
if (imageViewerSpace::ImageTypeDynamic != imageType && isPic && isReadable && isCopyable) {
appendAction(IdOcr, QObject::tr("Extract text"), ss("Extract text", "Alt+O"));
OcrButton->setEnabled(true);
} else {
if (!isCopyable) {
appendAction(IdOcr, QObject::tr("Extract text") + QObject::tr("(Disabled)"), ss("Extract text", "Alt+O"), false);
}
OcrButton->setEnabled(false);
}
}
//如果图片数量大于0才能有幻灯片
appendAction(IdStartSlideShow, QObject::tr("Slide show"), ss("Slide show", "F5"));
//添加到相册
if (isAlbum && isReadable && !enhanceImage) {
//这行代码是在向调用程序寻求album name,根据前面的connect,这里会采用回调的形式执行
emit ImageEngine::instance()->sigGetAlbumName(ItemInfo.path);
m_menu->addSeparator();
//为了达到真·ABI兼容,此处需要针对新旧相册分类讨论
//不管有无自定义相册,均需保留添加到相册
DMenu *am = new DMenu(tr("Add to album"), m_menu);
QAction *ac1 = new QAction(am);
ac1->setProperty("MenuID", IdAddToAlbum);
ac1->setText(tr("New album"));
ac1->setShortcut(QKeySequence("Ctrl+Shift+N"));
am->addAction(ac1);
am->addSeparator();
if (!m_useUID) { //采用UID之前的相册
ac1->setData("Add to new album");
if (!m_CustomAlbumName.isEmpty()) {
for (auto iter = m_CustomAlbumName.begin(); iter != m_CustomAlbumName.end(); iter++) {
QAction *ac = new QAction(am);
ac->setProperty("MenuID", IdAddToAlbum);
ac->setText(fontMetrics().elidedText(QString(iter.key()).replace("&", "&&"), Qt::ElideMiddle, 200));
ac->setData(iter.key());
am->addAction(ac);
if (iter.value()) {
ac->setEnabled(false);
}
}
}
} else { //其余情况代表采用UID后的相册
ac1->setData(-1);
if (!m_CustomAlbumAndUID.isEmpty()) {
for (auto iter = m_CustomAlbumAndUID.begin(); iter != m_CustomAlbumAndUID.end(); iter++) {
QAction *ac = new QAction(am);
ac->setProperty("MenuID", IdAddToAlbum);
ac->setText(fontMetrics().elidedText(QString(iter.value().first).replace("&", "&&"), Qt::ElideMiddle, 200));
ac->setData(iter.key());
am->addAction(ac);
if (iter.value().second) {
ac->setEnabled(false);
}
}
}
}
m_menu->addMenu(am);
}
m_menu->addSeparator();
// 处理收藏按钮,AI修图的增强图片不允许点击
if (isAlbum) {
DIconButton *Collection = m_bottomToolbar->getBottomtoolbarButton(imageViewerSpace::ButtonTypeCollection);
if (Collection) {
Collection->setEnabled(!enhanceImage);
}
}
// 添加AI模型选项,仅处理静态图
addAIMenu();
if (isAlbum && isReadable && !enhanceImage && isCopyable) {
appendAction(IdExport, tr("Export"), ss("Export", "Ctrl+E")); //导出
}
if (isReadable) {
if (isCopyable) {
appendAction(IdCopy, QObject::tr("Copy"), ss("Copy", "Ctrl+C"));
} else {
appendAction(IdCopy, QObject::tr("Copy") + QObject::tr("(Disabled)"), ss("Copy", "Ctrl+C"), false);
}
}
//如果程序有可读可写的权限,才能重命名,todo
//20211019新增:安卓手机和苹果手机也不进行重命名
if (isReadable && isWritable &&
imageViewerSpace::PathTypeMTP != pathType &&
imageViewerSpace::PathTypePTP != pathType &&
imageViewerSpace::PathTypeAPPLE != pathType && !isAlbum && authIns->checkAuthFlag(PermissionConfig::EnableRename)) {
appendAction(IdRename, QObject::tr("Rename"), ss("Rename", "F2"));
}
//apple phone的delete没有权限,保险箱无法删除,垃圾箱也无法删除,其他需要判断可读权限,todo
//20211019新增:安卓手机也不进行删除
DIconButton *TrashButton = m_bottomToolbar->getBottomtoolbarButton(imageViewerSpace::ButtonTypeTrash);
if ((imageViewerSpace::PathTypeAPPLE != pathType &&
imageViewerSpace::PathTypeSAFEBOX != pathType &&
imageViewerSpace::PathTypeRECYCLEBIN != pathType &&
imageViewerSpace::PathTypeMTP != pathType &&
imageViewerSpace::PathTypePTP != pathType &&
isWritable && isReadable && authIns->checkAuthFlag(PermissionConfig::EnableDelete)) || (isAlbum && isWritable)) {
if (isAlbum) {
appendAction(IdMoveToTrash, QObject::tr("Delete"), ss("Throw to trash", ""));
} else {
appendAction(IdMoveToTrash, QObject::tr("Delete"), ss("Throw to trash", "Delete"));
}
TrashButton->setEnabled(true);
} else {
TrashButton->setEnabled(false);
}
//IdRemoveFromAlbum
if (isAlbum && m_isCustomAlbum && isReadable && !enhanceImage) {
appendAction(IdRemoveFromAlbum, tr("Remove from album"), ss("Remove from album", ""));
}
m_menu->addSeparator();
//fav
if (isAlbum && isReadable && !enhanceImage) {
if (m_isFav) {
appendAction(IdRemoveFromFavorites, tr("Unfavorite"), "."); //取消收藏
} else {
appendAction(IdAddToFavorites, tr("Favorite"), "."); //收藏
}
m_menu->addSeparator();
}
//判断导航栏隐藏,需要添加一个当前是否有图片,todo
if (isReadable && isPic && !m_view->isWholeImageVisible() && m_nav->isAlwaysHidden()) {
appendAction(IdShowNavigationWindow, QObject::tr("Show navigation window"),
ss("Show navigation window", ""));
} else if (isReadable && isPic && !m_view->isWholeImageVisible() && !m_nav->isAlwaysHidden()) {
appendAction(IdHideNavigationWindow, QObject::tr("Hide navigation window"),
ss("Hide navigation window", ""));
}
//20211019修改:都可以转,但特殊位置不能执行写
if (isRotatable && isWritable && isPic) {
appendAction(IdRotateClockwise, QObject::tr("Rotate clockwise"), ss("Rotate clockwise", "Ctrl+R"));
appendAction(IdRotateCounterclockwise, QObject::tr("Rotate counterclockwise"),
ss("Rotate counterclockwise", "Ctrl+Shift+R"));
if (m_bottomToolbar) {
m_bottomToolbar->setRotateBtnClicked(true);
}
} else {
if (m_bottomToolbar) {
m_bottomToolbar->setRotateBtnClicked(false);
}
}
// 需要判断图片是否支持设置壁纸,若不支持则置灰设置壁纸菜单项
// 当配置权限控制时,屏蔽设置壁纸接口
bool enableWallpaper = authIns->checkAuthFlag(PermissionConfig::EnableWallpaper, currentPath);
if (isPic && enableWallpaper) {
QAction *ac = appendAction(IdSetAsWallpaper, QObject::tr("Set as wallpaper"), ss("Set as wallpaper", "Ctrl+F9"));
if (ac)
ac->setEnabled(Libutils::image::imageSupportWallPaper(ItemInfo.path));
}
if (isReadable) {
appendAction(IdDisplayInFileManager, QObject::tr("Display in file manager"),
ss("Display in file manager", "Alt+D"));
if (isAlbum) {
appendAction(IdImageInfo, QObject::tr("Photo info"), ss("Image info", "Ctrl+I"));
} else {
appendAction(IdImageInfo, QObject::tr("Image info"), ss("Image info", "Ctrl+I"));
}
}
}
}
void LibViewPanel::toggleFullScreen()
{
qInfo() << "Toggling fullscreen mode";
// m_view->setFitState(false, false);
if (window()->isFullScreen()) {
showNormal();
killTimer(m_hideCursorTid);
m_hideCursorTid = 0;
m_view->viewport()->setCursor(Qt::ArrowCursor);
emit ImageEngine::instance()->exitSlideShow(); //用于触发相册的从哪里来回哪里去,这个API应该改一下名字,但是流程过于麻烦
} else {
showFullScreen();
if (!m_menu || !m_menu->isVisible()) {
m_view->viewport()->setCursor(Qt::BlankCursor);
}
}
}
void LibViewPanel::showFullScreen()
{
qInfo() << "Entering fullscreen mode";
m_windowSize = window()->size();
m_windowX = window()->x();
m_windowY = window()->y();
if (m_view) {
m_view->setWindowIsFullScreen(true);
}
m_isMaximized = window()->isMaximized();
// Full screen then hide bars because hide animation depends on height()
//加入动画效果,掩盖左上角展开的视觉效果,以透明度0-1显示。,时间为50ms
//停止工具栏的动画
if (m_bottomAnimation) {
m_bottomAnimation->stop();
}
QPropertyAnimation *pAn = new QPropertyAnimation(window(), "windowOpacity");
pAn->setDuration(50);
pAn->setEasingCurve(QEasingCurve::Linear);
pAn->setEndValue(1);
pAn->setStartValue(0);
pAn->start(QAbstractAnimation::DeleteWhenStopped);
window()->showFullScreen();
m_hideCursorTid = startTimer(DELAY_HIDE_CURSOR_INTERVAL);
}
void LibViewPanel::showNormal()
{
qInfo() << "Exiting fullscreen mode";
if (m_view) {
m_view->setWindowIsFullScreen(false);
}
//加入动画效果,掩盖左上角展开的视觉效果,以透明度0-1显示。
//停止工具栏的动画
if (m_bottomAnimation) {
m_bottomAnimation->stop();
}
QPropertyAnimation *pAn = new QPropertyAnimation(window(), "windowOpacity");
pAn->setDuration(50);
pAn->setEasingCurve(QEasingCurve::Linear);
pAn->setEndValue(1);
pAn->setStartValue(0);
pAn->start(QAbstractAnimation::DeleteWhenStopped);
if (m_isMaximized) {
window()->showNormal();
window()->showMaximized();
} else {
window()->showNormal();
}
//增加切换全屏和默认大小下方工具栏的移动
connect(pAn, &QPropertyAnimation::destroyed, this, [ = ] {
m_bottomToolbar->move((width() - m_bottomToolbar->width()) / 2, height() - m_bottomToolbar->height() - BOTTOM_SPACING);
m_bottomToolbar->update();
//结束时候应该判断一次滑动
noAnimationBottomMove();
});
}
QAction *LibViewPanel::appendAction(int id, const QString &text, const QString &shortcut, bool enable)
{
if (m_menu && m_menuItemDisplaySwitch.test(static_cast<size_t>(id))) {
QAction *ac = new QAction(m_menu);
addAction(ac);
ac->setText(text);
ac->setProperty("MenuID", id);
ac->setShortcut(QKeySequence(shortcut));
ac->setEnabled(enable);
m_menu->addAction(ac);
return ac;
}
return nullptr;
}
void LibViewPanel::setContextMenuItemVisible(imageViewerSpace::NormalMenuItemId id, bool visible)
{
m_menuItemDisplaySwitch.set(id, visible);
updateMenuContent();
}
/**
@brief 通过DBus接口设置图片 \a path 为壁纸
task 32367: 同时设置为锁屏壁纸
*/
static void setWallpaperWithDBus(const QString &path)
{
//202011/12 bug54279 设置壁纸代码改变,采用DBus
qDebug() << "SettingWallpaper: " << "flatpak" << path;
QDBusInterface interfaceV20("com.deepin.daemon.Appearance",
"/com/deepin/daemon/Appearance",
"com.deepin.daemon.Appearance");
QDBusInterface interfaceV23("org.deepin.dde.Appearance1",
"/org/deepin/dde/Appearance1",
"org.deepin.dde.Appearance1");
QDBusInterface *interface = interfaceV23.isValid() ? &interfaceV23 : &interfaceV20;
if (interface->isValid()) {
QString screenname;
//判断环境是否是wayland
auto e = QProcessEnvironment::systemEnvironment();
QString XDG_SESSION_TYPE = e.value(QStringLiteral("XDG_SESSION_TYPE"));
QString WAYLAND_DISPLAY = e.value(QStringLiteral("WAYLAND_DISPLAY"));
bool isWayland = false;
if (XDG_SESSION_TYPE != QLatin1String("wayland") && !WAYLAND_DISPLAY.contains(QLatin1String("wayland"), Qt::CaseInsensitive)) {
isWayland = false;
} else {
isWayland = true;
}
// wayland下设置壁纸使用,2020/09/21
if (isWayland) {
QDBusInterface interfaceWaylandV20("com.deepin.daemon.Display", "/com/deepin/daemon/Display", "com.deepin.daemon.Display");
QDBusInterface interfaceWaylandV23("org.deepin.dde.Display1", "/org/deepin/dde/Display1", "org.deepin.dde.Display1");
QDBusInterface *interfaceWayland = interfaceWaylandV23.isValid() ? &interfaceWaylandV23 : &interfaceWaylandV20;
screenname = qvariant_cast< QString >(interfaceWayland->property("Primary"));
} else {
screenname = QGuiApplication::primaryScreen()->name();
}
QDBusMessage reply = interface->call(QStringLiteral("SetMonitorBackground"), screenname, path);
QString error = reply.errorMessage();
if (!error.isEmpty()) {
qWarning() << "SettingWallpaper: replay" << reply.errorMessage();
}
// 新增需求32367:同时设置锁屏壁纸
reply = interface->call(QStringLiteral("Set"), QStringLiteral("greeterbackground"), path);
error = reply.errorMessage();
if (!error.isEmpty()) {
qWarning() << "Set greeterbackground: replay" << reply.errorMessage();
}
// 通知触发设置壁纸动作(属于拷贝动作)
PermissionConfig::instance()->triggerAction(PermissionConfig::TidCopy, path);
} else {
qWarning() << "SettingWallpaper failed" << interface->lastError();
}
}
void LibViewPanel::setWallpaper(const QImage &img)
{
if (!img.isNull()) {
QString tempPathTemplate = Libutils::image::getCacheImagePath() + QDir::separator() + "XXXXXX_Wallpaper.jpg";
QThread *th1 = QThread::create([ = ]() {
// 设置锁屏壁纸不能使用相同名称,且临时文件不能立即删除(调用DBus接口拷贝需要时间),保留至缓存目录,程序退出自动清理
QTemporaryFile tmpImage;
tmpImage.setAutoRemove(false);
tmpImage.setFileTemplate(tempPathTemplate);
// 使用JPG压缩而不是PNG以压缩减少缓存图片大小
if (!tmpImage.open() || !img.save(tmpImage.fileName(), "JPG")) {
qWarning() << "Failed to save temporary wallpaper image:" << tmpImage.fileName();
return;
}
qInfo() << "Saving temporary wallpaper image:" << tmpImage.fileName();
setWallpaperWithDBus(tmpImage.fileName());
});
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
}
}
void LibViewPanel::setWallpaper(const QString &imgPath)
{
if (!imgPath.isEmpty()) {
qInfo() << "Setting wallpaper from file:" << imgPath;
QThread *th1 = QThread::create([ = ]() { setWallpaperWithDBus(imgPath); });
connect(th1, &QThread::finished, th1, &QObject::deleteLater);
th1->start();
}
}
bool LibViewPanel::startdragImage(const QStringList &paths, const QString &firstPath)
{
qInfo() << "Starting drag image operation with" << paths.size() << "images";
// 若为 MTP 挂载文件,转换为目录加载
QStringList realPaths = paths;
QString realPath = firstPath;
bool isMtpProxy = MtpFileProxy::instance()->checkAndCreateProxyFile(realPaths, realPath);
bool bRet = false;
QStringList image_list = realPaths;
if (image_list.isEmpty()) {
qWarning() << "No images to drag";
return false;
}
// 判断是否允许切换图片 (首次进入同样判断)
bool enableSwitch = PermissionConfig::instance()->checkAuthFlag(PermissionConfig::EnableSwitch, image_list.first());
if (!enableSwitch) {
if (image_list.first() != PermissionConfig::instance()->targetImage()) {
qWarning() << "Image switching not allowed by permission config";
return false;
}
}
bool permissionValid = PermissionConfig::instance()->isValid();
if (LibCommonService::instance()->getImgViewerType() == imageViewerSpace::ImgViewerTypeLocal) {
QString path = image_list.first();
// 判断是否允许切换图片,不切换图片时,将仅使用首张图片
if (permissionValid && !enableSwitch) {
image_list.clear();
if (ImageEngine::instance()->isImage(path)) {
image_list << path;
}
} else if ((path.indexOf("smb-share:server=") != -1 || path.indexOf("mtp:host=") != -1 || path.indexOf("gphoto2:host=") != -1)) {
image_list.clear();
//判断是否图片格式
if (ImageEngine::instance()->isImage(path)) {
image_list << path;
}
} else if (isMtpProxy) {
// 无需处理,使用默认 image_list
} else {
QString DirPath = image_list.first().left(image_list.first().lastIndexOf("/"));
QDir _dirinit(DirPath);
QFileInfoList m_AllPath = _dirinit.entryInfoList(QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot);
//修复Qt带后缀排序错误的问题
std::sort(m_AllPath.begin(), m_AllPath.end(), compareByFileInfo);
image_list.clear();
for (int i = 0; i < m_AllPath.size(); i++) {
QString tmpPath = m_AllPath.at(i).filePath();
if (tmpPath.isEmpty()) {
continue;
}
//判断是否图片格式
if (ImageEngine::instance()->isImage(tmpPath)) {
image_list << tmpPath;
}
}
}
if (image_list.count() > 0) {
bRet = true;
} else {
qWarning() << "No valid images found in directory";
bRet = false;
}
//解决拖入非图片文件会出现崩溃
QString loadingPath = "";
if (image_list.contains(path)) {
loadingPath = path;