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>
27 #include "player/player.h"
30 #include "timerdialog.h"
31 #include "equalizerdialog.h"
33 using namespace SomePlayer::DataObjects;
34 using namespace SomePlayer::Storage;
36 MainWindow::MainWindow(QWidget *parent) :
38 ui(new Ui::MainWindow)
41 _library = new Library(config.applicationDir(), config.applicationDir());
43 connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
44 connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
46 _player_form = new PlayerForm(_library, ui->stackedWidget);
47 _library_form = new LibraryForm(_library, ui->stackedWidget);
48 _busy_widget = new BusyWidget(ui->stackedWidget);
49 _timer = new QTimer(this);
50 _equalizer_dialog = new EqualizerDialog(this);
51 ui->stackedWidget->insertWidget(0, _player_form);
52 ui->stackedWidget->insertWidget(1, _library_form);
53 ui->stackedWidget->insertWidget(2, _busy_widget);
54 QAction *add_directory = ui->menuLibrary->addAction("Add directory");
55 QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
56 QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
57 QAction *add_files = ui->menuLibrary->addAction("Add file to current playlist");
58 QAction *set_timer = ui->menuBar->addAction("Set timer");
59 QAction *equalizer = ui->menuBar->addAction("Euqalizer");
60 connect(_player_form, SIGNAL(library()), this, SLOT(library()));
61 connect(_library_form, SIGNAL(player()), this, SLOT(player()));
62 connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
63 connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
64 connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
65 connect(add_files, SIGNAL(triggered()), this, SLOT(_add_files()));
66 connect(set_timer, SIGNAL(triggered()), this, SLOT(_set_timer()));
67 connect(equalizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
68 connect(_library, SIGNAL(done()), this, SLOT(library()));
69 connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
70 connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
71 connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
72 connect(_library_form, SIGNAL(done()), this, SLOT(library()));
73 connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
74 connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
75 connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
76 connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
77 connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
78 connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
79 connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
80 connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
81 connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
82 connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
83 connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
84 connect(_equalizer_dialog, SIGNAL(equalizerDisabled()), _player_form, SLOT(disableEqualizer()));
89 MainWindow::~MainWindow()
96 void MainWindow::aboutQt() {
97 QMessageBox::aboutQt(this, "About Qt");
100 void MainWindow::about() {
101 QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
102 "written in C++ with Qt4\n\n"
103 "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
106 void MainWindow::player() {
107 ui->stackedWidget->setCurrentIndex(0);
108 _player_form->reload();
109 setWindowTitle("SomePlayer");
112 void MainWindow::library() {
113 ui->menuBar->setEnabled(true);
114 ui->stackedWidget->setCurrentIndex(1);
116 setWindowTitle("SomePlayer Library");
119 void MainWindow::_add_directory() {
120 QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
121 if (!directory.isEmpty()) {
122 showBusyWidget("<H1>Scanning... Please wait</H1>");
123 _library->addDirectory(directory);
127 void MainWindow::_save_playlist() {
128 QString name = QInputDialog::getText(this, "Playlist name", "Name:");
129 QList<QString> playlists = _library->getPlaylistsNames();
131 if (playlists.contains(name)) {
132 if (QMessageBox::question(this, "Append to playlist?", "Playlist with name \""+name+"\" already exists.\n"
133 "Dow you want to append current playlist to it?",
134 QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Ok) {
141 Playlist cur = _library->getCurrentPlaylist();
142 Playlist target = _library->getPlaylist(name);
143 QList<Track> tracks = cur.tracks();
144 foreach (Track track, tracks) {
145 target.addTrack(track);
147 _library->savePlaylist(target);
149 Playlist playlist = _library->getCurrentPlaylist();
150 playlist.setName(name);
151 _library->savePlaylist(playlist);
155 void MainWindow::_clear_current_playlist() {
156 Playlist playlist = _library->getCurrentPlaylist();
158 _library->saveCurrentPlaylist(playlist);
159 _player_form->reload();
162 void MainWindow::showBusyWidget(QString caption) {
163 _busy_widget->setText(caption);
164 ui->menuBar->setEnabled(false);
166 ui->stackedWidget->setCurrentIndex(2);
169 void MainWindow::_toggle_search_line() {
170 if (ui->searchLine->isVisible()) {
171 ui->searchLine->setText("");
172 ui->searchLine->hide();
173 ui->nextButton->hide();
174 ui->prevButton->hide();
177 ui->searchLine->show();
178 ui->nextButton->show();
179 ui->prevButton->show();
183 void MainWindow::showSearchPanel() {
184 ui->searchButton->show();
185 ui->searchLine->setFocus();
188 void MainWindow::hideSearchPanel() {
189 ui->searchLine->setText("");
190 ui->searchLine->hide();
191 ui->nextButton->hide();
192 ui->prevButton->hide();
193 ui->searchButton->hide();
197 void MainWindow::_search(QString pattern) {
198 if (ui->stackedWidget->currentIndex() == 0) { // player
199 _player_form->search(pattern);
200 } else if (ui->stackedWidget->currentIndex() == 1) { // library
201 _library_form->search(pattern);
205 void MainWindow::_nextItem() {
206 if (ui->stackedWidget->currentIndex() == 0) { // player
207 _player_form->nextItem();
208 } else if (ui->stackedWidget->currentIndex() == 1) { // library
209 _library_form->nextItem();
213 void MainWindow::_prevItem() {
214 if (ui->stackedWidget->currentIndex() == 0) { // player
215 _player_form->prevItem();
216 } else if (ui->stackedWidget->currentIndex() == 1) { // library
217 _library_form->prevItem();
221 void MainWindow::_cancelSearch() {
222 if (ui->stackedWidget->currentIndex() == 0) { // player
223 _player_form->cancelSearch();
224 } else if (ui->stackedWidget->currentIndex() == 1) { // library
225 _library_form->cancelSearch();
229 void MainWindow::_toggle_full_screen() {
230 if (isFullScreen()) {
231 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
234 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
239 void MainWindow::_add_files() {
240 QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
241 if (!files.isEmpty()) _player_form->addFiles(files);
244 void MainWindow::_set_timer() {
245 TimerDialog dialog(this);
247 if (_timer->isActive()) {
248 dialog.showDisable();
249 int msec = _timer->interval();
250 int h = msec/3600000;
251 int m = msec/60000 - h * 60;
252 int s = msec/1000 - h * 3600 - m * 60;
253 dialog.setTime(h, m, s);
255 if (QDialog::Accepted == dialog.exec()) {
256 if (!dialog.timerDisabled()) {
258 dialog.getTime(&h, &m, &s);
259 _timer->setInterval(h*3600000+m*60000+s*1000);
261 } else if (_timer->isActive()) {
267 void MainWindow::_timeout() {
268 _player_form->stop();
272 void MainWindow::_equalizer() {
273 if (_player_form->isEqualizerAvailable()) {
275 for (int i = 0; i < 10; i++) {
276 _player_form->equalizerValue(i, &val);
277 _equalizer_dialog->setValue(i, (int)(val * 10 + 0.5));
279 _equalizer_dialog->setEqualizerEnabled(_player_form->isEqualizerEnabled());
280 _equalizer_dialog->reloadPresets();
281 _equalizer_dialog->exec();
283 QMessageBox::information(this, "Error", "No equalizer support. Please install gstreamer0.10-plugins-good-extra");
287 void MainWindow::_equalizer_value_changed(int band, int val) {
288 _player_form->setEqualizerValue(band, (val / 10.0));