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)));
123 _dbusadaptor = new DBusAdaptop(_player);
124 QDBusConnection connection = QDBusConnection::sessionBus();
125 bool ret = connection.registerService(_SERVICE_NAME_);
126 ret = connection.registerObject("/", _player);
129 PlayerForm::~PlayerForm()
134 void PlayerForm::_library() {
138 void PlayerForm::reload(bool reread) {
140 _current_playlist = _lib->getCurrentPlaylist();
141 _player->setPlaylist(_current_playlist);
142 __fill_list(_model, _current_playlist);
146 void PlayerForm::_toggle_view() {
147 int index = ui->stackedWidget->currentIndex();
148 index = (!index % 2);
150 ui->viewButton->setIcon(QIcon(":/icons/white/playlist.png"));
151 ui->moreButton->setEnabled(false);
153 ui->viewButton->setIcon(QIcon(":/icons/white/playback.png"));
154 ui->moreButton->setEnabled(true);
156 ui->stackedWidget->setCurrentIndex(index);
159 void PlayerForm::_process_click(QModelIndex index) {
160 int id = index.row();
162 _player->setTrackId(id);
164 _track_renderer->setActiveRow(id);
165 ui->playlistView->hide();
166 ui->playlistView->show();
169 void PlayerForm::_track_changed(Track track) {
170 int id = _current_playlist.tracks().indexOf(track);
171 QModelIndex index = _model->index(id, 0);
172 ui->playlistView->setCurrentIndex(index);
173 ui->playlistView->scrollTo(index);
174 _track_renderer->setActiveRow(id);
175 ui->playlistView->hide();
176 ui->playlistView->show();
177 _display_track(track);
180 void PlayerForm::_display_track(Track track) {
181 ui->countLabel->setText(QString("%1/%2").
182 arg(_current_playlist.tracks().indexOf(track)+1).
183 arg(_current_playlist.tracks().count()));
184 ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
185 ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
186 arg(track.metadata().artist()).
187 arg(track.metadata().album()));
188 ui->seekSlider->setMinimum(0);
189 ui->seekSlider->setMaximum(track.metadata().length());
190 _tick(0, track.metadata().length());
193 void PlayerForm::_tick(int done, int all) {
194 _time->setHMS(0, all/60, all%60);
195 ui->allTimeLabel->setText(_time->toString("mm:ss"));
196 _time->setHMS(0, done/60, done%60);
197 ui->doneTimeLabel->setText(_time->toString("mm:ss"));
198 ui->seekSlider->setValue(done);
201 void PlayerForm::_slider_released() {
202 _player->seek(ui->seekSlider->value());
205 void PlayerForm::_custom_context_menu_requested(const QPoint &pos) {
206 _context_menu->exec(pos);
209 void PlayerForm::_delete_track() {
210 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
211 int id = idx.first().row();
212 int aid = _track_renderer->activeRow();
214 _track_renderer->setActiveRow(aid-1);
215 } else if (aid == id) {
216 _track_renderer->setActiveRow(-1);
218 _current_playlist.removeTrackAt(id);
219 _lib->saveCurrentPlaylist(_current_playlist);
223 void PlayerForm::_enqueue_track() {
224 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
225 int id = idx.first().row();
226 _player->enqueue(id);
229 void PlayerForm::_add_to_favorites() {
230 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
231 int id = idx.first().row();
232 _lib->addToFavorites(_current_playlist.tracks().at(id));
235 void PlayerForm::_state_changed(PlayerState state) {
236 if (state == PLAYER_PLAYING) {
237 ui->playpauseButton->setIcon(QIcon(":/icons/white/pause.png"));
238 ui->seekSlider->setEnabled(true);
240 if (state == PLAYER_STOPPED) {
241 ui->seekSlider->setValue(0);
242 ui->doneTimeLabel->setText("00:00");
243 ui->seekSlider->setEnabled(false);
245 ui->playpauseButton->setIcon(QIcon(":/icons/white/play.png"));
249 void PlayerForm::_toggle_random() {
250 _player->toggleRandom();
251 if (_player->random()) {
252 ui->randomButton->setIcon(QIcon(":/icons/white/random_active.png"));
254 ui->randomButton->setIcon(QIcon(":/icons/white/random_inactive.png"));
258 void PlayerForm::_toggle_repeat() {
259 _player->toggleRepeat();
260 if (_player->repeat()) {
261 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_active.png"));
263 ui->repeatButton->setIcon(QIcon(":/icons/white/repeat_inactive.png"));
267 void PlayerForm::search(QString pattern) {
268 _search_pattern = pattern;
269 _search_current_id = -1;
273 void PlayerForm::nextItem() {
274 QString data = _model->index(_search_current_id, 0).data().toString();
275 for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
276 data = _model->index(i, 0).data().toString();
277 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
278 _search_current_id = i;
282 QModelIndex id = _model->index(_search_current_id, 0);
283 _track_renderer->setSearchRow(_search_current_id);
284 ui->playlistView->scrollTo(id);
285 ui->playlistView->hide();
286 ui->playlistView->show();
289 void PlayerForm::prevItem() {
290 QString data = _model->index(_search_current_id, 0).data().toString();
291 for (int i = _search_current_id-1; i >= 0; i--) {
292 data = _model->index(i, 0).data().toString();
293 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
294 _search_current_id = i;
298 QModelIndex id = _model->index(_search_current_id, 0);
299 _track_renderer->setSearchRow(_search_current_id);
300 ui->playlistView->scrollTo(id);
301 ui->playlistView->hide();
302 ui->playlistView->show();
305 void PlayerForm::cancelSearch() {
306 _search_pattern = "";
307 _track_renderer->setSearchRow(-1);
308 ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
309 ui->playlistView->hide();
310 ui->playlistView->show();
313 void PlayerForm::addFiles(QList<QString> files) {
314 _tag_resolver->decode(files);
317 void PlayerForm::_track_decoded(Track track) {
318 _current_playlist.addTrack(track);
319 __fill_list(_model, _current_playlist);
320 _lib->saveCurrentPlaylist(_current_playlist);
321 _player->setPlaylist(_current_playlist);
324 void PlayerForm::_add_to_playlists() {
325 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
326 int id = idx.first().row();
328 QList<QString> names = _lib->getPlaylistsNames();
329 names.removeOne(_CURRENT_PLAYLIST_SUBST_);
330 PlaylistDialog dialog(names, this);
331 if (dialog.exec() == QDialog::Accepted) {
332 QList<QString> selected = dialog.selected();
333 foreach (QString name, selected) {
334 Playlist pl = _lib->getPlaylist(name);
335 pl.addTrack(_current_playlist.tracks().at(id));
336 _lib->savePlaylist(pl);
341 void PlayerForm::_edit_tags() {
342 QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
343 Track track = _current_playlist.tracks().at(idx.first().row());
345 EditTagsDialog dialog(this);
346 dialog.setTrackMetadata(track.metadata());
347 if (dialog.exec() == QDialog::Accepted) {
348 track.setMetadata(dialog.meta());
349 _lib->updateTrackMetadata(track);
354 void PlayerForm::stop() {
358 void PlayerForm::_toggle_volume() {
359 if (ui->volumeSlider->isVisible()) {
360 ui->volumeSlider->hide();
362 ui->volumeSlider->show();
363 ui->volumeSlider->setValue(_player->volume());
367 void PlayerForm::_volume_changed() {
368 int value = ui->volumeSlider->value();
369 _player->setVolume(value);
372 void PlayerForm::landscapeMode() {
373 ui->progressLayout->removeItem(ui->seekSpacer);
374 ui->progressLayout->insertWidget(1, ui->seekSlider);
375 ui->progressWidget->setVisible(false);
378 void PlayerForm::portraitMode() {
379 ui->progressLayout->insertSpacerItem(1, ui->seekSpacer);
380 ui->progressWidget->layout()->addWidget(ui->seekSlider);
381 ui->progressWidget->setVisible(true);
384 void PlayerForm::_tools_widget_toggle() {
385 if (_tools_widget->isVisible()) {
386 ui->moreButton->setIcon(QIcon(":/icons/white/more.png"));
387 _tools_widget->hide();
390 ui->moreButton->setIcon(QIcon(":/icons/white/unmore.png"));
391 _tools_widget->show();