Debian Codegraph @

src/album/module/slideshow/slideshowpanel.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 "slideshowpanel.h"
22 #include "application.h"
23 #include "controller/configsetter.h"
24 #include "controller/signalmanager.h"
25 #include "module/view/viewpanel.h"
26 #include "utils/baseutils.h"
27 #include "utils/imageutils.h"
28 #include "utils/unionimage.h"
29 #include "ac-desktop-define.h"
30
31 #include <QResizeEvent>
32 #include <QStyleFactory>
33 #include <QScreen>
34 #include <QDesktopWidget>
35 #include <QGuiApplication>
36
37 namespace {
38 const int DELAY_HIDE_CURSOR_INTERVAL = 3000;
39 const QColor DARK_BG_COLOR = QColor("#252525");
40 const QColor LIGHT_BG_COLOR = QColor("#FFFFFF");
41 const QString SHORTCUTVIEW_GROUP = "SHORTCUTVIEW";
42 const int FROM_MAINWINDOW_POPVIEW = 4; // 从view页面进入幻灯片
43 }
44
45 DWIDGET_USE_NAMESPACE
46 namespace {
47 const QSize ICON_SIZE = QSize(50, 50);
48 const int ICON_SPACING = 10;
49 const int WIDTH = 260;
50 const int HEIGHT = 81;
51 }
52
53 SlideShowBottomBar::SlideShowBottomBar(QWidget *parent) : DFloatingWidget(parent)
54 {
55 setCursor(Qt::ArrowCursor);
56 setFixedSize(WIDTH, HEIGHT);
57 QHBoxLayout *hb = new QHBoxLayout();
58 hb->setContentsMargins(ICON_SPACING, 0, ICON_SPACING, 0);
59 m_preButton = new DIconButton(this);
60 AC_SET_OBJECT_NAME(m_preButton, Slider_Pre_Button);
61 AC_SET_ACCESSIBLE_NAME(m_preButton, Slider_Pre_Button);
62 m_preButton->setFixedSize(ICON_SIZE);
63 m_preButton->setIcon(QIcon::fromTheme("dcc_previous_normal"));
64 m_preButton->setIconSize(QSize(36, 36));
65 m_preButton->setToolTip(tr("Previous"));
66 m_preButton->setFocusPolicy(Qt::NoFocus);
67 hb->addWidget(m_preButton);
68 hb->addSpacing(4);
69 connect(m_preButton, &DIconButton::clicked, this, &SlideShowBottomBar::onPreButtonClicked);
70 m_playpauseButton = new DIconButton(this);
71 m_playpauseButton->setShortcut(Qt::Key_Space);
72 AC_SET_OBJECT_NAME(m_playpauseButton, Slider_Play_Pause_Button);
73 AC_SET_ACCESSIBLE_NAME(m_playpauseButton, Slider_Play_Pause_Button);
74 m_playpauseButton->setFixedSize(ICON_SIZE);
75 m_playpauseButton->setIcon(QIcon::fromTheme("dcc_suspend_normal"));
76 m_playpauseButton->setIconSize(QSize(36, 36));
77 m_playpauseButton->setToolTip(tr("Pause"));
78 m_playpauseButton->setFocusPolicy(Qt::NoFocus);
79 hb->addWidget(m_playpauseButton);
80 hb->addSpacing(4);
81 connect(m_playpauseButton, &DIconButton::clicked, this, &SlideShowBottomBar::onPlaypauseButtonClicked);
82 connect(dApp->signalM, &SignalManager::updatePauseButton, this, &SlideShowBottomBar::onUpdatePauseButton);
83 connect(dApp->signalM, &SignalManager::initSlideShowButton, this, &SlideShowBottomBar::onInitSlideShowButton);
84 m_nextButton = new DIconButton(this);
85 AC_SET_OBJECT_NAME(m_nextButton, Slider_Next_Button);
86 AC_SET_ACCESSIBLE_NAME(m_nextButton, Slider_Next_Button);
87 m_nextButton->setFixedSize(ICON_SIZE);
88 m_nextButton->setIcon(QIcon::fromTheme("dcc_next_normal"));
89 m_nextButton->setIconSize(QSize(36, 36));
90 m_nextButton->setToolTip(tr("Next"));
91 m_nextButton->setFocusPolicy(Qt::NoFocus);
92 hb->addWidget(m_nextButton);
93 hb->addSpacing(4);
94 connect(m_nextButton, &DIconButton::clicked, this, &SlideShowBottomBar::onNextButtonClicked);
95 m_cancelButton = new DIconButton(this);
96 AC_SET_OBJECT_NAME(m_cancelButton, Slider_Exit_Button);
97 AC_SET_ACCESSIBLE_NAME(m_cancelButton, Slider_Exit_Button);
98 m_cancelButton->setFixedSize(ICON_SIZE);
99 m_cancelButton->setIcon(QIcon::fromTheme("dcc_exit_normal"));
100 m_cancelButton->setIconSize(QSize(36, 36));
101 m_cancelButton->setToolTip(tr("Exit"));
102 m_cancelButton->setFocusPolicy(Qt::NoFocus);
103 hb->addWidget(m_cancelButton);
104 connect(m_cancelButton, &DIconButton::clicked, this, &SlideShowBottomBar::onCancelButtonClicked);
105 setLayout(hb);
106 }
107
108 void SlideShowBottomBar::onPreButtonClicked()
109 {
110 emit dApp->signalM->updatePauseButton();
111 emit dApp->signalM->updateButton();
112 emit showPrevious();
113 }
114
115 void SlideShowBottomBar::onPlaypauseButtonClicked()
116 {
117 if (0 == a) {
118 m_playpauseButton->setIcon(QIcon::fromTheme("dcc_play_normal"));
119 m_playpauseButton->setToolTip(tr("Play"));
120 a = 1;
121 emit dApp->signalM->updateButton();
122 emit showPause();
123 } else {
124 m_playpauseButton->setIcon(QIcon::fromTheme("dcc_suspend_normal"));
125 m_playpauseButton->setToolTip(tr("Pause"));
126 a = 0;
127 emit dApp->signalM->sigStartTimer();
128 emit showContinue();
129 }
130 }
131
132 void SlideShowBottomBar::onUpdatePauseButton()
133 {
134 m_playpauseButton->setIcon(QIcon::fromTheme("dcc_play_normal"));
135 m_playpauseButton->setToolTip(tr("Play"));
136 a = 1;
137 }
138
139 void SlideShowBottomBar::onInitSlideShowButton()
140 {
141 m_playpauseButton->setIcon(QIcon::fromTheme("dcc_suspend_normal"));
142 m_playpauseButton->setToolTip(tr("Pause"));
143 a = 0;
144 }
145
146 void SlideShowBottomBar::onNextButtonClicked()
147 {
148 emit dApp->signalM->updatePauseButton();
149 emit dApp->signalM->updateButton();
150 emit showNext();
151 }
152
153 void SlideShowBottomBar::onCancelButtonClicked()
154 {
155 emit showCancel();
156 }
157
158 SlideShowPanel::SlideShowPanel(QWidget *parent) : QWidget(parent)
159 , slideshowbottombar(new SlideShowBottomBar(this)), m_animation(new ImageAnimation(this))
160 , m_hideCursorTid(0)
161 {
162 //setFocusPolicy(Qt::StrongFocus);
163 initMenu();
164 initConnections();
165 setMouseTracking(true);
166 QVBoxLayout *layout = new QVBoxLayout(this);
167 layout->setMargin(0);
168 layout->addWidget(m_animation);
169 this->setLayout(layout);
170 //移出屏幕外
171 slideshowbottombar->move((QGuiApplication::primaryScreen()->geometry().width() - slideshowbottombar->width()) / 2, QGuiApplication::primaryScreen()->geometry().height());
172 //移至顶层
173 slideshowbottombar->raise();
174 }
175
176 void SlideShowPanel::initConnections()
177 {
178 // connect(m_animation, &ImageAnimation::singleAnimationEnd, this, &SlideShowPanel::onSingleAnimationEnd);
179 connect(dApp->signalM, &SignalManager::startSlideShow, this, &SlideShowPanel::startSlideShow);
180 connect(dApp->signalM, &SignalManager::sigESCKeyStopSlide, this, &SlideShowPanel::onESCKeyStopSlide);
181 connect(slideshowbottombar, &SlideShowBottomBar::showPause, this, &SlideShowPanel::onShowPause);
182 connect(slideshowbottombar, &SlideShowBottomBar::showContinue, this, &SlideShowPanel::onShowContinue);
183 connect(slideshowbottombar, &SlideShowBottomBar::showPrevious, this, &SlideShowPanel::onShowPrevious);
184 connect(slideshowbottombar, &SlideShowBottomBar::showNext, this, &SlideShowPanel::onShowNext);
185 connect(slideshowbottombar, &SlideShowBottomBar::showCancel, this, &SlideShowPanel::backToLastPanel);
186 }
187
188 void SlideShowPanel::initMenu()
189 {
190 this->setContextMenuPolicy(Qt::CustomContextMenu);
191 m_menu = new DMenu;
192 m_menu->setStyle(QStyleFactory::create("dlight"));
193 QString stopSc = dApp->setter->value(SHORTCUTVIEW_GROUP, "Slide show").toString();
194 stopSc.replace(" ", "");
195 appendAction(IdPlayOrPause, tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()), stopSc);
196 appendAction(IdStopslideshow, tr(slideshowbottombar->m_cancelButton->toolTip().toStdString().c_str()), stopSc);
197 connect(m_menu, &QMenu::triggered, this, &SlideShowPanel::onMenuItemClicked);
198 connect(this, &SlideShowPanel::customContextMenuRequested, this, &SlideShowPanel::onCustomContextMenuRequested);
199 }
200
201 void SlideShowPanel::appendAction(int id, const QString &text, const QString &shortcut)
202 {
203 QAction *ac = new QAction(m_menu);
204 addAction(ac);
205 ac->setText(text);
206 ac->setProperty("MenuID", id);
207 ac->setShortcut(QKeySequence(shortcut));
208 m_menu->addAction(ac);
209 if (id == IdPlayOrPause) {
210 connect(slideshowbottombar, &SlideShowBottomBar::showPause, ac, [ = ] {
211 ac->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
212 });
213 connect(slideshowbottombar, &SlideShowBottomBar::showContinue, ac, [ = ] {
214 ac->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
215 });
216 connect(slideshowbottombar, &SlideShowBottomBar::showNext, ac, [ = ] {
217 ac->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
218 });
219 connect(slideshowbottombar, &SlideShowBottomBar::showPrevious, ac, [ = ] {
220 ac->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
221 });
222 }
223 }
224
225 void SlideShowPanel::backToLastPanel()
226 {
227 m_animation->endSlider();
228 showNormal();
229 if (FROM_MAINWINDOW_POPVIEW == m_vinfo.viewMainWindowID) {
230 emit dApp->signalM->hideSlidePanel();
231 emit dApp->signalM->showBottomToolbar();
232 QEventLoop loop;
233 QTimer::singleShot(100, &loop, SLOT(quit()));
234 loop.exec();
235 int index = m_vinfo.paths.indexOf(m_vinfo.path);
236 emit dApp->signalM->viewImageNoNeedReload(index);
237 } else {
238 emit dApp->signalM->hideSlidePanel();
239 }
240 this->setCursor(Qt::ArrowCursor);
241 killTimer(m_hideCursorTid);
242 m_hideCursorTid = 0;
243 }
244
245 void SlideShowPanel::showNormal()
246 {
247 if (m_isMaximized) {
248 window()->showNormal();
249 window()->showMaximized();
250 } else {
251 window()->showNormal();
252 }
253 }
254
255 void SlideShowPanel::showFullScreen()
256 {
257 m_isMaximized = window()->isMaximized();
258 window()->showFullScreen();
259 emit dApp->signalM->hideBottomToolbar(true);
260 emit dApp->signalM->hideExtensionPanel(true);
261 emit dApp->signalM->hideTopToolbar(true);
262 }
263
264 void SlideShowPanel::setIsRandom(bool isRandom)
265 {
266 m_animation->setIsRandom(isRandom);
267 }
268
269 void SlideShowPanel::startSlideShow(const SignalManager::ViewInfo &vinfo, bool inDB)
270 {
271 Q_UNUSED(inDB);
272 if (vinfo.paths.isEmpty()) {
273 return;
274 }
275 m_vinfo = vinfo;
276 QStringList paths = QStringList();
277 QList<DBImgInfo> dBImgInfos;
278 //过滤掉视频
279 for (int i = 0; i < m_vinfo.paths.size(); i++) {
280 QString path = m_vinfo.paths.at(i);
281 if (!utils::base::isVideo(path)) {
282 paths << path;
283 }
284 }
285 for (int i = 0; i < m_vinfo.dBImgInfos.size(); i++) {
286 DBImgInfo info = m_vinfo.dBImgInfos.at(i);
287 if (!utils::base::isVideo(info.filePath)) {
288 dBImgInfos << info;
289 }
290 }
291 if (paths.size() > 0 && utils::base::isVideo(m_vinfo.path)) {
292 m_vinfo.path = paths.at(0);
293 }
294 m_vinfo.paths = paths;
295 m_vinfo.dBImgInfos = dBImgInfos;
296 this->setCursor(Qt::BlankCursor);
297 if (1 < vinfo.paths.length()) {
298 slideshowbottombar->m_preButton->setEnabled(true);
299 slideshowbottombar->m_nextButton->setEnabled(true);
300 slideshowbottombar->m_playpauseButton->setEnabled(true);
301 emit dApp->signalM->initSlideShowButton();
302 } else {
303 slideshowbottombar->m_preButton->setEnabled(false);
304 slideshowbottombar->m_nextButton->setEnabled(false);
305 slideshowbottombar->m_playpauseButton->setEnabled(false);
306 emit dApp->signalM->updatePauseButton();
307 }
308 int screenId = dApp->getDAppNew()->desktop()->screenNumber(this);
309 int nParentWidth = dApp->getDAppNew()->desktop()->screenGeometry(screenId).width();
310 int nParentHeight = dApp->getDAppNew()->desktop()->screenGeometry(screenId).height();
311 slideshowbottombar->move((nParentWidth - slideshowbottombar->width()) / 2, nParentHeight);
312 m_animation->startSlideShow(m_vinfo.path, m_vinfo.paths);
313 auto actionlist = m_menu->actions();
314 for (auto &action : actionlist) {
315 if (action->property("MenuID").toInt() == IdPlayOrPause) {
316 action->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
317 }
318 }
319 //加入显示动画效果,以透明度0-1显示,动态加载,视觉效果掩盖左上角展开
320 QPropertyAnimation *animation = new QPropertyAnimation(window(), "windowOpacity");
321 animation->setDuration(50);
322 animation->setEasingCurve(QEasingCurve::Linear);
323 animation->setStartValue(0);
324 animation->setEndValue(1);
325 animation->start(QAbstractAnimation::DeleteWhenStopped);
326 m_hideCursorTid = startTimer(DELAY_HIDE_CURSOR_INTERVAL);
327 showFullScreen();
328 }
329
330 void SlideShowPanel::onMenuItemClicked(QAction *action)
331 {
332 const int id = action->property("MenuID").toInt();
333 switch (id) {
334 case IdStopslideshow:
335 backToLastPanel();
336 break;
337 case IdPlayOrPause:
338 slideshowbottombar->m_playpauseButton->clicked();
339 action->setText(tr(slideshowbottombar->m_playpauseButton->toolTip().toStdString().c_str()));
340 break;
341 default:
342 break;
343 }
344 }
345
346 void SlideShowPanel::onThemeChanged(ViewerThemeManager::AppTheme dark)
347 {
348 if (dark == ViewerThemeManager::Dark) {
349 m_bgColor = DARK_BG_COLOR;
350 } else {
351 m_bgColor = LIGHT_BG_COLOR;
352 }
353 update();
354 }
355
356 //void SlideShowPanel::onSingleAnimationEnd()
357 //{
358 // return ;
359 //}
360
361 void SlideShowPanel::onESCKeyStopSlide()
362 {
363 if (isVisible())
364 backToLastPanel();
365 }
366
367 void SlideShowPanel::onShowPause()
368 {
369 m_animation->pauseAndNext();
370 }
371
372 void SlideShowPanel::onShowContinue()
373 {
374 m_animation->ifPauseAndContinue();
375 }
376
377 void SlideShowPanel::onShowPrevious()
378 {
379 m_animation->playAndPre();
380 }
381
382 void SlideShowPanel::onShowNext()
383 {
384 m_animation->playAndNext();
385 }
386
387 void SlideShowPanel::onCustomContextMenuRequested()
388 {
389 m_menu->popup(QCursor::pos());
390 }
391
392 void SlideShowPanel::mouseMoveEvent(QMouseEvent *event)
393 {
394 Q_UNUSED(event);
395 this->setCursor(Qt::ArrowCursor);
396 if (window()->isFullScreen()) {
397 QPoint pos = mapFromGlobal(QCursor::pos());
398 // 处理程序界面的初始高度和全屏下幻灯片界面不一致导致底部工具栏位置错误
399 int screenId = dApp->getDAppNew()->desktop()->screenNumber(this);
400 if (dApp->getDAppNew()->desktop()->screenGeometry(screenId).size().height() != height())
401 return;
402 if (height() - 20 < pos.y() && height() >= pos.y() && height() >= slideshowbottombar->y()) {
403 QPropertyAnimation *animation = new QPropertyAnimation(slideshowbottombar, "pos");
404 animation->setDuration(200);
405 animation->setEasingCurve(QEasingCurve::NCurveTypes);
406 animation->setStartValue(QPoint((width() - slideshowbottombar->width()) / 2, slideshowbottombar->y()));
407 animation->setEndValue(QPoint((width() - slideshowbottombar->width()) / 2, height() - slideshowbottombar->height() - 10));
408 animation->start(QAbstractAnimation::DeleteWhenStopped);
409 m_animation->update();
410 } else if (height() - slideshowbottombar->height() - 10 > pos.y() && height() - slideshowbottombar->height() - 10 <= slideshowbottombar->y()) {
411 QPropertyAnimation *animation = new QPropertyAnimation(slideshowbottombar, "pos");
412 animation->setDuration(200);
413 animation->setEasingCurve(QEasingCurve::NCurveTypes);
414 animation->setStartValue(QPoint((width() - slideshowbottombar->width()) / 2, slideshowbottombar->y()));
415 animation->setEndValue(QPoint((width() - slideshowbottombar->width()) / 2, height()));
416 animation->start(QAbstractAnimation::DeleteWhenStopped);
417 m_animation->update();
418 }
419 }
420 }
421
422 void SlideShowPanel::timerEvent(QTimerEvent *event)
423 {
424 if (event->timerId() == m_hideCursorTid && qApp->modalWindow() == nullptr) {
425 this->setCursor(Qt::BlankCursor);
426 }
427 QWidget::timerEvent(event);
428 }
429