Added column 'Action' to DeviceWidget

This commit is contained in:
Micke Prag 2009-01-10 11:25:51 +00:00
parent 582029b63c
commit 635e120847
15 changed files with 241 additions and 62 deletions

View file

@ -1,46 +1,49 @@
# -------------------------------------------------
# Project created by QtCreator 2008-12-11T15:48:29
# -------------------------------------------------
TEMPLATE = lib
DEFINES += TELLDUSGUI_LIBRARY
SOURCES += telldusgui.cpp \
devicewidget.cpp \
devicemodel.cpp \
device.cpp \
editdevicedialog.cpp \
vendordevicemodel.cpp \
vendordevicetreeitem.cpp \
devicesettingikea.cpp \
devicesettingnexa.cpp \
devicesettingnexabell.cpp \
devicesettingsartano.cpp \
devicesetting.cpp
HEADERS += telldusgui.h \
devicewidget.h \
devicemodel.h \
device.h \
editdevicedialog.h \
vendordevicemodel.h \
vendordevicetreeitem.h \
devicesettingikea.h \
devicesettingnexa.h \
devicesettingnexabell.h \
devicesettingsartano.h \
devicesetting.h
FORMS +=
macx {
LIBS += -framework \
telldus-core
ICON = images/telldussetup.icns
TARGET = TelldusGui
CONFIG += lib_bundle
}
!macx {
LIBS += -l \
telldus-core
TARGET = telldus-gui
}
win32 {
LIBS += -L .
}
RESOURCES += telldusgui.qrc
# -------------------------------------------------
# Project created by QtCreator 2008-12-11T15:48:29
# -------------------------------------------------
TEMPLATE = lib
DEFINES += TELLDUSGUI_LIBRARY
SOURCES += telldusgui.cpp \
devicewidget.cpp \
devicemodel.cpp \
device.cpp \
editdevicedialog.cpp \
vendordevicemodel.cpp \
vendordevicetreeitem.cpp \
devicesettingikea.cpp \
devicesettingnexa.cpp \
devicesettingnexabell.cpp \
devicesettingsartano.cpp \
devicesetting.cpp \
methodwidget.cpp \
deviceview.cpp
HEADERS += telldusgui.h \
devicewidget.h \
devicemodel.h \
device.h \
editdevicedialog.h \
vendordevicemodel.h \
vendordevicetreeitem.h \
devicesettingikea.h \
devicesettingnexa.h \
devicesettingnexabell.h \
devicesettingsartano.h \
devicesetting.h \
methodwidget.h \
deviceview.h
FORMS +=
macx {
LIBS += -framework \
telldus-core
ICON = images/telldussetup.icns
TARGET = TelldusGui
CONFIG += lib_bundle
}
!macx {
LIBS += -l \
telldus-core
TARGET = telldus-gui
}
win32:LIBS += -L \
.
RESOURCES += telldusgui.qrc

View file

@ -11,7 +11,8 @@ Device::Device(int id)
p_protocol(""),
p_modelChanged(false),
p_nameChanged(false),
p_protocolChanged(false)
p_protocolChanged(false),
p_methods(0)
{
if (id > 0) {
char *name = tdGetName(id);
@ -73,6 +74,12 @@ const QString &Device::protocol() {
return p_protocol;
}
int Device::methods() const {
if (p_methods == 0) {
const_cast<Device*>(this)->updateMethods();
}
return p_methods;
}
Device *Device::getDevice( int id ) {
@ -102,6 +109,7 @@ void Device::save() {
if (p_modelChanged) {
tdSetModel(p_id, p_model);
updateMethods();
p_modelChanged = false;
}
@ -114,3 +122,26 @@ void Device::save() {
emit deviceAdded(p_id);
}
}
void Device::turnOff() {
tdTurnOff( p_id );
}
void Device::turnOn() {
tdTurnOn( p_id );
}
void Device::bell() {
tdBell( p_id );
}
void Device::updateMethods() {
int methods = tdMethods(p_id, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_BELL | TELLSTICK_DIM);
if (p_methods != methods) {
bool doEmit = (p_methods > 0);
p_methods = methods;
if (doEmit) {
emit methodsChanged( p_methods );
}
}
}

View file

@ -28,20 +28,29 @@ public:
void setProtocol( const QString & protocol );
const QString &protocol();
int methods() const;
public slots:
void save();
void turnOff();
void turnOn();
void bell();
signals:
void deviceAdded( int id );
void methodsChanged( int newMethods );
private:
Device(int id);
void updateMethods();
static QHash<int, Device *> devices;
QHash<QString, QString> p_settings;
int p_id, p_model;
QString p_name, p_protocol;
bool p_modelChanged, p_nameChanged, p_protocolChanged;
mutable int p_methods;
};
#endif // DEVICE_H

View file

@ -19,11 +19,11 @@ int DeviceModel::rowCount(const QModelIndex &) const {
}
int DeviceModel::columnCount(const QModelIndex &) const {
return 2;
return 3;
}
QVariant DeviceModel::data(const QModelIndex &index, int role) const {
if (index.column() > 1) {
if (index.column() > 2) {
return QVariant();
}
@ -31,7 +31,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const {
if (role == Qt::DisplayRole) {
return tr("on");
} else if (role == Qt::DecorationRole) {
return QIcon( ":/images/list-add.png" );
return QIcon( ":/images/devices.png" );
} else if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
@ -40,11 +40,24 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const {
Device *device = this->device( index );
return device->name();
}
} else if (index.column() == 2) {
// if (role == Qt::DisplayRole) {
// Device *device = this->device( index );
// return device->methods();
// }
}
return QVariant();
}
/*Qt::ItemFlags DeviceModel::flags ( const QModelIndex & index ) const {
if (!index.isValid()) {
return Qt::ItemIsEnabled;
}
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}*/
QVariant DeviceModel::headerData ( int section, Qt::Orientation orientation, int role ) const {
if (orientation != Qt::Horizontal) {
return QVariant();
@ -59,6 +72,8 @@ QVariant DeviceModel::headerData ( int section, Qt::Orientation orientation, int
return tr("State");
case 1:
return tr("Device name");
case 2:
return tr("Action");
}
return QVariant();

View file

@ -16,6 +16,7 @@ public:
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
// virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
virtual bool removeRows ( int row, int count, const QModelIndex & parent = QModelIndex() );

View file

@ -0,0 +1,32 @@
#include "deviceview.h"
#include "methodwidget.h"
#include "devicemodel.h"
#include <QHeaderView>
#include <QDebug>
DeviceView::DeviceView(QWidget *parent)
: QTableView(parent)
{
setAlternatingRowColors( true );
setShowGrid( false );
setSelectionBehavior( QAbstractItemView::SelectRows );
horizontalHeader()->setStretchLastSection( true );
verticalHeader()->hide();
}
void DeviceView::setModel ( QAbstractItemModel * model ) {
QTableView::setModel( model );
rowsInserted(QModelIndex(), 0, model->rowCount());
}
void DeviceView::rowsInserted ( const QModelIndex & /*parent*/, int start, int end ) {
DeviceModel *model = qobject_cast<DeviceModel*>( this->model() );
if (!model) {
return;
}
for (int i = start; i < end; ++i) {
QModelIndex index = model->index( i, 2, QModelIndex() );
this->setIndexWidget( index, new MethodWidget( model->device(index), this ) );
}
}

View file

@ -0,0 +1,17 @@
#ifndef DEVICEVIEW_H
#define DEVICEVIEW_H
#include <QTableView>
class DeviceView : public QTableView
{
Q_OBJECT
public:
DeviceView( QWidget *parent = 0);
virtual void setModel ( QAbstractItemModel * model );
private slots:
void rowsInserted ( const QModelIndex & parent, int start, int end );
};
#endif // DEVICEVIEW_H

View file

@ -8,6 +8,7 @@
#include <QHeaderView>
#include "editdevicedialog.h"
#include "methodwidget.h"
DeviceWidget::DeviceWidget(QWidget *parent) :
QWidget(parent),
@ -19,13 +20,9 @@ DeviceWidget::DeviceWidget(QWidget *parent) :
deviceView.setModel( &model );
deviceView.resizeColumnsToContents();
deviceView.resizeRowsToContents();
deviceView.setAlternatingRowColors( true );
deviceView.setShowGrid( false );
deviceView.setSelectionBehavior( QAbstractItemView::SelectRows );
deviceView.horizontalHeader()->setStretchLastSection( true );
deviceView.verticalHeader()->hide();
connect( &deviceView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(listActivated(const QModelIndex &)) );
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(&deviceView);

View file

@ -5,6 +5,7 @@
#include <QtGui/QTableView>
#include <QtGui/QToolButton>
#include "devicemodel.h"
#include "deviceview.h"
class DeviceWidget : public QWidget {
@ -25,7 +26,7 @@ private slots:
private:
DeviceModel model;
QTableView deviceView;
DeviceView deviceView;
QToolButton addToolButton, removeToolButton, editToolButton;
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -0,0 +1,47 @@
#include "methodwidget.h"
#include "device.h"
#include <telldus-core.h>
#include <QHBoxLayout>
#include <QToolButton>
#include <QDebug>
MethodWidget::MethodWidget( Device *device, QWidget *parent )
:QWidget(parent),
turnOff( new QToolButton( this ) ),
turnOn( new QToolButton( this ) ),
bell( new QToolButton( this ) )
{
// this->setAutoFillBackground( true );
QHBoxLayout *layout = new QHBoxLayout(this);
layout->setMargin(0);
layout->setSpacing(0);
turnOff->setIcon( QIcon(":/images/turn-off.png") );
turnOff->setVisible( false );
layout->addWidget( turnOff );
turnOn->setIcon( QIcon(":/images/turn-on.png") );
turnOn->setVisible( false );
layout->addWidget( turnOn );
bell->setIcon( QIcon(":/images/bell.png") );
bell->setVisible( false );
layout->addWidget( bell );
connect(device, SIGNAL(methodsChanged(int)), this, SLOT(updateMethods(int)));
connect(turnOff, SIGNAL(clicked()), device, SLOT(turnOff()));
connect(turnOn, SIGNAL(clicked()), device, SLOT(turnOn()));
connect(bell, SIGNAL(clicked()), device, SLOT(bell()));
updateMethods(device->methods());
}
void MethodWidget::updateMethods(int newMethods) {
turnOff->setVisible( newMethods & TELLSTICK_TURNOFF );
turnOn->setVisible( newMethods & TELLSTICK_TURNON );
bell->setVisible( newMethods & TELLSTICK_BELL );
// qDebug() << "Methods: " << newMethods;
}

View file

@ -0,0 +1,23 @@
#ifndef METHODWIDGET_H
#define METHODWIDGET_H
#include <QWidget>
class Device;
class QToolButton;
class MethodWidget : public QWidget
{
Q_OBJECT
public:
MethodWidget( Device *device, QWidget *parent = 0 );
private slots:
void updateMethods(int newMethods);
private:
Device *device;
QToolButton *turnOff, *turnOn, *bell;
};
#endif // METHODWIDGET_H

View file

@ -1,23 +1,26 @@
<RCC>
<qresource prefix="/" >
<file>data/devices.xml</file>
<file>images/list-add.png</file>
<file>images/list-edit.png</file>
<file>images/list-remove.png</file>
<file>images/devices/17-356.jpg</file>
<file>images/bell.png</file>
<file>images/devices/143011.jpg</file>
<file>images/devices/14323.jpg</file>
<file>images/devices/14327.jpg</file>
<file>images/devices/51340.jpg</file>
<file>images/devices/143011.jpg</file>
<file>images/devices/145041.jpg</file>
<file>images/devices/145071.jpg</file>
<file>images/devices/145091.jpg</file>
<file>images/devices/17-356.jpg</file>
<file>images/devices/51340.jpg</file>
<file>images/devices/el2005.jpg</file>
<file>images/devices/el2019.jpg</file>
<file>images/devices/hdr.jpg</file>
<file>images/devices/koppla.jpg</file>
<file>images/devices/ml.jpg</file>
<file>images/devices/sycr.jpg</file>
<file>images/list-add.png</file>
<file>images/list-edit.png</file>
<file>images/list-remove.png</file>
<file>images/turn-off.png</file>
<file>images/turn-on.png</file>
<file>images/vendors/ikea.jpg</file>
<file>images/vendors/intertechno.jpg</file>
<file>images/vendors/nexa.jpg</file>