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 "mainwindow.h"
21 #include "ui_mainwindow.h"
22 #include <QFileDialog>
23 #include <QMessageBox>
24 #include <QInputDialog>
26 #include <QDesktopWidget>
28 #include "player/player.h"
31 #include "timerdialog.h"
32 #include "equalizerdialog.h"
33 #include "saveplaylistdialog.h"
34 #include "settingsdialog.h"
36 using namespace SomePlayer::DataObjects;
37 using namespace SomePlayer::Storage;
39 MainWindow::MainWindow(QWidget *parent) :
41 ui(new Ui::MainWindow)
44 _icons_theme = config.getValue("ui/iconstheme").toString();
45 _library = new Library(config.applicationDir(), config.applicationDir());
47 connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
48 connect(ui->actionSettings, SIGNAL(triggered()), this, SLOT(settings()));
50 _player_form = new PlayerForm(_library, ui->stackedWidget);
51 _library_form = new LibraryForm(_library, ui->stackedWidget);
52 _busy_widget = new BusyWidget(ui->stackedWidget);
53 _timer = new QTimer(this);
54 _equalizer_dialog = new EqualizerDialog(this);
55 ui->stackedWidget->insertWidget(0, _player_form);
56 ui->stackedWidget->insertWidget(1, _library_form);
57 ui->stackedWidget->insertWidget(2, _busy_widget);
58 QAction *add_directory = ui->menuLibrary->addAction("Add directory");
59 QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
60 QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
61 QAction *add_files = ui->menuLibrary->addAction("Add file to current playlist");
62 QAction *set_timer = ui->menuBar->addAction("Set timer");
63 QAction *equalizer = ui->menuBar->addAction("Equalizer");
64 connect(_player_form, SIGNAL(library()), this, SLOT(library()));
65 connect(_library_form, SIGNAL(player(bool)), this, SLOT(player(bool)));
66 connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
67 connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
68 connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
69 connect(add_files, SIGNAL(triggered()), this, SLOT(_add_files()));
70 connect(set_timer, SIGNAL(triggered()), this, SLOT(_set_timer()));
71 connect(equalizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
72 connect(_library, SIGNAL(done()), this, SLOT(library()));
73 connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
74 connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
75 connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
76 connect(_library_form, SIGNAL(done()), this, SLOT(library()));
77 connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
78 connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
79 connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
80 connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
81 connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
82 connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
83 connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
84 connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
85 connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
86 connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
87 connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
88 connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
89 connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
91 _library_form->updateIcons();
92 _player_form->updateIcons();
94 _player_form->reload(true);
98 MainWindow::~MainWindow()
101 delete _library_form;
105 void MainWindow::about() {
106 QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
107 "written in C++ with Qt4\n\n"
108 "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
111 void MainWindow::player(bool reread) {
113 setAttribute(Qt::WA_Maemo5AutoOrientation, config.getValue("ui/portraitmode").toString() != "disabled");
114 ui->stackedWidget->setCurrentIndex(0);
115 _player_form->reload(reread);
116 setWindowTitle("SomePlayer");
119 void MainWindow::library() {
120 setAttribute(Qt::WA_Maemo5AutoOrientation, false);
121 ui->menuBar->setEnabled(true);
122 ui->stackedWidget->setCurrentIndex(1);
124 setWindowTitle("SomePlayer Library");
127 void MainWindow::_add_directory() {
128 QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
129 if (!directory.isEmpty()) {
130 showBusyWidget("<H1>Scanning... Please wait</H1>");
131 _library->addDirectory(directory);
135 void MainWindow::_save_playlist() {
136 QList<QString> playlists = _library->getPlaylistsNames();
137 playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
138 SavePlaylistDialog dialog(this);
139 dialog.setPlaylistNames(playlists);
140 if (dialog.exec() == QDialog::Accepted) {
141 QString name = dialog.selectedName();
143 if (playlists.contains(name)) {
144 if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
145 "Dow you want to append current playlist to it?",
146 QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
153 Playlist cur = _library->getCurrentPlaylist();
154 Playlist target = _library->getPlaylist(name);
155 QList<Track> tracks = cur.tracks();
156 foreach (Track track, tracks) {
157 target.addTrack(track);
159 _library->savePlaylist(target);
161 Playlist playlist = _library->getCurrentPlaylist();
162 playlist.setName(name);
163 _library->savePlaylist(playlist);
168 void MainWindow::_clear_current_playlist() {
169 Playlist playlist = _library->getCurrentPlaylist();
171 _library->saveCurrentPlaylist(playlist);
172 _player_form->reload(true);
175 void MainWindow::showBusyWidget(QString caption) {
176 _busy_widget->setText(caption);
177 ui->menuBar->setEnabled(false);
179 ui->stackedWidget->setCurrentIndex(2);
182 void MainWindow::_toggle_search_line() {
183 if (ui->searchLine->isVisible()) {
184 ui->searchLine->setText("");
185 ui->searchLine->hide();
186 ui->nextButton->hide();
187 ui->prevButton->hide();
190 ui->searchLine->show();
191 ui->nextButton->show();
192 ui->prevButton->show();
193 ui->searchLine->setFocus(Qt::MouseFocusReason);
197 void MainWindow::showSearchPanel() {
198 ui->searchButton->show();
201 void MainWindow::hideSearchPanel() {
202 ui->searchLine->setText("");
203 ui->searchLine->hide();
204 ui->nextButton->hide();
205 ui->prevButton->hide();
206 ui->searchButton->hide();
210 void MainWindow::_search(QString pattern) {
211 if (ui->stackedWidget->currentIndex() == 0) { // player
212 _player_form->search(pattern);
213 } else if (ui->stackedWidget->currentIndex() == 1) { // library
214 _library_form->search(pattern);
218 void MainWindow::_nextItem() {
219 if (ui->stackedWidget->currentIndex() == 0) { // player
220 _player_form->nextItem();
221 } else if (ui->stackedWidget->currentIndex() == 1) { // library
222 _library_form->nextItem();
226 void MainWindow::_prevItem() {
227 if (ui->stackedWidget->currentIndex() == 0) { // player
228 _player_form->prevItem();
229 } else if (ui->stackedWidget->currentIndex() == 1) { // library
230 _library_form->prevItem();
234 void MainWindow::_cancelSearch() {
235 if (ui->stackedWidget->currentIndex() == 0) { // player
236 _player_form->cancelSearch();
237 } else if (ui->stackedWidget->currentIndex() == 1) { // library
238 _library_form->cancelSearch();
242 void MainWindow::_toggle_full_screen() {
243 if (isFullScreen()) {
244 ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/fullscreen.png"));
247 ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/window.png"));
252 void MainWindow::_add_files() {
253 QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
254 if (!files.isEmpty()) _player_form->addFiles(files);
257 void MainWindow::_set_timer() {
258 TimerDialog dialog(this);
260 if (_timer->isActive()) {
261 dialog.showDisable();
262 int msec = _timer->interval();
263 int h = msec/3600000;
264 int m = msec/60000 - h * 60;
265 int s = msec/1000 - h * 3600 - m * 60;
266 dialog.setTime(h, m, s);
268 if (QDialog::Accepted == dialog.exec()) {
269 if (!dialog.timerDisabled()) {
271 dialog.getTime(&h, &m, &s);
272 _timer->setInterval(h*3600000+m*60000+s*1000);
274 } else if (_timer->isActive()) {
280 void MainWindow::_timeout() {
281 _player_form->stop();
285 void MainWindow::_equalizer() {
286 if (_player_form->isEqualizerAvailable()) {
288 for (int i = 0; i < 10; i++) {
289 _player_form->equalizerValue(i, &val);
290 _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
292 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
293 _equalizer_dialog->reloadPresets();
294 _equalizer_dialog->exec();
296 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
300 void MainWindow::_equalizer_value_changed(int band, int val) {
301 _player_form->setEqualizerValue(band, (val / 10.0));
304 void MainWindow::settings() {
305 SettingsDialog dialog;
309 _player_form->updateIcons();
310 _library_form->updateIcons();
311 _library_form->refresh();
312 if (ui->stackedWidget->currentIndex() == 0) { // player view
313 setAttribute(Qt::WA_Maemo5AutoOrientation, config.getValue("ui/portraitmode").toString() != "disabled");
317 void MainWindow::updateIcons() {
319 _icons_theme = config.getValue("ui/iconstheme").toString();
320 ui->fscreenButton->setIcon(QIcon(":/icons/"+_icons_theme+"/fullscreen.png"));
321 ui->prevButton->setIcon(QIcon(":/icons/"+_icons_theme+"/back.png"));
322 ui->nextButton->setIcon(QIcon(":/icons/"+_icons_theme+"/forward.png"));
323 ui->searchButton->setIcon(QIcon(":/icons/"+_icons_theme+"/search.png"));
327 void MainWindow::_orientation_changed() {
328 QRect screenGeometry = QApplication::desktop()->screenGeometry();
329 if (screenGeometry.width() > screenGeometry.height()) {
330 _player_form->landscapeMode();
331 ui->toolsWidget->setVisible(true);
333 _player_form->portraitMode();
334 ui->toolsWidget->setVisible(false);