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