From 94b1edde2f01ae706f0843c5b3ff6bd7f22698a6 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 17 Jul 2010 22:53:27 +0000 Subject: [PATCH] Add more song-related things to the device menu --- src/devices/deviceview.cpp | 106 ++++++++++++++++++++++++++++++------- src/devices/deviceview.h | 22 +++++++- src/ui/mainwindow.cpp | 32 +++++++++++ src/ui/mainwindow.h | 5 ++ 4 files changed, 144 insertions(+), 21 deletions(-) diff --git a/src/devices/deviceview.cpp b/src/devices/deviceview.cpp index 158aaa9ae..7c71ef6f3 100644 --- a/src/devices/deviceview.cpp +++ b/src/devices/deviceview.cpp @@ -115,18 +115,29 @@ DeviceView::DeviceView(QWidget* parent) merged_model_(NULL), sort_model_(NULL), properties_dialog_(new DeviceProperties), - menu_(new QMenu(this)) + device_menu_(new QMenu(this)), + library_menu_(new QMenu(this)) { - connect_action_ = menu_->addAction( + // Device menu items + connect_action_ = device_menu_->addAction( IconLoader::Load("list-add"), tr("Connect device"), this, SLOT(Connect())); - disconnect_action_ = menu_->addAction( + disconnect_action_ = device_menu_->addAction( IconLoader::Load("list-remove"), tr("Disconnect device"), this, SLOT(Disconnect())); - forget_action_ = menu_->addAction( + forget_action_ = device_menu_->addAction( IconLoader::Load("list-remove"), tr("Forget device"), this, SLOT(Forget())); - menu_->addSeparator(); - properties_action_ = menu_->addAction( + device_menu_->addSeparator(); + properties_action_ = device_menu_->addAction( IconLoader::Load("configure"), tr("Device properties..."), this, SLOT(Properties())); + // Library menu items + load_action_ = library_menu_->addAction(IconLoader::Load("media-playback-start"), + tr("Load"), this, SLOT(Load())); + add_to_playlist_action_ = library_menu_->addAction(IconLoader::Load("media-playback-start"), + tr("Add to playlist"), this, SLOT(AddToPlaylist())); + library_menu_->addSeparator(); + delete_action_ = library_menu_->addAction(IconLoader::Load("edit-delete"), + tr("Delete from disk..."), this, SLOT(Delete())); + setItemDelegate(new DeviceItemDelegate(this)); SetExpandOnReset(false); } @@ -158,19 +169,26 @@ void DeviceView::SetDeviceManager(DeviceManager *manager) { void DeviceView::contextMenuEvent(QContextMenuEvent* e) { menu_index_ = currentIndex(); - QModelIndex device_index = MapToDevice(menu_index_); - bool is_device = device_index.isValid(); - bool is_connected = is_device && manager_->GetConnectedDevice(device_index.row()); - bool is_plugged_in = is_device && manager_->GetLister(device_index.row()); - bool is_remembered = is_device && manager_->GetDatabaseId(device_index.row()) != -1; - connect_action_->setEnabled(is_plugged_in); - disconnect_action_->setEnabled(is_plugged_in); - connect_action_->setVisible(!is_connected); - disconnect_action_->setVisible(is_connected); - forget_action_->setEnabled(is_remembered); + const QModelIndex device_index = MapToDevice(menu_index_); + const QModelIndex library_index = MapToLibrary(menu_index_); - menu_->popup(e->globalPos()); + if (device_index.isValid()) { + const bool is_connected = manager_->GetConnectedDevice(device_index.row()); + const bool is_plugged_in = manager_->GetLister(device_index.row()); + const bool is_remembered = manager_->GetDatabaseId(device_index.row()) != -1; + + connect_action_->setEnabled(is_plugged_in); + disconnect_action_->setEnabled(is_plugged_in); + forget_action_->setEnabled(is_remembered); + + connect_action_->setVisible(!is_connected); + disconnect_action_->setVisible(is_connected); + + device_menu_->popup(e->globalPos()); + } else if (library_index.isValid()) { + library_menu_->popup(e->globalPos()); + } } QModelIndex DeviceView::MapToDevice(const QModelIndex& merged_model_index) const { @@ -181,6 +199,16 @@ QModelIndex DeviceView::MapToDevice(const QModelIndex& merged_model_index) const return sort_model_->mapToSource(sort_model_index); } +QModelIndex DeviceView::MapToLibrary(const QModelIndex& merged_model_index) const { + QModelIndex sort_model_index = merged_model_->mapToSource(merged_model_index); + if (const QSortFilterProxyModel* sort_model = + qobject_cast(sort_model_index.model())) { + return sort_model->mapToSource(sort_model_index); + } + + return QModelIndex(); +} + void DeviceView::Connect() { QModelIndex device_idx = MapToDevice(menu_index_); QModelIndex sort_idx = sort_model_->mapFromSource(device_idx); @@ -247,8 +275,46 @@ void DeviceView::mouseDoubleClickEvent(QMouseEvent *event) { QModelIndex merged_index = indexAt(event->pos()); QModelIndex device_index = MapToDevice(merged_index); - if (device_index.isValid() && !manager_->GetConnectedDevice(device_index.row())) { - menu_index_ = merged_index; - Connect(); + if (device_index.isValid()) { + if (!manager_->GetConnectedDevice(device_index.row())) { + menu_index_ = merged_index; + Connect(); + } + return; + } + + QModelIndex library_index = MapToLibrary(merged_index); + if (library_index.isValid()) { + emit DoubleClicked(GetSelectedSongs()); } } + +SongList DeviceView::GetSelectedSongs() const { + QModelIndexList selected_merged_indexes = selectionModel()->selectedRows(); + SongList songs; + foreach (const QModelIndex& merged_index, selected_merged_indexes) { + QModelIndex library_index = MapToLibrary(merged_index); + if (!library_index.isValid()) + continue; + + const LibraryModel* library = qobject_cast( + library_index.model()); + if (!library) + continue; + + songs << library->GetChildSongs(library_index); + } + return songs; +} + +void DeviceView::Load() { + emit Load(GetSelectedSongs()); +} + +void DeviceView::AddToPlaylist() { + emit AddToPlaylist(GetSelectedSongs()); +} + +void DeviceView::Delete() { + +} diff --git a/src/devices/deviceview.h b/src/devices/deviceview.h index cea85ea44..20fbe0fd6 100644 --- a/src/devices/deviceview.h +++ b/src/devices/deviceview.h @@ -17,6 +17,7 @@ #ifndef DEVICEVIEW_H #define DEVICEVIEW_H +#include "core/song.h" #include "library/libraryview.h" #include "widgets/autoexpandingtreeview.h" @@ -47,20 +48,33 @@ public: void SetDeviceManager(DeviceManager* manager); +signals: + void Load(const SongList& songs); + void AddToPlaylist(const SongList& songs); + void DoubleClicked(const SongList& songs); + protected: void contextMenuEvent(QContextMenuEvent *); void mouseDoubleClickEvent(QMouseEvent *event); private slots: + // Device menu actions void Connect(); void Disconnect(); void Forget(); void Properties(); + // Library menu actions + void Load(); + void AddToPlaylist(); + void Delete(); + void DeviceDisconnected(int row); private: QModelIndex MapToDevice(const QModelIndex& merged_model_index) const; + QModelIndex MapToLibrary(const QModelIndex& merged_model_index) const; + SongList GetSelectedSongs() const; private: DeviceManager* manager_; @@ -69,11 +83,17 @@ private: boost::scoped_ptr properties_dialog_; - QMenu* menu_; + QMenu* device_menu_; QAction* connect_action_; QAction* disconnect_action_; QAction* forget_action_; QAction* properties_action_; + + QMenu* library_menu_; + QAction* load_action_; + QAction* add_to_playlist_action_; + QAction* delete_action_; + QModelIndex menu_index_; }; diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 729060585..02cda9c0d 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -364,6 +364,9 @@ MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidg // Devices connections connect(devices_, SIGNAL(Error(QString)), error_dialog_.get(), SLOT(ShowMessage(QString))); + connect(ui_->devices_view, SIGNAL(Load(SongList)), SLOT(LoadDeviceSongsToPlaylist(SongList))); + connect(ui_->devices_view, SIGNAL(AddToPlaylist(SongList)), SLOT(AddDeviceSongsToPlaylist(SongList))); + connect(ui_->devices_view, SIGNAL(DoubleClicked(SongList)), SLOT(DeviceSongsDoubleClicked(SongList))); // Library filter widget QAction* library_config_action = new QAction( @@ -632,6 +635,35 @@ void MainWindow::AddLibrarySongsToPlaylist(bool clear_first, const SongList &son } } +void MainWindow::AddDeviceSongsToPlaylist(const SongList &songs) { + AddDeviceSongsToPlaylist(false, songs); +} + +void MainWindow::LoadDeviceSongsToPlaylist(const SongList &songs) { + AddDeviceSongsToPlaylist(true, songs); +} + +void MainWindow::DeviceSongsDoubleClicked(const SongList &songs) { + AddDeviceSongsToPlaylist(autoclear_playlist_, songs); +} + +void MainWindow::AddDeviceSongsToPlaylist(bool clear_first, const SongList &songs) { + if (clear_first) + playlists_->ClearCurrent(); + + QModelIndex first_song = playlists_->current()->InsertSongs(songs); + + if (!playlists_->current()->proxy()->mapFromSource(first_song).isValid()) { + // The first song doesn't match the filter, so don't play it + return; + } + + if (first_song.isValid() && player_->GetState() != Engine::Playing) { + playlists_->SetActiveToCurrent(); + player_->PlayAt(first_song.row(), Engine::First, true); + } +} + void MainWindow::MediaStopped() { ui_->action_stop->setEnabled(false); ui_->action_stop_after_this_track->setEnabled(false); diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 64be9af80..344af7e2c 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -134,6 +134,10 @@ class MainWindow : public QMainWindow, public PlatformInterface { void AddLibrarySongsToPlaylist(const SongList& songs); void LibrarySongsDoubleClicked(const SongList& songs); + void LoadDeviceSongsToPlaylist(const SongList& songs); + void AddDeviceSongsToPlaylist(const SongList& songs); + void DeviceSongsDoubleClicked(const SongList& songs); + void VolumeWheelEvent(int delta); void ToggleShowHide(); @@ -168,6 +172,7 @@ class MainWindow : public QMainWindow, public PlatformInterface { void AddFilesToPlaylist(bool clear_first, const QList& urls); void AddLibraryItemToPlaylist(bool clear_first, const QModelIndexList& indexes); void AddLibrarySongsToPlaylist(bool clear_first, const SongList& songs); + void AddDeviceSongsToPlaylist(bool clear_first, const SongList& songs); void AddUrls(bool play_now, const QList& urls); private: