Debian Codegraph @

src/album/widgets/dialogs/imgdeletedialog.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "imgdeletedialog.h"
22
23 #include <QVBoxLayout>
24
25 #include <DLabel>
26 #include <DPushButton>
27 #include <DFontSizeManager>
28 #include <DApplicationHelper>
29 #include <DSuggestButton>
30 #include <DTitlebar>
31
32 ImgDeleteDialog::ImgDeleteDialog(DWidget *parent, int imgCount, int videoCount)
33 : DDialog(parent)
34 {
35 setModal(true);
36 setContentsMargins(0, 0, 0, 0);
37 QIcon icon = QIcon::fromTheme("deepin-album");
38 this->setIcon(icon);
39 DLabel *m_label = new DLabel(this);
40
41 if (imgCount > 0 && videoCount == 0) {
42 //仅删除图片
43 if (1 == imgCount) {
44 m_label->setText(tr("Are you sure you want to delete this photo from the album?"));
45 } else {
46 QString str = tr("Are you sure you want to delete %n photos from albums?", "", imgCount);
47 m_label->setText(str);
48 }
49 } else if (imgCount == 0 && videoCount > 0) {
50 //仅删除视频
51 if (1 == videoCount) {
52 m_label->setText(tr("Are you sure you want to delete this video from the album?"));
53 } else {
54 QString str = tr("Are you sure you want to delete %n videos from albums?", "", videoCount);
55 m_label->setText(str);
56 }
57 } else if (imgCount > 0 && videoCount > 0) {
58 //删除视频和图片
59 QString str = tr("Are you sure you want to delete %n items from albums?", "", (imgCount + videoCount));
60 m_label->setText(str);
61 }
62
63 m_label->setWordWrap(true);
64 m_label->setAlignment(Qt::AlignHCenter);
65 DWidget *contentWidget = new DWidget(this);
66 contentWidget->setFixedHeight(this->height() - 80);
67 contentWidget->setContentsMargins(0, 0, 0, 0);
68 QVBoxLayout *layout = new QVBoxLayout(contentWidget);
69 layout->setContentsMargins(0, 0, 0, 0);
70 layout->setSpacing(8);
71 layout->addStretch();
72 layout->addWidget(m_label);
73 layout->addStretch();
74 addContent(contentWidget);
75 insertButton(0, tr("Cancel"), false, DDialog::ButtonNormal);
76 insertButton(1, tr("Delete"), true, DDialog::ButtonWarning);
77
78 getButton(0)->setFocusPolicy(Qt::TabFocus);
79 getButton(1)->setFocusPolicy(Qt::TabFocus);
80 getButton(0)->installEventFilter(this);
81 getButton(1)->installEventFilter(this);
82
83 QWidget *closeButton = this->findChild<QWidget *>("DTitlebarDWindowCloseButton");
84 closeButton->setFocusPolicy(Qt::TabFocus);
85 closeButton->installEventFilter(this);
86
87 //剔除Titlebar焦点
88 DTitlebar *titlebar = findChild<DTitlebar *>();
89 if (titlebar) {
90 titlebar->setFocusPolicy(Qt::ClickFocus);
91 }
92
93 m_allTabOrder.clear();
94 m_allTabOrder.insert(0, closeButton);
95 m_allTabOrder.insert(1, getButton(0));
96 m_allTabOrder.insert(2, getButton(1));
97 }
98
99 bool ImgDeleteDialog::eventFilter(QObject *obj, QEvent *event)
100 {
101 if (event->type() == QEvent::KeyPress) {
102 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
103 if (keyEvent->key() == Qt::Key_Tab) {
104 if (getButton(0) == obj) {
105 getButton(1)->setFocus();
106 return true;
107 } else if (getButton(1) == obj) {
108 m_allTabOrder.at(0)->setFocus();
109 return true;
110 } else if (obj->objectName() == "DTitlebarDWindowCloseButton") {
111 getButton(0)->setFocus();
112 return true;
113 } else {
114 m_allTabOrder.at(0)->setFocus();
115 return true;
116 }
117 }
118 }
119 return QWidget::eventFilter(obj, event);
120 }