Made it possible to edit the device name and model for a device
This commit is contained in:
parent
ff74c0f387
commit
d7751732ef
12 changed files with 215 additions and 22 deletions
|
@ -1,16 +1,68 @@
|
|||
#include "device.h"
|
||||
#include "telldus-core.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
QHash<int, Device *> Device::devices;
|
||||
|
||||
Device::Device(int id)
|
||||
:p_id(id)
|
||||
:p_id(id),
|
||||
p_model(0),
|
||||
p_name(""),
|
||||
p_protocol(""),
|
||||
p_modelChanged(false),
|
||||
p_nameChanged(false),
|
||||
p_protocolChanged(false)
|
||||
{
|
||||
if (id > 0) {
|
||||
char *name = tdGetName(id);
|
||||
p_name = QString::fromLocal8Bit( name );
|
||||
free( name );
|
||||
|
||||
p_model = tdGetModel(id);
|
||||
|
||||
char *protocol = tdGetProtocol(id);
|
||||
p_protocol = QString::fromUtf8( protocol );
|
||||
free( protocol );
|
||||
}
|
||||
}
|
||||
|
||||
Device::~Device() {
|
||||
}
|
||||
|
||||
void Device::setModel( int model ) {
|
||||
p_model = model;
|
||||
p_modelChanged = true;
|
||||
}
|
||||
|
||||
int Device::model() {
|
||||
return p_model;
|
||||
}
|
||||
|
||||
void Device::setName( const QString & name ) {
|
||||
if (name.compare(p_name, Qt::CaseSensitive) == 0) {
|
||||
return;
|
||||
}
|
||||
p_name = name;
|
||||
p_nameChanged = true;
|
||||
}
|
||||
|
||||
const QString &Device::name() {
|
||||
return p_name;
|
||||
}
|
||||
|
||||
void Device::setProtocol( const QString & protocol ) {
|
||||
if (protocol.compare(p_protocol, Qt::CaseSensitive) == 0) {
|
||||
return;
|
||||
}
|
||||
p_protocol = protocol;
|
||||
p_protocolChanged = true;
|
||||
}
|
||||
|
||||
const QString &Device::protocol() {
|
||||
return p_protocol;
|
||||
}
|
||||
|
||||
|
||||
Device *Device::getDevice( int id ) {
|
||||
|
||||
if (devices.contains(id)) {
|
||||
|
@ -26,8 +78,28 @@ Device *Device::newDevice( ) {
|
|||
}
|
||||
|
||||
void Device::save() {
|
||||
bool deviceIsAdded = false;
|
||||
if (p_id == 0) { //This is a new device
|
||||
p_id = tdAddDevice();
|
||||
deviceIsAdded = true;
|
||||
}
|
||||
|
||||
if (p_nameChanged) {
|
||||
tdSetName(p_id, p_name.toLocal8Bit());
|
||||
p_nameChanged = false;
|
||||
}
|
||||
|
||||
if (p_modelChanged) {
|
||||
tdSetModel(p_id, p_model);
|
||||
p_modelChanged = false;
|
||||
}
|
||||
|
||||
if (p_protocolChanged) {
|
||||
tdSetProtocol(p_id, p_protocol.toUtf8());
|
||||
p_protocolChanged = false;
|
||||
}
|
||||
|
||||
if (deviceIsAdded) {
|
||||
emit deviceAdded(p_id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <QHash>
|
||||
#include <QPointer>
|
||||
#include <QString>
|
||||
|
||||
class Device : public QObject
|
||||
{
|
||||
|
@ -10,11 +11,20 @@ class Device : public QObject
|
|||
Q_DISABLE_COPY(Device)
|
||||
|
||||
public:
|
||||
virtual ~Device();
|
||||
~Device();
|
||||
|
||||
static Device *getDevice( int id );
|
||||
static Device *newDevice( );
|
||||
|
||||
void setModel( int model );
|
||||
int model();
|
||||
|
||||
void setName( const QString & name );
|
||||
const QString &name();
|
||||
|
||||
void setProtocol( const QString & protocol );
|
||||
const QString &protocol();
|
||||
|
||||
public slots:
|
||||
void save();
|
||||
|
||||
|
@ -26,7 +36,9 @@ private:
|
|||
|
||||
static QHash<int, Device *> devices;
|
||||
QHash<QString, QString> p_settings;
|
||||
int p_id;
|
||||
int p_id, p_model;
|
||||
QString p_name, p_protocol;
|
||||
bool p_modelChanged, p_nameChanged, p_protocolChanged;
|
||||
};
|
||||
|
||||
#endif // DEVICE_H
|
||||
|
|
|
@ -37,11 +37,8 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const {
|
|||
}
|
||||
} else if (index.column() == 1) {
|
||||
if (role == Qt::DisplayRole) {
|
||||
int id = deviceId( index );
|
||||
char *name = tdGetName( id );
|
||||
QString deviceName = QString( name );
|
||||
free( name );
|
||||
return deviceName;
|
||||
Device *device = this->device( index );
|
||||
return device->name();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,6 +81,10 @@ bool DeviceModel::removeRows ( int row, int count, const QModelIndex & parent )
|
|||
return true;
|
||||
}
|
||||
|
||||
Device *DeviceModel::device( const QModelIndex &index ) const {
|
||||
return Device::getDevice( deviceId( index ) );
|
||||
}
|
||||
|
||||
Device *DeviceModel::newDevice() const {
|
||||
Device *device = Device::newDevice();
|
||||
connect(device, SIGNAL(deviceAdded(int)), this, SLOT(deviceAdded(int)));
|
||||
|
@ -91,9 +92,9 @@ Device *DeviceModel::newDevice() const {
|
|||
}
|
||||
|
||||
void DeviceModel::deviceAdded( int id ) {
|
||||
Q_UNUSED(id);
|
||||
int deviceCount = tdGetNumberOfDevices();
|
||||
beginInsertRows( QModelIndex(), deviceCount - 1, deviceCount );
|
||||
qDebug() << "Ny enhet: " << id;
|
||||
beginInsertRows( QModelIndex(), deviceCount, deviceCount );
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ public:
|
|||
virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
|
||||
|
||||
Device *newDevice() const;
|
||||
Device *device( const QModelIndex & ) const;
|
||||
|
||||
private slots:
|
||||
void deviceAdded( int id );
|
||||
|
|
|
@ -40,6 +40,7 @@ DeviceWidget::DeviceWidget(QWidget *parent) :
|
|||
editToolButton.setText( tr("Edit") );
|
||||
editToolButton.setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
|
||||
editToolButton.setEnabled( false );
|
||||
connect(&editToolButton, SIGNAL(clicked()), this, SLOT(editDevice()));
|
||||
buttonLayout->addWidget( &editToolButton );
|
||||
|
||||
removeToolButton.setIcon( QIcon( ":/images/list-remove.png" ) );
|
||||
|
@ -72,9 +73,9 @@ void DeviceWidget::changeEvent(QEvent *e)
|
|||
void DeviceWidget::addDevice() {
|
||||
Device *device = model.newDevice();
|
||||
|
||||
EditDeviceDialog *dialog = new EditDeviceDialog( );
|
||||
EditDeviceDialog *dialog = new EditDeviceDialog(device);
|
||||
if (dialog->exec() == QDialog::Accepted) {
|
||||
//device->save();
|
||||
device->save();
|
||||
} else {
|
||||
delete device;
|
||||
}
|
||||
|
@ -95,6 +96,17 @@ void DeviceWidget::deleteDevice() {
|
|||
}
|
||||
}
|
||||
|
||||
void DeviceWidget::editDevice() {
|
||||
Device *device = model.device( deviceView.currentIndex() );
|
||||
|
||||
EditDeviceDialog *dialog = new EditDeviceDialog( device );
|
||||
if (dialog->exec() == QDialog::Accepted) {
|
||||
device->save();
|
||||
}
|
||||
|
||||
delete dialog;
|
||||
}
|
||||
|
||||
void DeviceWidget::listActivated(const QModelIndex &) {
|
||||
removeToolButton.setEnabled( true );
|
||||
editToolButton.setEnabled( true );
|
||||
|
|
|
@ -21,6 +21,7 @@ private slots:
|
|||
void listActivated(const QModelIndex &);
|
||||
void addDevice();
|
||||
void deleteDevice();
|
||||
void editDevice();
|
||||
|
||||
private:
|
||||
DeviceModel model;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "editdevicedialog.h"
|
||||
#include "vendordevicemodel.h"
|
||||
#include "vendordevicetreeitem.h"
|
||||
#include "device.h"
|
||||
|
||||
#include "devicesettingnexa.h"
|
||||
|
||||
|
@ -13,12 +14,18 @@
|
|||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QMessageBox>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
|
||||
EditDeviceDialog::EditDeviceDialog(Device *d, QWidget *parent, Qt::WFlags flags)
|
||||
:QDialog(parent, flags),
|
||||
model(new VendorDeviceModel(this))
|
||||
model(new VendorDeviceModel(this)),
|
||||
device(d),
|
||||
settingsLayout(0),
|
||||
deviceImage(0),
|
||||
nameLineEdit(0),
|
||||
selection(0)
|
||||
{
|
||||
QVBoxLayout *layout = new QVBoxLayout(this);
|
||||
|
||||
|
@ -26,7 +33,8 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
|
|||
|
||||
QTreeView *deviceView = new QTreeView(this);
|
||||
deviceView->setModel( model );
|
||||
QItemSelectionModel *selection = deviceView->selectionModel();
|
||||
|
||||
selection = deviceView->selectionModel();
|
||||
connect( selection, SIGNAL( currentChanged(const QModelIndex, const QModelIndex &) ), this, SLOT(selectionChanged( const QModelIndex & ) ));
|
||||
deviceLayout->addWidget(deviceView);
|
||||
|
||||
|
@ -48,7 +56,7 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
|
|||
|
||||
QLabel *nameLabel = new QLabel(this);
|
||||
nameLabel->setText( tr("&Name:") );
|
||||
QLineEdit *nameLineEdit = new QLineEdit( this );
|
||||
nameLineEdit = new QLineEdit(device->name(), this );
|
||||
nameLabel->setBuddy(nameLineEdit);
|
||||
|
||||
nameLayout->addRow(nameLabel, nameLineEdit);
|
||||
|
@ -67,7 +75,7 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
|
|||
|
||||
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
|
||||
buttonBox->setStandardButtons( QDialogButtonBox::Save | QDialogButtonBox::Cancel );
|
||||
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
|
||||
connect(buttonBox, SIGNAL(accepted()), this, SLOT(okClicked()));
|
||||
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
|
||||
layout->addWidget(buttonBox);
|
||||
|
||||
|
@ -75,6 +83,13 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
|
|||
foreach( DeviceSetting *s, deviceSettings ) {
|
||||
settingsLayout->addWidget( s );
|
||||
}
|
||||
|
||||
QModelIndex index = model->index( device );
|
||||
if (index.isValid()) {
|
||||
deviceView->expand( index.parent() );
|
||||
selection->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
EditDeviceDialog::~EditDeviceDialog() {
|
||||
|
@ -82,7 +97,7 @@ EditDeviceDialog::~EditDeviceDialog() {
|
|||
}
|
||||
|
||||
void EditDeviceDialog::selectionChanged( const QModelIndex & index ) {
|
||||
const VendorDeviceTreeItem* const item = model->item(index);
|
||||
VendorDeviceTreeItem* const item = model->item(index);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
@ -90,3 +105,33 @@ void EditDeviceDialog::selectionChanged( const QModelIndex & index ) {
|
|||
deviceImage->setPixmap( item->image() );
|
||||
settingsLayout->setCurrentIndex( item->widget() );
|
||||
}
|
||||
|
||||
void EditDeviceDialog::okClicked() {
|
||||
VendorDeviceTreeItem* const item = model->item( selection->currentIndex() );
|
||||
if (!item || !item->isDevice()) {
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText( tr("You must choose a device") );
|
||||
msgBox.setInformativeText( tr("Please select the device you have.") );
|
||||
msgBox.setIcon( QMessageBox::Critical );
|
||||
msgBox.setStandardButtons( QMessageBox::Ok );
|
||||
msgBox.exec();
|
||||
return;
|
||||
}
|
||||
|
||||
if (nameLineEdit->text().trimmed() == "") {
|
||||
QMessageBox msgBox;
|
||||
msgBox.setText( tr("The device must have a name.") );
|
||||
msgBox.setInformativeText( tr("Please fill in a name in the field under 'Name'") );
|
||||
msgBox.setIcon( QMessageBox::Critical );
|
||||
msgBox.setStandardButtons( QMessageBox::Ok );
|
||||
msgBox.exec();
|
||||
nameLineEdit->setFocus();
|
||||
return;
|
||||
}
|
||||
|
||||
device->setName( nameLineEdit->text().trimmed() );
|
||||
device->setModel( item->deviceModel() );
|
||||
device->setProtocol( item->deviceProtocol() );
|
||||
|
||||
this->accept();
|
||||
}
|
||||
|
|
|
@ -7,23 +7,30 @@
|
|||
|
||||
class VendorDeviceModel;
|
||||
class DeviceSetting;
|
||||
class Device;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QStackedLayout;
|
||||
class QItemSelectionModel;
|
||||
|
||||
class EditDeviceDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
EditDeviceDialog(QWidget *parent = 0, Qt::WFlags flags = 0);
|
||||
EditDeviceDialog(Device *device, QWidget *parent = 0, Qt::WFlags flags = 0);
|
||||
virtual ~EditDeviceDialog();
|
||||
|
||||
private slots:
|
||||
void selectionChanged( const QModelIndex & );
|
||||
void okClicked();
|
||||
|
||||
private:
|
||||
VendorDeviceModel *model;
|
||||
Device *device;
|
||||
QStackedLayout *settingsLayout;
|
||||
QLabel *deviceImage;
|
||||
QLineEdit *nameLineEdit;
|
||||
QItemSelectionModel *selection;
|
||||
QHash<int, DeviceSetting *> deviceSettings;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include "vendordevicemodel.h"
|
||||
#include "vendordevicetreeitem.h"
|
||||
#include "device.h"
|
||||
|
||||
VendorDeviceModel::VendorDeviceModel(QObject *parent)
|
||||
:QAbstractItemModel(parent),
|
||||
|
@ -69,6 +70,15 @@ QModelIndex VendorDeviceModel::index(int row, int column, const QModelIndex &par
|
|||
return QModelIndex();
|
||||
}
|
||||
|
||||
QModelIndex VendorDeviceModel::index(Device *device) const {
|
||||
VendorDeviceTreeItem *item = rootItem->findByDeviceId( device->model() );
|
||||
if (!item) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
return createIndex( item->row(), 0, item );
|
||||
}
|
||||
|
||||
QModelIndex VendorDeviceModel::parent(const QModelIndex &index) const {
|
||||
if (!index.isValid()) {
|
||||
return QModelIndex();
|
||||
|
@ -99,7 +109,7 @@ int VendorDeviceModel::rowCount(const QModelIndex &parent) const {
|
|||
return parentItem->childCount();
|
||||
}
|
||||
|
||||
const VendorDeviceTreeItem* const VendorDeviceModel::item( const QModelIndex &index ) const {
|
||||
VendorDeviceTreeItem* VendorDeviceModel::item( const QModelIndex &index ) const {
|
||||
if (!index.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include <QList>
|
||||
|
||||
class VendorDeviceTreeItem;
|
||||
class Device;
|
||||
|
||||
class VendorDeviceModel : public QAbstractItemModel
|
||||
{
|
||||
|
@ -18,10 +19,11 @@ public:
|
|||
Qt::ItemFlags flags(const QModelIndex &index) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent) const;
|
||||
QModelIndex index(Device *device) const;
|
||||
int rowCount(const QModelIndex &parent) const;
|
||||
QModelIndex parent(const QModelIndex &index) const;
|
||||
|
||||
const VendorDeviceTreeItem* const item( const QModelIndex &index ) const;
|
||||
VendorDeviceTreeItem* item( const QModelIndex &index ) const;
|
||||
|
||||
private:
|
||||
VendorDeviceTreeItem *rootItem;
|
||||
|
|
|
@ -70,6 +70,30 @@ int VendorDeviceTreeItem::widget() const {
|
|||
return settingsWidget;
|
||||
}
|
||||
|
||||
bool VendorDeviceTreeItem::isDevice() const {
|
||||
return deviceId > 0;
|
||||
}
|
||||
|
||||
int VendorDeviceTreeItem::deviceModel() const {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
const QString &VendorDeviceTreeItem::deviceProtocol() const {
|
||||
return protocol;
|
||||
}
|
||||
|
||||
VendorDeviceTreeItem * VendorDeviceTreeItem::findByDeviceId( int deviceId ) const {
|
||||
foreach( VendorDeviceTreeItem *item, childItems ) {
|
||||
if (item->deviceId == deviceId) {
|
||||
return item;
|
||||
}
|
||||
VendorDeviceTreeItem *i = item->findByDeviceId( deviceId );
|
||||
if (i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool VendorDeviceTreeItem::parseXml( const QString &filename ) {
|
||||
QFile file(filename);
|
||||
|
@ -144,6 +168,7 @@ void VendorDeviceTreeItem::parseDevice( QXmlStreamReader *reader, VendorDeviceTr
|
|||
VendorDeviceTreeItem *item = new VendorDeviceTreeItem(attributes.value("id").toString().toInt(), parent);
|
||||
item->img = attributes.value("image").toString();
|
||||
item->settingsWidget = attributes.value("widget").toString().toInt();
|
||||
item->protocol = attributes.value("protocol").toString();
|
||||
item->deviceName = reader->readElementText(); //This call must be the last one because it clears the attribute-list
|
||||
parent->appendChild(item);
|
||||
|
||||
|
|
|
@ -24,6 +24,11 @@ public:
|
|||
|
||||
QPixmap image() const;
|
||||
int widget() const;
|
||||
bool isDevice() const;
|
||||
int deviceModel() const;
|
||||
const QString &deviceProtocol() const;
|
||||
|
||||
VendorDeviceTreeItem *findByDeviceId( int deviceId ) const;
|
||||
|
||||
private:
|
||||
void parseVendor( QXmlStreamReader *reader );
|
||||
|
@ -33,7 +38,7 @@ private:
|
|||
QList<VendorDeviceTreeItem *> childItems;
|
||||
|
||||
int deviceId, settingsWidget;
|
||||
QString deviceName;
|
||||
QString deviceName, protocol;
|
||||
QString img;
|
||||
VendorDeviceTreeItem *parentItem;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue