Debian Codegraph @

src/album/searchview/searchview.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 "searchview.h"
22 #include <DApplicationHelper>
23 #include "imageengine/imageengineapi.h"
24 #include <QGraphicsDropShadowEffect>
25 #include <QPainter>
26 #include <QDebug>
27
28 SearchView::SearchView()
29 : m_stackWidget(nullptr), m_pNoSearchResultView(nullptr), m_pNoSearchResultLabel(nullptr)
30 , m_searchResultViewbody(nullptr), m_searchResultViewTop(nullptr)
31 , m_pSlideShowBtn(nullptr), m_pSearchResultLabel(nullptr), pNoResult(nullptr)
32 , pLabel1(nullptr), m_searchPicNum(0), m_pThumbnailListView(nullptr)
33 {
34 initNoSearchResultView();
35 initSearchResultView();
36 initMainStackWidget();
37 initConnections();
38 }
39
40 void SearchView::initConnections()
41 {
42 qRegisterMetaType<DBImgInfoList>("DBImgInfoList &");
43 connect(m_pSlideShowBtn, &DPushButton::clicked, this, &SearchView::onSlideShowBtnClicked);
44 connect(dApp->signalM, &SignalManager::sigSendKeywordsIntoALLPic, this, &SearchView::improtSearchResultsIntoThumbnailView);
45 connect(m_pThumbnailListView, &ThumbnailListView::openImage, this, &SearchView::onOpenImage);
46 connect(m_pThumbnailListView, &ThumbnailListView::sigSlideShow, this, &SearchView::onSlideShow);
47 connect(dApp->signalM, &SignalManager::sigUpdateImageLoader, this, &SearchView::updateSearchResultsIntoThumbnailView);
48 connect(dApp->signalM, &SignalManager::imagesInserted, this, &SearchView::updateSearchResultsIntoThumbnailView);
49 connect(dApp->signalM, &SignalManager::imagesRemoved, this, &SearchView::updateSearchResultsIntoThumbnailView);
50 connect(DApplicationHelper::instance(), &DApplicationHelper::themeTypeChanged, this, &SearchView::changeTheme);
51 connect(dApp->signalM, &SignalManager::sigShortcutKeyDelete, this, &SearchView::onKeyDelete);
52 }
53
54 void SearchView::initNoSearchResultView()
55 {
56 m_pNoSearchResultView = new DWidget();
57 QVBoxLayout *pNoSearchResultLayout = new QVBoxLayout();
58 pNoResult = new DLabel();
59 pNoResult->setText(tr("No search results"));
60 pNoResult->setFont(DFontSizeManager::instance()->get(DFontSizeManager::T4));
61 DPalette palette = DApplicationHelper::instance()->palette(pNoResult);
62 QColor color_TTT = palette.color(DPalette::ToolTipText);
63 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
64 if (themeType == DGuiApplicationHelper::LightType) {
65 color_TTT.setAlphaF(0.3);
66 palette.setBrush(DPalette::Text, color_TTT);
67 pNoResult->setForegroundRole(DPalette::Text);
68 pNoResult->setPalette(palette);
69 } else if (themeType == DGuiApplicationHelper::DarkType) {
70 color_TTT.setAlphaF(0.4);
71 palette.setBrush(DPalette::Text, color_TTT);
72 pNoResult->setForegroundRole(DPalette::Text);
73 pNoResult->setPalette(palette);
74 }
75
76 m_pNoSearchResultLabel = new DLabel();
77 pNoSearchResultLayout->addStretch();
78 pNoSearchResultLayout->addWidget(pNoResult, 0, Qt::AlignCenter);
79 pNoSearchResultLayout->addSpacing(10);
80 pNoSearchResultLayout->addWidget(m_pNoSearchResultLabel, 0, Qt::AlignCenter);
81 pNoSearchResultLayout->addStretch();
82 m_pNoSearchResultView->setLayout(pNoSearchResultLayout);
83 }
84
85 void SearchView::initSearchResultView()
86 {
87 m_pSearchResultWidget = new DWidget();
88 pLabel1 = new DLabel();
89 pLabel1->setText(tr("Search results"));
90 QFont font = DFontSizeManager::instance()->get(DFontSizeManager::T3);
91 font.setWeight(QFont::DemiBold);
92 pLabel1->setFont(font);
93 DPalette pa = DApplicationHelper::instance()->palette(pLabel1);
94 pa.setBrush(DPalette::Text, pa.color(DPalette::ToolTipText));
95 pLabel1->setForegroundRole(DPalette::Text);
96 pLabel1->setPalette(pa);
97 pLabel1->setContentsMargins(0, 0, 0, 0);
98
99 QHBoxLayout *pHBoxLayout = new QHBoxLayout();
100 pHBoxLayout->setSpacing(5);
101 //LMH0417 bug号20706
102 pHBoxLayout->setContentsMargins(0, 0, 0, 15);
103
104 m_pSlideShowBtn = new DPushButton(this);
105 m_pSlideShowBtn ->setFocusPolicy(Qt::NoFocus);
106 auto playAllPalette = m_pSlideShowBtn->palette();
107 playAllPalette.setColor(DPalette::ButtonText, Qt::white);
108 playAllPalette.setColor(DPalette::Dark, QColor("#FD5E5E"));
109 playAllPalette.setColor(DPalette::Light, QColor("#ED5656"));
110 m_pSlideShowBtn->setPalette(playAllPalette);
111 m_pSlideShowBtn->setIcon(QIcon::fromTheme("album_slider_show"));
112 // m_pSlideShowBtn->setObjectName(Search_Slider_Show_Button);
113 m_pSlideShowBtn->setText(tr("Slide Show"));
114 m_pSlideShowBtn->setFixedHeight(30);
115 m_pSlideShowBtn->setIconSize(QSize(18, 18));
116 DFontSizeManager::instance()->bind(m_pSlideShowBtn, DFontSizeManager::T6, QFont::Medium);
117
118
119 m_pSearchResultLabel = new DLabel();
120 m_pSearchResultLabel->setContentsMargins(0, 0, 0, 0);
121 pHBoxLayout->addWidget(m_pSlideShowBtn);
122 pHBoxLayout->addSpacing(5);
123 pHBoxLayout->addWidget(m_pSearchResultLabel);
124 pHBoxLayout->addStretch(0);
125
126
127 m_searchResultViewTop = new DWidget(m_pSearchResultWidget);
128
129 QGraphicsOpacityEffect *opacityEffect_light = new QGraphicsOpacityEffect;
130 opacityEffect_light->setOpacity(0.95);
131 m_searchResultViewTop->setGraphicsEffect(opacityEffect_light);
132 m_searchResultViewTop->setAutoFillBackground(true);
133 m_searchResultViewTop->setBackgroundRole(DPalette::Window);
134
135 m_searchResultViewbody = new DWidget(m_pSearchResultWidget);
136
137 QVBoxLayout *pSearchResultbodyLayout = new QVBoxLayout();
138 pSearchResultbodyLayout->setContentsMargins(8, 0, 0, 0);//搜索界面各边距
139 //LMH0417 bug号20706
140 m_pThumbnailListView = new ThumbnailListView(ThumbnailDelegate::SearchViewType, COMMON_STR_SEARCH);
141
142 m_pThumbnailListView->setFrameShape(QListView::NoFrame);
143
144 pSearchResultbodyLayout->addWidget(m_pThumbnailListView);
145 // zy 给搜索标题一个布局,将label固定在左侧,避免维语显示在右侧,bug69661
146 QVBoxLayout *pSearchResultLayout = new QVBoxLayout();
147 QHBoxLayout *searchlabellayout = new QHBoxLayout();
148 searchlabellayout->setContentsMargins(0, 0, 0, 0);
149 searchlabellayout->addWidget(pLabel1);
150 searchlabellayout->addStretch();
151 QWidget *wi = new QWidget();
152 wi->setContentsMargins(0, 0, 0, 0);
153 wi->setLayout(searchlabellayout);
154
155 pSearchResultLayout->setContentsMargins(13, 0, 0, 0);
156 pSearchResultLayout->setSpacing(0);
157 pSearchResultLayout->addSpacing(5);
158 pSearchResultLayout->addWidget(wi);
159 pSearchResultLayout->addSpacing(5);
160 pSearchResultLayout->addItem(pHBoxLayout);
161
162 m_searchResultViewTop->setFixedHeight(90);
163 m_searchResultViewTop->move(0, 0);
164 m_searchResultViewbody->setLayout(pSearchResultbodyLayout);
165 m_searchResultViewTop->setLayout(pSearchResultLayout);
166 m_searchResultViewTop->raise();
167 }
168
169 void SearchView::initMainStackWidget()
170 {
171 m_stackWidget = new DStackedWidget();
172 m_stackWidget->setContentsMargins(0, 0, 0, 0);
173 m_stackWidget->addWidget(m_pNoSearchResultView);
174 m_stackWidget->addWidget(m_pSearchResultWidget);
175
176 QLayout *layout = new QVBoxLayout(this);
177 layout->setContentsMargins(0, 0, 0, 0);
178 layout->addWidget(m_stackWidget);
179 }
180
181 void SearchView::improtSearchResultsIntoThumbnailView(QString s, const QString &album)
182 {
183 m_albumName = album;
184 using namespace utils::image;
185 m_keywords = s;
186 QList<DBImgInfo> thumbnaiItemList;
187 DBImgInfoList infos;
188 if (COMMON_STR_ALLPHOTOS == m_albumName
189 || COMMON_STR_TIMELINE == m_albumName
190 || COMMON_STR_RECENT_IMPORTED == m_albumName) {
191 infos = DBManager::instance()->getInfosForKeyword(s);
192 } else if (COMMON_STR_TRASH == m_albumName) {
193 infos = DBManager::instance()->getTrashInfosForKeyword(s);
194 } else {
195 infos = DBManager::instance()->getInfosForKeyword(m_albumName, s);
196 }
197
198 if (0 < infos.length()) {
199 m_pThumbnailListView->clearAll();
200 //插入空白项
201 m_pThumbnailListView->insertBlankOrTitleItem(ItemTypeBlank, "", "", 90);
202 //插入信息
203 m_pThumbnailListView->insertThumbnailByImgInfos(infos);
204 int photoCount = m_pThumbnailListView->getAppointTypeItemCount(ItemTypePic);
205 int videoCount = m_pThumbnailListView->getAppointTypeItemCount(ItemTypeVideo);
206 QString searchStr;
207 if (photoCount > 0 && videoCount == 0) {
208 if (photoCount == 1) {
209 searchStr = tr("1 photo found");
210 } else {
211 searchStr = tr("%n photos found", "", photoCount);
212 }
213 setSliderShowBtnEnable(true);
214 } else if (photoCount == 0 && videoCount > 0) {
215 if (videoCount == 1) {
216 searchStr = tr("1 video found");
217 } else {
218 searchStr = tr("%n videos found", "", videoCount);
219 }
220 setSliderShowBtnEnable(false);
221 } else if (photoCount > 0 && videoCount > 0) {
222 searchStr = tr("%n items found", "", (photoCount + videoCount));
223 setSliderShowBtnEnable(true);
224 }
225 QString str = QString::number(infos.length());
226 m_searchPicNum = photoCount + videoCount;
227 m_pSearchResultLabel->setText(searchStr);
228 m_pSearchResultLabel->setFont(DFontSizeManager::instance()->get(DFontSizeManager::T6));
229 DPalette palette = DApplicationHelper::instance()->palette(m_pSearchResultLabel);
230 QColor color_BT = palette.color(DPalette::BrightText);
231 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
232 if (themeType == DGuiApplicationHelper::LightType) {
233 color_BT.setAlphaF(0.5);
234 palette.setBrush(DPalette::Text, color_BT);
235 m_pSearchResultLabel->setForegroundRole(DPalette::Text);
236 m_pSearchResultLabel->setPalette(palette);
237 } else if (themeType == DGuiApplicationHelper::DarkType) {
238 color_BT.setAlphaF(0.75);
239 palette.setBrush(DPalette::Text, color_BT);
240 m_pSearchResultLabel->setForegroundRole(DPalette::Text);
241 m_pSearchResultLabel->setPalette(palette);
242 }
243
244 m_stackWidget->setCurrentIndex(1);
245 } else {
246 m_searchPicNum = 0;
247 m_stackWidget->setCurrentIndex(0);
248 }
249 }
250
251 void SearchView::onSlideShowBtnClicked()
252 {
253 DBImgInfoList imagelist;
254 if (COMMON_STR_ALLPHOTOS == m_albumName
255 || COMMON_STR_TIMELINE == m_albumName
256 || COMMON_STR_RECENT_IMPORTED == m_albumName) {
257 imagelist = DBManager::instance()->getInfosForKeyword(m_keywords);
258 } else if (COMMON_STR_TRASH == m_albumName) {
259 imagelist = DBManager::instance()->getTrashInfosForKeyword(m_keywords);
260 } else {
261 imagelist = DBManager::instance()->getInfosForKeyword(m_albumName, m_keywords);
262 }
263
264 QStringList paths;
265 for (auto image : imagelist) {
266 paths << image.filePath;
267 }
268
269 QString path = "";
270 if (paths.size() > 0) {
271 path = paths.first();
272 onSlideShow(path);
273 }
274 }
275
276 void SearchView::onOpenImage(int row, const QString &path, bool bFullScreen)
277 {
278 SignalManager::ViewInfo info;
279 info.album = "";
280 info.lastPanel = nullptr;
281 info.fullScreen = bFullScreen;
282 auto imagelist = m_pThumbnailListView->getFileList(row);
283 if (imagelist.size() > 0) {
284 info.paths << imagelist;
285 info.path = path;
286 } else {
287 info.paths.clear();
288 }
289 info.dBImgInfos = m_pThumbnailListView->getAllFileInfo(row);
290 info.viewType = utils::common::VIEW_SEARCH_SRN;
291
292 emit dApp->signalM->viewImage(info);
293
294 if (COMMON_STR_ALLPHOTOS == m_albumName) {
295 emit dApp->signalM->showImageView(0);
296 } else if (COMMON_STR_TIMELINE == m_albumName) {
297 emit dApp->signalM->showImageView(1);
298 } else {
299 emit dApp->signalM->showImageView(2);
300 }
301 }
302
303 void SearchView::onSlideShow(const QString &path)
304 {
305 SignalManager::ViewInfo info;
306 info.album = "";
307 info.lastPanel = nullptr;
308 auto photolist = m_pThumbnailListView->selectedPaths();
309 if (photolist.size() > 1) {
310 //如果选中数目大于1,则幻灯片播放选中项
311 info.paths = photolist;
312 info.path = photolist.at(0);
313 } else {
314 //如果选中项只有一项,则幻灯片播放全部
315 info.paths = m_pThumbnailListView->getFileList();
316 info.path = path;
317 }
318 info.fullScreen = true;
319 info.slideShow = true;
320 info.viewType = utils::common::VIEW_SEARCH_SRN;
321 emit dApp->signalM->startSlideShow(info);
322
323 if (COMMON_STR_ALLPHOTOS == m_albumName) {
324 emit dApp->signalM->showSlidePanel(0);
325 } else if (COMMON_STR_TIMELINE == m_albumName) {
326 emit dApp->signalM->showSlidePanel(1);
327 } else {
328 emit dApp->signalM->showSlidePanel(2);
329 }
330 }
331
332 void SearchView::updateSearchResultsIntoThumbnailView()
333 {
334 improtSearchResultsIntoThumbnailView(m_keywords, m_albumName);
335 }
336
337 void SearchView::changeTheme()
338 {
339
340 DPalette pale = DApplicationHelper::instance()->palette(pLabel1);
341 pale.setBrush(DPalette::Text, pale.color(DPalette::ToolTipText));
342 pLabel1->setPalette(pale);
343
344 DPalette pa = DApplicationHelper::instance()->palette(pNoResult);
345 QColor color_TTT = pa.color(DPalette::ToolTipText);
346 DPalette pat = DApplicationHelper::instance()->palette(m_pSearchResultLabel);
347 QColor color_BT = pat.color(DPalette::BrightText);
348
349 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
350 if (themeType == DGuiApplicationHelper::LightType) {
351 color_TTT.setAlphaF(0.3);
352 pa.setBrush(DPalette::Text, color_TTT);
353 pNoResult->setPalette(pa);
354 m_pNoSearchResultLabel->setPalette(pa);
355
356 color_BT.setAlphaF(0.5);
357 pat.setBrush(DPalette::Text, color_BT);
358 m_pSearchResultLabel->setPalette(pat);
359
360 auto playAllPalette = m_pSlideShowBtn->palette();
361 playAllPalette.setColor(DPalette::ButtonText, Qt::white);
362 playAllPalette.setColor(DPalette::Light, QColor("#FD5E5E"));
363 playAllPalette.setColor(DPalette::Dark, QColor("#ED5656"));
364 m_pSlideShowBtn->setPalette(playAllPalette);
365 } else if (themeType == DGuiApplicationHelper::DarkType) {
366 color_TTT.setAlphaF(0.4);
367 pa.setBrush(DPalette::Text, color_TTT);
368 pNoResult->setPalette(pa);
369 m_pNoSearchResultLabel->setPalette(pa);
370
371 color_BT.setAlphaF(0.75);
372 pat.setBrush(DPalette::Text, color_BT);
373 m_pSearchResultLabel->setPalette(pat);
374
375 auto playAllPalette = m_pSlideShowBtn->palette();
376 playAllPalette.setColor(DPalette::ButtonText, "#FFFFFF");
377 playAllPalette.setColor(DPalette::Light, QColor("#DA2D2D"));
378 playAllPalette.setColor(DPalette::Dark, QColor("#A51B1B"));
379 m_pSlideShowBtn->setPalette(playAllPalette);
380 }
381
382 }
383
384 void SearchView::setSliderShowBtnEnable(bool enabled)
385 {
386 if (enabled) {
387 changeTheme();
388 } else {
389 //结果中含视频则置灰
390 auto playAllPalette = m_pSlideShowBtn->palette();
391 playAllPalette.setColor(DPalette::ButtonText, QColor("white"));
392 QColor color = QColor("#F82C47");
393 color.setAlphaF(0.4);
394 playAllPalette.setColor(DPalette::Light, color);
395 playAllPalette.setColor(DPalette::Dark, color);
396 m_pSlideShowBtn->setPalette(playAllPalette);
397 }
398 m_pSlideShowBtn->setEnabled(enabled);
399 }
400
401 void SearchView::paintEvent(QPaintEvent *event)
402 {
403 QFont font;
404 int currentSize = DFontSizeManager::instance()->fontPixelSize(font);
405 if (currentSize != m_currentFontSize) {
406 m_currentFontSize = currentSize;
407 }
408 QWidget::paintEvent(event);
409 m_searchResultViewTop->setFixedWidth(m_pSearchResultWidget->width() - 10);
410 m_searchResultViewbody->setFixedSize(m_pSearchResultWidget->size());
411 }
412
413 void SearchView::resizeEvent(QResizeEvent *e)
414 {
415 QWidget::resizeEvent(e);
416 m_searchResultViewTop->setFixedWidth(m_pSearchResultWidget->width() - 10);
417 m_searchResultViewbody->setFixedSize(m_pSearchResultWidget->size());
418 }
419
420 void SearchView::onKeyDelete()
421 {
422 if (!isVisible()) return;
423
424 QStringList paths;
425 paths.clear();
426
427 paths = m_pThumbnailListView->selectedPaths();
428 if (0 >= paths.length()) {
429 return;
430 }
431 m_pThumbnailListView->clearSelection();
432 ImageEngineApi::instance()->moveImagesToTrash(paths);
433 }
434