Made it possible to edit the device name and model for a device

This commit is contained in:
Micke Prag 2008-12-29 19:18:48 +00:00
parent ff74c0f387
commit d7751732ef
12 changed files with 215 additions and 22 deletions

View file

@ -1,16 +1,68 @@
#include "device.h" #include "device.h"
#include "telldus-core.h" #include "telldus-core.h"
#include <stdlib.h>
QHash<int, Device *> Device::devices; QHash<int, Device *> Device::devices;
Device::Device(int id) 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() { 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 ) { Device *Device::getDevice( int id ) {
if (devices.contains(id)) { if (devices.contains(id)) {
@ -26,8 +78,28 @@ Device *Device::newDevice( ) {
} }
void Device::save() { void Device::save() {
bool deviceIsAdded = false;
if (p_id == 0) { //This is a new device if (p_id == 0) { //This is a new device
p_id = tdAddDevice(); 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); emit deviceAdded(p_id);
} }
} }

View file

@ -3,6 +3,7 @@
#include <QHash> #include <QHash>
#include <QPointer> #include <QPointer>
#include <QString>
class Device : public QObject class Device : public QObject
{ {
@ -10,11 +11,20 @@ class Device : public QObject
Q_DISABLE_COPY(Device) Q_DISABLE_COPY(Device)
public: public:
virtual ~Device(); ~Device();
static Device *getDevice( int id ); static Device *getDevice( int id );
static Device *newDevice( ); 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: public slots:
void save(); void save();
@ -26,7 +36,9 @@ private:
static QHash<int, Device *> devices; static QHash<int, Device *> devices;
QHash<QString, QString> p_settings; 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 #endif // DEVICE_H

View file

@ -37,11 +37,8 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const {
} }
} else if (index.column() == 1) { } else if (index.column() == 1) {
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
int id = deviceId( index ); Device *device = this->device( index );
char *name = tdGetName( id ); return device->name();
QString deviceName = QString( name );
free( name );
return deviceName;
} }
} }
@ -84,6 +81,10 @@ bool DeviceModel::removeRows ( int row, int count, const QModelIndex & parent )
return true; return true;
} }
Device *DeviceModel::device( const QModelIndex &index ) const {
return Device::getDevice( deviceId( index ) );
}
Device *DeviceModel::newDevice() const { Device *DeviceModel::newDevice() const {
Device *device = Device::newDevice(); Device *device = Device::newDevice();
connect(device, SIGNAL(deviceAdded(int)), this, SLOT(deviceAdded(int))); connect(device, SIGNAL(deviceAdded(int)), this, SLOT(deviceAdded(int)));
@ -91,9 +92,9 @@ Device *DeviceModel::newDevice() const {
} }
void DeviceModel::deviceAdded( int id ) { void DeviceModel::deviceAdded( int id ) {
Q_UNUSED(id);
int deviceCount = tdGetNumberOfDevices(); int deviceCount = tdGetNumberOfDevices();
beginInsertRows( QModelIndex(), deviceCount - 1, deviceCount ); beginInsertRows( QModelIndex(), deviceCount, deviceCount );
qDebug() << "Ny enhet: " << id;
endInsertRows(); endInsertRows();
} }

View file

@ -22,6 +22,7 @@ public:
virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
Device *newDevice() const; Device *newDevice() const;
Device *device( const QModelIndex & ) const;
private slots: private slots:
void deviceAdded( int id ); void deviceAdded( int id );

View file

@ -40,6 +40,7 @@ DeviceWidget::DeviceWidget(QWidget *parent) :
editToolButton.setText( tr("Edit") ); editToolButton.setText( tr("Edit") );
editToolButton.setToolButtonStyle( Qt::ToolButtonTextBesideIcon ); editToolButton.setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
editToolButton.setEnabled( false ); editToolButton.setEnabled( false );
connect(&editToolButton, SIGNAL(clicked()), this, SLOT(editDevice()));
buttonLayout->addWidget( &editToolButton ); buttonLayout->addWidget( &editToolButton );
removeToolButton.setIcon( QIcon( ":/images/list-remove.png" ) ); removeToolButton.setIcon( QIcon( ":/images/list-remove.png" ) );
@ -72,9 +73,9 @@ void DeviceWidget::changeEvent(QEvent *e)
void DeviceWidget::addDevice() { void DeviceWidget::addDevice() {
Device *device = model.newDevice(); Device *device = model.newDevice();
EditDeviceDialog *dialog = new EditDeviceDialog( ); EditDeviceDialog *dialog = new EditDeviceDialog(device);
if (dialog->exec() == QDialog::Accepted) { if (dialog->exec() == QDialog::Accepted) {
//device->save(); device->save();
} else { } else {
delete device; 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 &) { void DeviceWidget::listActivated(const QModelIndex &) {
removeToolButton.setEnabled( true ); removeToolButton.setEnabled( true );
editToolButton.setEnabled( true ); editToolButton.setEnabled( true );

View file

@ -21,6 +21,7 @@ private slots:
void listActivated(const QModelIndex &); void listActivated(const QModelIndex &);
void addDevice(); void addDevice();
void deleteDevice(); void deleteDevice();
void editDevice();
private: private:
DeviceModel model; DeviceModel model;

View file

@ -1,6 +1,7 @@
#include "editdevicedialog.h" #include "editdevicedialog.h"
#include "vendordevicemodel.h" #include "vendordevicemodel.h"
#include "vendordevicetreeitem.h" #include "vendordevicetreeitem.h"
#include "device.h"
#include "devicesettingnexa.h" #include "devicesettingnexa.h"
@ -13,12 +14,18 @@
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QMessageBox>
#include <QDebug> #include <QDebug>
EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags) EditDeviceDialog::EditDeviceDialog(Device *d, QWidget *parent, Qt::WFlags flags)
:QDialog(parent, 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); QVBoxLayout *layout = new QVBoxLayout(this);
@ -26,7 +33,8 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
QTreeView *deviceView = new QTreeView(this); QTreeView *deviceView = new QTreeView(this);
deviceView->setModel( model ); deviceView->setModel( model );
QItemSelectionModel *selection = deviceView->selectionModel();
selection = deviceView->selectionModel();
connect( selection, SIGNAL( currentChanged(const QModelIndex, const QModelIndex &) ), this, SLOT(selectionChanged( const QModelIndex & ) )); connect( selection, SIGNAL( currentChanged(const QModelIndex, const QModelIndex &) ), this, SLOT(selectionChanged( const QModelIndex & ) ));
deviceLayout->addWidget(deviceView); deviceLayout->addWidget(deviceView);
@ -48,7 +56,7 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
QLabel *nameLabel = new QLabel(this); QLabel *nameLabel = new QLabel(this);
nameLabel->setText( tr("&Name:") ); nameLabel->setText( tr("&Name:") );
QLineEdit *nameLineEdit = new QLineEdit( this ); nameLineEdit = new QLineEdit(device->name(), this );
nameLabel->setBuddy(nameLineEdit); nameLabel->setBuddy(nameLineEdit);
nameLayout->addRow(nameLabel, nameLineEdit); nameLayout->addRow(nameLabel, nameLineEdit);
@ -67,7 +75,7 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
QDialogButtonBox *buttonBox = new QDialogButtonBox(this); QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
buttonBox->setStandardButtons( QDialogButtonBox::Save | QDialogButtonBox::Cancel ); 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())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
layout->addWidget(buttonBox); layout->addWidget(buttonBox);
@ -75,6 +83,13 @@ EditDeviceDialog::EditDeviceDialog(QWidget *parent, Qt::WFlags flags)
foreach( DeviceSetting *s, deviceSettings ) { foreach( DeviceSetting *s, deviceSettings ) {
settingsLayout->addWidget( s ); settingsLayout->addWidget( s );
} }
QModelIndex index = model->index( device );
if (index.isValid()) {
deviceView->expand( index.parent() );
selection->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect );
}
} }
EditDeviceDialog::~EditDeviceDialog() { EditDeviceDialog::~EditDeviceDialog() {
@ -82,7 +97,7 @@ EditDeviceDialog::~EditDeviceDialog() {
} }
void EditDeviceDialog::selectionChanged( const QModelIndex & index ) { void EditDeviceDialog::selectionChanged( const QModelIndex & index ) {
const VendorDeviceTreeItem* const item = model->item(index); VendorDeviceTreeItem* const item = model->item(index);
if (!item) { if (!item) {
return; return;
} }
@ -90,3 +105,33 @@ void EditDeviceDialog::selectionChanged( const QModelIndex & index ) {
deviceImage->setPixmap( item->image() ); deviceImage->setPixmap( item->image() );
settingsLayout->setCurrentIndex( item->widget() ); 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();
}

View file

@ -7,23 +7,30 @@
class VendorDeviceModel; class VendorDeviceModel;
class DeviceSetting; class DeviceSetting;
class Device;
class QLabel; class QLabel;
class QLineEdit;
class QStackedLayout; class QStackedLayout;
class QItemSelectionModel;
class EditDeviceDialog : public QDialog class EditDeviceDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
public: public:
EditDeviceDialog(QWidget *parent = 0, Qt::WFlags flags = 0); EditDeviceDialog(Device *device, QWidget *parent = 0, Qt::WFlags flags = 0);
virtual ~EditDeviceDialog(); virtual ~EditDeviceDialog();
private slots: private slots:
void selectionChanged( const QModelIndex & ); void selectionChanged( const QModelIndex & );
void okClicked();
private: private:
VendorDeviceModel *model; VendorDeviceModel *model;
Device *device;
QStackedLayout *settingsLayout; QStackedLayout *settingsLayout;
QLabel *deviceImage; QLabel *deviceImage;
QLineEdit *nameLineEdit;
QItemSelectionModel *selection;
QHash<int, DeviceSetting *> deviceSettings; QHash<int, DeviceSetting *> deviceSettings;
}; };

View file

@ -1,5 +1,6 @@
#include "vendordevicemodel.h" #include "vendordevicemodel.h"
#include "vendordevicetreeitem.h" #include "vendordevicetreeitem.h"
#include "device.h"
VendorDeviceModel::VendorDeviceModel(QObject *parent) VendorDeviceModel::VendorDeviceModel(QObject *parent)
:QAbstractItemModel(parent), :QAbstractItemModel(parent),
@ -69,6 +70,15 @@ QModelIndex VendorDeviceModel::index(int row, int column, const QModelIndex &par
return QModelIndex(); 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 { QModelIndex VendorDeviceModel::parent(const QModelIndex &index) const {
if (!index.isValid()) { if (!index.isValid()) {
return QModelIndex(); return QModelIndex();
@ -99,7 +109,7 @@ int VendorDeviceModel::rowCount(const QModelIndex &parent) const {
return parentItem->childCount(); return parentItem->childCount();
} }
const VendorDeviceTreeItem* const VendorDeviceModel::item( const QModelIndex &index ) const { VendorDeviceTreeItem* VendorDeviceModel::item( const QModelIndex &index ) const {
if (!index.isValid()) { if (!index.isValid()) {
return 0; return 0;
} }

View file

@ -5,6 +5,7 @@
#include <QList> #include <QList>
class VendorDeviceTreeItem; class VendorDeviceTreeItem;
class Device;
class VendorDeviceModel : public QAbstractItemModel class VendorDeviceModel : public QAbstractItemModel
{ {
@ -18,10 +19,11 @@ public:
Qt::ItemFlags flags(const QModelIndex &index) const; Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const;
QModelIndex index(int row, int column, const QModelIndex &parent) const; QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex index(Device *device) const;
int rowCount(const QModelIndex &parent) const; int rowCount(const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &index) const; QModelIndex parent(const QModelIndex &index) const;
const VendorDeviceTreeItem* const item( const QModelIndex &index ) const; VendorDeviceTreeItem* item( const QModelIndex &index ) const;
private: private:
VendorDeviceTreeItem *rootItem; VendorDeviceTreeItem *rootItem;

View file

@ -70,6 +70,30 @@ int VendorDeviceTreeItem::widget() const {
return settingsWidget; 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 ) { bool VendorDeviceTreeItem::parseXml( const QString &filename ) {
QFile file(filename); QFile file(filename);
@ -144,6 +168,7 @@ void VendorDeviceTreeItem::parseDevice( QXmlStreamReader *reader, VendorDeviceTr
VendorDeviceTreeItem *item = new VendorDeviceTreeItem(attributes.value("id").toString().toInt(), parent); VendorDeviceTreeItem *item = new VendorDeviceTreeItem(attributes.value("id").toString().toInt(), parent);
item->img = attributes.value("image").toString(); item->img = attributes.value("image").toString();
item->settingsWidget = attributes.value("widget").toString().toInt(); 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 item->deviceName = reader->readElementText(); //This call must be the last one because it clears the attribute-list
parent->appendChild(item); parent->appendChild(item);

View file

@ -24,6 +24,11 @@ public:
QPixmap image() const; QPixmap image() const;
int widget() const; int widget() const;
bool isDevice() const;
int deviceModel() const;
const QString &deviceProtocol() const;
VendorDeviceTreeItem *findByDeviceId( int deviceId ) const;
private: private:
void parseVendor( QXmlStreamReader *reader ); void parseVendor( QXmlStreamReader *reader );
@ -33,7 +38,7 @@ private:
QList<VendorDeviceTreeItem *> childItems; QList<VendorDeviceTreeItem *> childItems;
int deviceId, settingsWidget; int deviceId, settingsWidget;
QString deviceName; QString deviceName, protocol;
QString img; QString img;
VendorDeviceTreeItem *parentItem; VendorDeviceTreeItem *parentItem;
}; };