new buttons, loads pls files
[tomamp] / mainwindow.cpp
1 #include <QtGui>
2 #include <QtDebug>
3 #include <QInputDialog>
4
5 #include "mainwindow.h"
6 #include "time.h"
7
8 //#define AVOID_INPUT_DIALOG 0
9
10 MainWindow::MainWindow()
11     : plman (this), settings (tr ("TomAmp"), "TomAmp")
12 {
13     audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
14     mediaObject = new Phonon::MediaObject(this);
15
16     mediaObject->setTickInterval(1000);
17     connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
18     connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
19         this, SLOT(stateChanged(Phonon::State,Phonon::State)));
20     connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
21         this, SLOT(sourceChanged(Phonon::MediaSource)));
22     connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
23     connect (&plman, SIGNAL (playlistChanged (int)), this, SLOT (playlistChanged(int)));
24     connect (&plman, SIGNAL (itemUpdated(int)), this, SLOT (itemUpdated (int)));
25
26     Phonon::createPath(mediaObject, audioOutput);
27
28     qsrand (time (NULL));
29     repeat = settings.value("repeat", false).toBool();
30     shuffle = settings.value("shuffle", false).toBool();
31     setupShuffleList();
32     setupActions();
33     setupMenus();
34     setupUi();
35     timeLcd->display("00:00");
36     plman.addStringList(settings.value("lastPlaylist").toStringList());
37     setupShuffleList();
38     int curind = settings.value("currentIndex", -1).toInt ();
39     if (curind >= 0)
40         setItem (curind, false);
41     audioOutput->setVolume(settings.value("volume", .5).toReal());
42     QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
43 }
44
45 MainWindow::~MainWindow()
46 {
47     settings.setValue("shuffle", shuffle);
48     settings.setValue("repeat", repeat);
49     settings.setValue("lastPlaylist", plman.playlistStrings());
50     settings.setValue("volume", audioOutput->volume());
51     settings.setValue("currentIndex", plman.indexOf(mediaObject->currentSource()));
52     for (int i = 0; i < musicTable->columnCount(); ++i)
53     {
54         QString lab = QString ("colWidth_%1").arg (i);
55         settings.setValue(lab, musicTable->columnWidth(i));
56     }
57 }
58
59 void MainWindow::addFiles()
60 {
61     QString folder = settings.value("LastFolder").toString();
62     if (folder.isEmpty())
63         folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
64     QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
65                     folder, "Music files (*.mp3 *.ogg *.wav *.flac);;Playlists (*.m3u *.pls)");
66
67     if (files.isEmpty())
68         return;
69
70     QString dir = QFileInfo (files[0]).absoluteDir().absolutePath();
71     settings.setValue("LastFolder", dir);
72     QStringList toadd;
73     foreach (QString string, files)
74     {
75         if (string.toLower().endsWith(".pls") || string.toLower().endsWith(".m3u"))
76             plman.addPlaylist(string);
77         else
78             toadd.append (string);
79     }
80     plman.addStringList(toadd);
81 }
82
83 void MainWindow::addFolder()
84 {
85     QString folder = settings.value("LastFolder").toString();
86     if (folder.isEmpty())
87         folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
88     QString dir = QFileDialog::getExistingDirectory(this,
89             tr("Select Directory To Add"),
90             folder);
91
92     QStringList filters;
93     QStringList files = QDir (dir).entryList(filters, QDir::AllDirs);
94     files.removeAll(".");
95     files.removeAll("..");
96     qDebug () << files;
97     bool recursive = false;
98     if (files.size())
99         recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
100     plman.parseAndAddFolder(dir, recursive);
101 }
102
103
104 void MainWindow::addUrl()
105 {
106 #ifdef AVOID_INPUT_DIALOG
107     QString url = "http://war.str3am.com:7970";
108 #else
109     QString url = QInputDialog::getText(this, "Get URL", "Please type in the stream URL");
110 #endif
111     QStringList toadd;
112     toadd << url;
113     plman.addStringList(toadd);
114 }
115
116
117 void MainWindow::about()
118 {
119     QMessageBox::information(this, tr("About TomAmp v0.1"),
120         tr("TomAmp is a simple playlist-based music player.\n\n"
121         "(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
122         "Please send comments and bug reports to the above e-mail address.\n\n"
123         "Icons by http://itweek.deviantart.com/"));
124 }
125
126 void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
127 {
128     qDebug () << "State: " << newState;
129     switch (newState)
130     {
131         case Phonon::ErrorState:
132             if (mediaObject->errorType() == Phonon::FatalError)
133             {
134 //                QMessageBox::warning(this, tr("Fatal Error"),
135 //                mediaObject->errorString() + mediaObject->currentSource().fileName() + ", " + mediaObject->currentSource().url().toString());
136             }
137             else
138             {
139 //                QMessageBox::warning(this, tr("Error"),
140 //                mediaObject->errorString());
141             }
142             next ();
143             break;
144         case Phonon::PlayingState:
145             setWindowTitle(mediaObject->metaData().value("TITLE") + " - TomAmp");
146             pauseAction->setVisible(true);
147             playAction->setVisible (false);
148             playAction->setEnabled(false);
149             pauseAction->setEnabled(true);
150             stopAction->setEnabled(true);
151             lastPlayed = plman.indexOf(mediaObject->currentSource());
152             break;
153         case Phonon::StoppedState:
154             setWindowTitle("TomAmp");
155             stopAction->setEnabled(false);
156             playAction->setEnabled(true);
157             pauseAction->setVisible(false);
158             playAction->setVisible(true);
159             pauseAction->setEnabled(false);
160             timeLcd->display("00:00");
161             unhighlightRow(plman.indexOf(mediaObject->currentSource()));
162             break;
163         case Phonon::PausedState:
164             pauseAction->setEnabled(false);
165             stopAction->setEnabled(true);
166             pauseAction->setVisible(false);
167             playAction->setVisible(true);
168             playAction->setEnabled(true);
169             qDebug () << "Queue size: " << mediaObject->queue().size ();
170             if (mediaObject->queue().size ())
171             {
172 /*                mediaObject->setCurrentSource(mediaObject->queue()[0]);
173                 musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));*/
174                 setItem (plman.indexOf(mediaObject->queue()[0]), true);
175 //                mediaObject->play();
176             }
177             mediaObject->clearQueue();
178             break;
179         case Phonon::BufferingState:
180             qDebug () << "Buffering";
181             break;
182         default:
183         ;
184     }
185 }
186
187 void MainWindow::next()
188 {
189     bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
190     if (mediaObject->state () == Phonon::ErrorState)
191         wasPlaying = true;
192     qDebug () << "NEXT, repeat=" << repeat << ", shuffle=" << shuffle << ", ShuffleLis size=" << shuffleList.size ();
193     if (shuffleList.size() != plman.size())
194         qDebug () << "WHOA STRANGE SHUFFLE: " << shuffleList;
195     int index = plman.indexOf(mediaObject->currentSource());
196     qDebug () << "Current index is " << index;
197     if (shuffle)
198     {
199         qDebug () << "Shuffle next " << index;
200         index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
201         while (index < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
202         {
203             index += 1;
204             qDebug () << "Index increase a " << index;
205         }
206         qDebug () << "Shuffle next 2 " << index;
207         if (index < shuffleList.size ())
208         {
209             setItem (index, wasPlaying);
210         }
211         else if (repeat)
212         {
213             index = 0;
214             while ((index) < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
215             {
216                 qDebug () << "Index increase 2a " << index;
217                 index += 1;
218             }
219             setItem (index, wasPlaying);
220         }
221 /*        if (index >= shuffleList.size ())
222             wasPlaying = false;*/
223
224     }
225     else
226     {
227         index++;
228         qDebug () << "Normal next";
229         while ((index) < plman.size () && !plman.getItem(index).playable)
230         {
231             index += 1;
232             qDebug () << "Index increase " << index;
233         }
234         qDebug () << "Normal next 2 " << index;
235         if (index < plman.size())
236         {
237             setItem (index, wasPlaying);
238         }
239         else if (repeat)
240         {
241             qDebug () << "Repeat on";
242             index = 0;
243             while ((index) < plman.size () && !plman.getItem(index).playable)
244             {
245                 index += 1;
246                 qDebug () << "Index increase " << index;
247             }
248             setItem (index, wasPlaying);
249         }
250 /*        if (index >= shuffleList.size ())
251             wasPlaying = false;*/
252     }
253 /*    if (wasPlaying)
254         mediaObject->play();*/
255     qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
256 }
257
258 void MainWindow::setItem(int i, bool doplay)
259 {
260     if (i < plman.size() && i >= 0)
261     {
262         if (lastPlayed >= 0)
263             unhighlightRow(lastPlayed);
264         if (shuffle)
265         {
266             qDebug () << "SetItem with shuffle, index = " << i << " real index is " << shuffleList[i];
267             qDebug () << "ShuffleList: " << shuffleList;
268             if (doplay)
269                 highlightRow(shuffleList[i]);
270             mediaObject->setCurrentSource(plman.at (shuffleList[i]));
271 //            musicTable->selectRow (shuffleList[i]);
272         }
273         else
274         {
275             qDebug () << "SetItem without shuffle, index = " << i;
276             if (doplay)
277                 highlightRow(i);
278             mediaObject->setCurrentSource(plman.at(i));
279 //            musicTable->selectRow (i);
280         }
281     }
282     if (doplay && mediaObject->currentSource().type() != Phonon::MediaSource::Invalid)
283         mediaObject->play();
284 }
285
286 void MainWindow::previous()
287 {
288     bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
289     if (mediaObject->state () == Phonon::ErrorState)
290         wasPlaying = true;
291     qDebug () << "PREVIOUS, repeat=" << repeat << ", shuffle=" << shuffle;
292     int index = plman.indexOf(mediaObject->currentSource());
293     qDebug () << "Current index is " << index;
294     if (shuffle)
295     {
296         qDebug () << "Shuffle next";
297         index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
298         while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
299         {
300             index--;
301             qDebug () << "Index increase a " << index;
302         }
303         qDebug () << "Shuffle next 2 " << index;
304         if (index >= 0)
305         {
306             setItem (index, wasPlaying);
307         }
308         else if (repeat)
309         {
310             index = plman.size () - 1;
311             while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
312             {
313                 qDebug () << "Index increase 2a " << index;
314                 index--;
315             }
316             setItem (index, wasPlaying);
317         }
318 /*        if (index < 0)
319             wasPlaying = false;*/
320
321     }
322     else
323     {
324         index--;
325         qDebug () << "Normal next";
326         while ((index) >= 0 && !plman.getItem(index).playable)
327         {
328             index--;
329             qDebug () << "Index increase " << index;
330         }
331         qDebug () << "Normal next 2 " << index;
332         if (index >= 0)
333         {
334             setItem (index, wasPlaying);
335         }
336         else if (repeat)
337         {
338             qDebug () << "Repeat on";
339             index = plman.size() - 1;
340             while ((index) >= 0 && !plman.getItem(index).playable)
341             {
342                 index--;
343                 qDebug () << "Index increase " << index;
344             }
345             setItem (index, wasPlaying);
346         }
347 /*        if (index < 0)
348             wasPlaying = false;*/
349     }
350 /*    if (wasPlaying)
351         mediaObject->play();*/
352     qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
353
354
355 }
356
357 void MainWindow::highlightRow (int i)
358 {
359     for (int j = 0; j < 3; ++j)
360     {
361         QTableWidgetItem* item = musicTable->item(i, j);
362         if (item)
363         {
364             QFont font = item->font();
365             font.setBold(true);
366             font.setItalic(true);
367             item->setFont(font);
368         }
369     }
370 }
371
372 void MainWindow::unhighlightRow (int i)
373 {
374     for (int j = 0; j < 3; ++j)
375     {
376         QTableWidgetItem* item = musicTable->item(i, j);
377         if (item)
378         {
379             QFont font = item->font();
380             font.setBold(false);
381             font.setItalic(false);
382             item->setFont(font);
383         }
384     }
385 }
386
387
388 void MainWindow::tick(qint64 time)
389 {
390     QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
391
392     timeLcd->display(displayTime.toString("mm:ss"));
393 }
394
395 void MainWindow::tableClicked(int row, int /* column */)
396 {
397 //    bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
398
399 /*    mediaObject->stop();
400     mediaObject->clearQueue();*/
401
402     if (row >= plman.size())
403         return;
404
405     int index = row;
406     while (index < shuffleList.size () && !plman.getItem(index).playable)
407     {
408         index += 1;
409     }
410     if (plman.size() > index)
411     {
412         if (shuffle)
413             index = shuffleList.indexOf(index);
414         setItem (index, true);
415 //        mediaObject->play();
416     }
417     else
418     {
419         index = 0;
420         while (index < plman.size () && !plman.getItem(index).playable)
421         {
422             index += 1;
423         }
424         if (plman.size() > index)
425         {
426             if (shuffle)
427                 index = shuffleList.indexOf(index);
428             setItem (index, true);
429 //            mediaObject->play();
430         }
431     }
432
433 }
434
435 void MainWindow::sourceChanged(const Phonon::MediaSource &source)
436 {
437     musicTable->selectRow(plman.indexOf(source));
438     timeLcd->display("00:00");
439 }
440
441
442 void MainWindow::aboutToFinish()
443 {
444     qDebug () << "Abouttotfinish";
445     int index = plman.indexOf(mediaObject->currentSource()) + 1;
446     if (shuffle)
447     {
448         index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
449         if (index < shuffleList.size ())
450         {
451             mediaObject->enqueue(plman.at (shuffleList[index]));
452         }
453         else if (repeat)
454         {
455             mediaObject->enqueue(plman.at (shuffleList[0]));
456         }
457
458     }
459     else
460     {
461         if (plman.size() > index)
462         {
463             mediaObject->enqueue(plman.at(index));
464             qDebug () << "Enqueue " << index << " pfm " << mediaObject->prefinishMark();
465         }
466         else if (repeat)
467         {
468             mediaObject->enqueue(plman.at(0));
469             qDebug () << "Enqueue " << 0 << " pfm " << mediaObject->prefinishMark();
470         }
471     }
472 }
473
474 void MainWindow::finished()
475 {
476     qDebug () << "Finished";
477 }
478
479 void MainWindow::setupActions()
480 {
481     playAction = new QAction(QIcon (QPixmap (":images/play")), tr("Play"), this);
482     playAction->setShortcut(tr("Crl+P"));
483     playAction->setDisabled(true);
484     pauseAction = new QAction(QIcon (QPixmap (":images/pause")), tr("Pause"), this);
485     pauseAction->setShortcut(tr("Ctrl+A"));
486     pauseAction->setDisabled(true);
487     pauseAction->setVisible(false);
488     stopAction = new QAction(QIcon (QPixmap (":images/stop")), tr("Stop"), this);
489     stopAction->setShortcut(tr("Ctrl+S"));
490     stopAction->setDisabled(true);
491     nextAction = new QAction(QIcon (QPixmap (":images/next")), tr("Next"), this);
492     nextAction->setShortcut(tr("Ctrl+N"));
493     previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
494     previousAction->setShortcut(tr("Ctrl+R"));
495     if (repeat)
496         repeatAction = new QAction(QIcon (QPixmap (":images/repeatActive")), tr("Repeat"), this);
497     else
498         repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
499     repeatAction->setCheckable(true);
500     repeatAction->setChecked(repeat);
501     repeatAction->setShortcut(tr("Ctrl+I"));
502     if (shuffle)
503         shuffleAction = new QAction(QIcon (QPixmap (":images/shuffleActive")), tr("Shuffle"), this);
504     else
505         shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
506     shuffleAction->setCheckable(true);
507     shuffleAction->setChecked(shuffle);
508     shuffleAction->setShortcut(tr("Ctrl+H"));
509     volumeAction = new QAction(QIcon (QPixmap (":images/volume")), tr("Volume"), this);
510     volumeAction->setCheckable(true);
511     volumeAction->setShortcut(tr("Ctrl+V"));
512     addFilesAction = new QAction(tr("Add &File"), this);
513     addFilesAction->setShortcut(tr("Ctrl+F"));
514     addFoldersAction = new QAction(tr("Add F&older"), this);
515     addFoldersAction->setShortcut(tr("Ctrl+O"));
516     addUrlAction = new QAction(tr("Add &Url"), this);
517     addUrlAction->setShortcut(tr("Ctrl+U"));
518     savePlaylistAction = new QAction (tr("Sa&ve Playlist"), this);
519     savePlaylistAction->setShortcut(tr ("Ctrl+V"));
520     loadPlaylistAction = new QAction (tr("&Load Playlist"), this);
521     loadPlaylistAction->setShortcut(tr("Ctrl+L"));
522     clearPlaylistAction = new QAction (tr("&Clear Playlist"), this);
523     clearPlaylistAction->setShortcut(tr("Ctrl+C"));
524     exitAction = new QAction(tr("E&xit"), this);
525     exitAction->setShortcut(tr("Ctrl+X"));
526     aboutAction = new QAction(tr("A&bout"), this);
527     aboutAction->setShortcut(tr("Ctrl+B"));
528     aboutQtAction = new QAction(tr("About &Qt"), this);
529     aboutQtAction->setShortcut(tr("Ctrl+Q"));
530 /*    removeSelected = new QAction (tr("&Delete from playlist"));
531     removeSelected->setShortcut(tr ("Ctrl+D"));*/
532
533     connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
534     connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
535     connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));
536     connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
537     connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
538     connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
539
540     connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
541     connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
542     connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
543     connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
544     connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
545     connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
546     connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
547     connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
548     connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
549     connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
550     connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
551 //    connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
552 }
553
554 void MainWindow::removeSelectedItem()
555 {
556     qDebug () << "Remove Selected!";
557     int row = musicTable->currentRow();
558     if (row >= 0)
559         plman.removeItem(row);
560 }
561
562 void MainWindow::removeAllButSelectedItem()
563 {
564     qDebug () << "Remove Selected!";
565     int row = musicTable->currentRow();
566     if (row >= 0)
567     {
568         QString uri = plman.getItem(row).uri;
569         QStringList lst;
570         lst << uri;
571         plman.clearPlaylist();
572         plman.addStringList(lst);
573     }
574 }
575
576 void MainWindow::repeatToggle ()
577 {
578     repeat = !repeat;
579     qDebug() << "Repeat toggled to " << repeat;
580     settings.setValue("repeat", QVariant (repeat));
581     if (repeat)
582         repeatAction->setIcon(QIcon (QPixmap (":images/repeatActive")));
583     else
584         repeatAction->setIcon(QIcon (QPixmap (":images/repeat")));
585 }
586
587 void MainWindow::shuffleToggle ()
588 {
589     shuffle = !shuffle;
590     settings.setValue("shuffle", QVariant (shuffle));
591     if (shuffle)
592         shuffleAction->setIcon(QIcon (QPixmap (":images/shuffleActive")));
593     else
594         shuffleAction->setIcon(QIcon (QPixmap (":images/shuffle")));
595 }
596
597 void MainWindow::volumeToggle ()
598 {
599     qDebug () << "Volumetoggle: " << volumeAction->isChecked();
600     volumeSlider->setVisible(volumeAction->isChecked());
601 }
602
603 void MainWindow::play()
604 {
605
606 }
607
608
609 void MainWindow::setupMenus()
610 {
611     QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
612     fileMenu->addAction(addFilesAction);
613     fileMenu->addAction(addFoldersAction);
614     fileMenu->addAction(addUrlAction);
615     fileMenu->addSeparator();
616     fileMenu->addAction(savePlaylistAction);
617     fileMenu->addAction(loadPlaylistAction);
618     fileMenu->addAction(clearPlaylistAction);
619 //    fileMenu->addAction(exitAction);
620
621     QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
622     aboutMenu->addAction(aboutAction);
623     aboutMenu->addAction(aboutQtAction);
624 }
625
626 void MainWindow::setupUi()
627 {
628     QToolBar *bar = new QToolBar;
629
630     bar->setOrientation(Qt::Vertical);
631     bar->setStyleSheet("padding:7px");
632     //bar->addAction(volumeAction);
633
634     seekSlider = new Phonon::SeekSlider(this);
635     seekSlider->setMediaObject(mediaObject);
636
637     volumeSlider = new Phonon::VolumeSlider(this);
638     volumeSlider->setAudioOutput(audioOutput);
639     volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
640     volumeSlider->setOrientation(Qt::Horizontal);
641     volumeSlider->setMuteVisible(false);
642 //    volumeAddedAction = bar->addWidget(volumeSlider);
643 //    volumeAddedAction->setVisible(false);
644     bar->addAction(playAction);
645     bar->addAction(pauseAction);
646     bar->addAction(stopAction);
647     bar->addAction(repeatAction);
648     bar->addAction(shuffleAction);
649     bar->addAction(nextAction);
650     bar->addAction(previousAction);
651
652     contextMenu = new QMenu (this);
653     removeSelected = contextMenu->addAction(tr ("Remove selected"));
654     removeAllButSelected = contextMenu->addAction(tr("Remove all but selected"));
655     connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
656     connect (removeAllButSelected, SIGNAL (triggered()), this, SLOT (removeAllButSelectedItem()));
657
658
659     timeLcd = new QLCDNumber;
660
661     QStringList headers;
662     headers << tr("Artist") << tr("Title") << tr("Album");
663
664     musicTable = new QTableWidget(0, 3);
665     musicTable->setHorizontalHeaderLabels(headers);
666     musicTable->setSelectionMode(QAbstractItemView::SingleSelection);
667     musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
668     connect(musicTable, SIGNAL(cellDoubleClicked(int,int)),
669         this, SLOT(tableClicked(int,int)));
670     connect(musicTable, SIGNAL(cellClicked(int,int)),
671         this, SLOT(cellClicked(int,int)));
672 /*    for (int i = 0; i < 3; ++i)
673     {
674         if (!musicTable->horizontalHeaderItem(i))
675             continue;
676         musicTable->horizontalHeaderItem(i)->setBackgroundColor(QColor (128, 128, 255));;
677         musicTable->horizontalHeaderItem(i)->setForeground(QColor (255, 255, 255));
678     }*/
679     for (int i = 0; i < musicTable->columnCount(); ++i)
680     {
681         QString lab = QString ("colWidth_%1").arg (i);
682         int val = settings.value(lab, 0).toInt();
683         if (val)
684             musicTable->setColumnWidth(i, val);
685 //        settings.setValue(lab, musicTable->columnWidth(i));
686     }
687
688
689     QHBoxLayout *seekerLayout = new QHBoxLayout;
690     QToolBar* bar2 = new QToolBar;
691     bar2->addAction(volumeAction);
692     seekerLayout->addWidget(bar2);
693     seekerLayout->addWidget(volumeSlider);
694     seekerLayout->addWidget(seekSlider);
695     seekerLayout->addWidget(timeLcd);
696
697     QVBoxLayout *playbackLayout = new QVBoxLayout;
698     volumeSlider->hide ();
699     playbackLayout->addWidget(bar);
700
701     QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
702
703     seekAndTableLayout->addWidget(musicTable);
704     seekAndTableLayout->addLayout(seekerLayout);
705
706     QHBoxLayout *mainLayout = new QHBoxLayout;
707     mainLayout->addLayout(seekAndTableLayout);
708     mainLayout->addLayout(playbackLayout);
709
710     QWidget *widget = new QWidget;
711     widget->setLayout(mainLayout);
712
713     setCentralWidget(widget);
714     setWindowTitle("TomAmp");
715     qDebug () << "cucc: " << musicTable->columnWidth(1);
716 }
717
718 void MainWindow::cellClicked(int /*row*/, int)
719 {
720 }
721
722 void MainWindow::contextMenuEvent (QContextMenuEvent*e)
723 {
724     qDebug () << "Context menu event!";
725     if (childAt (e->pos())->parentWidget() != musicTable)
726         return;
727     contextMenu->popup(e->globalPos());
728 }
729
730
731 void MainWindow::setupShuffleList()
732 {
733     QList<int> tmp;
734     int index = plman.indexOf(mediaObject->currentSource());
735     if (index < 0)
736         index = 0;
737     for (int i = 0; i < plman.size(); ++i)
738     {
739         if ((i != index))
740             tmp.append(i);
741     }
742     shuffleList.clear();
743     shuffleList.append (index);
744     while (tmp.size ())
745     {
746         int ind = qrand () % tmp.size();
747         shuffleList.append(tmp[ind]);
748         tmp.removeAt(ind);
749     }
750     qDebug () << shuffleList;
751 }
752
753 void MainWindow::savePlaylist ()
754 {
755     QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
756     if (filename.isEmpty())
757         return;
758     plman.savePlaylist(filename);
759 }
760
761 void MainWindow::loadPlaylist ()
762 {
763     QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u *.pls");
764     if (filename.isEmpty())
765         return;
766     plman.loadPlaylist (filename);
767 }
768
769 void MainWindow::playlistChanged(int from)
770 {
771     while (musicTable->rowCount() > from)
772     {
773         musicTable->removeRow(musicTable->rowCount () - 1);
774     }
775     int firstGood = -1;
776     for (int i = from; i < plman.size (); ++i)
777     {
778         if (firstGood < 0 && plman.getItem (i).playable)
779             firstGood = i;
780         int currentRow = musicTable->rowCount();
781         musicTable->insertRow(currentRow);
782         setRowFromItem (currentRow, plman.getItem(i));
783     }
784     if (plman.indexOf(mediaObject->currentSource()) < 0)
785     {
786         setItem (firstGood, false);
787     }
788     setupShuffleList();
789 }
790
791 void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
792 {
793     if (row >= musicTable->rowCount())
794         return;
795     if (item.artist.isEmpty() && item.title.isEmpty())
796     {
797         QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
798         item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
799         musicTable->setItem(row, 1, item1);
800     }
801     else
802     {
803         QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
804         item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
805         musicTable->setItem(row, 0, item1);
806         QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
807         item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
808         musicTable->setItem(row, 1, item2);
809         QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
810         item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
811         musicTable->setItem(row, 2, item3);
812     }
813 }
814
815 void MainWindow::itemUpdated(int index)
816 {
817     if (plman.indexOf(mediaObject->currentSource()) < 0 && plman.getItem (index).playable)
818     {
819         setItem (index, false);
820     }
821     setRowFromItem (index, plman.getItem(index));
822     if (plman.indexOf(mediaObject->currentSource()) == index)
823     {
824         if (shuffle) index = shuffleList.indexOf(index);
825         setItem (index, false);
826     }
827 }