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"
26 #include "trackrenderer.h"
28 #include "playlistdialog.h"
29 #include "edittagsdialog.h"
30 #include <QSpacerItem>
33 using namespace SomePlayer::DataObjects;
34 using namespace SomePlayer::Playback;
35 using namespace SomePlayer::Storage;
37 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
39 QList<Track> tracks = playlist.tracks();
40 int count = tracks.count();
41 _model->setRowCount(count);
43 for (int i = 0; i < count; i++) {
44 TrackMetadata meta = tracks.at(i).metadata();
45 time.setHMS(0, meta.length()/60, meta.length() % 60);
46 QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album()+"#_#"+time.toString("mm:ss");
47 _model->setItem(i, 0, new QStandardItem(t));
51 PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
53 ui(new Ui::PlayerForm)
56 _player = new Player(this);
59 if (_player->random()) {
60 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
62 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
64 if (_player->repeat()) {
65 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
67 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
69 ui->volumeSlider->setMinimum(0);
70 ui->volumeSlider->setMaximum(100);
71 ui->volumeSlider->hide();
72 ui->seekSlider->setEnabled(false);
73 ui->progressLayout->removeItem(ui->seekSpacer);
74 _tools_widget = new ToolsWidget(this);
75 ui->toolsLayout->insertWidget(0, _tools_widget);
76 _tools_widget->hide();
77 _model = new QStandardItemModel(0, 2, this);
78 ui->playlistView->setModel(_model);
79 _context_menu = new QMenu(ui->playlistView);
80 QAction *delete_action = _context_menu->addAction("Delete");
81 QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
82 QAction *enqueue_action = _context_menu->addAction("Enqueue");
83 QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
84 QAction *edit_tags = _context_menu->addAction("Edit tags");
86 _track_renderer = new TrackRenderer(this);
87 _track_renderer->setActiveRow(-1);
88 _track_renderer->setSearchRow(-1);
89 ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
91 _tag_resolver = new TagResolver(this);
93 connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
94 connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
95 connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
96 connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
97 connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
98 connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
99 connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
100 connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
101 connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
102 connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
103 connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
104 connect(ui->seekSlider, SIGNAL(sliderMoved(int)), _player, SLOT(seek(int)));
105 connect(ui->volumeSlider, SIGNAL(sliderMoved(int)), _player, SLOT(setVolume(int)));
106 connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_menu_requested(QPoint)));
107 connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
108 connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
109 connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
110 connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
111 connect(edit_tags, SIGNAL(triggered()), this, SLOT(_edit_tags()));
112 connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
113 connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
114 connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
115 connect(ui->volumeButton, SIGNAL(clicked()), this, SLOT(_toggle_volume()));
116 connect(ui->moreButton, SIGNAL(clicked()), this, SLOT(_tools_widget_toggle()));
117 connect(_tools_widget, SIGNAL(search(QString)), this, SLOT(search(QString)));
118 connect(_tools_widget, SIGNAL(nextSearch()), this, SLOT(nextItem()));
119 connect(_tools_widget, SIGNAL(prevSearch()), this, SLOT(prevItem()));
120 connect(_tools_widget, SIGNAL(toggleFullscreen(bool)), this, SIGNAL(fullscreen(bool)));
121 ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
124 _dbusadaptor = new DBusAdaptop(_player);
125 QDBusConnection connection = QDBusConnection::sessionBus();
126 bool ret = connection.registerService(_SERVICE_NAME_);
127 ret = connection.registerObject("/", _player);
130 PlayerForm::~PlayerForm()
135 void PlayerForm::_library() {
139 void PlayerForm::reload(bool reread) {
141 _current_playlist = _lib->getCurrentPlaylist();
142 _player->setPlaylist(_current_playlist);
143 __fill_list(_model, _current_playlist);
147 void PlayerForm::_toggle_view() {
148 int index = ui->stackedWidget->currentIndex();
149 index = (!index % 2);
151 ui->viewButton->setIcon(QIcon(":/icons/white/playlist.png"));
152 ui->moreButton->setEnabled(false);
154 ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
155 ui->moreButton->setEnabled(true);
157 ui->stackedWidget->setCurrentIndex(index);
160 void PlayerForm::_process_click(QModelIndex index) {
161 int id = index.row();
163 _player->setTrackId(id);
165 _track_renderer->setActiveRow(id);
166 ui->playlistView->hide();
167 ui->playlistView->show();
170 void PlayerForm::_track_changed(Track track) {
171 int id = _current_playlist.tracks().indexOf(track);
172 QModelIndex index = _model->index(id, 0);
173 ui->playlistView->setCurrentIndex(index);
174 ui->playlistView->scrollTo(index);
175 _track_renderer->setActiveRow(id);
176 ui->playlistView->hide();
177 ui->playlistView->show();
178 _display_track(track);
181 void PlayerForm::_display_track(Track track) {
182 ui->countLabel->setText(QString("%1/%2").
183 arg(_current_playlist.tracks().indexOf(track)+1).
184 arg(_current_playlist.tracks().count()));
185 ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
186 ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
187 arg(track.metadata().artist()).
188 arg(track.metadata().album()));
189 ui->seekSlider->setMinimum(0);
190 ui->seekSlider->setMaximum(track.metadata().length());
191 _tick(0, track.metadata().length());
194 void PlayerForm::_tick(int done, int all) {
195 _time->setHMS(0, all/60, all%60);
196 ui->allTimeLabel->setText(_time->toString("mm:ss"));
197 _time->setHMS(0, done/60, done%60);
198 ui->doneTimeLabel->setText(_time->toString("mm:ss"));
199 ui->seekSlider->setValue(done);
202 void PlayerForm::_slider_released() {
203 _player->seek(ui->seekSlider->value());
206 void PlayerForm::_custom_context_menu_requested(const QPoint &pos) {
207 _context_menu->exec(pos);
210 void PlayerForm::_delete_track() {
211 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
212 int id = idx.first().row();
213 int aid = _track_renderer->activeRow();
215 _track_renderer->setActiveRow(aid-1);
216 } else if (aid == id) {
217 _track_renderer->setActiveRow(-1);
219 _current_playlist.removeTrackAt(id);
220 _lib->saveCurrentPlaylist(_current_playlist);
224 void PlayerForm::_enqueue_track() {
225 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
226 int id = idx.first().row();
227 _player->enqueue(id);
230 void PlayerForm::_add_to_favorites() {
231 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
232 int id = idx.first().row();
233 _lib->addToFavorites(_current_playlist.tracks().at(id));
236 void PlayerForm::_state_changed(PlayerState state) {
237 if (state == PLAYER_PLAYING) {
238 ui->playpauseButton->setIcon(QIcon(":/icons/white/pause.png"));
239 ui->seekSlider->setEnabled(true);
241 if (state == PLAYER_STOPPED) {
242 ui->seekSlider->setValue(0);
243 ui->doneTimeLabel->setText("00:00");
244 ui->seekSlider->setEnabled(false);
246 ui->playpauseButton->setIcon(QIcon(":/icons/white/play.png"));
250 void PlayerForm::_toggle_random() {
251 _player->toggleRandom();
252 if (_player->random()) {
253 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
255 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
259 void PlayerForm::_toggle_repeat() {
260 _player->toggleRepeat();
261 if (_player->repeat()) {
262 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
264 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
268 void PlayerForm::search(QString pattern) {
269 _search_pattern = pattern;
270 _search_current_id = -1;
274 void PlayerForm::nextItem() {
275 QString data = _model->index(_search_current_id, 0).data().toString();
276 for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
277 data = _model->index(i, 0).data().toString();
278 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
279 _search_current_id = i;
283 QModelIndex id = _model->index(_search_current_id, 0);
284 _track_renderer->setSearchRow(_search_current_id);
285 ui->playlistView->scrollTo(id);
286 ui->playlistView->hide();
287 ui->playlistView->show();
290 void PlayerForm::prevItem() {
291 QString data = _model->index(_search_current_id, 0).data().toString();
292 for (int i = _search_current_id-1; i >= 0; i--) {
293 data = _model->index(i, 0).data().toString();
294 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
295 _search_current_id = i;
299 QModelIndex id = _model->index(_search_current_id, 0);
300 _track_renderer->setSearchRow(_search_current_id);
301 ui->playlistView->scrollTo(id);
302 ui->playlistView->hide();
303 ui->playlistView->show();
306 void PlayerForm::cancelSearch() {
307 _search_pattern = "";
308 _track_renderer->setSearchRow(-1);
309 ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
310 ui->playlistView->hide();
311 ui->playlistView->show();
314 void PlayerForm::addFiles(QList<QString> files) {
315 _tag_resolver->decode(files);
318 void PlayerForm::_track_decoded(Track track) {
319 _current_playlist.addTrack(track);
320 __fill_list(_model, _current_playlist);
321 _lib->saveCurrentPlaylist(_current_playlist);
322 _player->setPlaylist(_current_playlist);
325 void PlayerForm::_add_to_playlists() {
326 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
327 int id = idx.first().row();
329 QList<QString> names = _lib->getPlaylistsNames();
330 names.removeOne(_CURRENT_PLAYLIST_SUBST_);
331 PlaylistDialog dialog(names, this);
332 if (dialog.exec() == QDialog::Accepted) {
333 QList<QString> selected = dialog.selected();
334 foreach (QString name, selected) {
335 Playlist pl = _lib->getPlaylist(name);
336 pl.addTrack(_current_playlist.tracks().at(id));
337 _lib->savePlaylist(pl);
342 void PlayerForm::_edit_tags() {
343 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
344 Track track = _current_playlist.tracks().at(idx.first().row());
346 EditTagsDialog dialog(this);
347 dialog.setTrackMetadata(track.metadata());
348 if (dialog.exec() == QDialog::Accepted) {
349 track.setMetadata(dialog.meta());
350 _lib->updateTrackMetadata(track);
355 void PlayerForm::stop() {
359 void PlayerForm::_toggle_volume() {
360 if (ui->volumeSlider->isVisible()) {
361 ui->volumeSlider->hide();
363 ui->volumeSlider->show();
364 ui->volumeSlider->setValue(_player->volume());
368 void PlayerForm::_volume_changed() {
369 int value = ui->volumeSlider->value();
370 _player->setVolume(value);
373 void PlayerForm::landscapeMode() {
374 ui->progressLayout->removeItem(ui->seekSpacer);
375 ui->progressLayout->insertWidget(1, ui->seekSlider);
376 ui->progressWidget->setVisible(false);
379 void PlayerForm::portraitMode() {
380 ui->progressLayout->insertSpacerItem(1, ui->seekSpacer);
381 ui->progressWidget->layout()->addWidget(ui->seekSlider);
382 ui->progressWidget->setVisible(true);
385 void PlayerForm::_tools_widget_toggle() {
386 if (_tools_widget->isVisible()) {
387 ui->moreButton->setIcon(QIcon(":/icons/white/more.png"));
388 _tools_widget->hide();
389 _tools_widget->reset();
392 ui->moreButton->setIcon(QIcon(":/icons/white/unmore.png"));
393 _tools_widget->show();
394 _tools_widget->setFocus();