Debian Codegraph @

src/album/frame/mainwidget.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 "mainwidget.h"
22 #include "application.h"
23 #include "controller/configsetter.h"
24 //#include "module/slideshow/slideshowpanel.h"
25 //#include "module/view/viewpanel.h"
26 #include "utils/baseutils.h"
27 #include "widgets/dialogs/imginfodialog.h"
28 #include "ac-desktop-define.h"
29
30 #include <QFileSystemWatcher>
31 #include <QLabel>
32 #include <QDebug>
33 #include <QDesktopWidget>
34 #include <QFile>
35 #include <QHBoxLayout>
36 #include <QDebug>
37 #include "controller/signalmanager.h"
38 #include <ddialog.h>
39 using namespace Dtk::Widget;
40
41 namespace {
42
43 const int TOP_TOOLBAR_HEIGHT = 50;
44 const int BOTTOM_TOOLBAR_HEIGHT = 70 + 10;
45 //const int EXTENSION_PANEL_WIDTH = 300;
46 const int BOTTOM_TOOLBAR_WIDTH_1 = 532 + 10 + 2;
47 const int BOTTOM_TOOLBAR_WIDTH_2 = 782 + 10 + 2;
48 const int THUMBNAIL_ADD_WIDTH = 32;
49 const int BOTTOM_SPACING = 10;
50 const int RT_SPACING = 10;
51 const int BOTTOM_REPAIR_SPACING = 5;
52 const int TOOLBAR_MINIMUN_WIDTH = 630 - 20 + 10 + 2;
53 const int BOTTOM_ADJUST = 0;
54
55 const QString SETTINGS_GROUP = "MAINWIDGET";
56 const QString SETTINGS_MAINPANEL_KEY = "MainPanel";
57
58 } // namespace
59
60 MainWidget::MainWidget(bool manager, QWidget *parent)
61 : QFrame(parent)
62 {
63 // initStyleSheet();
64 #ifndef LITE_DIV
65 initPanelStack(manager);
66 #else
67 Q_UNUSED(manager)
68 initPanelStack(false);
69 #endif
70 initExtensionPanel();
71 initTopToolbar();
72 initBottomToolbar();
73
74 initConnection();
75 setMouseTracking(true);
76 setObjectName("MainWidget");
77 }
78
79 MainWidget::~MainWidget()
80 {
81
82 }
83
84 void MainWidget::resizeEvent(QResizeEvent *e)
85 {
86 if (m_topToolbar) {
87 m_topToolbar->resize(width(), TOP_TOOLBAR_HEIGHT);
88
89 emit dApp->signalM->resizeFileName();
90 if (e->oldSize() != e->size()) {
91 emit m_topToolbar->updateMaxBtn();
92 }
93
94 if (window()->isFullScreen()) {
95 m_topToolbar->setVisible(false);
96 } else {
97 m_topToolbar->setVisible(true);
98 }
99 }
100
101 if (m_bottomToolbar) {
102 if (m_viewPanel->getPicCount() <= 1) {
103 m_bottomToolbar->setFixedWidth(BOTTOM_TOOLBAR_WIDTH_1);
104 } else if (m_viewPanel->getPicCount() <= 3) {
105 m_bottomToolbar->setFixedWidth(BOTTOM_TOOLBAR_WIDTH_2);
106 } else {
107 m_bottomToolbar->setFixedWidth(qMin((BOTTOM_TOOLBAR_WIDTH_2 + THUMBNAIL_ADD_WIDTH * (m_viewPanel->getPicCount() - 3)) + BOTTOM_ADJUST, qMax(this->width() - RT_SPACING, TOOLBAR_MINIMUN_WIDTH)));
108 }
109 m_bottomToolbar->move((this->width() - m_bottomToolbar->width()) / 2, this->height() - m_bottomToolbar->height() - BOTTOM_SPACING + BOTTOM_REPAIR_SPACING);
110 if (window()->isFullScreen()) {
111 emit dApp->signalM->sigShowFullScreen();
112 }
113 }
114 if (m_btmSeparatorLine) {
115 m_btmSeparatorLine->resize(window()->width(), 1);
116 m_btmSeparatorLine->move(0, window()->height() -
117 m_bottomToolbar->height() - 1);
118 }
119 }
120
121 void MainWidget::showEvent(QShowEvent *event)
122 {
123 Q_UNUSED(event);
124 if (m_bottomToolbar) {
125 if (window()->isFullScreen()) {
126 emit dApp->signalM->sigShowFullScreen();
127 }
128 }
129 }
130
131 void MainWidget::onGotoPanel(ModulePanel *panel)
132 {
133 QPointer<ModulePanel> p(panel);
134 if (p.isNull()) {
135 return;
136 }
137 // Record the last panel for restore in the next time launch
138 if (p->isMainPanel() && ! p->moduleName().isEmpty()) {
139 dApp->setter->setValue(SETTINGS_GROUP, SETTINGS_MAINPANEL_KEY,
140 QVariant(p->moduleName()));
141 }
142 m_panelStack->setCurrentWidget(panel);
143 }
144
145 void MainWidget::onBackToMainPanel()
146 {
147 window()->show();
148 window()->raise();
149 window()->activateWindow();
150 QString name = dApp->setter->value(SETTINGS_GROUP,
151 SETTINGS_MAINPANEL_KEY).toString();
152 if (name.isEmpty()) {
153 emit dApp->signalM->gotoTimelinePanel();
154 return;
155 }
156
157 for (int i = 0; i < m_panelStack->count(); i++) {
158 if (ModulePanel *p =
159 static_cast<ModulePanel *>(m_panelStack->widget(i))) {
160 if ((p->moduleName() == name) && p->isMainPanel()) {
161 emit dApp->signalM->gotoPanel(p);
162 return;
163 }
164 }
165 }
166 }
167
168 void MainWidget::onActiveWindow()
169 {
170 window()->raise();
171 window()->activateWindow();
172 }
173
174 void MainWidget::onShowInFileManager(const QString &path)
175 {
176 utils::base::showInFileManager(path);
177 }
178
179 void MainWidget::onMouseMove(bool show)
180 {
181 #ifdef tablet_PC
182 if (!show) {
183 QPropertyAnimation *animation = new QPropertyAnimation(m_bottomToolbar, "pos", this);
184 animation->setDuration(200);
185 animation->setEasingCurve(QEasingCurve::NCurveTypes);
186 animation->setStartValue(QPoint((width() - m_bottomToolbar->width()) / 2, m_bottomToolbar->y()));
187 animation->setEndValue(QPoint((width() - m_bottomToolbar->width()) / 2, height()));
188 animation->start(QAbstractAnimation::DeleteWhenStopped);
189 } else {
190 QPropertyAnimation *animation = new QPropertyAnimation(m_bottomToolbar, "pos", this);
191 animation->setDuration(200);
192 animation->setEasingCurve(QEasingCurve::NCurveTypes);
193 animation->setStartValue(QPoint((width() - m_bottomToolbar->width()) / 2, m_bottomToolbar->y()));
194 animation->setEndValue(QPoint((width() - m_bottomToolbar->width()) / 2, height() - m_bottomToolbar->height() - 10));
195 animation->start(QAbstractAnimation::DeleteWhenStopped);
196 }
197 #else
198 Q_UNUSED(show)
199 if (window()->isFullScreen()) {
200 QPoint pos = mapFromGlobal(QCursor::pos());
201 if (height() - 90 < pos.y() && height() > pos.y() && height() >= m_bottomToolbar->y()) {
202 QPropertyAnimation *animation = new QPropertyAnimation(m_bottomToolbar, "pos", this);
203 animation->setDuration(200);
204 animation->setEasingCurve(QEasingCurve::NCurveTypes);
205 animation->setStartValue(QPoint((width() - m_bottomToolbar->width()) / 2, m_bottomToolbar->y()));
206 animation->setEndValue(QPoint((width() - m_bottomToolbar->width()) / 2, height() - m_bottomToolbar->height() - 10));
207 animation->start(QAbstractAnimation::DeleteWhenStopped);
208 } else if (height() - m_bottomToolbar->height() - 10 > pos.y()
209 && height() - m_bottomToolbar->height() - 10 <= m_bottomToolbar->y()) {
210 //隐藏状态下,区域外的移动事件不响应
211 if (m_bottomToolbar->y() >= height()) {
212 return;
213 }
214 QPropertyAnimation *animation = new QPropertyAnimation(m_bottomToolbar, "pos", this);
215 animation->setDuration(200);
216 animation->setEasingCurve(QEasingCurve::NCurveTypes);
217 animation->setStartValue(QPoint((width() - m_bottomToolbar->width()) / 2, m_bottomToolbar->y()));
218 animation->setEndValue(QPoint((width() - m_bottomToolbar->width()) / 2, height()));
219 animation->start(QAbstractAnimation::DeleteWhenStopped);
220 }
221 }
222 #endif
223 }
224
225 void MainWidget::onShowFullScreen()
226 {
227 m_bottomToolbar->move((width() - m_bottomToolbar->width()) / 2, height());
228 }
229
230 void MainWidget::onUpdateBottomToolbar(bool wideMode)
231 {
232 if (wideMode) {
233 m_bottomToolbar->setFixedHeight(BOTTOM_TOOLBAR_HEIGHT);
234 if (m_viewPanel->getPicCount() <= 3) {
235 m_bottomToolbar->setFixedWidth(BOTTOM_TOOLBAR_WIDTH_2);
236 } else {
237 m_bottomToolbar->setFixedWidth(qMin(BOTTOM_TOOLBAR_WIDTH_2 + THUMBNAIL_ADD_WIDTH * (m_viewPanel->getPicCount() - 3) + BOTTOM_ADJUST, qMax(this->width() - RT_SPACING, TOOLBAR_MINIMUN_WIDTH)));
238 }
239 m_bottomToolbar->setVisible(true);
240 m_btmSeparatorLine->setVisible(m_bottomToolbar->isVisible());
241 } else {
242 m_bottomToolbar->setFixedHeight(BOTTOM_TOOLBAR_HEIGHT);
243 m_bottomToolbar->setFixedWidth(BOTTOM_TOOLBAR_WIDTH_1);
244 if (m_viewPanel->getPicCount() == 1) {
245 m_bottomToolbar->setVisible(true);
246 m_btmSeparatorLine->setVisible(m_bottomToolbar->isVisible());
247 } else {
248 m_bottomToolbar->setVisible(false);
249 m_btmSeparatorLine->setVisible(m_bottomToolbar->isVisible());
250 }
251 }
252 m_bottomToolbar->move((this->width() - m_bottomToolbar->width()) / 2,
253 this->height() - BOTTOM_TOOLBAR_HEIGHT - BOTTOM_SPACING + BOTTOM_REPAIR_SPACING);
254 }
255
256 void MainWidget::onUpdateBottomToolbarContent(QWidget *c, bool wideMode)
257 {
258 if (c == nullptr)
259 return;
260 m_bottomToolbar->setContent(c);
261 onUpdateBottomToolbar(wideMode);
262 }
263
264 void MainWidget::onShowBottomToolbar()
265 {
266 // 显示时先停止动画
267 QList<QPropertyAnimation *> lis = this->findChildren<QPropertyAnimation *>();
268 for (auto animation : lis) {
269 animation->stop();
270 }
271 m_bottomToolbar->setVisible(true);
272 m_btmSeparatorLine->setVisible(m_bottomToolbar->isVisible());
273 }
274
275 void MainWidget::onHideBottomToolbar(bool immediately)
276 {
277 m_bottomToolbar->move((width() - m_bottomToolbar->width()) / 2, height());
278 m_btmSeparatorLine->setVisible(m_bottomToolbar->isVisible());
279 Q_UNUSED(immediately)
280 }
281
282 void MainWidget::onUpdateExtensionPanelContent(QWidget *c)
283 {
284 if (c != nullptr)
285 m_extensionPanel->setContent(c);
286 }
287
288 void MainWidget::onShowExtensionPanel()
289 {
290 m_extensionPanel->move(window()->x() + (window()->width() - m_extensionPanel->width()) / 2,
291 window()->y() + (window()->height() - m_extensionPanel->height()) / 2);
292 m_extensionPanel->show();
293 }
294
295 void MainWidget::initPanelStack(bool manager)
296 {
297 #ifndef LITE_DIV
298 m_manager = manager;
299 #else
300 Q_UNUSED(manager)
301 #endif
302 m_panelStack = new QStackedWidget(this);
303 m_panelStack->setObjectName("PanelStack");
304
305 QHBoxLayout *mainLayout = new QHBoxLayout(this);
306 mainLayout->setContentsMargins(0, 0, 0, 0);
307 mainLayout->addWidget(m_panelStack);
308 m_viewPanel = new ViewPanel();
309 m_panelStack->addWidget(m_viewPanel);
310 }
311
312 void MainWidget::initTopToolbar()
313 {
314 m_topToolbar = new TopToolbar(false, this);
315 m_topToolbar->resize(width(), TOP_TOOLBAR_HEIGHT);
316 m_topToolbar->move(0, 0);
317 }
318
319 void MainWidget::initConnection()
320 {
321 connect(dApp->signalM, &SignalManager::backToMainPanel, this, &MainWidget::onBackToMainPanel);
322 connect(dApp->signalM, &SignalManager::activeWindow, this, &MainWidget::onActiveWindow);
323 connect(dApp->signalM, &SignalManager::gotoPanel, this, &MainWidget::onGotoPanel);
324 connect(dApp->signalM, &SignalManager::showInFileManager, this, &MainWidget::onShowInFileManager);
325 connect(dApp->signalM, &SignalManager::sigMouseMove, this, &MainWidget::onMouseMove);
326 connect(dApp->signalM, &SignalManager::sigShowFullScreen, this, &MainWidget::onShowFullScreen);
327 }
328
329 void MainWidget::initBottomToolbar()
330 {
331 m_bottomToolbar = new BottomToolbar(this);
332 m_bottomToolbar->resize(532, BOTTOM_TOOLBAR_HEIGHT);
333 m_bottomToolbar->move((width() - m_bottomToolbar->width()) / 2, height() - m_bottomToolbar->height() - 10);
334 m_btmSeparatorLine = new QLabel(this);
335 connect(dApp->signalM, &SignalManager::updateBottomToolbar, this, &MainWidget::onUpdateBottomToolbar);
336 connect(dApp->signalM, &SignalManager::updateBottomToolbarContent, this, &MainWidget::onUpdateBottomToolbarContent);
337 connect(dApp->signalM, &SignalManager::showBottomToolbar, this, &MainWidget::onShowBottomToolbar);
338 connect(dApp->signalM, &SignalManager::hideBottomToolbar, this, &MainWidget::onHideBottomToolbar);
339 }
340
341 void MainWidget::initExtensionPanel()
342 {
343 m_extensionPanel = ExtensionPanel::getInstance(this);
344 m_extensionPanel->close();
345 connect(dApp->signalM, &SignalManager::updateExtensionPanelContent, this, &MainWidget::onUpdateExtensionPanelContent);
346 connect(dApp->signalM, &SignalManager::showExtensionPanel, this, &MainWidget::onShowExtensionPanel);
347 #if 0
348 connect(dApp->signalM, &SignalManager::hideExtensionPanel,
349 this, [ = ](bool immediately) {
350 if (immediately) {
351 m_extensionPanel->requestStopAnimation();
352 if (this->window()->isFullScreen()) {
353 // m_extensionPanel->move(- qMax(m_extensionPanel->width(),
354 // EXTENSION_PANEL_WIDTH), 0);
355 m_extensionPanel->move(width(), 0);
356 } else {
357 // m_extensionPanel->move(- qMax(m_extensionPanel->width(),
358 // EXTENSION_PANEL_WIDTH), TOP_TOOLBAR_HEIGHT);
359 m_extensionPanel->move(width(), TOP_TOOLBAR_HEIGHT);
360 }
361 } else {
362 if (this->window()->isFullScreen()) {
363 // m_extensionPanel->moveWithAnimation(- qMax(m_extensionPanel->width(),
364 // EXTENSION_PANEL_WIDTH), 0);
365 m_extensionPanel->moveWithAnimation(width(), 0);
366 } else {
367 // m_extensionPanel->moveWithAnimation(- qMax(m_extensionPanel->width(),
368 // EXTENSION_PANEL_WIDTH), TOP_TOOLBAR_HEIGHT);
369 m_extensionPanel->moveWithAnimation(width(), TOP_TOOLBAR_HEIGHT);
370 }
371 }
372 });
373 #endif
374 }