forked from zonyitoo/doubanfm-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwidget.cpp
More file actions
326 lines (282 loc) · 12.7 KB
/
Copy pathmainwidget.cpp
File metadata and controls
326 lines (282 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include "libs/doubanplayer.h"
#include <QPoint>
#include <QMouseEvent>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QMenu>
#ifdef MSVC2012
#include "STRINGS_GBK.H"
#else
#include "STRINGS_UTF8.H"
#endif
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget),
_isChannelWidgetShowing(false),
_isAnimStarted(false),
_isLyricWidgetShowing(false),
topBorder(new QLabel(this)),
bottomBorder(new QLabel(this)),
systemTrayIcon(nullptr)
{
ui->setupUi(this);
ui->lyricWidget->lower();
ui->channelWidget->raise();
ui->controlWidget->raise();
// Configure borders
topBorder->setMaximumSize(this->width(), 5);
topBorder->setMinimumSize(this->width(), 5);
topBorder->setStyleSheet("background: qlineargradient(x1: 0, y1: 1, x2: 0, y2: 0, stop: 0 rgba(0,0,0,80), stop: 1 rgba(0,0,0,0));");
topBorder->raise();
topBorder->move(0, -topBorder->height());
bottomBorder->setMaximumSize(this->width(), 10);
bottomBorder->setMinimumSize(this->width(), 10);
bottomBorder->setStyleSheet("background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 rgba(0,0,0,120), stop: 1 rgba(0,0,0,0));");
bottomBorder->raise();
bottomBorder->move(0, ui->controlWidget->height());
ui->pauseWidget->raise();
ui->pauseWidget->setVisible(false);
connect(ui->pauseWidget, SIGNAL(clicked()), ui->controlWidget, SLOT(play()));
exitShortcut = new QShortcut(QKeySequence("Ctrl+Q"), this);
connect(exitShortcut, SIGNAL(activated()), qApp, SLOT(quit()));
pauseShortcut = new QShortcut(QKeySequence("Space"), this);
connect(pauseShortcut, &QShortcut::activated, [this] () {
bool visiable = ui->pauseWidget->isVisible();
if (visiable)
ui->controlWidget->play();
else
ui->controlWidget->pause();
ui->pauseWidget->setVisible(!visiable);
});
nextShortcut = new QShortcut(QKeySequence("S"), this);
connect(nextShortcut, SIGNAL(activated()), ui->controlWidget, SLOT(on_nextButton_clicked()));
deleteShortcut = new QShortcut(QKeySequence("D"), this);
connect(deleteShortcut, SIGNAL(activated()), ui->controlWidget, SLOT(on_trashButton_clicked()));
likeShortcut = new QShortcut(QKeySequence("F"), this);
connect(likeShortcut, SIGNAL(activated()), ui->controlWidget, SLOT(on_likeButton_clicked()));
hideShortcut = new QShortcut(QKeySequence("Ctrl+W"), this);
connect(hideShortcut, SIGNAL(activated()), this, SLOT(hide()));
connect(ui->channelWidget, SIGNAL(mouseLeave()), this, SLOT(animHideChannelWidget()));
connect(ui->controlWidget, SIGNAL(openChannelPanel()), this, SLOT(animShowChannelWidget()));
connect(ui->controlWidget, SIGNAL(closeChannelPanel()), this, SLOT(animHideChannelWidget()));
connect(ui->controlWidget, SIGNAL(openLyricPanel()), this, SLOT(animShowLyricWidget()));
connect(ui->controlWidget, SIGNAL(closeLyricPanel()), this, SLOT(animHideLyricWidget()));
// FIXME: Ubuntu doesn't support QSystemTrayIcon
// Following code will cause confusing behaviour
#ifdef WITH_SYSTEM_TRAY_ICON
if (QSystemTrayIcon::isSystemTrayAvailable()) {
systemTrayIcon = new QSystemTrayIcon(QIcon("://icon.png"), this);
connect(systemTrayIcon, &QSystemTrayIcon::activated, [&] () {
if (this->isHidden())
this->show();
});
QMenu *trayMenu = new QMenu(this);
auto _openAction = trayMenu->addAction(STRINGS_OPENMAINUI);
trayMenu->addSeparator();
auto _closeAction = trayMenu->addAction(STRINGS_EXIT);
connect(trayMenu, &QMenu::triggered, [this, _openAction, _closeAction] (QAction *action) {
if (action == _openAction)
this->show();
else if (action == _closeAction)
qApp->quit();
});
systemTrayIcon->setContextMenu(trayMenu);
systemTrayIcon->show();
}
#endif
}
MainWidget::~MainWidget()
{
if (systemTrayIcon) {
systemTrayIcon->hide();
systemTrayIcon->deleteLater();
}
delete ui;
delete exitShortcut;
delete hideShortcut;
delete pauseShortcut;
delete topBorder;
delete bottomBorder;
}
void MainWidget::mousePressEvent(QMouseEvent *) {
}
void MainWidget::animHideChannelWidget() {
if (!isChannelWidgetShowing() || isAnimationStarted()) return;
this->animStart();
QParallelAnimationGroup *animgroup = new QParallelAnimationGroup(this);
if (this->height() == ui->channelWidget->height() + ui->controlWidget->height()) {
this->setMinimumHeight(ui->controlWidget->height());
QPropertyAnimation *main_anim = new QPropertyAnimation(this, "geometry");
main_anim->setDuration(400);
main_anim->setStartValue(this->geometry());
QRect endval2 = this->geometry();
endval2.setHeight(endval2.height() - ui->channelWidget->height());
main_anim->setEndValue(endval2);
main_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(main_anim);
}
QPropertyAnimation *control_anim = new QPropertyAnimation(ui->controlWidget, "pos");
control_anim->setDuration(400);
control_anim->setStartValue(ui->controlWidget->pos());
QPoint endpos = ui->controlWidget->pos();
endpos.setY(0);
control_anim->setEndValue(endpos);
control_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(control_anim);
QPropertyAnimation *topborder_anim = new QPropertyAnimation(this->topBorder, "pos");
topborder_anim->setDuration(400);
QPoint topborder_pos = topBorder->pos();
topborder_anim->setStartValue(topborder_pos);
topborder_pos.setY(-topBorder->height());
topborder_anim->setEndValue(topborder_pos);
topborder_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(topborder_anim);
QPropertyAnimation *bottomborder_anim = new QPropertyAnimation(this->bottomBorder, "pos");
bottomborder_anim->setDuration(400);
QPoint bottomborder_pos = bottomBorder->pos();
bottomborder_anim->setStartValue(bottomborder_pos);
bottomborder_pos.setY(ui->controlWidget->height());
bottomborder_anim->setEndValue(bottomborder_pos);
bottomborder_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(bottomborder_anim);
connect(animgroup, &QParallelAnimationGroup::finished, [this] () {
_isChannelWidgetShowing = false;
this->animFinish();
QRect pauseGeo = ui->pauseWidget->geometry();
pauseGeo.setHeight(this->geometry().height());
ui->pauseWidget->setGeometry(pauseGeo);
this->setMaximumHeight(this->height());
});
animgroup->start(QAbstractAnimation::DeleteWhenStopped);
}
void MainWidget::animShowChannelWidget() {
if (isChannelWidgetShowing() || isAnimationStarted()) return;
this->animStart();
auto _maxhei = ui->controlWidget->height() + ui->channelWidget->height();
QParallelAnimationGroup *animgroup = new QParallelAnimationGroup(this);
if (_maxhei > this->height()) {
this->setMaximumHeight(_maxhei);
QPropertyAnimation *main_anim = new QPropertyAnimation(this, "geometry");
main_anim->setDuration(400);
main_anim->setStartValue(this->geometry());
QRect endval2 = this->geometry();
endval2.setHeight(endval2.height() + ui->channelWidget->geometry().height());
main_anim->setEndValue(endval2);
main_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(main_anim);
}
QPropertyAnimation *control_anim = new QPropertyAnimation(ui->controlWidget, "pos");
control_anim->setDuration(400);
control_anim->setStartValue(ui->controlWidget->pos());
QPoint endpos = ui->controlWidget->pos();
endpos.setY(ui->channelWidget->height());
control_anim->setEndValue(endpos);
control_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(control_anim);
QPropertyAnimation *topborder_anim = new QPropertyAnimation(this->topBorder, "pos");
topborder_anim->setDuration(400);
QPoint topborder_pos = topBorder->pos();
topborder_anim->setStartValue(topborder_pos);
topborder_pos.setY(ui->channelWidget->height() - topBorder->height());
topborder_anim->setEndValue(topborder_pos);
topborder_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(topborder_anim);
QPropertyAnimation *bottomborder_anim = new QPropertyAnimation(this->bottomBorder, "pos");
bottomborder_anim->setDuration(400);
QPoint bottomborder_pos = bottomBorder->pos();
bottomborder_anim->setStartValue(bottomborder_pos);
bottomborder_pos.setY(ui->channelWidget->height() + ui->controlWidget->height());
bottomborder_anim->setEndValue(bottomborder_pos);
bottomborder_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(bottomborder_anim);
connect(animgroup, &QParallelAnimationGroup::finished, [this] () {
_isChannelWidgetShowing = true;
this->animFinish();
ui->pauseWidget->setGeometry(0, 0, ui->pauseWidget->geometry().width(), this->geometry().height());
this->setMinimumHeight(this->height());
});
animgroup->start(QAbstractAnimation::DeleteWhenStopped);
}
bool MainWidget::isChannelWidgetShowing() const {
return _isChannelWidgetShowing;
}
bool MainWidget::isAnimationStarted() const {
return _isAnimStarted;
}
bool MainWidget::isLyricWidgetShowing() const {
return _isLyricWidgetShowing;
}
void MainWidget::animStart() {
_isAnimStarted = true;
emit animationStart();
}
void MainWidget::animFinish() {
_isAnimStarted = false;
emit animationFinish();
}
void MainWidget::animHideLyricWidget() {
if (!isLyricWidgetShowing() || isAnimationStarted()) return;
this->animStart();
this->setMinimumHeight(ui->controlWidget->pos().y() + ui->controlWidget->height());
QParallelAnimationGroup *animgroup = new QParallelAnimationGroup(this);
QPoint lyric_cur = ui->lyricWidget->pos();
QRect self_cur = this->geometry();
QPropertyAnimation *lyric_anim = new QPropertyAnimation(ui->lyricWidget, "pos");
lyric_anim->setDuration(400);
lyric_anim->setStartValue(lyric_cur);
lyric_cur.setY(ui->controlWidget->pos().y()
+ ui->controlWidget->height()
- ui->lyricWidget->height());
lyric_anim->setEndValue(lyric_cur);
lyric_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(lyric_anim);
QPropertyAnimation *self_anim = new QPropertyAnimation(this, "geometry");
self_anim->setDuration(400);
self_anim->setStartValue(self_cur);
self_cur.setHeight(ui->controlWidget->pos().y() + ui->controlWidget->height());
self_anim->setEndValue(self_cur);
self_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(self_anim);
connect(animgroup, &QAnimationGroup::finished, [=] () {
_isLyricWidgetShowing = false;
animFinish();
ui->pauseWidget->setGeometry(0, 0, ui->pauseWidget->geometry().width(), this->geometry().height());
this->setMaximumHeight(this->height());
ui->lyricWidget->setShowing(false);
ui->lyricWidget->move(0, ui->controlWidget->height()
- ui->lyricWidget->height());
});
animgroup->start(QAbstractAnimation::DeleteWhenStopped);
}
void MainWidget::animShowLyricWidget() {
if (isLyricWidgetShowing() || isAnimationStarted()) return;
this->animStart();
this->setMaximumHeight(this->height() + ui->lyricWidget->height());
QParallelAnimationGroup *animgroup = new QParallelAnimationGroup(this);
QPoint lyric_cur = ui->lyricWidget->pos();
QRect self_cur = this->geometry();
QPropertyAnimation *lyric_anim = new QPropertyAnimation(ui->lyricWidget, "pos");
lyric_anim->setDuration(400);
lyric_anim->setStartValue(lyric_cur);
lyric_cur.setY(ui->controlWidget->height());
lyric_anim->setEndValue(lyric_cur);
lyric_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(lyric_anim);
QPropertyAnimation *self_anim = new QPropertyAnimation(this, "geometry");
self_anim->setDuration(400);
self_anim->setStartValue(self_cur);
self_cur.setHeight(ui->controlWidget->height() + ui->lyricWidget->height());
self_anim->setEndValue(self_cur);
self_anim->setEasingCurve(QEasingCurve::OutCubic);
animgroup->addAnimation(self_anim);
connect(animgroup, &QAnimationGroup::finished, [=] () {
_isLyricWidgetShowing = true;
animFinish();
ui->pauseWidget->setGeometry(0, 0, ui->pauseWidget->geometry().width(), this->geometry().height());
this->setMinimumHeight(this->height());
ui->lyricWidget->setShowing(true);
});
animgroup->start(QAbstractAnimation::DeleteWhenStopped);
}