e0fab75fa6a565e3889cd7eccf8e73d07f0546e5
[someplayer] / src / playerform.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "playerform.h"
21 #include "ui_playerform.h"
22 #include "library.h"
23 #include "player/player.h"
24 #include <QTime>
25 #include <QSlider>
26 #include "trackrenderer.h"
27 #include <QResource>
28 #include "playlistdialog.h"
29 #include "edittagsdialog.h"
30 #include "config.h"
31
32 using namespace SomePlayer::DataObjects;
33 using namespace SomePlayer::Playback;
34 using namespace SomePlayer::Storage;
35
36 inline void __fill_list(QStandardItemModel *_model, Playlist playlist) {
37         _model->clear();
38         QList<Track> tracks = playlist.tracks();
39         int count = tracks.count();
40         _model->setRowCount(count);
41         for (int i = 0; i < count; i++) {
42                 TrackMetadata meta = tracks.at(i).metadata();
43                 QString t = meta.title()+"#_#"+meta.artist()+"#_#"+meta.album();
44                 _model->setItem(i, 0, new QStandardItem(t));
45         }
46 }
47
48 PlayerForm::PlayerForm(Library* lib, QWidget *parent) :
49     QWidget(parent),
50     ui(new Ui::PlayerForm)
51 {
52         _lib = lib;
53         _player = new Player(this);
54         _time = new QTime();
55         Config config;
56         _icons_theme = config.getValue("ui/iconstheme").toString();
57         ui->setupUi(this);
58         if (_player->random()) {
59                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
60         } else {
61                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
62         }
63         if (_player->repeat()) {
64                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
65         } else {
66                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_inactive.png"));
67         }
68         ui->volumeSlider->setMinimum(0);
69         ui->volumeSlider->setMaximum(100);
70         ui->volumeSlider->hide();
71         _seek_slider = new QSlider(Qt::Horizontal);
72         _seek_slider->setEnabled(false);
73         ui->progressLayout->insertWidget(1, _seek_slider);
74         _seek_slider->setTracking(false);
75         _model = new QStandardItemModel(0, 2, this);
76         ui->playlistView->setModel(_model);
77         _context_menu = new QMenu(ui->playlistView);
78         QAction *delete_action = _context_menu->addAction("Delete");
79         QAction *add_to_favorites = _context_menu->addAction("Add to favorites");
80         QAction *enqueue_action = _context_menu->addAction("Enqueue");
81         QAction *add_to_playlists = _context_menu->addAction("Add to playlists");
82         QAction *edit_tags = _context_menu->addAction("Edit tags");
83
84         _track_renderer = new TrackRenderer(this);
85         ui->playlistView->setItemDelegateForColumn(0, _track_renderer);
86
87         _tag_resolver = new TagResolver(this);
88
89         connect(ui->libraryButton, SIGNAL(clicked()), this, SLOT(_library()));
90         connect(ui->viewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
91         connect(ui->aViewButton, SIGNAL(clicked()), this, SLOT(_toggle_view()));
92         connect(ui->playlistView, SIGNAL(clicked(QModelIndex)), this, SLOT(_process_click(QModelIndex)));
93         connect(ui->playpauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
94         connect(ui->aPlayPauseButton, SIGNAL(clicked()), _player, SLOT(toggle()));
95         connect(ui->stopButton, SIGNAL(clicked()), _player, SLOT(stop()));
96         connect(ui->aStopButton, SIGNAL(clicked()), _player, SLOT(stop()));
97         connect(ui->nextButton, SIGNAL(clicked()), _player, SLOT(next()));
98         connect(ui->aNextButton, SIGNAL(clicked()), _player, SLOT(next()));
99         connect(ui->prevButton, SIGNAL(clicked()), _player, SLOT(prev()));
100         connect(ui->aPrevButton, SIGNAL(clicked()), _player, SLOT(prev()));
101         connect(_player, SIGNAL(trackChanged(Track)), this, SLOT(_track_changed(Track)));
102         connect(_player, SIGNAL(tick(int,int)), this, SLOT(_tick(int,int)));
103         connect(ui->randomButton, SIGNAL(clicked()), this, SLOT(_toggle_random()));
104         connect(ui->repeatButton, SIGNAL(clicked()), this, SLOT(_toggle_repeat()));
105         connect(_seek_slider, SIGNAL(sliderMoved(int)), _player, SLOT(seek(int)));
106         //connect(_seek_slider, SIGNAL(sliderReleased()), this, SLOT(_slider_released()));
107         connect(ui->volumeSlider, SIGNAL(sliderMoved(int)), _player, SLOT(setVolume(int)));
108         connect(ui->playlistView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(_custom_context_venu_requested(QPoint)));
109         connect(delete_action, SIGNAL(triggered()), this, SLOT(_delete_track()));
110         connect(enqueue_action, SIGNAL(triggered()), this, SLOT(_enqueue_track()));
111         connect(add_to_favorites, SIGNAL(triggered()), this, SLOT(_add_to_favorites()));
112         connect(add_to_playlists, SIGNAL(triggered()), this, SLOT(_add_to_playlists()));
113         connect(edit_tags, SIGNAL(triggered()), this, SLOT(_edit_tags()));
114         connect(_player, SIGNAL(stateChanged(PlayerState)), this, SLOT(_state_changed(PlayerState)));
115         connect(_player, SIGNAL(trackDone(Track)), _lib, SLOT(updateTrackCount(Track)));
116         connect(_tag_resolver, SIGNAL(decoded(Track)), this, SLOT(_track_decoded(Track)));
117         connect(ui->volumeButton, SIGNAL(clicked()), this, SLOT(_toggle_volume()));
118         ui->topWidget->setVisible(false);
119 }
120
121 PlayerForm::~PlayerForm()
122 {
123     delete ui;
124 }
125
126 void PlayerForm::_library() {
127         emit library();
128 }
129
130 void PlayerForm::reload(bool reread) {
131         if (ui->stackedWidget->currentIndex() == 1) {
132                 emit hideSearchPanel();
133         }
134         if (reread) {
135                 _current_playlist = _lib->getCurrentPlaylist();
136                 _player->setPlaylist(_current_playlist);
137                 __fill_list(_model, _current_playlist);
138         }
139 }
140
141 void PlayerForm::_toggle_view() {
142         int index = ui->stackedWidget->currentIndex();
143         index = (!index % 2);
144         if (index) {
145                 ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
146                 ui->aViewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
147                 emit hideSearchPanel();
148         } else {
149                 ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
150                 ui->aViewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
151                 emit showSearchPanel();
152         }
153         ui->stackedWidget->setCurrentIndex(index);
154 }
155
156 void PlayerForm::_process_click(QModelIndex index) {
157         int id = index.row();
158         _player->stop();
159         _player->setTrackId(id);
160         _player->play();
161         _track_renderer->setActiveRow(id);
162         ui->playlistView->hide();
163         ui->playlistView->show();
164 }
165
166 void PlayerForm::_track_changed(Track track) {
167         int id = _current_playlist.tracks().indexOf(track);
168         QModelIndex index = _model->index(id, 0);
169         ui->playlistView->setCurrentIndex(index);
170         ui->playlistView->scrollTo(index);
171         _track_renderer->setActiveRow(id);
172         ui->playlistView->hide();
173         ui->playlistView->show();
174         _display_track(track);
175 }
176
177 void PlayerForm::_display_track(Track track) {
178         ui->countLabel->setText(QString("%1/%2").
179                                                         arg(_current_playlist.tracks().indexOf(track)+1).
180                                                         arg(_current_playlist.tracks().count()));
181         ui->titleLabel->setText(QString("<h3>%1</h3>").arg(track.metadata().title()));
182         ui->artistAlbumLabel->setText(QString("<b>%1</b><br/>%2").
183                                                                   arg(track.metadata().artist()).
184                                                                   arg(track.metadata().album()));
185         _seek_slider->setMinimum(0);
186         _seek_slider->setMaximum(track.metadata().length());
187         _tick(0, track.metadata().length());
188 }
189
190 void PlayerForm::_tick(int done, int all) {
191         _time->setHMS(0, all/60, all%60);
192         ui->allTimeLabel->setText(_time->toString("mm:ss"));
193         _time->setHMS(0, done/60, done%60);
194         ui->doneTimeLabel->setText(_time->toString("mm:ss"));
195         _seek_slider->setValue(done);
196 }
197
198 void PlayerForm::_slider_released() {
199         _player->seek(_seek_slider->value());
200 }
201
202 void PlayerForm::_custom_context_venu_requested(const QPoint &pos) {
203         _context_menu->exec(pos);
204 }
205
206 void PlayerForm::_delete_track() {
207         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
208         int id = idx.first().row();
209         _current_playlist.removeTrackAt(id);
210         _lib->saveCurrentPlaylist(_current_playlist);
211         reload(true);
212 }
213
214 void PlayerForm::_enqueue_track() {
215         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
216         int id = idx.first().row();
217         _player->enqueue(id);
218 }
219
220 void PlayerForm::_add_to_favorites() {
221         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
222         int id = idx.first().row();
223         _lib->addToFavorites(_current_playlist.tracks().at(id));
224 }
225
226 void PlayerForm::_state_changed(PlayerState state) {
227         if (state == PLAYER_PLAYING) {
228                 ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
229                 ui->aPlayPauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
230                 _seek_slider->setEnabled(true);
231         } else {
232                 if (state == PLAYER_STOPPED) {
233                         _seek_slider->setValue(0);
234                         ui->doneTimeLabel->setText("00:00");
235                         _seek_slider->setEnabled(false);
236                 }
237                 ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
238                 ui->aPlayPauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
239         }
240 }
241
242 void PlayerForm::_toggle_random() {
243         _player->toggleRandom();
244         if (_player->random()) {
245                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
246         } else {
247                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
248         }
249 }
250
251 void PlayerForm::_toggle_repeat() {
252         _player->toggleRepeat();
253         if (_player->repeat()) {
254                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
255         } else {
256                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_inactive.png"));
257         }
258 }
259
260 void PlayerForm::search(QString &pattern) {
261         _search_pattern = pattern;
262         _search_current_id = -1;
263         nextItem();
264 }
265
266 void PlayerForm::nextItem() {
267         QString data = _model->index(_search_current_id, 0).data().toString();
268         for (int i = _search_current_id+1; i < _model->rowCount(); i++) {
269                 data = _model->index(i, 0).data().toString();
270                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
271                         _search_current_id = i;
272                         break;
273                 }
274         }
275         QModelIndex id = _model->index(_search_current_id, 0);
276         _track_renderer->setSearchRow(_search_current_id);
277         ui->playlistView->scrollTo(id);
278         ui->playlistView->hide();
279         ui->playlistView->show();
280 }
281
282 void PlayerForm::prevItem() {
283         QString data = _model->index(_search_current_id, 0).data().toString();
284         for (int i = _search_current_id-1; i >= 0; i--) {
285                 data = _model->index(i, 0).data().toString();
286                 if (data.contains(_search_pattern, Qt::CaseInsensitive)) {
287                         _search_current_id = i;
288                         break;
289                 }
290         }
291         QModelIndex id = _model->index(_search_current_id, 0);
292         _track_renderer->setSearchRow(_search_current_id);
293         ui->playlistView->scrollTo(id);
294         ui->playlistView->hide();
295         ui->playlistView->show();
296 }
297
298 void PlayerForm::cancelSearch() {
299         _search_pattern = "";
300         _track_renderer->setSearchRow(-1);
301         ui->playlistView->scrollTo(_model->index(_track_renderer->activeRow(), 0));
302         ui->playlistView->hide();
303         ui->playlistView->show();
304 }
305
306 void PlayerForm::addFiles(QList<QString> files) {
307         _tag_resolver->decode(files);
308 }
309
310 void PlayerForm::_track_decoded(Track track) {
311         _current_playlist.addTrack(track);
312         __fill_list(_model, _current_playlist);
313         _lib->saveCurrentPlaylist(_current_playlist);
314         _player->setPlaylist(_current_playlist);
315 }
316
317 void PlayerForm::_add_to_playlists() {
318         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
319         int id = idx.first().row();
320
321         QList<QString> names = _lib->getPlaylistsNames();
322         names.removeOne(_CURRENT_PLAYLIST_SUBST_);
323         PlaylistDialog dialog(names, this);
324         if (dialog.exec() == QDialog::Accepted) {
325                 QList<QString> selected = dialog.selected();
326                 foreach (QString name, selected) {
327                         Playlist pl = _lib->getPlaylist(name);
328                         pl.addTrack(_current_playlist.tracks().at(id));
329                         _lib->savePlaylist(pl);
330                 }
331         }
332 }
333
334 void PlayerForm::_edit_tags() {
335         QList<QModelIndex> idx = ui->playlistView->selectionModel()->selectedIndexes();
336         Track track = _current_playlist.tracks().at(idx.first().row());
337
338         EditTagsDialog dialog(this);
339         dialog.setTrackMetadata(track.metadata());
340         if (dialog.exec() == QDialog::Accepted) {
341                 track.setMetadata(dialog.meta());
342                 _lib->updateTrackMetadata(track);
343                 reload(true);
344         }
345 }
346
347 void PlayerForm::stop() {
348         _player->stop();
349 }
350
351 void PlayerForm::_toggle_volume() {
352         if (ui->volumeSlider->isVisible()) {
353                 ui->volumeSlider->hide();
354         } else {
355                 ui->volumeSlider->show();
356                 ui->volumeSlider->setValue(_player->volume());
357         }
358 }
359
360 void PlayerForm::_volume_changed() {
361         int value = ui->volumeSlider->value();
362         _player->setVolume(value);
363 }
364
365 void PlayerForm::updateIcons() {
366         Config config;
367         _icons_theme = config.getValue("ui/iconstheme").toString();
368         ui->libraryButton->setIcon(QIcon(":/icons/"+_icons_theme+"/library.png"));
369         if (ui->stackedWidget->currentIndex()) {
370                 ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
371                 ui->aViewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playlist.png"));
372         } else {
373                 ui->viewButton->setIcon(QIcon(":/icons/"+_icons_theme+"/playback.png"));
374         }
375         if (_player->random())
376                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_active.png"));
377         else
378                 ui->randomButton->setIcon(QIcon(":/icons/"+_icons_theme+"/random_inactive.png"));
379         if (_player->repeat())
380                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_active.png"));
381         else
382                 ui->repeatButton->setIcon(QIcon(":/icons/"+_icons_theme+"/repeat_inactive.png"));
383         ui->prevButton->setIcon(QIcon(":/icons/"+_icons_theme+"/prev.png"));
384         ui->aPrevButton->setIcon(QIcon(":/icons/"+_icons_theme+"/prev.png"));
385         if (_player->state() == PLAYER_PLAYING) {
386                 ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
387                 ui->aPlayPauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/pause.png"));
388         } else {
389                 ui->playpauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
390                 ui->aPlayPauseButton->setIcon(QIcon(":/icons/"+_icons_theme+"/play.png"));
391         }
392         ui->stopButton->setIcon(QIcon(":/icons/"+_icons_theme+"/stop.png"));
393         ui->aStopButton->setIcon(QIcon(":/icons/"+_icons_theme+"/stop.png"));
394         ui->nextButton->setIcon(QIcon(":/icons/"+_icons_theme+"/next.png"));
395         ui->aNextButton->setIcon(QIcon(":/icons/"+_icons_theme+"/next.png"));
396         ui->volumeButton->setIcon(QIcon(":/icons/"+_icons_theme+"/volume.png"));
397 }
398
399 void PlayerForm::landscapeMode() {
400         ui->bottomWidget->setVisible(true);
401         ui->topWidget->setVisible(false);
402 }
403
404 void PlayerForm::portraitMode() {
405         ui->bottomWidget->setVisible(false);
406         ui->volumeSlider->setVisible(false);
407         ui->topWidget->setVisible(true);
408 }