-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
executable file
·662 lines (567 loc) · 19.2 KB
/
mainwindow.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// checksum 0xfd0b version 0x20001
/*
This file was generated by the Mobile Qt Application wizard of Qt Creator.
MainWindow is a convenience class containing mobile device specific code
such as screen orientation handling.
It is recommended not to modify this file, since newer versions of Qt Creator
may offer an updated version of it.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore/QCoreApplication>
#if defined(Q_OS_SYMBIAN) && defined(ORIENTATIONLOCK)
#include <eikenv.h>
#include <eikappui.h>
#include <aknenv.h>
#include <aknappui.h>
#endif // Q_OS_SYMBIAN && ORIENTATIONLOCK
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
netaccess = new NetworkAccess(this);
netaccess->setUpdateInterval(5000);
//Load settings
//TODO cleanup
loadSettings();
CommonDebug("Start creating context view");
//Create context view
contextview = new Ui_ContextView(this,netaccess);
ui->verticalLayout_2->addWidget(contextview,1);
ui->verticalLayout_2->setEnabled(true);
contextview->show();
//Create Menu
ui->menuBar->clear();
QFont tempfont = QFont("Arial",8);
tempfont.setBold(false);
menuwidget = 0;
currentsongview = 0;
//FIXME
//WORKAROUND no scrollbar on popup menu see QTBUG-17633
menuscroller = new QScrollArea(NULL);
viewmenu = new QMenu(this);
#ifdef Q_OS_SYMBIAN
viewmenu->setFont(tempfont);
viewmenu->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Expanding);
#endif
QWidget::updateGeometry();
menuscroller->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
CommonDebug("geometry menuscroller:"+QString::number(viewmenu->sizeHint().width()+20).toAscii()+":"+QString::number(ui->btnNext->geometry().y()).toAscii());
// menuscroller->setWidget(viewmenu);
menuscroller->hide();
menuscroller->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
connectacion = viewmenu->addAction(tr("Connect to host"));
//Connect Menubar Signals
//connect(trackaction,SIGNAL(triggered()),contextview,SLOT(showTracks()));
connect(ui->btnPlay,SIGNAL(clicked()),netaccess,SLOT(pause()));
connect(ui->btnStop,SIGNAL(clicked()),netaccess,SLOT(stop()));
connect(ui->btnNext,SIGNAL(clicked()),netaccess,SLOT(next()));
connect(ui->btnPrev,SIGNAL(clicked()),netaccess,SLOT(previous()));
connect(ui->btn_Menu,SIGNAL(clicked()),this,SLOT(showMenu()));
connect(ui->btnVolume,SIGNAL(clicked()),this,SLOT(toggleVolumeSlider()));
connect(ui->btnRepeat,SIGNAL(clicked(bool)),netaccess,SLOT(setRepeat(bool)));
connect(ui->btnShuffle,SIGNAL(clicked(bool)),netaccess,SLOT(setRandom(bool)));
connect(contextview,SIGNAL(exitrequested()),this,SLOT(close()));
connect(netaccess,SIGNAL(disconnected()),this,SLOT(disconnected()));
connect(netaccess,SIGNAL(statusUpdate(status_struct)),this,SLOT(updateStatusLabel(status_struct)));
connect(netaccess,SIGNAL(connectionestablished()),this,SLOT(connected()));
connect(contextview,SIGNAL(requestMaximised(bool)),this,SLOT(setMaximised(bool)));
connect(contextview,SIGNAL(showCurrentSongInfo()),this,SLOT(showCurrentSongInfo()));
connect(viewmenu,SIGNAL(aboutToHide()),menuscroller,SLOT(hide()));
//connect(viewmenu,SIGNAL(aboutToHide()),this,SLOT(sh));
this->setWindowTitle("qMobileMPD");
//Volume slider
volumeslider = new QSlider(Qt::Vertical,this);
volumeslider->setTickInterval(1);
volumeslider->setMaximum(100);
volumeslider->setMinimum(0);
volumeslider->hide();
connect(volumeslider,SIGNAL(valueChanged(int)),this,SLOT(changeServerVolume(int)));
showExpanded();
currentstatusfield = StatusField_Title;
ui->btnShuffle->setAutoExclusive(false);
ui->btnRepeat->setAutoExclusive(false);
ui->btnRepeat->setChecked(false);
ui->btnShuffle->setChecked(false);
// this->setOrientation(ScreenOrientationLockPortrait);
if(searchDefaultServer())
{
// tryConnect();
}
else{
setMaximised(false);
QMessageBox::information(this,tr("Default Server"),tr("Please define a default server under server properties which is used for connection."),QMessageBox::Ok,QMessageBox::NoButton);
setMaximised(true);
}
currentsongview = NULL;
netaccess->setUpdateInterval(5000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::setOrientation(ScreenOrientation orientation)
{
#ifdef Q_OS_SYMBIAN
if (orientation != ScreenOrientationAuto) {
#if defined(ORIENTATIONLOCK)
const CAknAppUiBase::TAppUiOrientation uiOrientation =
(orientation == ScreenOrientationLockPortrait) ? CAknAppUi::EAppUiOrientationPortrait
: CAknAppUi::EAppUiOrientationLandscape;
CAknAppUi* appUi = dynamic_cast<CAknAppUi*> (CEikonEnv::Static()->AppUi());
TRAPD(error,
if (appUi)
appUi->SetOrientationL(uiOrientation);
);
Q_UNUSED(error)
#else // ORIENTATIONLOCK
qWarning("'ORIENTATIONLOCK' needs to be defined on Symbian when locking the orientation.");
#endif // ORIENTATIONLOCK
}
#elif defined(Q_WS_MAEMO_5)
Qt::WidgetAttribute attribute;
switch (orientation) {
case ScreenOrientationLockPortrait:
attribute = Qt::WA_Maemo5PortraitOrientation;
break;
case ScreenOrientationLockLandscape:
attribute = Qt::WA_Maemo5LandscapeOrientation;
break;
case ScreenOrientationAuto:
default:
attribute = Qt::WA_Maemo5AutoOrientation;
break;
}
setAttribute(attribute, true);
#else // Q_OS_SYMBIAN
Q_UNUSED(orientation);
#endif // Q_OS_SYMBIAN
}
void MainWindow::showExpanded()
{
#ifdef Q_OS_SYMBIAN
showFullScreen();
//showMaximized();
#elif defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
showMaximized();
#elif defined(QT_SIMULATOR)
showMaximized();
#else
show();
#endif
}
void MainWindow::updateStatusLabel(status_struct tempstruct)
{
volumeslider->blockSignals(true);
ui->pgr_time->setValue(tempstruct.percent);
switch(currentstatusfield) {
case StatusField_Artist:{ui->pgr_time->setFormat(tempstruct.artist);break;}
case StatusField_Album:{ui->pgr_time->setFormat(tempstruct.album);break;}
case StatusField_SongInfo:{ui->pgr_time->setFormat(tempstruct.info);break;}
case StatusField_Title:{ui->pgr_time->setFormat(tempstruct.title);break;}
};
currentstatusfield = (currentstatusfield+1)%NR_StatusFieldShown;
ui->btnShuffle->setChecked(tempstruct.shuffle);
ui->btnRepeat->setChecked(tempstruct.repeat);
updateVolumeSlider(tempstruct.volume);
CommonDebug("PLayStatus:"+QString::number(tempstruct.playing));
if(laststate!=tempstruct.playing){
if(tempstruct.playing==NetworkAccess::PAUSE||tempstruct.playing==NetworkAccess::STOP)
{
ui->btnPlay->setIcon(QIcon(":/icons/media-playback-start.png"));
CommonDebug("Set icon to play");
}
else if(tempstruct.playing==NetworkAccess::PLAYING)
{
ui->btnPlay->setIcon(QIcon(":/icons/media-playback-pause.png"));
CommonDebug("Set icon to pause");
} }
laststate=tempstruct.playing;
volumeslider->blockSignals(false);
}
//Settings
void MainWindow::loadSettings()
{
QSettings settings;
settings.beginGroup("server_properties");
int size = settings.beginReadArray("profiles");
CommonDebug(QString::number(size).toAscii()+" Settings found");
for(int i = 0;i<size;i++)
{
serverprofile tempstruct;
settings.setArrayIndex(i);
tempstruct.hostname = settings.value("hostname").toString();
tempstruct.password = settings.value("password").toString();
tempstruct.profilename = settings.value("profilename").toString();
tempstruct.port = settings.value("port").toUInt();
tempstruct.defaultprofile = settings.value("default").toBool();
connectionprofiles.append(tempstruct);
CommonDebug("loaded Settings:"+tempstruct.hostname.toAscii()+":"+QString::number(tempstruct.port).toAscii()+":"+tempstruct.password.toAscii());
}
settings.endArray();
settings.endGroup();
}
void MainWindow::writeSettings()
{
QSettings settings;
settings.beginGroup("server_properties");
settings.beginWriteArray("profiles");
for(int i=0;i<connectionprofiles.length();i++)
{
settings.setArrayIndex(i);
settings.setValue("hostname",connectionprofiles.at(i).hostname);
settings.setValue("password",connectionprofiles.at(i).password);
settings.setValue("profilename",connectionprofiles.at(i).profilename);
settings.setValue("port",connectionprofiles.at(i).port);
settings.setValue("default",connectionprofiles.at(i).defaultprofile);
CommonDebug("wrote setting:"+QString::number(i).toAscii());
}
settings.endArray();
settings.endGroup();
CommonDebug("Settings written");
}
void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();
QMainWindow::closeEvent(event);
}
void MainWindow::showAbout()
{
setMaximised(false);
QMessageBox::about(this,tr("qmobilempd"),tr("qMobileMPD Version:") + QString(VERSION) + tr(" Copyright 2011 by Hendrik Borghorst, licensed under GPLv3 for more information visit: <a href='http://code.google.com/p/qmobilempd'>Homepage</a> . This applications uses <a href='http://www.tango-project.org'>Tango icons</a>.")+tr("Using qt version: ")+qVersion());
setMaximised(true);
}
void MainWindow::showMenu()
{
//setMaximised(false);
// if(!viewmenu->isVisible())
// {
// viewmenu->show();
// }
// else{
// menuscroller->hide();
// viewmenu->hide();
// }
if(menuwidget == 0)
{
//FIXME Vertical orientation
if(this->size().height()>this->size().width())
{
menuwidget = new VerticalMenuWidget(this);
}
else
menuwidget = new HorizontalMenuWidget(this);
contextview->hide();
if(currentsongview!=0)
currentsongview->hide();
ui->verticalLayout_2->addWidget(menuwidget);
menuwidget->show();
connect(menuwidget,SIGNAL(showAbout()),this,SLOT(showAbout()));
connect(menuwidget,SIGNAL(showAbout()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(showSettings()),this,SLOT(openSettings()));
connect(menuwidget,SIGNAL(connectClicked()),this,SLOT(tryConnect()));
connect(menuwidget,SIGNAL(connectClicked()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(showAlbums()),contextview,SLOT(showAlbums()));
connect(menuwidget,SIGNAL(showAlbums()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(showArtists()),contextview,SLOT(showAlbums()));
connect(menuwidget,SIGNAL(showArtists()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(showFiles()),this,SLOT(showFiles()));
connect(menuwidget,SIGNAL(showFiles()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(showPlaylist()),contextview,SLOT(showCurrentPlaylist()));
connect(menuwidget,SIGNAL(showPlaylist()),this,SLOT(hideMenu()));
connect(menuwidget,SIGNAL(exitClicked()),this,SLOT(close()));
connect(menuwidget,SIGNAL(showCurrentSong()),this,SLOT(showCurrentSongInfo()));
}
else
{
disconnect(menuwidget,0);
ui->verticalLayout_2->removeWidget(menuwidget);
if(currentsongview!=0)
currentsongview->show();
else
contextview->show();
menuwidget->hide();
delete(menuwidget);
menuwidget = 0;
}
}
void MainWindow::hideMenu()
{
CommonDebug("hideMenu()");
if(menuwidget != 0)
{
disconnect(menuwidget,0);
ui->verticalLayout_2->removeWidget(menuwidget);
contextview->show();
menuwidget->hide();
delete(menuwidget);
menuwidget = 0;
}
}
void MainWindow::toggleVolumeSlider()
{
if(volumeslider->isVisible())
{
volumeslider->hide();
CommonDebug("hide volume slider");
}
else {
CommonDebug("show volume slide");
int x,y;
x = ui->btnVolume->geometry().x();
y = (ui->btnVolume->geometry().y())- 240;
CommonDebug("sliderpos:"+QString::number(x).toAscii()+":"+QString::number(y).toAscii());
volumeslider->setGeometry(x,y,50,240);
volumeslider->show();
volumeslider->setVisible(true);
volumeslider->setFocus();
}
}
void MainWindow::updateVolumeSlider(quint8 volume)
{
if(volume>=85)
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-high.png"));
}
else if((volume<85)&&(volume>=40))
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-medium.png"));
}
else if((volume<40)&&(volume>0))
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-low.png"));
}
else if(volume<=0)
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-muted.png"));
}
volumeslider->setValue(volume);
}
void MainWindow::changeServerVolume(int volume)
{
if(volume>=85)
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-high.png"));
}
else if((volume<85)&&(volume>=40))
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-medium.png"));
}
else if((volume<40)&&(volume>0))
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-low.png"));
}
else if(volume<=0)
{
ui->btnVolume->setIcon(QIcon(":/icons/audio-volume-muted.png"));
}
netaccess->setVolume(volume);
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
#ifdef Q_OS_SYMBIAN
switch (event->nativeVirtualKey())
{
case 63552:
{
// TODO
netaccess->setVolume(volumeslider->value()+2);
volumeslider->setValue(volumeslider->value()+2);
break;
}
case 63553:
{
// TODO
netaccess->setVolume(volumeslider->value()-2);
volumeslider->setValue(volumeslider->value()-2);
break;
}
default:
{
break;
}
};
#endif
}
void MainWindow::tryConnect()
{
//Get default host and connect
if(searchDefaultServer()){
bool success = netaccess->connectToHost(hostname,port);
if(success)
{
CommonDebug("sucessfully connected to valid mpd,try authentication if any");
if(password!="")
{
bool pwsuccess = netaccess->authenticate(password);
if(!pwsuccess)
{
QMessageBox::warning(this,tr("Not authenticated"),tr("Functions could be limited without password, check settings and reconnect"),QMessageBox::Ok,QMessageBox::NoButton);
}
}
//get Playlist
connectacion->setText("Disconnect");
disconnect(connectacion,SIGNAL(triggered()),0,0);
connect(connectacion,SIGNAL(triggered()),this,SLOT(tryDisconnect()));
}
else
{
QMessageBox::critical(this,tr("Connection error"),tr("Could not connect to default host check settings"));
}
}
}
void MainWindow::tryDisconnect()
{
netaccess->disconnect();
}
void MainWindow::disconnected()
{
connectacion->setText("Connect");
disconnect(connectacion,SIGNAL(triggered()),0,0);
connect(connectacion,SIGNAL(triggered()),this,SLOT(tryConnect()));
//Clear old content
ui->pgr_time->setFormat("");
ui->pgr_time->setValue(0);
}
void MainWindow::openSettings()
{
if(menuwidget != 0)
{
disconnect(menuwidget,0);
ui->verticalLayout_2->removeWidget(menuwidget);
contextview->hide();
menuwidget->hide();
delete(menuwidget);
menuwidget = 0;
}
settingsdialogobject = new SettingsDialog(connectionprofiles,this);
ui->verticalLayout_2->addWidget(settingsdialogobject);
ui->pgr_time->hide();
ui->pgr_time->hide();
ui->btnNext->hide();
ui->btnPlay->hide();
ui->btnPrev->hide();
ui->btnRepeat->hide();
ui->btnShuffle->hide();
ui->btnStop->hide();
ui->btn_Menu->hide();
ui->btnVolume->hide();
connect(settingsdialogobject,SIGNAL(okRequested(QList<serverprofile>)),this,SLOT(applySettings(QList<serverprofile>)));
connect(settingsdialogobject,SIGNAL(cancelRequested()),this,SLOT(discardSettings()));
}
void MainWindow::applySettings(QList<serverprofile> profiles)
{
connectionprofiles = profiles;
discardSettings();
}
void MainWindow::discardSettings()
{
settingsdialogobject->hide();
ui->verticalLayout_2->removeWidget(settingsdialogobject);
contextview->show();
ui->pgr_time->show();
ui->btnNext->show();
ui->btnPlay->show();
ui->btnPrev->show();
ui->btnRepeat->show();
ui->btnShuffle->show();
ui->btnStop->show();
ui->btn_Menu->show();
ui->btnVolume->show();
delete settingsdialogobject;
settingsdialogobject = 0;
}
bool MainWindow::searchDefaultServer()
{
for(int i=0;i<connectionprofiles.length();i++)
{
if(connectionprofiles.at(i).defaultprofile)
{
hostname = connectionprofiles.at(i).hostname;
password = connectionprofiles.at(i).password;
port = connectionprofiles.at(i).port;
return true;
}
}
return false;
}
bool MainWindow::event(QEvent *event)
{
#if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
if(event->type()==QEvent::Leave)
{
netaccess->suspendUpdates();
}
if(event->type()==QEvent::Enter)
{
netaccess->resumeUpdates();
}
#elif defined(Q_OS_SYMBIAN)
if(event->type()==QEvent::WindowDeactivate)
{
netaccess->setUpdateInterval(30000);
}
if(event->type()==QEvent::WindowActivate)
{
if(currentsongview==NULL)
{
netaccess->setUpdateInterval(5000);
}
else{netaccess->setUpdateInterval(1000);}
}
#endif
return QMainWindow::event(event);
}
void MainWindow::focusInEvent(QFocusEvent *event)
{
showExpanded();
QMainWindow::focusInEvent(event);
}
void MainWindow::showFiles()
{
contextview->showFiles("");
}
void MainWindow::setMaximised(bool value)
{
CommonDebug("set maximised called");
if(!value)
{
showNormal();
//ui->centralWidget->showMaximized();
}
else
{
showExpanded();
}
}
void MainWindow::showCurrentSongInfo()
{
if(menuwidget!=0)
showMenu();
if(currentsongview==0)
{
currentsongview = new CurrentSongWidget(this,netaccess);
//Hide other stuff
contextview->hide();
ui->pgr_time->hide();
ui->verticalLayout_2->addWidget(currentsongview);
connect(currentsongview,SIGNAL(backRequested()),this,SLOT(hideCurrentSongInfo()));
}
}
void MainWindow::hideCurrentSongInfo()
{
if(currentsongview!=0)
{
ui->verticalLayout_2->removeWidget(currentsongview);
contextview->show();
ui->pgr_time->show();
disconnect(currentsongview,SIGNAL(backRequested()),0,0);
delete (currentsongview);
currentsongview = 0;
netaccess->setUpdateInterval(5000);
}
}
void MainWindow::connected()
{
//updateStatusLabel(netaccess->getStatus());
}