-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwidget.cpp
343 lines (295 loc) · 8.58 KB
/
mainwidget.cpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QTimer>
#include <QMenu>
#include <QAction>
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWidget)
{
ui->setupUi(this);
m_setIndexFileName = "./../MusicPlayerZ/index.ini";
initWidgetUnits();
connect(ui->pb_play,SIGNAL(clicked()),
this,SIGNAL(siganlPlayStatusChanged()));
m_curIndex = 0;//当前角码
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &MainWidget::showTicker);
timer->start(1000);//定时器
//显示系统托盘图标
//新建QSystemTrayIcon对象
mSysTrayIcon = new QSystemTrayIcon(this);
//新建托盘要显示的icon
QIcon icon = QIcon("../MusicPlayerZ/icon/trayicon.ico");
//将icon设到QSystemTrayIcon对象中
mSysTrayIcon->setIcon(icon);
//当鼠标移动到托盘上的图标时,会显示此处设置的内容
mSysTrayIcon->setToolTip(QObject::trUtf8("测试系统托盘图标"));
//给QSystemTrayIcon添加槽函数
connect(mSysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
//建立托盘操作的菜单
createActions();
createMenu();
//在系统托盘显示此对象
mSysTrayIcon->show();
}
MainWidget::~MainWidget()
{
delete ui;
}
///最小化到托盘
void MainWidget::minimizeToTray()
{
//隐藏程序主窗口
this->hide();
}
///点击系统托盘图标,显示主界面
void MainWidget::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch(reason){
case QSystemTrayIcon::Trigger:
//单击托盘图标
break;
case QSystemTrayIcon::DoubleClick:
//双击托盘图标
//双击后显示主程序窗口
this->show();
break;
default:
break;
}
}
void MainWidget::createActions()
{
mShowMainAction = new QAction(QObject::trUtf8("显示主界面"),this);
connect(mShowMainAction,SIGNAL(triggered()),this,SLOT(on_showMainAction()));
mExitAppAction = new QAction(QObject::trUtf8("退出"),this);
connect(mExitAppAction,SIGNAL(triggered()),this,SLOT(on_exitAppAction()));
}
void MainWidget::createMenu()
{
mMenu = new QMenu(this);
//新增菜单项---显示主界面
mMenu->addAction(mShowMainAction);
//增加分隔符
mMenu->addSeparator();
//新增菜单项---退出程序
mMenu->addAction(mExitAppAction);
//把QMenu赋给QSystemTrayIcon对象
mSysTrayIcon->setContextMenu(mMenu);
}
/*
* 当在系统托盘点击菜单内的显示主界面操作
*/
void MainWidget::on_showMainAction()
{
this->show();
}
/*
* 当在系统托盘点击菜单内的退出程序操作
*/
void MainWidget::on_exitAppAction()
{
this->close();
mSysTrayIcon->deleteLater();//删除图标
}
///托盘结束
void MainWidget::clearWidgetSongListAndLyricList(void){
ui->lw_songNameList->clear();
ui->lw_lyricShow->clear();
}
void MainWidget::setCurrentPlayStatus(QString status){
ui->pb_play->setText(status);
}
void MainWidget::setInitPlayStatus(QString initstatus){
ui->pb_play->setText(initstatus);
}
void MainWidget::setCurrentSongSource(int index,QString name){
ui->lw_songNameList->setCurrentRow(index);
ui->lb_songName->setText(name);
m_showText = name;
}
void MainWidget::showTicker()
{
int iSize = m_showText.size();
if (m_curIndex > iSize)
m_curIndex = 0;
// qDebug()<<m_curIndex;
// qDebug()<<m_showText.mid(m_curIndex++);
ui->lb_songName->setText(m_showText.mid(m_curIndex++));
}
void MainWidget::setCurrentPlayMode(int m_playMode){
if(m_playMode == 0){
ui->pb_playMode->setText("COnce");
}else if(m_playMode == 1){
ui->pb_playMode->setText("CLoop");
}else if(m_playMode == 2){
ui->pb_playMode->setText("Sequential");
}else if(m_playMode == 3){
ui->pb_playMode->setText("Loop");
}else if(m_playMode == 4){
ui->pb_playMode->setText("Random");
}
}
void MainWidget::setWidgetSongNameList(QStringList namelist){
ui->lw_songNameList->addItems(namelist);
qDebug()<<"Set Song Name Success";
}
void MainWidget::setWidgetLyricShow(QStringList lyriclist){
ui->lw_lyricShow->clear();
ui->lw_lyricShow->addItems(lyriclist);
for(int i = 0; i < lyriclist.length(); i++)
{
ui->lw_lyricShow->item(i)->setTextAlignment(Qt::AlignCenter);
}
}
void MainWidget::setCurrentLyricPos(int index){
ui->lw_lyricShow->setCurrentRow(index);
}
void MainWidget::setInitVolume(int volume){
ui->hs_volume->setValue(volume);
}
void MainWidget::setCurrentSongTime(QString time){
ui->lb_position->setText(time);
}
void MainWidget::setCurrentSongPosition(qint64 position){
if(!ui->hs_position->isSliderDown()) //当slider不被按下时
{
ui->hs_position->setValue(position);
}
}
void MainWidget::setCurrentSongMaxPosition(qint64 totalPosition){
ui->hs_position->setMaximum(totalPosition);
}
void MainWidget::mousePressEvent(QMouseEvent *ev)
{
if(ev->buttons() == Qt::LeftButton)
{
m_widgetMove = ev->globalPos() -
this->frameGeometry().topLeft();
ev->accept(); //鼠标事件被系统接收
}
}
///当鼠标左键被按下时,移动主窗口
void MainWidget::mouseMoveEvent(QMouseEvent *ev)
{
if(ev->buttons() == Qt::LeftButton)
{
this->move(ev->globalPos() - m_widgetMove);
ev->accept();
}
}
///初始化界面布局
void MainWidget::initWidgetUnits(void)
{
//去除Widget界面边框
this->setWindowFlags(Qt::FramelessWindowHint);
}
///最小化界面
void MainWidget::on_pb_min_clicked()
{
this->showMinimized();
}
///最大化界面
void MainWidget::on_pb_max_clicked()
{
if(this->isMaximized()){
this->showNormal();
}else{
this->showMaximized();
}
}
///关闭界面
void MainWidget::on_pb_close_clicked()
{
emit signalCloseSettingWidget();
emit signalCloseDeskLyricWidget();
emit signalCloseDesktopPremoye();
emit signalCloseMainWidget();
emit signalCloseWelcomeWidget();
}
void MainWidget::on_hs_position_sliderMoved(int position)
{
emit signalMusicSliderPositionChanged(position);
}
void MainWidget::on_hs_volume_sliderMoved(int position)
{
emit signalVolumeSliderPositionChanged(position);
}
void MainWidget::on_lw_songNameList_doubleClicked(const QModelIndex &index)
{
emit signalControlPlaySongs(index.row());
//行信息写入文件
//writeInit("Index","CurrentIndex",index.row());
ui->pb_play->setText("pause");
}
void MainWidget::on_pb_playMode_clicked()
{
emit signalPlayModeChanged();
}
void MainWidget::on_pb_prev_clicked()
{
emit signalPlayPrevSong();
}
void MainWidget::on_pb_next_clicked()
{
emit signalPlayNextSong();
}
void MainWidget::on_pb_setting_clicked()
{
emit signalOpenSettingWidget();
}
void MainWidget::slotInitPlaySongsIndexBegin(void){
readInit("Index","CurrentIndex",m_getIniIndex);
//qDebug()<<"iniIndex:"<<m_getIniIndex;
emit signalInitIndexSuccess(m_getIniIndex);
}
void MainWidget::slotPathsChanged(void){
writeInit("Index","CurrentIndex",INITINDEX);
emit signalInitIndexSuccess(INITINDEX);
//qDebug()<<"hhhhhhhhh";
}
///操作读写文件 头 #include <QSettings>
bool MainWidget::writeInit(QString group, QString key, int value)
{
if(group.isEmpty() || key.isEmpty())
{
return false;
}
else
{
//创建配置文件操作对象
QSettings config(m_setIndexFileName, QSettings::IniFormat);
//将信息写入配置文件
config.beginGroup(group);
config.setValue(key, value);
config.endGroup();
return true;
}
}
bool MainWidget::readInit(QString group, QString key, int &value)
{
value = 0;
if(m_setIndexFileName.isEmpty() || key.isEmpty())
{
return false;
}
else
{
//创建配置文件操作对象
QSettings config(m_setIndexFileName, QSettings::IniFormat);
//读取用户配置信息
value = config.value(group + "/" + key).toInt();
qDebug()<<value;
return true;
}
}
void MainWidget::on_pb_deskLyric_clicked()
{
emit signalOpenDeskLyricWidget();
}
void MainWidget::on_pb_remote_clicked()
{
emit signalOpenDesktopPremoye();
}