Device group functionallity added to Telldus Center.

This commit is contained in:
Stefan Persson 2010-11-18 16:24:23 +00:00
parent 1244d0b22c
commit 7651831397
4 changed files with 101 additions and 24 deletions

View file

@ -28,13 +28,14 @@ signals:
void eventTriggered( const QString &name, const QString &title ); void eventTriggered( const QString &name, const QString &title );
private slots: private slots:
void deviceStateChanged( int deviceId );
void deviceChanged( int deviceId, int, int ); void deviceChanged( int deviceId, int, int );
void deviceStateChanged( int deviceId );
void nameChanged ( int, const QString& ); void nameChanged ( int, const QString& );
private: private:
void triggerCellUpdate(int row, int column);
int rowForId( int deviceId ) const; int rowForId( int deviceId ) const;
void triggerCellUpdate(int row, int column);
// static void deviceEvent(int deviceId, int method, const char *data, int callbackId, void *context); // static void deviceEvent(int deviceId, int method, const char *data, int callbackId, void *context);
static void WINAPI deviceChangeEvent(int deviceId, int, int, int, void *); static void WINAPI deviceChangeEvent(int deviceId, int, int, int, void *);

View file

@ -44,8 +44,8 @@ DeviceWidget::DeviceWidget(QWidget *parent) :
addToolButton.setIcon( QIcon( ":/images/list-add.png" ) ); addToolButton.setIcon( QIcon( ":/images/list-add.png" ) );
addToolButton.setText( tr("New") ); addToolButton.setText( tr("New") );
addToolButton.setToolButtonStyle( Qt::ToolButtonTextBesideIcon ); addToolButton.setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
//addToolButton.setPopupMode( QToolButton::MenuButtonPopup ); addToolButton.setPopupMode( QToolButton::MenuButtonPopup );
//addToolButton.setMenu( newMenu ); addToolButton.setMenu( newMenu );
connect(&addToolButton, SIGNAL(clicked()), this, SLOT(addDevice())); connect(&addToolButton, SIGNAL(clicked()), this, SLOT(addDevice()));
buttonLayout->addWidget( &addToolButton ); buttonLayout->addWidget( &addToolButton );
@ -100,16 +100,17 @@ void DeviceWidget::addDevice() {
} }
void DeviceWidget::addGroup() { void DeviceWidget::addGroup() {
/*Device *device = model.newDevice(); //Device *device = model.newDevice();
Device device(0, 0);
EditGroupDialog *dialog = new EditGroupDialog(device, &model); EditGroupDialog *dialog = new EditGroupDialog(&device, &model);
if (dialog->exec() == QDialog::Accepted) { if (dialog->exec() == QDialog::Accepted) {
device->save(); device.save();
} else { } else {
delete device; //delete device;
} }
delete dialog;*/ delete dialog;
} }
void DeviceWidget::deleteDevice() { void DeviceWidget::deleteDevice() {

View file

@ -13,6 +13,12 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QDebug> #include <QDebug>
#include <QLineEdit>
#include <QFormLayout>
#include <QLabel>
#include <QMessageBox>
class ProxyModel : public QSortFilterProxyModel { class ProxyModel : public QSortFilterProxyModel {
public: public:
ProxyModel( QObject * parent = 0 ); ProxyModel( QObject * parent = 0 );
@ -22,21 +28,41 @@ public:
void showAllRows(); void showAllRows();
void hideRow( int row ); void hideRow( int row );
void showRow( int row ); void showRow( int row );
QString getShownIds();
protected: protected:
virtual bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const; virtual bool filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const;
private: private:
QSet<int> rows; QSet<int> rows;
}; };
EditGroupDialog::EditGroupDialog(Device *d, DeviceModel *m, QWidget *parent, Qt::WFlags flags) class EditGroupDialog::PrivateData {
public:
QLineEdit *nameLineEdit;
};
EditGroupDialog::EditGroupDialog(Device *devicein, DeviceModel *m, QWidget *parent, Qt::WFlags flags)
:QDialog(parent, flags), :QDialog(parent, flags),
device(d), device(devicein),
model(m) model(m),
d(new PrivateData)
{ {
QVBoxLayout *layout = new QVBoxLayout(this); QVBoxLayout *layout = new QVBoxLayout(this);
QHBoxLayout *mainLayout = new QHBoxLayout; QHBoxLayout *mainLayout = new QHBoxLayout;
QHBoxLayout *nameLayout = new QHBoxLayout;
QLabel *nameLabel = new QLabel(this);
nameLabel->setText( tr("&Name:") );
d->nameLineEdit = new QLineEdit(device->name(), this );
nameLabel->setBuddy(d->nameLineEdit);
nameLayout->addWidget(nameLabel);
nameLayout->addWidget(d->nameLineEdit);
nameLayout->addStretch();
layout->addLayout(nameLayout);
availableProxyModel = new ProxyModel( this ); availableProxyModel = new ProxyModel( this );
availableProxyModel->setSourceModel( model ); availableProxyModel->setSourceModel( model );
availableProxyModel->showAllRows(); availableProxyModel->showAllRows();
@ -72,6 +98,7 @@ EditGroupDialog::EditGroupDialog(Device *d, DeviceModel *m, QWidget *parent, Qt:
addedProxyModel = new ProxyModel( this ); addedProxyModel = new ProxyModel( this );
addedProxyModel->setSourceModel( model ); addedProxyModel->setSourceModel( model );
setDevicesVisible();
addedListView = new QTableView( this ); addedListView = new QTableView( this );
addedListView->setAlternatingRowColors( true ); addedListView->setAlternatingRowColors( true );
@ -97,9 +124,31 @@ EditGroupDialog::EditGroupDialog(Device *d, DeviceModel *m, QWidget *parent, Qt:
EditGroupDialog::~EditGroupDialog() { EditGroupDialog::~EditGroupDialog() {
} }
void EditGroupDialog::setDevicesVisible(){
QString devicesstring = device->parameter("devices", "");
QList<int> devices;
foreach(QString device, devicesstring.split(",")) {
devices << device.toInt();
}
for (int i = 0; i < model->rowCount(); ++i){
int id = model->deviceId(model->index(i, 0));
if (id == device->id()) {
//shouldnt be able to add itself, check that here
availableProxyModel->hideRow(i);
} else if (devices.contains(id)) {
availableProxyModel->hideRow(i);
addedProxyModel->showRow(i);
}
}
}
void EditGroupDialog::addClicked() { void EditGroupDialog::addClicked() {
int row = availableProxyModel->mapToSource( availableListView->currentIndex() ).row(); int row = availableProxyModel->mapToSource( availableListView->currentIndex() ).row();
availableProxyModel->hideRow(row); availableProxyModel->hideRow(row);
addedProxyModel->showRow(row); addedProxyModel->showRow(row);
} }
@ -111,15 +160,21 @@ void EditGroupDialog::removeClicked() {
void EditGroupDialog::okClicked() { void EditGroupDialog::okClicked() {
// if (!item || !item->isDevice()) { if (d->nameLineEdit->text().trimmed() == "") {
// QMessageBox msgBox; QMessageBox msgBox;
// msgBox.setText( tr("You must choose a device") ); msgBox.setText( tr("The device must have a name.") );
// msgBox.setInformativeText( tr("Please select the device you have.") ); msgBox.setInformativeText( tr("Please fill in a name in the field under 'Name'") );
// msgBox.setIcon( QMessageBox::Critical ); msgBox.setIcon( QMessageBox::Critical );
// msgBox.setStandardButtons( QMessageBox::Ok ); msgBox.setStandardButtons( QMessageBox::Ok );
// msgBox.exec(); msgBox.exec();
// return; d->nameLineEdit->setFocus();
// } return;
}
device->setName( d->nameLineEdit->text().trimmed() );
device->setModel("group");
device->setProtocol("group");
QString shownIds = addedProxyModel->getShownIds();
device->setParameter("devices", shownIds);
this->accept(); this->accept();
} }
@ -145,6 +200,21 @@ void ProxyModel::showAllRows() {
} }
} }
QString ProxyModel::getShownIds(){
QStringList addedIds;
foreach (int index, rows){ //TODO why does this crash when debugging? Unsafe?
if(index >= 0){ //TODO why can this be
DeviceModel *model = reinterpret_cast<DeviceModel *>(this->sourceModel());
if (model) {
int deviceId = model->deviceId(model->index(index, 0));
addedIds << QString::number(deviceId);
}
}
}
return addedIds.join(",");
}
void ProxyModel::hideRow( int row ) { void ProxyModel::hideRow( int row ) {
if (rows.contains(row)) { if (rows.contains(row)) {
rows.remove(row); rows.remove(row);
@ -153,6 +223,7 @@ void ProxyModel::hideRow( int row ) {
} }
void ProxyModel::showRow( int row ) { void ProxyModel::showRow( int row ) {
if (!rows.contains(row)) { if (!rows.contains(row)) {
rows.insert(row); rows.insert(row);
invalidateFilter(); invalidateFilter();

View file

@ -23,6 +23,7 @@ private slots:
void addClicked(); void addClicked();
void removeClicked(); void removeClicked();
void okClicked(); void okClicked();
void setDevicesVisible();
private: private:
QToolButton *addToolButton, *removeToolButton; QToolButton *addToolButton, *removeToolButton;
@ -30,6 +31,9 @@ private:
Device *device; Device *device;
DeviceModel *model; DeviceModel *model;
ProxyModel *availableProxyModel, *addedProxyModel; ProxyModel *availableProxyModel, *addedProxyModel;
class PrivateData;
PrivateData *d;
}; };
#endif // EDITGROUPDIALOG_H #endif // EDITGROUPDIALOG_H