Implemented new features:
[someplayer] / src / mainwindow.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 "mainwindow.h"
21 #include "ui_mainwindow.h"
22 #include <QFileDialog>
23 #include <QMessageBox>
24 #include <QInputDialog>
25 #include <QFile>
26
27 #include "player/player.h"
28
29 #include "library.h"
30 #include "timerdialog.h"
31
32 using namespace SomePlayer::DataObjects;
33 using namespace SomePlayer::Storage;
34
35 MainWindow::MainWindow(QWidget *parent) :
36         QMainWindow(parent),
37         ui(new Ui::MainWindow)
38 {
39         Config config;
40         _library = new Library(config.applicationDir(), config.applicationDir());
41         ui->setupUi(this);
42         connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
43         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
44         setAnimated(true);
45         _player_form = new PlayerForm(_library, ui->stackedWidget);
46         _library_form = new LibraryForm(_library, ui->stackedWidget);
47         _busy_widget = new BusyWidget(ui->stackedWidget);
48         _timer = new QTimer(this);
49         ui->stackedWidget->insertWidget(0, _player_form);
50         ui->stackedWidget->insertWidget(1, _library_form);
51         ui->stackedWidget->insertWidget(2, _busy_widget);
52         QAction *add_directory = ui->menuLibrary->addAction("Add directory");
53         QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
54         QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
55         QAction *add_files = ui->menuLibrary->addAction("Add file to current playlist");
56         QAction *set_timer = ui->menuBar->addAction("Set timer");
57         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
58         connect(_library_form, SIGNAL(player()), this, SLOT(player()));
59         connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
60         connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
61         connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
62         connect(add_files, SIGNAL(triggered()), this, SLOT(_add_files()));
63         connect(set_timer, SIGNAL(triggered()), this, SLOT(_set_timer()));
64         connect(_library, SIGNAL(done()), this, SLOT(library()));
65         connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
66         connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
67         connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
68         connect(_library_form, SIGNAL(done()), this, SLOT(library()));
69         connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
70         connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
71         connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
72         connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
73         connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
74         connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
75         connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
76         connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
77         connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
78         hideSearchPanel();
79         library();
80 }
81
82 MainWindow::~MainWindow()
83 {
84         delete _player_form;
85         delete _library_form;
86         delete ui;
87 }
88
89 void MainWindow::aboutQt() {
90         QMessageBox::aboutQt(this, "About Qt");
91 }
92
93 void MainWindow::about() {
94         QMessageBox::about(this, QString("About SomePlayer v")+_SOMEPLAYER_VERSION_, "Alternate music player for Maemo 5 "
95                                            "written in C++ with Qt4\n\n"
96                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
97 }
98
99 void MainWindow::player() {
100         ui->stackedWidget->setCurrentIndex(0);
101         _player_form->reload();
102         setWindowTitle("SomePlayer");
103 }
104
105 void MainWindow::library() {
106         ui->menuBar->setEnabled(true);
107         ui->stackedWidget->setCurrentIndex(1);
108         showSearchPanel();
109         setWindowTitle("SomePlayer Library");
110 }
111
112 void MainWindow::_add_directory() {
113         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
114         if (!directory.isEmpty()) {
115                 showBusyWidget("<H1>Scanning... Please wait</H1>");
116                 _library->addDirectory(directory);
117         }
118 }
119
120 void MainWindow::_save_playlist() {
121         QString name = QInputDialog::getText(this, "Playlist name", "Name:");
122         Playlist playlist = _library->getCurrentPlaylist();
123         playlist.setName(name);
124         _library->savePlaylist(playlist);
125 }
126
127 void MainWindow::_clear_current_playlist() {
128         Playlist playlist = _library->getCurrentPlaylist();
129         playlist.clear();
130         _library->saveCurrentPlaylist(playlist);
131         _player_form->reload();
132 }
133
134 void MainWindow::showBusyWidget(QString caption) {
135         _busy_widget->setText(caption);
136         ui->menuBar->setEnabled(false);
137         hideSearchPanel();
138         ui->stackedWidget->setCurrentIndex(2);
139 }
140
141 void MainWindow::_toggle_search_line() {
142         if (ui->searchLine->isVisible()) {
143                 ui->searchLine->setText("");
144                 ui->searchLine->hide();
145                 ui->nextButton->hide();
146                 ui->prevButton->hide();
147                 _cancelSearch();
148         } else {
149                 ui->searchLine->show();
150                 ui->nextButton->show();
151                 ui->prevButton->show();
152         }
153 }
154
155 void MainWindow::showSearchPanel() {
156         ui->searchButton->show();
157         ui->searchLine->setFocus();
158 }
159
160 void MainWindow::hideSearchPanel() {
161         ui->searchLine->setText("");
162         ui->searchLine->hide();
163         ui->nextButton->hide();
164         ui->prevButton->hide();
165         ui->searchButton->hide();
166         _cancelSearch();
167 }
168
169 void MainWindow::_search(QString pattern) {
170         if (ui->stackedWidget->currentIndex() == 0) { // player
171                 _player_form->search(pattern);
172         } else if (ui->stackedWidget->currentIndex() == 1) { // library
173                 _library_form->search(pattern);
174         }
175 }
176
177 void MainWindow::_nextItem() {
178         if (ui->stackedWidget->currentIndex() == 0) { // player
179                 _player_form->nextItem();
180         } else if (ui->stackedWidget->currentIndex() == 1) { // library
181                 _library_form->nextItem();
182         }
183 }
184
185 void MainWindow::_prevItem() {
186         if (ui->stackedWidget->currentIndex() == 0) { // player
187                 _player_form->prevItem();
188         } else if (ui->stackedWidget->currentIndex() == 1) { // library
189                 _library_form->prevItem();
190         }
191 }
192
193 void MainWindow::_cancelSearch() {
194         if (ui->stackedWidget->currentIndex() == 0) { // player
195                 _player_form->cancelSearch();
196         } else if (ui->stackedWidget->currentIndex() == 1) { // library
197                 _library_form->cancelSearch();
198         }
199 }
200
201 void MainWindow::_toggle_full_screen() {
202         if (isFullScreen()) {
203                 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
204                 showNormal();
205         } else {
206                 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
207                 showFullScreen();
208         }
209 }
210
211 void MainWindow::_add_files() {
212         QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
213         if (!files.isEmpty()) _player_form->addFiles(files);
214 }
215
216 void MainWindow::_set_timer() {
217         TimerDialog dialog(this);
218         dialog.init();
219         if (_timer->isActive()) {
220                 dialog.showDisable();
221                 int msec = _timer->interval();
222                 int h = msec/3600000;
223                 int m = msec/60000 - h * 60;
224                 int s = msec/1000 - h * 3600 - m * 60;
225                 dialog.setTime(h, m, s);
226         }
227         if (QDialog::Accepted == dialog.exec()) {
228                 if (!dialog.timerDisabled()) {
229                         int h, m, s;
230                         dialog.getTime(&h, &m, &s);
231                         _timer->setInterval(h*3600000+m*60000+s*1000);
232                         _timer->start();
233                 } else if (_timer->isActive()) {
234                         _timer->stop();
235                 }
236         }
237 }
238
239 void MainWindow::_timeout() {
240         _player_form->stop();
241         _timer->stop();
242 }