2010-07-03 23:05:55 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
#include "connecteddevice.h"
|
|
|
|
#include "devicemanager.h"
|
2010-07-04 17:01:24 +02:00
|
|
|
#include "deviceproperties.h"
|
2010-07-03 23:05:55 +02:00
|
|
|
#include "deviceview.h"
|
2010-07-04 01:00:07 +02:00
|
|
|
#include "core/mergedproxymodel.h"
|
2010-07-18 19:13:43 +02:00
|
|
|
#include "library/librarydirectorymodel.h"
|
2010-07-04 01:00:07 +02:00
|
|
|
#include "library/librarymodel.h"
|
|
|
|
#include "ui/iconloader.h"
|
2010-07-18 19:13:43 +02:00
|
|
|
#include "ui/organisedialog.h"
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
#include <QContextMenuEvent>
|
2010-07-03 23:05:55 +02:00
|
|
|
#include <QMenu>
|
2010-07-04 14:56:49 +02:00
|
|
|
#include <QMessageBox>
|
2010-07-04 13:34:25 +02:00
|
|
|
#include <QPainter>
|
2010-07-04 14:56:49 +02:00
|
|
|
#include <QPushButton>
|
2010-07-04 01:00:07 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
const int DeviceItemDelegate::kIconPadding = 6;
|
|
|
|
|
|
|
|
DeviceItemDelegate::DeviceItemDelegate(QObject *parent)
|
|
|
|
: LibraryItemDelegate(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceItemDelegate::paint(QPainter* p, const QStyleOptionViewItem& opt, const QModelIndex& index) const {
|
|
|
|
// Is it a device or a library item?
|
|
|
|
if (index.data(DeviceManager::Role_State).isNull()) {
|
|
|
|
LibraryItemDelegate::paint(p, opt, index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
QStyledItemDelegate::paint(p, opt, QModelIndex());
|
|
|
|
|
|
|
|
p->save();
|
|
|
|
|
|
|
|
// Font for the status line
|
|
|
|
QFont status_font(opt.font);
|
|
|
|
status_font.setItalic(true);
|
|
|
|
status_font.setPointSize(status_font.pointSize() - 2);
|
|
|
|
|
|
|
|
const int text_height = QFontMetrics(opt.font).height() +
|
|
|
|
QFontMetrics(status_font).height();
|
|
|
|
|
|
|
|
QRect line1(opt.rect);
|
|
|
|
QRect line2(opt.rect);
|
|
|
|
line1.setTop(line1.top() + (opt.rect.height() - text_height) / 2);
|
|
|
|
line2.setTop(line1.top() + QFontMetrics(opt.font).height());
|
|
|
|
line1.setLeft(line1.left() + DeviceManager::kDeviceIconSize + kIconPadding);
|
|
|
|
line2.setLeft(line2.left() + DeviceManager::kDeviceIconSize + kIconPadding);
|
|
|
|
|
|
|
|
// Change the color for selected items
|
|
|
|
if (opt.state & QStyle::State_Selected) {
|
|
|
|
p->setPen(opt.palette.color(QPalette::HighlightedText));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the icon
|
|
|
|
p->drawPixmap(opt.rect.topLeft(), index.data(Qt::DecorationRole).value<QPixmap>());
|
|
|
|
|
|
|
|
// Draw the first line (device name)
|
|
|
|
p->drawText(line1, Qt::AlignLeft | Qt::AlignTop, index.data().toString());
|
|
|
|
|
|
|
|
// Draw the second line (status)
|
|
|
|
p->setFont(status_font);
|
|
|
|
|
|
|
|
DeviceManager::State state =
|
|
|
|
static_cast<DeviceManager::State>(index.data(DeviceManager::Role_State).toInt());
|
2010-07-04 17:56:08 +02:00
|
|
|
QVariant progress = index.data(DeviceManager::Role_UpdatingPercentage);
|
|
|
|
QString status_text;
|
|
|
|
|
|
|
|
if (progress.isValid()) {
|
|
|
|
status_text = tr("Updating %1%...").arg(progress.toInt());
|
|
|
|
} else {
|
|
|
|
switch (state) {
|
|
|
|
case DeviceManager::State_Remembered:
|
|
|
|
status_text = tr("Not connected");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DeviceManager::State_NotConnected:
|
|
|
|
status_text = tr("Double click to open");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DeviceManager::State_Connected:
|
|
|
|
status_text = tr("Connected");
|
|
|
|
break;
|
|
|
|
}
|
2010-07-04 13:34:25 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 17:56:08 +02:00
|
|
|
p->drawText(line2, Qt::AlignLeft | Qt::AlignTop, status_text);
|
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
p->restore();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-07-03 23:05:55 +02:00
|
|
|
DeviceView::DeviceView(QWidget* parent)
|
2010-07-04 02:37:41 +02:00
|
|
|
: AutoExpandingTreeView(parent),
|
2010-07-04 01:00:07 +02:00
|
|
|
manager_(NULL),
|
|
|
|
merged_model_(NULL),
|
|
|
|
sort_model_(NULL),
|
2010-07-04 17:01:24 +02:00
|
|
|
properties_dialog_(new DeviceProperties),
|
2010-07-18 00:53:27 +02:00
|
|
|
device_menu_(new QMenu(this)),
|
|
|
|
library_menu_(new QMenu(this))
|
2010-07-03 23:05:55 +02:00
|
|
|
{
|
2010-07-18 00:53:27 +02:00
|
|
|
// Device menu items
|
|
|
|
connect_action_ = device_menu_->addAction(
|
2010-07-04 01:00:07 +02:00
|
|
|
IconLoader::Load("list-add"), tr("Connect device"), this, SLOT(Connect()));
|
2010-07-18 00:53:27 +02:00
|
|
|
disconnect_action_ = device_menu_->addAction(
|
2010-07-04 02:33:34 +02:00
|
|
|
IconLoader::Load("list-remove"), tr("Disconnect device"), this, SLOT(Disconnect()));
|
2010-07-18 00:53:27 +02:00
|
|
|
forget_action_ = device_menu_->addAction(
|
2010-07-04 14:56:49 +02:00
|
|
|
IconLoader::Load("list-remove"), tr("Forget device"), this, SLOT(Forget()));
|
2010-07-18 00:53:27 +02:00
|
|
|
device_menu_->addSeparator();
|
|
|
|
properties_action_ = device_menu_->addAction(
|
2010-07-04 17:01:24 +02:00
|
|
|
IconLoader::Load("configure"), tr("Device properties..."), this, SLOT(Properties()));
|
2010-07-04 02:37:41 +02:00
|
|
|
|
2010-07-18 00:53:27 +02:00
|
|
|
// 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();
|
2010-07-18 19:13:43 +02:00
|
|
|
organise_action_ = library_menu_->addAction(IconLoader::Load("edit-copy"),
|
|
|
|
tr("Copy to library..."), this, SLOT(Organise()));
|
2010-07-18 00:53:27 +02:00
|
|
|
delete_action_ = library_menu_->addAction(IconLoader::Load("edit-delete"),
|
2010-07-18 19:13:43 +02:00
|
|
|
tr("Delete from device..."), this, SLOT(Delete()));
|
2010-07-18 00:53:27 +02:00
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
setItemDelegate(new DeviceItemDelegate(this));
|
2010-07-04 02:37:41 +02:00
|
|
|
SetExpandOnReset(false);
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
DeviceView::~DeviceView() {
|
|
|
|
}
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
void DeviceView::SetDeviceManager(DeviceManager *manager) {
|
|
|
|
Q_ASSERT(manager_ == NULL);
|
|
|
|
|
|
|
|
manager_ = manager;
|
2010-07-04 02:33:34 +02:00
|
|
|
connect(manager_, SIGNAL(DeviceDisconnected(int)), SLOT(DeviceDisconnected(int)));
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2010-07-04 02:58:01 +02:00
|
|
|
sort_model_ = new QSortFilterProxyModel(this);
|
|
|
|
sort_model_->setSourceModel(manager_);
|
|
|
|
sort_model_->setDynamicSortFilter(true);
|
|
|
|
sort_model_->sort(0);
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
merged_model_ = new MergedProxyModel(this);
|
2010-07-04 02:58:01 +02:00
|
|
|
merged_model_->setSourceModel(sort_model_);
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2010-07-04 02:37:41 +02:00
|
|
|
connect(merged_model_,
|
|
|
|
SIGNAL(SubModelReset(QModelIndex,QAbstractItemModel*)),
|
|
|
|
SLOT(RecursivelyExpand(QModelIndex)));
|
|
|
|
|
2010-07-04 02:58:01 +02:00
|
|
|
setModel(merged_model_);
|
2010-07-04 17:01:24 +02:00
|
|
|
properties_dialog_->SetDeviceManager(manager_);
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2010-07-18 19:13:43 +02:00
|
|
|
void DeviceView::SetLibrary(LibraryModel* library) {
|
|
|
|
Q_ASSERT(manager_);
|
|
|
|
|
|
|
|
library_ = library;
|
|
|
|
|
|
|
|
organise_dialog_.reset(new OrganiseDialog(manager_->task_manager()));
|
|
|
|
organise_dialog_->AddDirectoryModel(library_->directory_model());
|
|
|
|
}
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
void DeviceView::contextMenuEvent(QContextMenuEvent* e) {
|
|
|
|
menu_index_ = currentIndex();
|
|
|
|
|
2010-07-18 00:53:27 +02:00
|
|
|
const QModelIndex device_index = MapToDevice(menu_index_);
|
|
|
|
const QModelIndex library_index = MapToLibrary(menu_index_);
|
|
|
|
|
|
|
|
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);
|
2010-07-04 02:33:34 +02:00
|
|
|
|
2010-07-18 00:53:27 +02:00
|
|
|
device_menu_->popup(e->globalPos());
|
|
|
|
} else if (library_index.isValid()) {
|
|
|
|
library_menu_->popup(e->globalPos());
|
|
|
|
}
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2010-07-04 02:58:01 +02:00
|
|
|
QModelIndex DeviceView::MapToDevice(const QModelIndex& merged_model_index) const {
|
|
|
|
QModelIndex sort_model_index = merged_model_->mapToSource(merged_model_index);
|
2010-07-04 01:00:07 +02:00
|
|
|
if (sort_model_index.model() != sort_model_)
|
|
|
|
return QModelIndex();
|
|
|
|
|
2010-07-04 02:58:01 +02:00
|
|
|
return sort_model_->mapToSource(sort_model_index);
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2010-07-18 00:53:27 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
void DeviceView::Connect() {
|
|
|
|
QModelIndex device_idx = MapToDevice(menu_index_);
|
2010-07-04 02:58:01 +02:00
|
|
|
QModelIndex sort_idx = sort_model_->mapFromSource(device_idx);
|
2010-07-04 14:56:49 +02:00
|
|
|
bool first_time = manager_->GetDatabaseId(device_idx.row()) == -1;
|
|
|
|
|
|
|
|
if (first_time) {
|
|
|
|
boost::scoped_ptr<QMessageBox> dialog(new QMessageBox(
|
|
|
|
QMessageBox::Information, tr("Connect device"),
|
|
|
|
tr("This is the first time you have connected this device. Clementine will now scan the device to find music files - this may take some time."),
|
|
|
|
QMessageBox::Cancel, this));
|
|
|
|
QPushButton* connect =
|
|
|
|
dialog->addButton(tr("Connect device"), QMessageBox::AcceptRole);
|
|
|
|
dialog->exec();
|
|
|
|
|
|
|
|
if (dialog->clickedButton() != connect)
|
|
|
|
return;
|
|
|
|
}
|
2010-07-04 01:00:07 +02:00
|
|
|
|
|
|
|
boost::shared_ptr<ConnectedDevice> device = manager_->Connect(device_idx.row());
|
2010-07-04 13:34:25 +02:00
|
|
|
if (!device)
|
|
|
|
return;
|
2010-07-04 02:58:01 +02:00
|
|
|
|
|
|
|
QSortFilterProxyModel* sort_model = new QSortFilterProxyModel(device->model());
|
|
|
|
sort_model->setSourceModel(device->model());
|
|
|
|
sort_model->setSortRole(LibraryModel::Role_SortText);
|
|
|
|
sort_model->setDynamicSortFilter(true);
|
|
|
|
sort_model->sort(0);
|
|
|
|
merged_model_->AddSubModel(sort_idx, sort_model);
|
2010-07-04 02:37:41 +02:00
|
|
|
|
|
|
|
expand(menu_index_);
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|
2010-07-04 02:33:34 +02:00
|
|
|
|
|
|
|
void DeviceView::Disconnect() {
|
|
|
|
QModelIndex device_idx = MapToDevice(menu_index_);
|
|
|
|
manager_->Disconnect(device_idx.row());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceView::DeviceDisconnected(int row) {
|
2010-07-04 02:58:01 +02:00
|
|
|
merged_model_->RemoveSubModel(sort_model_->mapFromSource(manager_->index(row)));
|
2010-07-04 02:33:34 +02:00
|
|
|
}
|
2010-07-04 14:56:49 +02:00
|
|
|
|
|
|
|
void DeviceView::Forget() {
|
|
|
|
boost::scoped_ptr<QMessageBox> dialog(new QMessageBox(
|
|
|
|
QMessageBox::Question, tr("Forget device"),
|
|
|
|
tr("Forgetting a device will remove it from this list and Clementine will have to rescan all the songs again next time you connect it."),
|
|
|
|
QMessageBox::Cancel, this));
|
|
|
|
QPushButton* forget =
|
|
|
|
dialog->addButton(tr("Forget device"), QMessageBox::DestructiveRole);
|
|
|
|
dialog->exec();
|
|
|
|
|
|
|
|
if (dialog->clickedButton() != forget)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QModelIndex device_idx = MapToDevice(menu_index_);
|
|
|
|
manager_->Forget(device_idx.row());
|
|
|
|
}
|
2010-07-04 17:01:24 +02:00
|
|
|
|
|
|
|
void DeviceView::Properties() {
|
|
|
|
properties_dialog_->ShowDevice(MapToDevice(menu_index_).row());
|
|
|
|
}
|
2010-07-04 17:25:11 +02:00
|
|
|
|
|
|
|
void DeviceView::mouseDoubleClickEvent(QMouseEvent *event) {
|
|
|
|
AutoExpandingTreeView::mouseDoubleClickEvent(event);
|
|
|
|
|
|
|
|
QModelIndex merged_index = indexAt(event->pos());
|
|
|
|
QModelIndex device_index = MapToDevice(merged_index);
|
2010-07-18 00:53:27 +02:00
|
|
|
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());
|
2010-07-04 17:25:11 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-18 00:53:27 +02:00
|
|
|
|
|
|
|
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() {
|
|
|
|
|
|
|
|
}
|
2010-07-18 19:13:43 +02:00
|
|
|
|
|
|
|
void DeviceView::Organise() {
|
|
|
|
SongList songs = GetSelectedSongs();
|
|
|
|
QStringList filenames;
|
|
|
|
foreach (const Song& song, songs) {
|
|
|
|
filenames << song.filename();
|
|
|
|
}
|
|
|
|
|
|
|
|
organise_dialog_->SetCopy(true);
|
|
|
|
organise_dialog_->SetFilenames(filenames);
|
|
|
|
organise_dialog_->show();
|
|
|
|
}
|