Big commit:
[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         hideSearchPanel();
47         library();
48 }
49
50 MainWindow::~MainWindow()
51 {
52         delete _player_form;
53         delete _library_form;
54         delete ui;
55 }
56
57 void MainWindow::aboutQt() {
58         QMessageBox::aboutQt(this, "About Qt");
59 }
60
61 void MainWindow::about() {
62         QMessageBox::about(this, "About SomePlayer", "Alternate music player for Maemo 5 "
63                                            "written in C++ with Qt4\n\n"
64                                            "Author: Nikolay Tischenko aka \"somebody\" <niktischenko@gmail.com>");
65 }
66
67 void MainWindow::player() {
68         ui->stackedWidget->setCurrentIndex(0);
69         _player_form->reload();
70         setWindowTitle("SomePlayer");
71 }
72
73 void MainWindow::library() {
74         ui->menuBar->setEnabled(true);
75         ui->stackedWidget->setCurrentIndex(1);
76         showSearchPanel();
77         setWindowTitle("SomePlayer Library");
78 }
79
80 void MainWindow::_add_directory() {
81         QString directory = QFileDialog::getExistingDirectory (this, "Select directory", "/home/user/MyDocs", QFileDialog::ShowDirsOnly );
82         if (!directory.isEmpty()) {
83                 showBusyWidget("<H1>Scanning... Please wait</H1>");
84                 _library->addDirectory(directory);
85         }
86 }
87
88 void MainWindow::_save_playlist() {
89         QString name = QInputDialog::getText(this, "Playlist name", "Name:");
90         Playlist playlist = _library->getCurrentPlaylist();
91         playlist.setName(name);
92         _library->savePlaylist(playlist);
93 }
94
95 void MainWindow::_clear_current_playlist() {
96         Playlist playlist = _library->getCurrentPlaylist();
97         playlist.clear();
98         _library->saveCurrentPlaylist(playlist);
99         _player_form->reload();
100 }
101
102 void MainWindow::showBusyWidget(QString caption) {
103         _busy_widget->setText(caption);
104         ui->menuBar->setEnabled(false);
105         ui->stackedWidget->setCurrentIndex(2);
106 }
107
108 void MainWindow::_toggle_search_line() {
109         if (ui->searchLine->isVisible()) {
110                 ui->searchLine->setText("");
111                 ui->searchLine->hide();
112                 ui->nextButton->hide();
113                 ui->prevButton->hide();
114                 _cancelSearch();
115         } else {
116                 ui->searchLine->show();
117                 ui->nextButton->show();
118                 ui->prevButton->show();
119         }
120 }
121
122 void MainWindow::showSearchPanel() {
123         ui->searchButton->show();
124 }
125
126 void MainWindow::hideSearchPanel() {
127         ui->searchLine->setText("");
128         ui->searchLine->hide();
129         ui->nextButton->hide();
130         ui->prevButton->hide();
131         ui->searchButton->hide();
132         _cancelSearch();
133 }
134
135 void MainWindow::_search(QString pattern) {
136         if (ui->stackedWidget->currentIndex() == 0) { // player
137                 _player_form->search(pattern);
138         }
139 }
140
141 void MainWindow::_nextItem() {
142         if (ui->stackedWidget->currentIndex() == 0) { // player
143                 _player_form->nextItem();
144         }
145 }
146
147 void MainWindow::_prevItem() {
148         if (ui->stackedWidget->currentIndex() == 0) { // player
149                 _player_form->prevItem();
150         }
151 }
152
153 void MainWindow::_cancelSearch() {
154         if (ui->stackedWidget->currentIndex() == 0) { // player
155                 _player_form->cancelSearch();
156         }
157 }