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