Debian Codegraph @

src/album/widgets/albumlefttabitem.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 "widgets/albumlefttabitem.h"
22 #include "dbmanager/dbmanager.h"
23 #include "utils/baseutils.h"
24 #include "../dialogs/albumcreatedialog.h"
25 #include "application.h"
26 #include <QHBoxLayout>
27 #include "controller/signalmanager.h"
28 #include <DFontSizeManager>
29 #include <QPainter>
30
31 #include "ac-desktop-define.h"
32
33 namespace {
34 const int OPE_MODE_ADDNEWALBUM = 0;
35 const int OPE_MODE_RENAMEALBUM = 1;
36 const int OPE_MODE_ADDRENAMEALBUM = 2;
37 }// namespace
38
39 using namespace utils::common;
40
41 AlbumLeftTabItem::AlbumLeftTabItem(QString str, QString strAlbumType)
42 : m_albumNameStr(str), m_albumTypeStr(strAlbumType), m_opeMode(0)
43 , m_pLineEdit(nullptr), m_pNewLineEdit(nullptr), m_nameLabel(nullptr)
44 , pImageLabel(nullptr), m_unMountBtn(nullptr), m_pListWidget(nullptr)
45 , m_pListWidgetItem(nullptr)
46 {
47 initUI();
48 initConnections();
49 }
50
51 AlbumLeftTabItem::~AlbumLeftTabItem()
52 {
53
54 }
55
56 void AlbumLeftTabItem::initConnections()
57 {
58 connect(m_pLineEdit, &DLineEdit::editingFinished, this, &AlbumLeftTabItem::onCheckNameValid);
59 connect(m_unMountBtn, &MountExternalBtn::sigMountExternalBtnClicked, this, &AlbumLeftTabItem::unMountBtnClicked);
60 connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, m_nameLabel, [ = ] {
61 oriAlbumStatus();
62 });
63 }
64
65 void AlbumLeftTabItem::initUI()
66 {
67 setFocusPolicy(Qt::NoFocus);
68 setFixedSize(160, 40);
69 QHBoxLayout *pHBoxLayout = new QHBoxLayout(this);
70 pHBoxLayout->setContentsMargins(15, 0, 0, 0);
71 pHBoxLayout->setSpacing(0);
72
73 pImageLabel = new DLabel();
74 pImageLabel->setFixedSize(24, 24);
75 QPixmap pixmap;
76 if (ALBUM_PATHTYPE_BY_PHONE == m_albumTypeStr) {
77 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_iphone_normal.svg", QSize(24, 24));
78 } else if (ALBUM_PATHTYPE_BY_U == m_albumTypeStr) {
79 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
80 if (themeType == DGuiApplicationHelper::LightType) {
81 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_usb_normal.svg", QSize(24, 24));
82 } else if (themeType == DGuiApplicationHelper::DarkType) {
83 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_usb_normal_dark.svg", QSize(24, 24));
84 }
85 } else if (COMMON_STR_CUSTOM == m_albumTypeStr || COMMON_STR_CREATEALBUM == m_albumTypeStr) {
86 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal.svg", QSize(24, 24));
87 } else if (COMMON_STR_RECENT_IMPORTED == m_albumNameStr) {
88 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_import_normal.svg", QSize(24, 24));
89 } else if (COMMON_STR_TRASH == m_albumNameStr) {
90 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_trash_normal.svg", QSize(24, 24));
91 } else if (COMMON_STR_FAVORITES == m_albumNameStr) {
92 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_collection_normal.svg", QSize(24, 24));
93 } else {
94 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal.svg", QSize(24, 24));
95
96 }
97 pImageLabel->setPixmap(pixmap);
98
99 DWidget *pWidget = new DWidget();
100
101 // QWidget *widget = m_pListWidget->itemDelegate()->createEditor(m_pListWidget, m_pListWidget->viewOptions(), m_pListWidget->getModelIndex(m_pListWidgetItem));
102 // m_pLineEdit = dynamic_cast<QLineEdit*>(widget);
103 // m_pLineEdit = dynamic_cast<DLineEdit*>(widget);
104
105 m_pLineEdit = new DLineEdit();
106
107 if (!m_pLineEdit) {
108 qDebug() << "Create LineEdit Error!!!";
109 return;
110 }
111
112 m_nameLabel = new DDlabel(pWidget);
113 m_nameLabel->setGeometry(QRect(16, 0, 118, 40));
114
115 QFontMetrics elideFont(m_nameLabel->font());
116 if (COMMON_STR_RECENT_IMPORTED == m_albumNameStr) {
117 DFontSizeManager::instance()->bind(m_nameLabel, DFontSizeManager::T6, Qt::ElideRight);
118 m_nameLabel->Settext(tr("Import"));
119 AC_SET_OBJECT_NAME(m_nameLabel, Album_Import_Label);
120 AC_SET_ACCESSIBLE_NAME(m_nameLabel, Album_Import_Label);
121 } else if (COMMON_STR_TRASH == m_albumNameStr) {
122 DFontSizeManager::instance()->bind(m_nameLabel, DFontSizeManager::T6, Qt::ElideRight);
123 m_nameLabel->Settext(tr("Trash"));
124 AC_SET_OBJECT_NAME(m_nameLabel, Album_Trash_Label);
125 AC_SET_ACCESSIBLE_NAME(m_nameLabel, Album_Trash_Label);
126 } else if (COMMON_STR_FAVORITES == m_albumNameStr) {
127 DFontSizeManager::instance()->bind(m_nameLabel, DFontSizeManager::T6, Qt::ElideRight);
128 m_nameLabel->Settext(tr("Favorites"));
129 AC_SET_OBJECT_NAME(m_nameLabel, Album_Fav_Label);
130 AC_SET_ACCESSIBLE_NAME(m_nameLabel, Album_Fav_Label);
131 } else {
132 DFontSizeManager::instance()->bind(m_nameLabel, DFontSizeManager::T6, Qt::ElideRight);
133 m_nameLabel->Settext(m_albumNameStr);
134 AC_SET_OBJECT_NAME(m_nameLabel, m_albumNameStr);
135 AC_SET_ACCESSIBLE_NAME(m_nameLabel, m_albumNameStr);
136 }
137 m_nameLabel->setAlignment(Qt::AlignVCenter);
138
139 QFont ft = DFontSizeManager::instance()->get(DFontSizeManager::T6);
140 ft.setFamily("SourceHanSansSC");
141 ft.setWeight(QFont::Medium);
142 m_nameLabel->setFont(ft);
143
144 // DPalette pa = DApplicationHelper::instance()->palette(m_nameLabel);
145 // pa.setBrush(DPalette::Text, pa.color(DPalette::ToolTipText));
146 m_nameLabel->setForegroundRole(DPalette::TextTitle);
147 // m_nameLabel->setPalette(pa);
148
149 m_pLineEdit->setParent(pWidget);
150 m_pLineEdit->setGeometry(QRect(0, 0, 120, 40));
151 if (COMMON_STR_RECENT_IMPORTED == m_albumNameStr) {
152 m_pLineEdit->setText(tr("Import"));
153 } else if (COMMON_STR_TRASH == m_albumNameStr) {
154 m_pLineEdit->setText(tr("Trash"));
155 } else if (COMMON_STR_FAVORITES == m_albumNameStr) {
156 m_pLineEdit->setText(tr("Favorites"));
157 } else {
158 m_pLineEdit->setText(m_albumNameStr);
159 }
160
161 // m_pLineEdit->setTextMargins(5,0,0,0);
162 m_pLineEdit->lineEdit()->setTextMargins(5, 0, 0, 0);
163 // m_pLineEdit->setAlignment(Qt::AlignVCenter| Qt::AlignLeft);
164 m_pLineEdit->lineEdit()->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
165
166 m_pLineEdit->setVisible(false);
167 // m_pLineEdit->setMaxLength(64);
168 m_pLineEdit->lineEdit()->setMaxLength(64);
169
170 m_pLineEdit->setClearButtonEnabled(false);
171
172 pHBoxLayout->addWidget(pImageLabel, Qt::AlignVCenter);
173 pHBoxLayout->addWidget(pWidget, Qt::AlignVCenter);
174
175
176 m_unMountBtn = new MountExternalBtn(m_nameLabel);
177 //外部设备插入,需要添加卸载按钮
178 if (ALBUM_PATHTYPE_BY_PHONE == m_albumTypeStr || ALBUM_PATHTYPE_BY_U == m_albumTypeStr) {
179 QPixmap pixmap;
180 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_exit_normal.svg", QSize(24, 24));
181 m_unMountBtn->setPixmap(pixmap);
182 pHBoxLayout->addWidget(m_unMountBtn);
183 //m_unMountBtn->move(92, 9);
184 }
185 this->setLayout(pHBoxLayout);
186 }
187
188 void AlbumLeftTabItem::unMountBtnClicked()
189 {
190 emit unMountExternalDevices(m_mountPath);
191 }
192
193 void AlbumLeftTabItem::editAlbumEdit()
194 {
195 m_nameLabel->setVisible(false);
196 m_pLineEdit->setVisible(true);
197 // m_pLineEdit->selectAll();
198 m_pLineEdit->lineEdit()->selectAll();
199 // m_pLineEdit->setFocus();
200 m_pLineEdit->lineEdit()->setFocus();
201 }
202
203 //设置外部设备挂载的path,在相册中卸载外部设备时用(如果有两个外部设置挂载点name一样,就不能使用name做卸载判断,使用path没问题)
204 void AlbumLeftTabItem::setExternalDevicesMountPath(QString strPath)
205 {
206 m_mountPath = strPath;
207 }
208
209 QString AlbumLeftTabItem::getalbumname()
210 {
211 return m_nameLabel->text();
212 }
213
214 void AlbumLeftTabItem::onCheckNameValid()
215 {
216 QString newNameStr = m_pLineEdit->lineEdit()->text().trimmed();
217 if (newNameStr.isEmpty()) {
218 newNameStr = AlbumCreateDialog::getNewAlbumName("", true, m_nameLabel->text());
219 }
220 if (OPE_MODE_RENAMEALBUM == m_opeMode || OPE_MODE_ADDRENAMEALBUM == m_opeMode) {
221 if (newNameStr != m_nameLabel->oldstr && DBManager::instance()->isAlbumExistInDB(newNameStr)) {
222 newNameStr = AlbumCreateDialog::getNewAlbumName(newNameStr, true, m_nameLabel->text());
223 }
224 QFontMetrics elideFont(m_nameLabel->font());
225 m_nameLabel->Settext(elideFont.elidedText(newNameStr, Qt::ElideRight, 85));
226 m_nameLabel->oldstr = newNameStr;
227 QFont ft;
228 ft.setPixelSize(14);
229
230 m_pLineEdit->setText(newNameStr);
231 m_nameLabel->setVisible(true);
232 m_pLineEdit->setVisible(false);
233
234 DBManager::instance()->renameAlbum(m_albumNameStr, newNameStr);
235
236 m_albumNameStr = newNameStr;
237 emit dApp->signalM->sigUpdataAlbumRightTitle(m_albumNameStr);
238 } else if (OPE_MODE_ADDNEWALBUM == m_opeMode) {
239 // m_nameLabel->setText(newNameStr);
240 if (newNameStr != m_nameLabel->text() && DBManager::instance()->isAlbumExistInDB(newNameStr)) {
241 newNameStr = AlbumCreateDialog::getNewAlbumName(newNameStr);
242 }
243 QFontMetrics elideFont(m_nameLabel->font());
244 m_nameLabel->Settext(elideFont.elidedText(newNameStr, Qt::ElideRight, 85));
245 m_nameLabel->oldstr = newNameStr;
246 QFont ft;
247 ft.setPixelSize(14);
248
249 m_pLineEdit->setText(newNameStr);
250
251 m_nameLabel->setVisible(true);
252 m_pLineEdit->setVisible(false);
253
254 DBManager::instance()->insertIntoAlbum(newNameStr, QStringList(" "));
255
256 m_albumNameStr = newNameStr;
257 emit dApp->signalM->sigUpdataAlbumRightTitle(m_albumNameStr);
258 }
259 }
260
261 void AlbumLeftTabItem::oriAlbumStatus()
262 {
263 if (ALBUM_PATHTYPE_BY_PHONE == m_albumTypeStr) {
264 QPixmap pixmap;
265 QPixmap mountpixmap;
266
267 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
268 if (themeType == DGuiApplicationHelper::LightType) {
269 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_iphone_normal.svg", QSize(24, 24));
270 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_exit_normal.svg", QSize(24, 24));
271 }
272 if (themeType == DGuiApplicationHelper::DarkType) {
273 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_iphone_normal_dark.svg", QSize(24, 24));
274 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_exit_normal_dark.svg", QSize(24, 24));
275 }
276
277 pImageLabel->setPixmap(pixmap);
278 m_unMountBtn->setPixmap(mountpixmap);
279 } else if (ALBUM_PATHTYPE_BY_U == m_albumTypeStr) {
280 QPixmap pixmap;
281 QPixmap mountpixmap;
282
283 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
284 if (themeType == DGuiApplicationHelper::LightType) {
285 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_usb_normal.svg", QSize(24, 24));
286 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_exit_normal.svg", QSize(24, 24));
287 }
288 if (themeType == DGuiApplicationHelper::DarkType) {
289 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_usb_normal_dark.svg", QSize(24, 24));
290 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_exit_normal_dark.svg", QSize(24, 24));
291 }
292
293 pImageLabel->setPixmap(pixmap);
294 m_unMountBtn->setPixmap(mountpixmap);
295 } else if (COMMON_STR_CUSTOM == m_albumTypeStr || COMMON_STR_CREATEALBUM == m_albumTypeStr) {
296 QPixmap pixmap;
297 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
298 if (themeType == DGuiApplicationHelper::LightType) {
299 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal.svg", QSize(24, 24));
300 }
301 if (themeType == DGuiApplicationHelper::DarkType) {
302 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal_dark.svg", QSize(24, 24));
303 }
304 pImageLabel->setPixmap(pixmap);
305 } else if (COMMON_STR_RECENT_IMPORTED == m_albumNameStr) {
306 QPixmap pixmap;
307 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
308 if (themeType == DGuiApplicationHelper::LightType) {
309 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_import_normal.svg", QSize(24, 24));
310 }
311 if (themeType == DGuiApplicationHelper::DarkType) {
312 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_import_normal_dark.svg", QSize(24, 24));
313 }
314 pImageLabel->setPixmap(pixmap);
315
316 } else if (COMMON_STR_TRASH == m_albumNameStr) {
317 QPixmap pixmap;
318 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
319 if (themeType == DGuiApplicationHelper::LightType) {
320 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_trash_normal.svg", QSize(24, 24));
321 }
322 if (themeType == DGuiApplicationHelper::DarkType) {
323 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_trash_normal_dark.svg", QSize(24, 24));
324 }
325 pImageLabel->setPixmap(pixmap);
326
327 } else if (COMMON_STR_FAVORITES == m_albumNameStr) {
328 QPixmap pixmap;
329 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
330 if (themeType == DGuiApplicationHelper::LightType) {
331 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_collection_normal.svg", QSize(24, 24));
332 }
333 if (themeType == DGuiApplicationHelper::DarkType) {
334 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_collection_normal_dark.svg", QSize(24, 24));
335 }
336 pImageLabel->setPixmap(pixmap);
337
338 } else {
339 QPixmap pixmap;
340 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
341 if (themeType == DGuiApplicationHelper::LightType) {
342 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal.svg", QSize(24, 24));
343 }
344 if (themeType == DGuiApplicationHelper::DarkType) {
345 pixmap = utils::base::renderSVG(":/resources/images/sidebar/normal/icon_album_normal_dark.svg", QSize(24, 24));
346 }
347 pImageLabel->setPixmap(pixmap);
348 }
349
350 m_nameLabel->setForegroundRole(DPalette::TextTitle);
351 }
352
353 void AlbumLeftTabItem::newAlbumStatus()
354 {
355 QPixmap pixmap;
356 if (ALBUM_PATHTYPE_BY_PHONE == m_albumTypeStr) {
357 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_iphone_active.svg", QSize(24, 24));
358
359 QPixmap mountpixmap;
360 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_exit_active.svg", QSize(24, 24));
361 m_unMountBtn->setPixmap(mountpixmap);
362 } else if (ALBUM_PATHTYPE_BY_U == m_albumTypeStr) {
363 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_usb_active.svg", QSize(24, 24));
364
365 QPixmap mountpixmap;
366 mountpixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_exit_active.svg", QSize(24, 24));
367 m_unMountBtn->setPixmap(mountpixmap);
368 } else if (COMMON_STR_CUSTOM == m_albumTypeStr || COMMON_STR_CREATEALBUM == m_albumTypeStr) {
369 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_album_active.svg", QSize(24, 24));
370 } else if (COMMON_STR_RECENT_IMPORTED == m_albumNameStr) {
371 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_import_active.svg", QSize(24, 24));
372 } else if (COMMON_STR_TRASH == m_albumNameStr) {
373 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_trash_active.svg", QSize(24, 24));
374 } else if (COMMON_STR_FAVORITES == m_albumNameStr) {
375 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_collection_active.svg", QSize(24, 24));
376 } else {
377 pixmap = utils::base::renderSVG(":/resources/images/sidebar/active/icon_album_active.svg", QSize(24, 24));
378 }
379 pImageLabel->setPixmap(pixmap);
380 m_nameLabel->setForegroundRole(DPalette::HighlightedText);
381 }
382