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-03 23:05:55 +02:00
|
|
|
#include "deviceview.h"
|
2010-07-04 01:00:07 +02:00
|
|
|
#include "core/mergedproxymodel.h"
|
|
|
|
#include "library/librarymodel.h"
|
|
|
|
#include "ui/iconloader.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 01:00:07 +02:00
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2010-07-03 23:05:55 +02:00
|
|
|
|
|
|
|
DeviceView::DeviceView(QWidget* parent)
|
|
|
|
: QTreeView(parent),
|
2010-07-04 01:00:07 +02:00
|
|
|
manager_(NULL),
|
|
|
|
merged_model_(NULL),
|
|
|
|
sort_model_(NULL),
|
2010-07-03 23:05:55 +02:00
|
|
|
menu_(new QMenu(this))
|
|
|
|
{
|
2010-07-04 01:00:07 +02:00
|
|
|
connect_action_ = menu_->addAction(
|
|
|
|
IconLoader::Load("list-add"), tr("Connect device"), this, SLOT(Connect()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceView::SetDeviceManager(DeviceManager *manager) {
|
|
|
|
Q_ASSERT(manager_ == NULL);
|
|
|
|
|
|
|
|
manager_ = manager;
|
|
|
|
|
|
|
|
merged_model_ = new MergedProxyModel(this);
|
|
|
|
merged_model_->setSourceModel(manager_);
|
|
|
|
|
|
|
|
sort_model_ = new QSortFilterProxyModel(this);
|
|
|
|
sort_model_->setSourceModel(merged_model_);
|
|
|
|
sort_model_->setDynamicSortFilter(true);
|
|
|
|
sort_model_->sort(0);
|
|
|
|
|
|
|
|
setModel(sort_model_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceView::contextMenuEvent(QContextMenuEvent* e) {
|
|
|
|
menu_index_ = currentIndex();
|
|
|
|
bool is_device = (MapToDevice(menu_index_).isValid());
|
|
|
|
|
|
|
|
connect_action_->setEnabled(is_device);
|
|
|
|
menu_->popup(e->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DeviceView::MapToDevice(const QModelIndex &sort_model_index) const {
|
|
|
|
if (sort_model_index.model() != sort_model_)
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
QModelIndex index =
|
|
|
|
merged_model_->mapToSource(sort_model_->mapToSource(sort_model_index));
|
|
|
|
if (index.model() != manager_)
|
|
|
|
return QModelIndex();
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceView::Connect() {
|
|
|
|
QModelIndex device_idx = MapToDevice(menu_index_);
|
|
|
|
|
|
|
|
boost::shared_ptr<ConnectedDevice> device = manager_->Connect(device_idx.row());
|
|
|
|
merged_model_->AddSubModel(device_idx, device->model());
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|