Implemented directory browser
[someplayer] / src / mainwindow.cpp
index 40c5304..9f3dd12 100644 (file)
@@ -50,16 +50,11 @@ MainWindow::MainWindow(QWidget *parent) :
        _player_form = new PlayerForm(_library, this);
        ui->centralWidget->layout()->addWidget(_player_form);
        _library_form = new LibraryForm(_library, this);
-       _library_form->setAttribute(Qt::WA_Maemo5StackedWindow);
-       _library_form->setWindowFlags(_library_form->windowFlags() | Qt::Window);
-       _busy_widget = new BusyWidget(this);
-       ui->centralWidget->layout()->addWidget(_busy_widget);
-       _busy_widget->hide();
+       _directory_form = new DirectoryView(this);
+       _directory_form->hide();
        _timer = new QTimer(this);
        _equalizer_dialog = new EqualizerDialog(this);
        _manage_library_form = new ManageLibraryForm(_library, this);
-       _manage_library_form->setAttribute(Qt::WA_Maemo5StackedWindow);
-       _manage_library_form->setWindowFlags(Qt::Window | _manage_library_form->windowFlags());
        connect(_player_form, SIGNAL(library()), this, SLOT(library()));
        connect(_library_form, SIGNAL(refreshPlayer()), this, SLOT(player()));
        connect(ui->actionManageLibrary, SIGNAL(triggered()), this, SLOT(_manage_library()));
@@ -67,10 +62,9 @@ MainWindow::MainWindow(QWidget *parent) :
        connect(_player_form, SIGNAL(clearPlaylist()), this, SLOT(_clear_current_playlist()));
        connect(ui->actionSetTimer, SIGNAL(triggered()), this, SLOT(_set_timer()));
        connect(ui->actionEqualizer, SIGNAL(triggered()), this, SLOT(_equalizer()));
-       connect(_library, SIGNAL(done()), this, SLOT(library()));
        connect(_library, SIGNAL(done()), _library_form, SLOT(refresh()));
-       connect(_library, SIGNAL(addingTracks(int)), _busy_widget, SLOT(setMax(int)));
-       connect(_library, SIGNAL(trackAdded()), _busy_widget, SLOT(tick()));
+       connect(_player_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
+       connect(_manage_library_form, SIGNAL(refreshLibrary()), _library_form, SLOT(refresh()));
        connect(_timer, SIGNAL(timeout()), this, SLOT(_timeout()));
        connect(_equalizer_dialog, SIGNAL(valueChanged(int,int)), this, SLOT(_equalizer_value_changed(int, int)));
        connect(_equalizer_dialog, SIGNAL(equalizerEnabled()), _player_form, SLOT(enableEqualizer()));
@@ -78,6 +72,9 @@ MainWindow::MainWindow(QWidget *parent) :
        connect(QApplication::desktop(), SIGNAL(resized(int)), this, SLOT(_orientation_changed()));
        connect(_player_form, SIGNAL(fullscreen(bool)), this, SLOT(_fullscreen(bool)));
        connect(_library_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
+       connect(_directory_form, SIGNAL(addAndPlay(Track)), _player_form, SLOT(play(Track)));
+       connect(_player_form, SIGNAL(dirView()), _directory_form, SLOT(show()));
+       connect(_directory_form, SIGNAL(addTracks(QList<Track>)), this, SLOT(_add_tracks(QList<Track>)));
        _player_form->reload(true);
        QString mode = config.getValue("ui/orientation").toString();
        if (mode == "landscape") {
@@ -85,21 +82,27 @@ MainWindow::MainWindow(QWidget *parent) :
                _player_form->landscapeMode();
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
+               _directory_form->lanscapeMode();
        } else if (mode == "portrait") {
                setAttribute(Qt::WA_Maemo5PortraitOrientation);
                _player_form->portraitMode();
                _library_form->portraitMode();
                _equalizer_dialog->portraitMode();
+               _directory_form->portraitMode();
        } else if (mode == "auto") { // initialization in landscape
                _player_form->landscapeMode();
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
+               _directory_form->lanscapeMode();
                setAttribute(Qt::WA_Maemo5AutoOrientation);
        }
        _library_form->updateIcons();
        _player_form->updateIcons();
+       _manage_library_form->updateIcons();
+       _directory_form->updateIcons();
        _player_form->checkGradient();
        _library_form->checkGradient();
+       _directory_form->updateGradient();
        setWindowTitle("SomePlayer");
 }
 
@@ -125,19 +128,38 @@ void MainWindow::library() {
        ui->menuBar->setEnabled(true);
        _library_form->show();
        _orientation_changed(); // workaround
-       _busy_widget->hide();
        _manage_library_form->hide();
-       _player_form->show();
 }
 
 void MainWindow::_manage_library() {
+       _manage_library_form->refresh();
        _manage_library_form->show();
 }
 
 void MainWindow::_save_playlist() {
        QList<QString> playlists = _library->getPlaylistsNames();
        playlists.removeOne(_CURRENT_PLAYLIST_SUBST_);
-       SavePlaylistDialog dialog(this);
+       Playlist cur = _library->getCurrentPlaylist();
+       // construct playlist name if possible
+       QString suggest_name;
+       QList<Track> tracks = cur.tracks();
+       QString artist = tracks.at(0).metadata().artist(), album = tracks.at(0).metadata().album();
+       foreach (Track t, tracks) {
+               if (t.metadata().album() != album)
+                       album = "";
+               if (t.metadata().artist() != artist)
+                       artist = "";
+       }
+       if (album.isEmpty() && artist.isEmpty()) {
+               suggest_name = "New playlist";
+       } else if (album.isEmpty()) {
+               suggest_name = artist;
+       } else {
+               suggest_name = QString("%1 - %2").arg(artist).arg(album);
+       }
+
+       // constructed
+       SavePlaylistDialog dialog(suggest_name, this);
        dialog.setPlaylistNames(playlists);
        if (dialog.exec() == QDialog::Accepted) {
                QString name = dialog.selectedName();
@@ -152,7 +174,6 @@ void MainWindow::_save_playlist() {
                        }
                }
                if (append) {
-                       Playlist cur = _library->getCurrentPlaylist();
                        Playlist target = _library->getPlaylist(name);
                        QList<Track> tracks = cur.tracks();
                        foreach (Track track, tracks) {
@@ -160,9 +181,8 @@ void MainWindow::_save_playlist() {
                        }
                        _library->savePlaylist(target);
                } else {
-                       Playlist playlist = _library->getCurrentPlaylist();
-                       playlist.setName(name);
-                       _library->savePlaylist(playlist);
+                       cur.setName(name);
+                       _library->savePlaylist(cur);
                }
        }
 }
@@ -174,18 +194,6 @@ void MainWindow::_clear_current_playlist() {
        _player_form->reload(true);
 }
 
-void MainWindow::showBusyWidget(QString caption) {
-       _busy_widget->setText(caption);
-       ui->menuBar->setEnabled(false);
-       _player_form->hide();
-       _busy_widget->show();
-}
-
-void MainWindow::_add_files() {
-       QStringList files = QFileDialog::getOpenFileNames(this, "Add file");
-       if (!files.isEmpty()) _player_form->addFiles(files);
-}
-
 void MainWindow::_set_timer() {
        TimerDialog dialog(this);
        dialog.init();
@@ -248,8 +256,11 @@ void MainWindow::settings() {
        }
        _player_form->updateIcons();
        _library_form->updateIcons();
+       _manage_library_form->updateIcons();
        _player_form->checkGradient();
        _library_form->checkGradient();
+       _directory_form->updateIcons();
+       _directory_form->updateGradient();
 }
 
 void MainWindow::_orientation_changed() {
@@ -258,10 +269,12 @@ void MainWindow::_orientation_changed() {
                _player_form->landscapeMode();
                _library_form->landscapeMode();
                _equalizer_dialog->landscapeMode();
+               _directory_form->lanscapeMode();
        } else {
                _player_form->portraitMode();
                _library_form->portraitMode();
                _equalizer_dialog->portraitMode();
+               _directory_form->portraitMode();
        }
 }
 
@@ -269,3 +282,12 @@ void MainWindow::_fullscreen(bool f) {
        if (f) showFullScreen();
        else showNormal();
 }
+
+void MainWindow::_add_tracks(QList<Track> tracks) {
+       Playlist cur = _library->getCurrentPlaylist();
+       foreach (Track track, tracks) {
+               cur.addTrack(track);
+       }
+       _library->saveCurrentPlaylist(cur);
+       _player_form->reload(true);
+}