Now track without tags has title from file basename
[someplayer] / src / playerform.cpp
index d0bb5dc..fd2bdab 100644 (file)
@@ -1,10 +1,32 @@
+/*
+ * SomePlayer - An alternate music player for Maemo 5
+ * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
 #include "playerform.h"
 #include "ui_playerform.h"
 #include "library.h"
+#include "player/player.h"
 #include <QDebug>
 #include <QTime>
 #include <QSlider>
 #include "trackrenderer.h"
+#include <QResource>
+#include "playlistdialog.h"
 
 using namespace SomePlayer::DataObjects;
 using namespace SomePlayer::Playback;
@@ -27,7 +49,35 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
 {
        _lib = lib;
        _player = new Player(this);
-    ui->setupUi(this);
+       _time = new QTime();
+       ui->setupUi(this);
+       if (_player->random()) {
+               ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
+       } else {
+               ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
+       }
+       if (_player->repeat()) {
+               ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
+       } else {
+               ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
+       }
+       _seek_slider = new QSlider(Qt::Horizontal);
+       _seek_slider->setEnabled(false);
+       ui->progressLayout->insertWidget(1, _seek_slider);
+       _seek_slider->setTracking(false);
+       _model = new QStandardItemModel(0, 2, this);
+       ui->playlistView->setModel(_model);
+       _context_menu = new QMenu(ui->playlistView);
+       QAction *delete_action = _context_menu->addAction("Delete");
+       QAction *enqueue_action = _context_menu->addAction("Enqueue");
+       QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
+       QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
+
+       _track_renderer = new TrackRenderer(this);
+       ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
+
+       _tag_resolver = new TagResolver(this);
+
        connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
        connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
        connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
@@ -37,28 +87,17 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
        connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
        connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
        connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
-       connect(ui->randomButton, SIGNAL(clicked()), _player, SLOT(toggleRandom()));
-       connect(ui->repeatButton, SIGNAL(clicked()), _player, SLOT(toggleRepeat()));
-       ui->randomButton->setChecked(_player->random());
-       ui->repeatButton->setChecked(_player->repeat());
-       _seek_slider = new QSlider(Qt::Horizontal);
-       ui->progressLayout->insertWidget(1, _seek_slider);
-       _seek_slider->setTracking(false);
+       connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
+       connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
        connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
        connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
-       _model = new QStandardItemModel(0, 2, this);
-       ui->playlistView->setModel(_model);
-       _context_menu = new QMenu(ui->playlistView);
-       QAction *delete_action = _context_menu->addAction("Delete");
        connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
-       QAction *enqueue_action = _context_menu->addAction("Enqueue");
        connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
-       QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
        connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
-
-       _track_renderer = new TrackRenderer(this);
-       ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
+       connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
+       connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
        connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
+       connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
 }
 
 PlayerForm::~PlayerForm()
@@ -71,6 +110,9 @@ void PlayerForm::_library() {
 }
 
 void PlayerForm::reload() {
+       if (ui->stackedWidget->currentIndex() == 1) {
+               emit hideSearchPanel();
+       }
        _current_playlist = _lib->getCurrentPlaylist();
        _player->setPlaylist(_current_playlist);
        __fill_list(_model, _current_playlist);
@@ -79,6 +121,13 @@ void PlayerForm::reload() {
 void PlayerForm::_toggle_view() {
        int index = ui->stackedWidget->currentIndex();
        index = (!index % 2);
+       if (index) {
+               ui->viewButton->setIcon(QIcon(":/icons/playlist.png"));
+               emit hideSearchPanel();
+       } else {
+               ui->viewButton->setIcon(QIcon(":/icons/playback.png"));
+               emit showSearchPanel();
+       }
        ui->stackedWidget->setCurrentIndex(index);
 }
 
@@ -108,7 +157,7 @@ void PlayerForm::_display_track(Track track) {
                                                        arg(_current_playlist.tracks().indexOf(track)+1).
                                                        arg(_current_playlist.tracks().count()));
        ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
-       ui->artistAlbumLabel->setText(QString("<h3>%1</h3><br/>%2").
+       ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
                                                                  arg(track.metadata().artist()).
                                                                  arg(track.metadata().album()));
        _seek_slider->setMinimum(0);
@@ -117,10 +166,10 @@ void PlayerForm::_display_track(Track track) {
 }
 
 void PlayerForm::_tick(int done, int all) {
-       QTime time(0, all/60, all%60);
-       ui->allTimeLabel->setText(time.toString("mm:ss"));
-       time.setHMS(0, done/60, done%60);
-       ui->doneTimeLabel->setText(time.toString("mm:ss"));
+       _time->setHMS(0, all/60, all%60);
+       ui->allTimeLabel->setText(_time->toString("mm:ss"));
+       _time->setHMS(0, done/60, done%60);
+       ui->doneTimeLabel->setText(_time->toString("mm:ss"));
        _seek_slider->setValue(done);
 }
 
@@ -151,3 +200,108 @@ void PlayerForm::_add_to_favorites() {
        int id = idx.first().row();
        _lib->addToFavorites(_current_playlist.tracks().at(id));
 }
+
+void PlayerForm::_state_changed(PlayerState state) {
+       if (state == PLAYER_PLAYING) {
+               ui->playpauseButton->setIcon(QIcon(":/icons/pause.png"));
+               _seek_slider->setEnabled(true);
+       } else {
+               if (state == PLAYER_STOPPED) {
+                       _seek_slider->setValue(0);
+                       ui->doneTimeLabel->setText("00:00");
+                       _seek_slider->setEnabled(false);
+               }
+               ui->playpauseButton->setIcon(QIcon(":/icons/play.png"));
+       }
+}
+
+void PlayerForm::_toggle_random() {
+       _player->toggleRandom();
+       if (_player->random()) {
+               ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
+       } else {
+               ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
+       }
+}
+
+void PlayerForm::_toggle_repeat() {
+       _player->toggleRepeat();
+       if (_player->repeat()) {
+               ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
+       } else {
+               ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
+       }
+}
+
+void PlayerForm::search(QString &pattern) {
+       _search_pattern = pattern;
+       _search_current_id = -1;
+       nextItem();
+}
+
+void PlayerForm::nextItem() {
+       QString data = _model->index(_search_current_id, 0).data().toString();
+       for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
+               data = _model->index(i, 0).data().toString();
+               if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
+                       _search_current_id = i;
+                       break;
+               }
+       }
+       QModelIndex id = _model->index(_search_current_id, 0);
+       _track_renderer->setSearchRow(_search_current_id);
+       ui->playlistView->scrollTo(id);
+       ui->playlistView->hide();
+       ui->playlistView->show();
+}
+
+void PlayerForm::prevItem() {
+       QString data = _model->index(_search_current_id, 0).data().toString();
+       for (int i = _search_current_id-1; i >= 0; i--) {
+               data = _model->index(i, 0).data().toString();
+               if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
+                       _search_current_id = i;
+                       break;
+               }
+       }
+       QModelIndex id = _model->index(_search_current_id, 0);
+       _track_renderer->setSearchRow(_search_current_id);
+       ui->playlistView->scrollTo(id);
+       ui->playlistView->hide();
+       ui->playlistView->show();
+}
+
+void PlayerForm::cancelSearch() {
+       _search_pattern = "";
+       _track_renderer->setSearchRow(-1);
+       ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
+       ui->playlistView->hide();
+       ui->playlistView->show();
+}
+
+void PlayerForm::addFiles(QList<QString> files) {
+       _tag_resolver->decode(files);
+}
+
+void PlayerForm::_track_decoded(Track track) {
+       _current_playlist.addTrack(track);
+       __fill_list(_model, _current_playlist);
+       _lib->saveCurrentPlaylist(_current_playlist);
+       _player->setPlaylist(_current_playlist);
+}
+
+void PlayerForm::_add_to_playlists() {
+       QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
+       int id = idx.first().row();
+
+       QList<QString> names = _lib->getPlaylistsNames();
+       names.removeOne(_CURRENT_PLAYLIST_SUBST_);
+       PlaylistDialog dialog(names, this);
+       dialog.exec();
+       QList<QString> selected = dialog.selected();
+       foreach (QString name, selected) {
+               Playlist pl = _lib->getPlaylist(name);
+               pl.addTrack(_current_playlist.tracks().at(id));
+               _lib->savePlaylist(pl);
+       }
+}