Icons themes, Portrait mode, bugfixes
[someplayer] / src / playerform.cpp
index 6bb2379..b55e461 100644 (file)
@@ -1,15 +1,37 @@
+/*
+ * 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"
+#include "edittagsdialog.h"
+#include "config.h"
 
 using namespace SomePlayer::DataObjects;
 using namespace SomePlayer::Playback;
+using namespace SomePlayer::Storage;
 
 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
        _model->clear();
@@ -30,48 +52,64 @@ PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
        _lib = lib;
        _player = new Player(this);
        _time = new QTime();
-    ui->setupUi(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)));
-       connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
-       connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
-       connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
-       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()), this, SLOT(_toggle_random()));
-       connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
+       Config config;
+       _icons_theme = config.getValue("ui/iconstheme").toString();
+       ui->setupUi(this);
        if (_player->random()) {
-               ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
        } else {
-               ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
        }
        if (_player->repeat()) {
-               ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
        } else {
-               ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_inactive.png"));
        }
+       ui->volumeSlider->setMinimum(0);
+       ui->volumeSlider->setMaximum(100);
+       ui->volumeSlider->hide();
        _seek_slider = new QSlider(Qt::Horizontal);
        _seek_slider->setEnabled(false);
        ui->progressLayout->insertWidget(1, _seek_slider);
        _seek_slider->setTracking(false);
-       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(QIcon(":/icons/delete.png"), "Delete");
+       QAction *delete_action = _context_menu->addAction("Delete");
+       QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
+       QAction *enqueue_action = _context_menu->addAction("Enqueue");
+       QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
+       QAction *edit_tags = _context_menu->addAction("Edit tags");
+
+       _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)));
+       connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
+       connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
+       connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
+       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()), this, SLOT(_toggle_random()));
+       connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
+       connect(_seek_slider, SIGNAL(sliderMoved(int)), _player, SLOT(seek(int)));
+       //connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
+       connect(ui->volumeSlider, SIGNAL(sliderMoved(int)), _player, SLOT(setVolume(int)));
+       connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
        connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
-       QAction *enqueue_action = _context_menu->addAction(QIcon(":/icons/add.png"), "Enqueue");
        connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
-       QAction *add_to_favorites = _context_menu->addAction(QIcon(":/icons/fav.png"), "Add to favorites");
        connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
+       connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
+       connect(edit_tags, SIGNAL(triggered()), this, SLOT(_edit_tags()));
        connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
-
-       _track_renderer = new TrackRenderer(this);
-       ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
        connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
+       connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
+       connect(ui->volumeButton, SIGNAL(clicked()), this, SLOT(_toggle_volume()));
 }
 
 PlayerForm::~PlayerForm()
@@ -83,23 +121,25 @@ void PlayerForm::_library() {
        emit library();
 }
 
-void PlayerForm::reload() {
+void PlayerForm::reload(bool reread) {
        if (ui->stackedWidget->currentIndex() == 1) {
                emit hideSearchPanel();
        }
-       _current_playlist = _lib->getCurrentPlaylist();
-       _player->setPlaylist(_current_playlist);
-       __fill_list(_model, _current_playlist);
+       if (reread) {
+               _current_playlist = _lib->getCurrentPlaylist();
+               _player->setPlaylist(_current_playlist);
+               __fill_list(_model, _current_playlist);
+       }
 }
 
 void PlayerForm::_toggle_view() {
        int index = ui->stackedWidget->currentIndex();
        index = (!index % 2);
        if (index) {
-               ui->viewButton->setIcon(QIcon(":/icons/playlist.png"));
+               ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
                emit hideSearchPanel();
        } else {
-               ui->viewButton->setIcon(QIcon(":/icons/playback.png"));
+               ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
                emit showSearchPanel();
        }
        ui->stackedWidget->setCurrentIndex(index);
@@ -160,7 +200,7 @@ void PlayerForm::_delete_track() {
        int id = idx.first().row();
        _current_playlist.removeTrackAt(id);
        _lib->saveCurrentPlaylist(_current_playlist);
-       reload();
+       reload(true);
 }
 
 void PlayerForm::_enqueue_track() {
@@ -177,7 +217,7 @@ void PlayerForm::_add_to_favorites() {
 
 void PlayerForm::_state_changed(PlayerState state) {
        if (state == PLAYER_PLAYING) {
-               ui->playpauseButton->setIcon(QIcon(":/icons/pause.png"));
+               ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
                _seek_slider->setEnabled(true);
        } else {
                if (state == PLAYER_STOPPED) {
@@ -185,38 +225,169 @@ void PlayerForm::_state_changed(PlayerState state) {
                        ui->doneTimeLabel->setText("00:00");
                        _seek_slider->setEnabled(false);
                }
-               ui->playpauseButton->setIcon(QIcon(":/icons/play.png"));
+               ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
        }
 }
 
 void PlayerForm::_toggle_random() {
        _player->toggleRandom();
        if (_player->random()) {
-               ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
        } else {
-               ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
        }
 }
 
 void PlayerForm::_toggle_repeat() {
        _player->toggleRepeat();
        if (_player->repeat()) {
-               ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
        } else {
-               ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/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);
+       if (dialog.exec() == QDialog::Accepted) {
+               QList<QString> selected = dialog.selected();
+               foreach (QString name, selected) {
+                       Playlist pl = _lib->getPlaylist(name);
+                       pl.addTrack(_current_playlist.tracks().at(id));
+                       _lib->savePlaylist(pl);
+               }
+       }
+}
+
+void PlayerForm::_edit_tags() {
+       QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
+       Track track = _current_playlist.tracks().at(idx.first().row());
+
+       EditTagsDialog dialog(this);
+       dialog.setTrackMetadata(track.metadata());
+       if (dialog.exec() == QDialog::Accepted) {
+               track.setMetadata(dialog.meta());
+               _lib->updateTrackMetadata(track);
+               reload(true);
+       }
+}
+
+void PlayerForm::stop() {
+       _player->stop();
+}
+
+void PlayerForm::_toggle_volume() {
+       if (ui->volumeSlider->isVisible()) {
+               ui->volumeSlider->hide();
+       } else {
+               ui->volumeSlider->show();
+               ui->volumeSlider->setValue(_player->volume());
+       }
+}
+
+void PlayerForm::_volume_changed() {
+       int value = ui->volumeSlider->value();
+       _player->setVolume(value);
+}
+
+void PlayerForm::updateIcons() {
+       Config config;
+       _icons_theme = config.getValue("ui/iconstheme").toString();
+       ui->libraryButton->setIcon(QIcon(":/icons/"+_icons_theme+"/library.png"));
+       if (ui->stackedWidget->currentIndex())
+               ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
+       else
+               ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
+       if (_player->random())
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
+       else
+               ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
+       if (_player->repeat())
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
+       else
+               ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_inactive.png"));
+       ui->prevButton->setIcon(QIcon(":/icons/"+_icons_theme+"/prev.png"));
+       if (_player->state() == PLAYER_PLAYING)
+               ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
+       else
+               ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
+       ui->stopButton->setIcon(QIcon(":/icons/"+_icons_theme+"/stop.png"));
+       ui->nextButton->setIcon(QIcon(":/icons/"+_icons_theme+"/next.png"));
+       ui->volumeButton->setIcon(QIcon(":/icons/"+_icons_theme+"/volume.png"));
+}
+
+void PlayerForm::landscapeMode() {
+       ui->libraryButton->setVisible(true);
+       ui->repeatButton->setVisible(true);
+       ui->randomButton->setVisible(true);
+       ui->volumeButton->setVisible(true);
+}
+
+void PlayerForm::portraitMode() {
+       ui->libraryButton->setVisible(false);
+       ui->repeatButton->setVisible(false);
+       ui->randomButton->setVisible(false);
+       ui->volumeButton->setVisible(false);
 }