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 connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
78 connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
79 connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
80 connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
81 connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
82 connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
83 connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
84 connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
85 connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
86 connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
87 connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
88 connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
89 connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
90 connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
91 connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
92 connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
93 connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
94 connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
97 PlayerForm::~PlayerForm()
102 void PlayerForm::_library() {
106 void PlayerForm::reload() {
107 if (ui->stackedWidget->currentIndex() == 1) {
108 emit hideSearchPanel();
110 _current_playlist = _lib->getCurrentPlaylist();
111 _player->setPlaylist(_current_playlist);
112 __fill_list(_model, _current_playlist);
115 void PlayerForm::_toggle_view() {
116 int index = ui->stackedWidget->currentIndex();
117 index = (!index % 2);
119 ui->viewButton->setIcon(QIcon(":/icons/playlist.png"));
120 emit hideSearchPanel();
122 ui->viewButton->setIcon(QIcon(":/icons/playback.png"));
123 emit showSearchPanel();
125 ui->stackedWidget->setCurrentIndex(index);
128 void PlayerForm::_process_click(QModelIndex index) {
129 int id = index.row();
131 _player->setTrackId(id);
133 _track_renderer->setActiveRow(id);
134 ui->playlistView->hide();
135 ui->playlistView->show();
138 void PlayerForm::_track_changed(Track track) {
139 int id = _current_playlist.tracks().indexOf(track);
140 QModelIndex index = _model->index(id, 0);
141 ui->playlistView->setCurrentIndex(index);
142 ui->playlistView->scrollTo(index);
143 _track_renderer->setActiveRow(id);
144 ui->playlistView->hide();
145 ui->playlistView->show();
146 _display_track(track);
149 void PlayerForm::_display_track(Track track) {
150 ui->countLabel->setText(QString("%1/%2").
151 arg(_current_playlist.tracks().indexOf(track)+1).
152 arg(_current_playlist.tracks().count()));
153 ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
154 ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
155 arg(track.metadata().artist()).
156 arg(track.metadata().album()));
157 _seek_slider->setMinimum(0);
158 _seek_slider->setMaximum(track.metadata().length());
159 _tick(0, track.metadata().length());
162 void PlayerForm::_tick(int done, int all) {
163 _time->setHMS(0, all/60, all%60);
164 ui->allTimeLabel->setText(_time->toString("mm:ss"));
165 _time->setHMS(0, done/60, done%60);
166 ui->doneTimeLabel->setText(_time->toString("mm:ss"));
167 _seek_slider->setValue(done);
170 void PlayerForm::_slider_released() {
171 _player->seek(_seek_slider->value());
174 void PlayerForm::_custom_context_venu_requested(const QPoint &pos) {
175 _context_menu->exec(pos);
178 void PlayerForm::_delete_track() {
179 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
180 int id = idx.first().row();
181 _current_playlist.removeTrackAt(id);
182 _lib->saveCurrentPlaylist(_current_playlist);
186 void PlayerForm::_enqueue_track() {
187 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
188 int id = idx.first().row();
189 _player->enqueue(id);
192 void PlayerForm::_add_to_favorites() {
193 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
194 int id = idx.first().row();
195 _lib->addToFavorites(_current_playlist.tracks().at(id));
198 void PlayerForm::_state_changed(PlayerState state) {
199 if (state == PLAYER_PLAYING) {
200 ui->playpauseButton->setIcon(QIcon(":/icons/pause.png"));
201 _seek_slider->setEnabled(true);
203 if (state == PLAYER_STOPPED) {
204 _seek_slider->setValue(0);
205 ui->doneTimeLabel->setText("00:00");
206 _seek_slider->setEnabled(false);
208 ui->playpauseButton->setIcon(QIcon(":/icons/play.png"));
212 void PlayerForm::_toggle_random() {
213 _player->toggleRandom();
214 if (_player->random()) {
215 ui->randomButton->setIcon(QIcon(":/icons/random_active.png"));
217 ui->randomButton->setIcon(QIcon(":/icons/random_inactive.png"));
221 void PlayerForm::_toggle_repeat() {
222 _player->toggleRepeat();
223 if (_player->repeat()) {
224 ui->repeatButton->setIcon(QIcon(":/icons/repeat_active.png"));
226 ui->repeatButton->setIcon(QIcon(":/icons/repeat_inactive.png"));
230 void PlayerForm::search(QString &pattern) {
231 _search_pattern = pattern;
232 _search_current_id = -1;
236 void PlayerForm::nextItem() {
237 QString data = _model->index(_search_current_id, 0).data().toString();
238 for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
239 data = _model->index(i, 0).data().toString();
240 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
241 _search_current_id = i;
245 QModelIndex id = _model->index(_search_current_id, 0);
246 _track_renderer->setSearchRow(_search_current_id);
247 ui->playlistView->scrollTo(id);
248 ui->playlistView->hide();
249 ui->playlistView->show();
252 void PlayerForm::prevItem() {
253 QString data = _model->index(_search_current_id, 0).data().toString();
254 for (int i = _search_current_id-1; i >= 0; i--) {
255 data = _model->index(i, 0).data().toString();
256 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
257 _search_current_id = i;
261 QModelIndex id = _model->index(_search_current_id, 0);
262 _track_renderer->setSearchRow(_search_current_id);
263 ui->playlistView->scrollTo(id);
264 ui->playlistView->hide();
265 ui->playlistView->show();
268 void PlayerForm::cancelSearch() {
269 _search_pattern = "";
270 _track_renderer->setSearchRow(-1);
271 ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
272 ui->playlistView->hide();
273 ui->playlistView->show();