0570563e4213f989a426c633b59c382d812477d5
[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
31 using namespace SomePlayer::DataObjects;
32 using namespace SomePlayer::Storage;
33
34 MainWindow::MainWindow(QWidget *parent) :
35         QMainWindow(parent),
36         ui(new Ui::MainWindow)
37 {
38         Config config;
39         _library = new Library(config.applicationDir(), config.applicationDir());
40         ui->setupUi(this);
41         connect(ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(aboutQt()));
42         connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about()));
43         setAnimated(true);
44         _player_form = new PlayerForm(_library, ui->stackedWidget);
45         _library_form = new LibraryForm(_library, ui->stackedWidget);
46         _busy_widget = new BusyWidget(ui->stackedWidget);
47         ui->stackedWidget->insertWidget(0, _player_form);
48         ui->stackedWidget->insertWidget(1, _library_form);
49         ui->stackedWidget->insertWidget(2, _busy_widget);
50         QAction *add_directory = ui->menuLibrary->addAction("Add directory");
51         QAction *save_playlist = ui->menuLibrary->addAction("Save playlist");
52         QAction *clear_playlist = ui->menuLibrary->addAction("Clear current playlist");
53         connect(_player_form, SIGNAL(library()), this, SLOT(library()));
54         connect(_library_form, SIGNAL(player()), this, SLOT(player()));
55         connect(add_directory, SIGNAL(triggered()), this, SLOT(_add_directory()));
56         connect(save_playlist, SIGNAL(triggered()), this, SLOT(_save_playlist()));
57         connect(clear_playlist, SIGNAL(triggered()), this, SLOT(_clear_current_playlist()));
58         connect(_library, SIGNAL(done()), this, SLOT(library()));
59         connect(_library_form, SIGNAL(done()), this, SLOT(library()));
60         connect(_library_form, SIGNAL(busy(QString)), this, SLOT(showBusyWidget(QString)));
61         connect(ui->searchButton, SIGNAL(clicked()), this, SLOT(_toggle_search_line()));
62         connect(_player_form, SIGNAL(showSearchPanel()), this, SLOT(showSearchPanel()));
63         connect(_player_form, SIGNAL(hideSearchPanel()), this, SLOT(hideSearchPanel()));
64         connect(ui->searchLine, SIGNAL(textChanged(QString)), this, SLOT(_search(QString)));
65         connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(_nextItem()));
66         connect(ui->prevButton, SIGNAL(clicked()), this, SLOT(_prevItem()));
67         connect(ui->fscreenButton, SIGNAL(clicked()), this, SLOT(_toggle_full_screen()));
68         hideSearchPanel();
69         library();
70 }
71
72 MainWindow::~MainWindow()
73 {
74         delete _player_form;
75         delete _library_form;
76         delete ui;
77 }
78
79 void MainWindow::aboutQt() {
80         QMessageBox::aboutQt(this, "About Qt");
81 }
82
83 void MainWindow::about() {
84         QMessageBox::about(this, "About SomePlayer", "Alternate music player for Maemo 5 "
85                                            "written in C++ with Qt4\n\n"
86                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
87 }
88
89 void MainWindow::player() {
90         ui->stackedWidget->setCurrentIndex(0);
91         _player_form->reload();
92         setWindowTitle("SomePlayer");
93 }
94
95 void MainWindow::library() {
96         ui->menuBar->setEnabled(true);
97         ui->stackedWidget->setCurrentIndex(1);
98         showSearchPanel();
99         setWindowTitle("SomePlayer Library");
100 }
101
102 void MainWindow::_add_directory() {
103         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
104         if (!directory.isEmpty()) {
105                 showBusyWidget("<H1>Scanning... Please wait</H1>");
106                 _library->addDirectory(directory);
107         }
108 }
109
110 void MainWindow::_save_playlist() {
111         QString name = QInputDialog::getText(this, "Playlist name", "Name:");
112         Playlist playlist = _library->getCurrentPlaylist();
113         playlist.setName(name);
114         _library->savePlaylist(playlist);
115 }
116
117 void MainWindow::_clear_current_playlist() {
118         Playlist playlist = _library->getCurrentPlaylist();
119         playlist.clear();
120         _library->saveCurrentPlaylist(playlist);
121         _player_form->reload();
122 }
123
124 void MainWindow::showBusyWidget(QString caption) {
125         _busy_widget->setText(caption);
126         ui->menuBar->setEnabled(false);
127         hideSearchPanel();
128         ui->stackedWidget->setCurrentIndex(2);
129 }
130
131 void MainWindow::_toggle_search_line() {
132         if (ui->searchLine->isVisible()) {
133                 ui->searchLine->setText("");
134                 ui->searchLine->hide();
135                 ui->nextButton->hide();
136                 ui->prevButton->hide();
137                 _cancelSearch();
138         } else {
139                 ui->searchLine->show();
140                 ui->nextButton->show();
141                 ui->prevButton->show();
142         }
143 }
144
145 void MainWindow::showSearchPanel() {
146         ui->searchButton->show();
147         ui->searchLine->setFocus();
148 }
149
150 void MainWindow::hideSearchPanel() {
151         ui->searchLine->setText("");
152         ui->searchLine->hide();
153         ui->nextButton->hide();
154         ui->prevButton->hide();
155         ui->searchButton->hide();
156         _cancelSearch();
157 }
158
159 void MainWindow::_search(QString pattern) {
160         if (ui->stackedWidget->currentIndex() == 0) { // player
161                 _player_form->search(pattern);
162         } else if (ui->stackedWidget->currentIndex() == 1) { // library
163                 _library_form->search(pattern);
164         }
165 }
166
167 void MainWindow::_nextItem() {
168         if (ui->stackedWidget->currentIndex() == 0) { // player
169                 _player_form->nextItem();
170         } else if (ui->stackedWidget->currentIndex() == 1) { // library
171                 _library_form->nextItem();
172         }
173 }
174
175 void MainWindow::_prevItem() {
176         if (ui->stackedWidget->currentIndex() == 0) { // player
177                 _player_form->prevItem();
178         } else if (ui->stackedWidget->currentIndex() == 1) { // library
179                 _library_form->prevItem();
180         }
181 }
182
183 void MainWindow::_cancelSearch() {
184         if (ui->stackedWidget->currentIndex() == 0) { // player
185                 _player_form->cancelSearch();
186         } else if (ui->stackedWidget->currentIndex() == 1) { // library
187                 _library_form->cancelSearch();
188         }
189 }
190
191 void MainWindow::_toggle_full_screen() {
192         if (isFullScreen()) {
193                 ui->fscreenButton->setIcon(QIcon(":/icons/fullscreen.png"));
194                 showNormal();
195         } else {
196                 ui->fscreenButton->setIcon(QIcon(":/icons/window.png"));
197                 showFullScreen();
198         }
199 }