Implement the controller callback
This commit is contained in:
parent
6196793ff6
commit
c0fe38e0eb
2 changed files with 61 additions and 1 deletions
|
@ -1,12 +1,12 @@
|
||||||
#include "controllerlist.h"
|
#include "controllerlist.h"
|
||||||
#include "controller.h"
|
#include "controller.h"
|
||||||
#include <telldus-core.h>
|
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
class ControllerList::PrivateData {
|
class ControllerList::PrivateData {
|
||||||
public:
|
public:
|
||||||
QList<Controller *> list;
|
QList<Controller *> list;
|
||||||
|
int callbackId;
|
||||||
};
|
};
|
||||||
|
|
||||||
ControllerList::ControllerList(QObject *parent) :
|
ControllerList::ControllerList(QObject *parent) :
|
||||||
|
@ -18,6 +18,9 @@ ControllerList::ControllerList(QObject *parent) :
|
||||||
roles[Qt::UserRole+1] = "controller";
|
roles[Qt::UserRole+1] = "controller";
|
||||||
setRoleNames(roles);
|
setRoleNames(roles);
|
||||||
|
|
||||||
|
connect(this, SIGNAL(controllerEventSignal(int,int,int,QString)), this, SLOT(controllerEventSlot(int,int,int,QString)), Qt::QueuedConnection);
|
||||||
|
d->callbackId = tdRegisterControllerEvent(&ControllerList::controllerEvent, this);
|
||||||
|
|
||||||
const int DATA_LENGTH = 255;
|
const int DATA_LENGTH = 255;
|
||||||
char name[DATA_LENGTH];
|
char name[DATA_LENGTH];
|
||||||
int available, controllerId, type;
|
int available, controllerId, type;
|
||||||
|
@ -30,6 +33,7 @@ ControllerList::ControllerList(QObject *parent) :
|
||||||
}
|
}
|
||||||
|
|
||||||
ControllerList::~ControllerList() {
|
ControllerList::~ControllerList() {
|
||||||
|
tdUnregisterCallback(d->callbackId);
|
||||||
delete d;
|
delete d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,3 +50,53 @@ void ControllerList::save() {
|
||||||
d->list.at(i)->save();
|
d->list.at(i)->save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ControllerList::controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue) {
|
||||||
|
if (changeEvent == TELLSTICK_DEVICE_STATE_CHANGED) {
|
||||||
|
for(int i = 0; i < d->list.size(); ++i) {
|
||||||
|
if (d->list.at(i)->id() != controllerId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (changeType == TELLSTICK_CHANGE_AVAILABLE) {
|
||||||
|
if (newValue == "1") {
|
||||||
|
d->list.at(i)->setAvailable(true);
|
||||||
|
} else if (newValue == "0") {
|
||||||
|
d->list.at(i)->setAvailable(false);
|
||||||
|
}
|
||||||
|
} else if (changeType == TELLSTICK_CHANGE_FIRMWARE) {
|
||||||
|
d->list.at(i)->setFirmware(newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changeEvent == TELLSTICK_DEVICE_ADDED) {
|
||||||
|
beginInsertRows( QModelIndex(), d->list.size(), d->list.size() );
|
||||||
|
Controller *controller = new Controller(controllerId, changeType, "", this);
|
||||||
|
controller->setAvailable(true);
|
||||||
|
connect(controller, SIGNAL(nameChanged()), this, SIGNAL(changed()));
|
||||||
|
d->list.append(controller);
|
||||||
|
endInsertRows();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changeEvent == TELLSTICK_DEVICE_REMOVED) {
|
||||||
|
for(int i = 0; i < d->list.size(); ++i) {
|
||||||
|
if (d->list.at(i)->id() != controllerId) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
beginRemoveRows( QModelIndex(), i, i );
|
||||||
|
d->list.takeAt(i);
|
||||||
|
endRemoveRows();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WINAPI ControllerList::controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context) {
|
||||||
|
ControllerList *controllerList = reinterpret_cast<ControllerList *>(context);
|
||||||
|
if (!controllerList) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit controllerList->controllerEventSignal(controllerId, changeEvent, changeType, QString::fromUtf8(newValue));
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QScriptValue>
|
#include <QScriptValue>
|
||||||
|
#include <telldus-core.h>
|
||||||
|
|
||||||
class ControllerList : public QAbstractListModel
|
class ControllerList : public QAbstractListModel
|
||||||
{
|
{
|
||||||
|
@ -17,11 +18,16 @@ public:
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void changed();
|
void changed();
|
||||||
|
void controllerEventSignal(int controllerId, int changeEvent, int changeType, const QString &newValue);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void save();
|
void save();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void controllerEventSlot(int controllerId, int changeEvent, int changeType, const QString &newValue);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static void WINAPI controllerEvent( int controllerId, int changeEvent, int changeType, const char *newValue, int callbackId, void *context);
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
PrivateData *d;
|
PrivateData *d;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue