2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "playerform.h"
21 #include "ui_playerform.h"
23 #include "player/player.h"
27 #include "trackrenderer.h"
30 using namespace SomePlayer::DataObjects;
31 using namespace SomePlayer::Playback;
33 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
35 QList<Track> tracks = playlist.tracks();
36 int count = tracks.count();
37 _model->setRowCount(count);
38 for (int i = 0; i < count; i++) {
39 TrackMetadata meta = tracks.at(i).metadata();
40 QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album();
41 _model->setItem(i, 0, new QStandardItem(t));
45 PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
47 ui(new Ui::PlayerForm)
50 _player = new Player(this);
53 if (_player->random()) {
54 ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
56 ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
58 if (_player->repeat()) {
59 ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
61 ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
63 _seek_slider = new QSlider(Qt::Horizontal);
64 _seek_slider->setEnabled(false);
65 ui->progressLayout->insertWidget(1, _seek_slider);
66 _seek_slider->setTracking(false);
67 _model = new QStandardItemModel(0, 2, this);
68 ui->playlistView->setModel(_model);
69 _context_menu = new QMenu(ui->playlistView);
70 QAction *delete_action = _context_menu->addAction("Delete");
71 QAction *enqueue_action = _context_menu->addAction("Enqueue");
72 QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
74 _track_renderer = new TrackRenderer(this);
75 ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
77 _tag_resolver = new TagResolver(this);
79 connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
80 connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
81 connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
82 connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
83 connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
84 connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
85 connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
86 connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
87 connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
88 connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
89 connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
90 connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
91 connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
92 connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
93 connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
94 connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
95 connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
96 connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
97 connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
100 PlayerForm::~PlayerForm()
105 void PlayerForm::_library() {
109 void PlayerForm::reload() {
110 if (ui->stackedWidget->currentIndex() == 1) {
111 emit hideSearchPanel();
113 _current_playlist = _lib->getCurrentPlaylist();
114 _player->setPlaylist(_current_playlist);
115 __fill_list(_model, _current_playlist);
118 void PlayerForm::_toggle_view() {
119 int index = ui->stackedWidget->currentIndex();
120 index = (!index % 2);
122 ui->viewButton->setIcon(QIcon(":/icons/playlist.png"));
123 emit hideSearchPanel();
125 ui->viewButton->setIcon(QIcon(":/icons/playback.png"));
126 emit showSearchPanel();
128 ui->stackedWidget->setCurrentIndex(index);
131 void PlayerForm::_process_click(QModelIndex index) {
132 int id = index.row();
134 _player->setTrackId(id);
136 _track_renderer->setActiveRow(id);
137 ui->playlistView->hide();
138 ui->playlistView->show();
141 void PlayerForm::_track_changed(Track track) {
142 int id = _current_playlist.tracks().indexOf(track);
143 QModelIndex index = _model->index(id, 0);
144 ui->playlistView->setCurrentIndex(index);
145 ui->playlistView->scrollTo(index);
146 _track_renderer->setActiveRow(id);
147 ui->playlistView->hide();
148 ui->playlistView->show();
149 _display_track(track);
152 void PlayerForm::_display_track(Track track) {
153 ui->countLabel->setText(QString("%1/%2").
154 arg(_current_playlist.tracks().indexOf(track)+1).
155 arg(_current_playlist.tracks().count()));
156 ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
157 ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
158 arg(track.metadata().artist()).
159 arg(track.metadata().album()));
160 _seek_slider->setMinimum(0);
161 _seek_slider->setMaximum(track.metadata().length());
162 _tick(0, track.metadata().length());
165 void PlayerForm::_tick(int done, int all) {
166 _time->setHMS(0, all/60, all%60);
167 ui->allTimeLabel->setText(_time->toString("mm:ss"));
168 _time->setHMS(0, done/60, done%60);
169 ui->doneTimeLabel->setText(_time->toString("mm:ss"));
170 _seek_slider->setValue(done);
173 void PlayerForm::_slider_released() {
174 _player->seek(_seek_slider->value());
177 void PlayerForm::_custom_context_venu_requested(const QPoint &pos) {
178 _context_menu->exec(pos);
181 void PlayerForm::_delete_track() {
182 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
183 int id = idx.first().row();
184 _current_playlist.removeTrackAt(id);
185 _lib->saveCurrentPlaylist(_current_playlist);
189 void PlayerForm::_enqueue_track() {
190 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
191 int id = idx.first().row();
192 _player->enqueue(id);
195 void PlayerForm::_add_to_favorites() {
196 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
197 int id = idx.first().row();
198 _lib->addToFavorites(_current_playlist.tracks().at(id));
201 void PlayerForm::_state_changed(PlayerState state) {
202 if (state == PLAYER_PLAYING) {
203 ui->playpauseButton->setIcon(QIcon(":/icons/pause.png"));
204 _seek_slider->setEnabled(true);
206 if (state == PLAYER_STOPPED) {
207 _seek_slider->setValue(0);
208 ui->doneTimeLabel->setText("00:00");
209 _seek_slider->setEnabled(false);
211 ui->playpauseButton->setIcon(QIcon(":/icons/play.png"));
215 void PlayerForm::_toggle_random() {
216 _player->toggleRandom();
217 if (_player->random()) {
218 ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
220 ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
224 void PlayerForm::_toggle_repeat() {
225 _player->toggleRepeat();
226 if (_player->repeat()) {
227 ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
229 ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
233 void PlayerForm::search(QString &pattern) {
234 _search_pattern = pattern;
235 _search_current_id = -1;
239 void PlayerForm::nextItem() {
240 QString data = _model->index(_search_current_id, 0).data().toString();
241 for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
242 data = _model->index(i, 0).data().toString();
243 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
244 _search_current_id = i;
248 QModelIndex id = _model->index(_search_current_id, 0);
249 _track_renderer->setSearchRow(_search_current_id);
250 ui->playlistView->scrollTo(id);
251 ui->playlistView->hide();
252 ui->playlistView->show();
255 void PlayerForm::prevItem() {
256 QString data = _model->index(_search_current_id, 0).data().toString();
257 for (int i = _search_current_id-1; i >= 0; i--) {
258 data = _model->index(i, 0).data().toString();
259 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
260 _search_current_id = i;
264 QModelIndex id = _model->index(_search_current_id, 0);
265 _track_renderer->setSearchRow(_search_current_id);
266 ui->playlistView->scrollTo(id);
267 ui->playlistView->hide();
268 ui->playlistView->show();
271 void PlayerForm::cancelSearch() {
272 _search_pattern = "";
273 _track_renderer->setSearchRow(-1);
274 ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
275 ui->playlistView->hide();
276 ui->playlistView->show();
279 void PlayerForm::addFiles(QList<QString> files) {
280 _tag_resolver->decode(files);
283 void PlayerForm::_track_decoded(Track track) {
284 _current_playlist.addTrack(track);
285 __fill_list(_model, _current_playlist);
286 _lib->saveCurrentPlaylist(_current_playlist);
287 _player->setPlaylist(_current_playlist);