#include "mainwindow.h"
#include "time.h"
-#define AVOID_INPUT_DIALOG 0
+//#define AVOID_INPUT_DIALOG 0
MainWindow::MainWindow()
: plman (this), settings (tr ("TomAmp"), "TomAmp")
connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
this, SLOT(sourceChanged(Phonon::MediaSource)));
connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));
+ connect (&plman, SIGNAL (playlistChanged (int)), this, SLOT (playlistChanged(int)));
+ connect (&plman, SIGNAL (itemUpdated(int)), this, SLOT (itemUpdated (int)));
Phonon::createPath(mediaObject, audioOutput);
timeLcd->display("00:00");
plman.addStringList(settings.value("lastPlaylist").toStringList());
setupShuffleList();
+ int curind = settings.value("currentIndex", -1).toInt ();
+ if (curind >= 0)
+ setItem (curind, false);
audioOutput->setVolume(settings.value("volume", .5).toReal());
+ QApplication::setWindowIcon(QIcon (QPixmap (":images/tomamp")));
}
MainWindow::~MainWindow()
{
settings.setValue("shuffle", shuffle);
settings.setValue("repeat", repeat);
- settings.setValue("lastPlaylist", plman.playlist());
+ settings.setValue("lastPlaylist", plman.playlistStrings());
settings.setValue("volume", audioOutput->volume());
+ settings.setValue("currentIndex", plman.indexOf(mediaObject->currentSource()));
+ for (int i = 0; i < musicTable->columnCount(); ++i)
+ {
+ QString lab = QString ("colWidth_%1").arg (i);
+ settings.setValue(lab, musicTable->columnWidth(i));
+ }
}
void MainWindow::addFiles()
if (folder.isEmpty())
folder = QDesktopServices::storageLocation(QDesktopServices::MusicLocation);
QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Files To Add"),
- folder);
+ folder, "Music files (*.mp3 *.ogg *.wav *.flac);;Playlists (*.m3u *.pls)");
if (files.isEmpty())
return;
QStringList toadd;
foreach (QString string, files)
{
- toadd.append (string);
+ if (string.toLower().endsWith(".pls") || string.toLower().endsWith(".m3u"))
+ plman.addPlaylist(string);
+ else
+ toadd.append (string);
}
plman.addStringList(toadd);
- setupShuffleList();
}
void MainWindow::addFolder()
if (files.size())
recursive = QMessageBox::question(this, "Add all folders", "Subfolders have been detected, add everything?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes;
plman.parseAndAddFolder(dir, recursive);
- setupShuffleList();
}
QStringList toadd;
toadd << url;
plman.addStringList(toadd);
- setupShuffleList();
}
tr("TomAmp is a simple playlist-based music player.\n\n"
"(c) 2010 Tamas Marki <tmarki@gmail.com>\n\n"
"Please send comments and bug reports to the above e-mail address.\n\n"
- "Icons by deleket (http://www.deleket.com)"));
+ "Icons by http://itweek.deviantart.com/"));
}
void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
playAction->setEnabled(false);
pauseAction->setEnabled(true);
stopAction->setEnabled(true);
+ lastPlayed = plman.indexOf(mediaObject->currentSource());
break;
case Phonon::StoppedState:
+ setWindowTitle("TomAmp");
stopAction->setEnabled(false);
playAction->setEnabled(true);
pauseAction->setVisible(false);
playAction->setVisible(true);
pauseAction->setEnabled(false);
timeLcd->display("00:00");
+ unhighlightRow(plman.indexOf(mediaObject->currentSource()));
break;
case Phonon::PausedState:
pauseAction->setEnabled(false);
qDebug () << "Queue size: " << mediaObject->queue().size ();
if (mediaObject->queue().size ())
{
- mediaObject->setCurrentSource(mediaObject->queue()[0]);
- musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));
- mediaObject->play();
+/* mediaObject->setCurrentSource(mediaObject->queue()[0]);
+ musicTable->selectRow(plman.indexOf(mediaObject->currentSource()));*/
+ setItem (plman.indexOf(mediaObject->queue()[0]), true);
+// mediaObject->play();
}
mediaObject->clearQueue();
break;
void MainWindow::next()
{
bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
- int index = plman.indexOf(mediaObject->currentSource()) + 1;
+ if (mediaObject->state () == Phonon::ErrorState)
+ wasPlaying = true;
+ qDebug () << "NEXT, repeat=" << repeat << ", shuffle=" << shuffle << ", ShuffleLis size=" << shuffleList.size ();
+ if (shuffleList.size() != plman.size())
+ qDebug () << "WHOA STRANGE SHUFFLE: " << shuffleList;
+ int index = plman.indexOf(mediaObject->currentSource());
+ qDebug () << "Current index is " << index;
if (shuffle)
{
+ qDebug () << "Shuffle next " << index;
index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) + 1;
+ while (index < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
+ {
+ index += 1;
+ qDebug () << "Index increase a " << index;
+ }
+ qDebug () << "Shuffle next 2 " << index;
if (index < shuffleList.size ())
{
- mediaObject->setCurrentSource(plman.at (shuffleList[index]));
+ setItem (index, wasPlaying);
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at (shuffleList[0]));
+ index = 0;
+ while ((index) < shuffleList.size () && !plman.getItem(shuffleList[index]).playable)
+ {
+ qDebug () << "Index increase 2a " << index;
+ index += 1;
+ }
+ setItem (index, wasPlaying);
}
+/* if (index >= shuffleList.size ())
+ wasPlaying = false;*/
}
else
{
- if (plman.size() > index)
+ index++;
+ qDebug () << "Normal next";
+ while ((index) < plman.size () && !plman.getItem(index).playable)
+ {
+ index += 1;
+ qDebug () << "Index increase " << index;
+ }
+ qDebug () << "Normal next 2 " << index;
+ if (index < plman.size())
{
- mediaObject->setCurrentSource(plman.at(index));
+ setItem (index, wasPlaying);
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at(0));
+ qDebug () << "Repeat on";
+ index = 0;
+ while ((index) < plman.size () && !plman.getItem(index).playable)
+ {
+ index += 1;
+ qDebug () << "Index increase " << index;
+ }
+ setItem (index, wasPlaying);
}
+/* if (index >= shuffleList.size ())
+ wasPlaying = false;*/
}
- if (wasPlaying)
+/* if (wasPlaying)
+ mediaObject->play();*/
+ qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
+}
+
+void MainWindow::setItem(int i, bool doplay)
+{
+ if (i < plman.size() && i >= 0)
+ {
+ if (lastPlayed >= 0)
+ unhighlightRow(lastPlayed);
+ if (shuffle)
+ {
+ qDebug () << "SetItem with shuffle, index = " << i << " real index is " << shuffleList[i];
+ qDebug () << "ShuffleList: " << shuffleList;
+ if (doplay)
+ highlightRow(shuffleList[i]);
+ mediaObject->setCurrentSource(plman.at (shuffleList[i]));
+// musicTable->selectRow (shuffleList[i]);
+ }
+ else
+ {
+ qDebug () << "SetItem without shuffle, index = " << i;
+ if (doplay)
+ highlightRow(i);
+ mediaObject->setCurrentSource(plman.at(i));
+// musicTable->selectRow (i);
+ }
+ }
+ if (doplay && mediaObject->currentSource().type() != Phonon::MediaSource::Invalid)
mediaObject->play();
}
void MainWindow::previous()
{
bool wasPlaying = (mediaObject->state () == Phonon::PlayingState);
- int index = plman.indexOf(mediaObject->currentSource()) - 1;
+ if (mediaObject->state () == Phonon::ErrorState)
+ wasPlaying = true;
+ qDebug () << "PREVIOUS, repeat=" << repeat << ", shuffle=" << shuffle;
+ int index = plman.indexOf(mediaObject->currentSource());
+ qDebug () << "Current index is " << index;
if (shuffle)
{
+ qDebug () << "Shuffle next";
index = shuffleList.indexOf(plman.indexOf(mediaObject->currentSource())) - 1;
+ while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
+ {
+ index--;
+ qDebug () << "Index increase a " << index;
+ }
+ qDebug () << "Shuffle next 2 " << index;
if (index >= 0)
{
- mediaObject->setCurrentSource(plman.at (shuffleList[index]));
+ setItem (index, wasPlaying);
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at (shuffleList[shuffleList.size() - 1]));
+ index = plman.size () - 1;
+ while (index >= 0 && !plman.getItem(shuffleList[index]).playable)
+ {
+ qDebug () << "Index increase 2a " << index;
+ index--;
+ }
+ setItem (index, wasPlaying);
}
+/* if (index < 0)
+ wasPlaying = false;*/
}
else
{
+ index--;
+ qDebug () << "Normal next";
+ while ((index) >= 0 && !plman.getItem(index).playable)
+ {
+ index--;
+ qDebug () << "Index increase " << index;
+ }
+ qDebug () << "Normal next 2 " << index;
if (index >= 0)
{
- mediaObject->setCurrentSource(plman.at(index));
+ setItem (index, wasPlaying);
}
else if (repeat)
{
- mediaObject->setCurrentSource(plman.at(plman.size() - 1));
+ qDebug () << "Repeat on";
+ index = plman.size() - 1;
+ while ((index) >= 0 && !plman.getItem(index).playable)
+ {
+ index--;
+ qDebug () << "Index increase " << index;
+ }
+ setItem (index, wasPlaying);
}
+/* if (index < 0)
+ wasPlaying = false;*/
}
- if (wasPlaying)
- mediaObject->play();
+/* if (wasPlaying)
+ mediaObject->play();*/
+ qDebug () << "wasPlaying: " << wasPlaying << ", playbutton visible: " << playAction->isVisible();
+
+
+}
+
+void MainWindow::highlightRow (int i)
+{
+ for (int j = 0; j < 3; ++j)
+ {
+ QTableWidgetItem* item = musicTable->item(i, j);
+ if (item)
+ {
+ QFont font = item->font();
+ font.setBold(true);
+ font.setItalic(true);
+ item->setFont(font);
+ }
+ }
+}
+void MainWindow::unhighlightRow (int i)
+{
+ for (int j = 0; j < 3; ++j)
+ {
+ QTableWidgetItem* item = musicTable->item(i, j);
+ if (item)
+ {
+ QFont font = item->font();
+ font.setBold(false);
+ font.setItalic(false);
+ item->setFont(font);
+ }
+ }
}
+
void MainWindow::tick(qint64 time)
{
QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);
{
// bool wasPlaying = mediaObject->state() == Phonon::PlayingState;
- mediaObject->stop();
- mediaObject->clearQueue();
+/* mediaObject->stop();
+ mediaObject->clearQueue();*/
if (row >= plman.size())
return;
- mediaObject->setCurrentSource(plman[row]);
+ int index = row;
+ while (index < shuffleList.size () && !plman.getItem(index).playable)
+ {
+ index += 1;
+ }
+ if (plman.size() > index)
+ {
+ if (shuffle)
+ index = shuffleList.indexOf(index);
+ setItem (index, true);
+// mediaObject->play();
+ }
+ else
+ {
+ index = 0;
+ while (index < plman.size () && !plman.getItem(index).playable)
+ {
+ index += 1;
+ }
+ if (plman.size() > index)
+ {
+ if (shuffle)
+ index = shuffleList.indexOf(index);
+ setItem (index, true);
+// mediaObject->play();
+ }
+ }
- mediaObject->play();
- int ind = shuffleList.indexOf(row);
- shuffleList.removeAt(ind);
- shuffleList.insert(0, row);
- qDebug () << "Modified shuffle list: " << shuffleList;
}
void MainWindow::sourceChanged(const Phonon::MediaSource &source)
nextAction->setShortcut(tr("Ctrl+N"));
previousAction = new QAction(QIcon (QPixmap (":images/previous")), tr("Previous"), this);
previousAction->setShortcut(tr("Ctrl+R"));
- repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
+ if (repeat)
+ repeatAction = new QAction(QIcon (QPixmap (":images/repeatActive")), tr("Repeat"), this);
+ else
+ repeatAction = new QAction(QIcon (QPixmap (":images/repeat")), tr("Repeat"), this);
repeatAction->setCheckable(true);
repeatAction->setChecked(repeat);
repeatAction->setShortcut(tr("Ctrl+I"));
- shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
+ if (shuffle)
+ shuffleAction = new QAction(QIcon (QPixmap (":images/shuffleActive")), tr("Shuffle"), this);
+ else
+ shuffleAction = new QAction(QIcon (QPixmap (":images/shuffle")), tr("Shuffle"), this);
shuffleAction->setCheckable(true);
shuffleAction->setChecked(shuffle);
shuffleAction->setShortcut(tr("Ctrl+H"));
aboutAction->setShortcut(tr("Ctrl+B"));
aboutQtAction = new QAction(tr("About &Qt"), this);
aboutQtAction->setShortcut(tr("Ctrl+Q"));
+/* removeSelected = new QAction (tr("&Delete from playlist"));
+ removeSelected->setShortcut(tr ("Ctrl+D"));*/
connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
connect(repeatAction, SIGNAL(triggered()), this, SLOT(repeatToggle()));
connect(shuffleAction, SIGNAL(triggered()), this, SLOT(shuffleToggle()));
connect(volumeAction, SIGNAL(triggered()), this, SLOT(volumeToggle()));
+
connect(addFilesAction, SIGNAL(triggered()), this, SLOT(addFiles()));
connect(addFoldersAction, SIGNAL(triggered()), this, SLOT(addFolder()));
connect(addUrlAction, SIGNAL(triggered()), this, SLOT(addUrl()));
connect (savePlaylistAction, SIGNAL (triggered()), this, SLOT (savePlaylist()));
connect (loadPlaylistAction, SIGNAL (triggered()), this, SLOT (loadPlaylist()));
- connect (clearPlaylistAction, SIGNAL (triggered()), this, SLOT (clearPlaylist()));
+ connect (clearPlaylistAction, SIGNAL (triggered()), &plman, SLOT (clearPlaylist()));
connect (nextAction, SIGNAL(triggered()), this, SLOT(next()));
connect (previousAction, SIGNAL(triggered()), this, SLOT(previous()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
+// connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
}
+void MainWindow::removeSelectedItem()
+{
+ qDebug () << "Remove Selected!";
+ int row = musicTable->currentRow();
+ if (row >= 0)
+ plman.removeItem(row);
+}
+
+void MainWindow::removeAllButSelectedItem()
+{
+ qDebug () << "Remove Selected!";
+ int row = musicTable->currentRow();
+ if (row >= 0)
+ {
+ QString uri = plman.getItem(row).uri;
+ QStringList lst;
+ lst << uri;
+ plman.clearPlaylist();
+ plman.addStringList(lst);
+ }
+}
void MainWindow::repeatToggle ()
{
repeat = !repeat;
qDebug() << "Repeat toggled to " << repeat;
settings.setValue("repeat", QVariant (repeat));
+ if (repeat)
+ repeatAction->setIcon(QIcon (QPixmap (":images/repeatActive")));
+ else
+ repeatAction->setIcon(QIcon (QPixmap (":images/repeat")));
}
void MainWindow::shuffleToggle ()
{
shuffle = !shuffle;
settings.setValue("shuffle", QVariant (shuffle));
+ if (shuffle)
+ shuffleAction->setIcon(QIcon (QPixmap (":images/shuffleActive")));
+ else
+ shuffleAction->setIcon(QIcon (QPixmap (":images/shuffle")));
}
void MainWindow::volumeToggle ()
bar->addAction(nextAction);
bar->addAction(previousAction);
-/* QLabel *volumeLabel = new QLabel;
- volumeLabel->setPixmap(QPixmap("images/volume.png"));*/
+ contextMenu = new QMenu (this);
+ removeSelected = contextMenu->addAction(tr ("Remove selected"));
+ removeAllButSelected = contextMenu->addAction(tr("Remove all but selected"));
+ connect (removeSelected, SIGNAL (triggered()), this, SLOT (removeSelectedItem()));
+ connect (removeAllButSelected, SIGNAL (triggered()), this, SLOT (removeAllButSelectedItem()));
-/* QPalette palette;
- palette.setBrush(QPalette::Light, Qt::darkGray);*/
timeLcd = new QLCDNumber;
-// timeLcd->setPalette(palette);
QStringList headers;
headers << tr("Artist") << tr("Title") << tr("Album");
this, SLOT(tableClicked(int,int)));
connect(musicTable, SIGNAL(cellClicked(int,int)),
this, SLOT(cellClicked(int,int)));
- musicTable->setSelectionBehavior(QAbstractItemView::SelectRows);
+/* for (int i = 0; i < 3; ++i)
+ {
+ if (!musicTable->horizontalHeaderItem(i))
+ continue;
+ musicTable->horizontalHeaderItem(i)->setBackgroundColor(QColor (128, 128, 255));;
+ musicTable->horizontalHeaderItem(i)->setForeground(QColor (255, 255, 255));
+ }*/
+ for (int i = 0; i < musicTable->columnCount(); ++i)
+ {
+ QString lab = QString ("colWidth_%1").arg (i);
+ int val = settings.value(lab, 0).toInt();
+ if (val)
+ musicTable->setColumnWidth(i, val);
+// settings.setValue(lab, musicTable->columnWidth(i));
+ }
+
QHBoxLayout *seekerLayout = new QHBoxLayout;
QToolBar* bar2 = new QToolBar;
QVBoxLayout *playbackLayout = new QVBoxLayout;
volumeSlider->hide ();
playbackLayout->addWidget(bar);
-// playbackLayout->addStretch();
-// playbackLayout->addWidget(volumeSlider);
-// playbackLayout->addWidget(volumeLabel);
QVBoxLayout *seekAndTableLayout = new QVBoxLayout;
qDebug () << "cucc: " << musicTable->columnWidth(1);
}
-void MainWindow::cellClicked(int row, int)
+void MainWindow::cellClicked(int /*row*/, int)
{
- if (mediaObject->state() == Phonon::PlayingState)
- {
- int index = plman.indexOf(mediaObject->currentSource());
- if (index >= 0)
- {
- musicTable->selectRow(index);
- }
- }
- else if (row < plman.size())
- {
- mediaObject->setCurrentSource(plman.at(row));
- shuffleList.removeAll(row);
- shuffleList.insert(0, row);
- qDebug () << shuffleList;
- }
}
+void MainWindow::contextMenuEvent (QContextMenuEvent*e)
+{
+ qDebug () << "Context menu event!";
+ if (childAt (e->pos())->parentWidget() != musicTable)
+ return;
+ contextMenu->popup(e->globalPos());
+}
+
+
void MainWindow::setupShuffleList()
{
QList<int> tmp;
index = 0;
for (int i = 0; i < plman.size(); ++i)
{
- if (i != index)
+ if ((i != index))
tmp.append(i);
}
shuffleList.clear();
tmp.removeAt(ind);
}
qDebug () << shuffleList;
- qDebug () << shuffleList;
+}
+
+void MainWindow::savePlaylist ()
+{
+ QString filename = QFileDialog::getSaveFileName(this, tr("Please select file name"), "", "Playlist Files (*.m3u *.pls)");
+ if (filename.isEmpty())
+ return;
+ plman.savePlaylist(filename);
+}
+
+void MainWindow::loadPlaylist ()
+{
+ QString filename = QFileDialog::getOpenFileName(this, tr("Select playlist file to load"), "", "*.m3u *.pls");
+ if (filename.isEmpty())
+ return;
+ plman.loadPlaylist (filename);
+}
+
+void MainWindow::playlistChanged(int from)
+{
+ while (musicTable->rowCount() > from)
+ {
+ musicTable->removeRow(musicTable->rowCount () - 1);
+ }
+ int firstGood = -1;
+ for (int i = from; i < plman.size (); ++i)
+ {
+ if (firstGood < 0 && plman.getItem (i).playable)
+ firstGood = i;
+ int currentRow = musicTable->rowCount();
+ musicTable->insertRow(currentRow);
+ setRowFromItem (currentRow, plman.getItem(i));
+ }
+ if (plman.indexOf(mediaObject->currentSource()) < 0)
+ {
+ setItem (firstGood, false);
+ }
+ setupShuffleList();
+}
+
+void MainWindow::setRowFromItem (int row, const PlaylistItem& item)
+{
+ if (row >= musicTable->rowCount())
+ return;
+ if (item.artist.isEmpty() && item.title.isEmpty())
+ {
+ QTableWidgetItem *item1 = new QTableWidgetItem(item.uri);
+ item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 1, item1);
+ }
+ else
+ {
+ QTableWidgetItem *item1 = new QTableWidgetItem(item.artist);
+ item1->setFlags(item1->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 0, item1);
+ QTableWidgetItem *item2 = new QTableWidgetItem(item.title);
+ item2->setFlags(item2->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 1, item2);
+ QTableWidgetItem *item3 = new QTableWidgetItem(item.album);
+ item3->setFlags(item3->flags() ^ Qt::ItemIsEditable);
+ musicTable->setItem(row, 2, item3);
+ }
+}
+
+void MainWindow::itemUpdated(int index)
+{
+ if (plman.indexOf(mediaObject->currentSource()) < 0 && plman.getItem (index).playable)
+ {
+ setItem (index, false);
+ }
+ setRowFromItem (index, plman.getItem(index));
+ if (plman.indexOf(mediaObject->currentSource()) == index)
+ {
+ if (shuffle) index = shuffleList.indexOf(index);
+ setItem (index, false);
+ }
}