Sort devices by status in KCM.

Some coded added to keep the current selection when reordering.
This commit is contained in:
Albert Vaca 2013-08-16 00:25:13 +02:00
parent 05983267e3
commit d1658564f9
7 changed files with 150 additions and 49 deletions

View file

@ -1,5 +1,5 @@
set(kcm_SRCS devicesmodel.cpp
set(kcm_SRCS devicessortproxymodel.cpp devicesmodel.cpp
kcm.cpp
#wizard.cpp
dbusinterfaces.cpp

View file

@ -73,6 +73,8 @@ void DevicesModel::deviceStatusChanged(const QString& id)
//FIXME: Emitting dataChanged does not invalidate the view, refreshDeviceList does
//Q_EMIT dataChanged(index(0),index(rowCount()));
refreshDeviceList();
}
void DevicesModel::refreshDeviceList()
@ -91,36 +93,10 @@ void DevicesModel::refreshDeviceList()
endInsertRows();
}
Q_EMIT dataChanged(index(0),index(deviceIds.count()));
Q_EMIT dataChanged(index(0), index(deviceIds.count()));
}
/*
bool DevicesModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row < 0 || row > m_deviceList.count() || count < 1) {
return false;
}
beginInsertRows(parent, row, row + count - 1);
for (int i = row; i < row + count; ++i) {
m_deviceList.insert(i, new Device());
}
endInsertRows();
return true;
}
bool DevicesModel::removeRows(int row, int count, const QModelIndex &parent)
{
if (row < 0 || row > m_deviceList.count() || count < 1) {
return false;
}
beginRemoveRows(parent, row, row + count - 1);
for (int i = row; i < row + count; ++i) {
m_deviceList.removeAt(row);
}
endRemoveRows();
return true;
}
*/
QVariant DevicesModel::data(const QModelIndex &index, int role) const
{
@ -159,8 +135,10 @@ QVariant DevicesModel::data(const QModelIndex &index, int role) const
return QString(device->name());
case StatusModelRole: {
int status = StatusUnknown;
if (device->paired()) status |= StatusPaired;
if (device->reachable()) status |= StatusReachable;
if (device->reachable()) {
status |= StatusReachable;
if (device->paired()) status |= StatusPaired;
}
return status;
}
default:

View file

@ -38,8 +38,8 @@ public:
enum ModelRoles {
NameModelRole = Qt::DisplayRole,
IconModelRole = Qt::DecorationRole,
StatusModelRole = Qt::InitialSortOrderRole,
IdModelRole = Qt::UserRole,
StatusModelRole //= Qt::UserRole+1
};
enum StatusFlags {
StatusUnknown = 0x00,
@ -53,8 +53,6 @@ public:
virtual QVariant data(const QModelIndex &index, int role) const;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
//virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex());
//virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
DeviceDbusInterface* getDevice(const QModelIndex& index);

View file

@ -0,0 +1,57 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 <copyright holder> <email>
*
* This program 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "devicessortproxymodel.h"
#include "devicesmodel.h"
#include <QDebug>
DevicesSortProxyModel::DevicesSortProxyModel(DevicesModel* devicesModel)
: QSortFilterProxyModel(devicesModel)
{
setSourceModel(devicesModel);
setSortRole(DevicesModel::StatusModelRole);
connect(devicesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(sourceDataChanged(QModelIndex,QModelIndex)));
sort(0);
}
void DevicesSortProxyModel::sourceDataChanged(QModelIndex , QModelIndex )
{
sort(0);
}
bool DevicesSortProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
{
QVariant leftData = sourceModel()->data(left, Qt::InitialSortOrderRole);
QVariant rightData = sourceModel()->data(right, Qt::InitialSortOrderRole);
return leftData.toInt() > rightData.toInt();
}
bool DevicesSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const
{
Q_UNUSED(source_row);
Q_UNUSED(source_parent);
//Possible to-do: Implement filter
return true;
}

View file

@ -0,0 +1,42 @@
/*
* <one line to give the library's name and an idea of what it does.>
* Copyright 2013 <copyright holder> <email>
*
* This program 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 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef DEVICESSORTPROXYMODEL_H
#define DEVICESSORTPROXYMODEL_H
#include <QSortFilterProxyModel>
class DevicesModel;
class DevicesSortProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
DevicesSortProxyModel(DevicesModel* devicesModel);
virtual bool lessThan(const QModelIndex& left, const QModelIndex& right) const;
virtual bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const;
public slots:
void sourceDataChanged(QModelIndex,QModelIndex);
};
#endif // DEVICESSORTPROXYMODEL_H

View file

@ -32,6 +32,8 @@
#include <QDBusConnection>
#include <QDBusInterface>
#include "devicessortproxymodel.h"
#include <KServiceTypeTrader>
#include <KPluginInfo>
#include <KDebug>
@ -44,22 +46,33 @@ K_EXPORT_PLUGIN(KdeConnectKcmFactory("kdeconnect-kcm", "kdeconnect-kcm"))
KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
: KCModule(KdeConnectKcmFactory::componentData(), parent)
, kcmUi(new Ui::KdeConnectKcmUi())
, pairedDevicesList(new DevicesModel(this))
, devicesModel(new DevicesModel(this))
, currentDevice(0)
//, config(KSharedConfig::openConfig("kdeconnectrc"))
{
kcmUi->setupUi(this);
kcmUi->deviceList->setIconSize(QSize(32,32));
kcmUi->deviceList->setModel(pairedDevicesList);
sortProxyModel = new DevicesSortProxyModel(devicesModel);
kcmUi->deviceList->setModel(sortProxyModel);
kcmUi->deviceInfo->setVisible(false);
setButtons(KCModule::NoAdditionalButton);
connect(kcmUi->deviceList, SIGNAL(pressed(QModelIndex)), this, SLOT(deviceSelected(QModelIndex)));
connect(kcmUi->ping_button, SIGNAL(pressed()), this, SLOT(sendPing()));
connect(kcmUi->trust_checkbox,SIGNAL(toggled(bool)), this, SLOT(trustedStateChanged(bool)));
connect(devicesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(resetSelection()));
connect(kcmUi->deviceList, SIGNAL(pressed(QModelIndex)),
this, SLOT(deviceSelected(QModelIndex)));
connect(kcmUi->ping_button, SIGNAL(pressed()),
this, SLOT(sendPing()));
connect(kcmUi->trust_checkbox, SIGNAL(toggled(bool)),
this, SLOT(trustedStateChanged(bool)));
}
KdeConnectKcm::~KdeConnectKcm()
@ -67,21 +80,29 @@ KdeConnectKcm::~KdeConnectKcm()
}
void KdeConnectKcm::resetSelection()
{
kcmUi->deviceList->selectionModel()->setCurrentIndex(sortProxyModel->mapFromSource(currentIndex), QItemSelectionModel::ClearAndSelect);
}
void KdeConnectKcm::deviceSelected(const QModelIndex& current)
{
//Store previous selection
//Store previous device config
pluginsConfigChanged();
currentIndex = sortProxyModel->mapToSource(current);
bool valid = currentIndex.isValid();
kcmUi->deviceInfo->setVisible(valid);
if (!valid) return;
//FIXME: KPluginSelector has no way to remove a list of plugins and load another, so we need to destroy and recreate it each time
delete kcmUi->pluginSelector;
kcmUi->pluginSelector = new KPluginSelector(this);
kcmUi->verticalLayout_2->addWidget(kcmUi->pluginSelector);
bool valid = current.isValid();
kcmUi->deviceInfo->setVisible(valid);
if (!valid) return;
currentDevice = pairedDevicesList->getDevice(current);
currentDevice = devicesModel->getDevice(currentIndex);
kcmUi->deviceName->setText(currentDevice->name());
kcmUi->trust_checkbox->setChecked(currentDevice->paired());
@ -92,14 +113,16 @@ void KdeConnectKcm::deviceSelected(const QModelIndex& current)
KSharedConfigPtr deviceConfig = KSharedConfig::openConfig(path + currentDevice->id());
kcmUi->pluginSelector->addPlugins(scriptinfos, KPluginSelector::ReadConfigFile, "Plugins", QString(), deviceConfig);
connect(kcmUi->pluginSelector, SIGNAL(changed(bool)), this, SLOT(pluginsConfigChanged()));
connect(kcmUi->pluginSelector, SIGNAL(changed(bool)),
this, SLOT(pluginsConfigChanged()));
}
void KdeConnectKcm::trustedStateChanged(bool b)
{
if (!currentDevice) return;
currentDevice->setPair(b);
pairedDevicesList->deviceStatusChanged(currentDevice->id());
devicesModel->deviceStatusChanged(currentDevice->id());
}
void KdeConnectKcm::pluginsConfigChanged()
@ -121,11 +144,9 @@ void KdeConnectKcm::save()
KCModule::save();
}
void KdeConnectKcm::sendPing()
{
if (!currentDevice) return;
currentDevice->sendPing();
}

View file

@ -37,6 +37,7 @@ class QStackedLayout;
class QItemSelectionModel;
class QDBusInterface;
class DeviceDbusInterface;
class DevicesSortProxyModel;
namespace Ui {
class KdeConnectKcmUi;
@ -58,14 +59,18 @@ private Q_SLOTS:
void trustedStateChanged(bool);
void pluginsConfigChanged();
void sendPing();
void resetSelection();
private:
Ui::KdeConnectKcmUi* kcmUi;
DevicesModel* pairedDevicesList;
DevicesModel* devicesModel;
DevicesSortProxyModel* sortProxyModel;
AddDeviceWizard* addDeviceWizard;
DeviceDbusInterface* currentDevice;
QModelIndex currentIndex;
//KSharedConfigPtr config;
};
#endif