Add more song-related things to the device menu

This commit is contained in:
David Sansome 2010-07-17 22:53:27 +00:00
parent fc5929b79b
commit 94b1edde2f
4 changed files with 144 additions and 21 deletions

View File

@ -115,18 +115,29 @@ DeviceView::DeviceView(QWidget* parent)
merged_model_(NULL), merged_model_(NULL),
sort_model_(NULL), sort_model_(NULL),
properties_dialog_(new DeviceProperties), 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())); 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())); 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())); IconLoader::Load("list-remove"), tr("Forget device"), this, SLOT(Forget()));
menu_->addSeparator(); device_menu_->addSeparator();
properties_action_ = menu_->addAction( properties_action_ = device_menu_->addAction(
IconLoader::Load("configure"), tr("Device properties..."), this, SLOT(Properties())); 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)); setItemDelegate(new DeviceItemDelegate(this));
SetExpandOnReset(false); SetExpandOnReset(false);
} }
@ -158,19 +169,26 @@ void DeviceView::SetDeviceManager(DeviceManager *manager) {
void DeviceView::contextMenuEvent(QContextMenuEvent* e) { void DeviceView::contextMenuEvent(QContextMenuEvent* e) {
menu_index_ = currentIndex(); menu_index_ = currentIndex();
QModelIndex device_index = MapToDevice(menu_index_);
bool is_device = device_index.isValid(); const QModelIndex device_index = MapToDevice(menu_index_);
bool is_connected = is_device && manager_->GetConnectedDevice(device_index.row()); const QModelIndex library_index = MapToLibrary(menu_index_);
bool is_plugged_in = is_device && manager_->GetLister(device_index.row());
bool is_remembered = is_device && manager_->GetDatabaseId(device_index.row()) != -1; 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); connect_action_->setEnabled(is_plugged_in);
disconnect_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); forget_action_->setEnabled(is_remembered);
menu_->popup(e->globalPos()); 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 { 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); 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<const QSortFilterProxyModel*>(sort_model_index.model())) {
return sort_model->mapToSource(sort_model_index);
}
return QModelIndex();
}
void DeviceView::Connect() { void DeviceView::Connect() {
QModelIndex device_idx = MapToDevice(menu_index_); QModelIndex device_idx = MapToDevice(menu_index_);
QModelIndex sort_idx = sort_model_->mapFromSource(device_idx); QModelIndex sort_idx = sort_model_->mapFromSource(device_idx);
@ -247,8 +275,46 @@ void DeviceView::mouseDoubleClickEvent(QMouseEvent *event) {
QModelIndex merged_index = indexAt(event->pos()); QModelIndex merged_index = indexAt(event->pos());
QModelIndex device_index = MapToDevice(merged_index); QModelIndex device_index = MapToDevice(merged_index);
if (device_index.isValid() && !manager_->GetConnectedDevice(device_index.row())) { if (device_index.isValid()) {
if (!manager_->GetConnectedDevice(device_index.row())) {
menu_index_ = merged_index; menu_index_ = merged_index;
Connect(); 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<const LibraryModel*>(
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() {
} }

View File

@ -17,6 +17,7 @@
#ifndef DEVICEVIEW_H #ifndef DEVICEVIEW_H
#define DEVICEVIEW_H #define DEVICEVIEW_H
#include "core/song.h"
#include "library/libraryview.h" #include "library/libraryview.h"
#include "widgets/autoexpandingtreeview.h" #include "widgets/autoexpandingtreeview.h"
@ -47,20 +48,33 @@ public:
void SetDeviceManager(DeviceManager* manager); void SetDeviceManager(DeviceManager* manager);
signals:
void Load(const SongList& songs);
void AddToPlaylist(const SongList& songs);
void DoubleClicked(const SongList& songs);
protected: protected:
void contextMenuEvent(QContextMenuEvent *); void contextMenuEvent(QContextMenuEvent *);
void mouseDoubleClickEvent(QMouseEvent *event); void mouseDoubleClickEvent(QMouseEvent *event);
private slots: private slots:
// Device menu actions
void Connect(); void Connect();
void Disconnect(); void Disconnect();
void Forget(); void Forget();
void Properties(); void Properties();
// Library menu actions
void Load();
void AddToPlaylist();
void Delete();
void DeviceDisconnected(int row); void DeviceDisconnected(int row);
private: private:
QModelIndex MapToDevice(const QModelIndex& merged_model_index) const; QModelIndex MapToDevice(const QModelIndex& merged_model_index) const;
QModelIndex MapToLibrary(const QModelIndex& merged_model_index) const;
SongList GetSelectedSongs() const;
private: private:
DeviceManager* manager_; DeviceManager* manager_;
@ -69,11 +83,17 @@ private:
boost::scoped_ptr<DeviceProperties> properties_dialog_; boost::scoped_ptr<DeviceProperties> properties_dialog_;
QMenu* menu_; QMenu* device_menu_;
QAction* connect_action_; QAction* connect_action_;
QAction* disconnect_action_; QAction* disconnect_action_;
QAction* forget_action_; QAction* forget_action_;
QAction* properties_action_; QAction* properties_action_;
QMenu* library_menu_;
QAction* load_action_;
QAction* add_to_playlist_action_;
QAction* delete_action_;
QModelIndex menu_index_; QModelIndex menu_index_;
}; };

View File

@ -364,6 +364,9 @@ MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidg
// Devices connections // Devices connections
connect(devices_, SIGNAL(Error(QString)), error_dialog_.get(), SLOT(ShowMessage(QString))); 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 // Library filter widget
QAction* library_config_action = new QAction( 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() { void MainWindow::MediaStopped() {
ui_->action_stop->setEnabled(false); ui_->action_stop->setEnabled(false);
ui_->action_stop_after_this_track->setEnabled(false); ui_->action_stop_after_this_track->setEnabled(false);

View File

@ -134,6 +134,10 @@ class MainWindow : public QMainWindow, public PlatformInterface {
void AddLibrarySongsToPlaylist(const SongList& songs); void AddLibrarySongsToPlaylist(const SongList& songs);
void LibrarySongsDoubleClicked(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 VolumeWheelEvent(int delta);
void ToggleShowHide(); void ToggleShowHide();
@ -168,6 +172,7 @@ class MainWindow : public QMainWindow, public PlatformInterface {
void AddFilesToPlaylist(bool clear_first, const QList<QUrl>& urls); void AddFilesToPlaylist(bool clear_first, const QList<QUrl>& urls);
void AddLibraryItemToPlaylist(bool clear_first, const QModelIndexList& indexes); void AddLibraryItemToPlaylist(bool clear_first, const QModelIndexList& indexes);
void AddLibrarySongsToPlaylist(bool clear_first, const SongList& songs); void AddLibrarySongsToPlaylist(bool clear_first, const SongList& songs);
void AddDeviceSongsToPlaylist(bool clear_first, const SongList& songs);
void AddUrls(bool play_now, const QList<QUrl>& urls); void AddUrls(bool play_now, const QList<QUrl>& urls);
private: private: