Skip to content

Commit

Permalink
Use signals for announcing button state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vogtinator committed Jul 22, 2020
1 parent 770a207 commit 523afe4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 33 deletions.
3 changes: 2 additions & 1 deletion qml/Firebird/Emu/Emu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ QtObject {
signal toastMessage(string msg)
signal touchpadStateChanged(real x, real y, bool down, bool contact)
function setTouchpadState(x, y, down, contact) { touchpadStateChanged(x, y, down, contact); }
function keypadStateChanged(keymap_id, down) {}
signal buttonStateChanged(int id, bool state)
function setButtonState(keymap_id, down) {}
}
10 changes: 7 additions & 3 deletions qml/NButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ Rectangle {
if(pressed)
clicked();

Emu.keypadStateChanged(keymap_id, pressed);
Emu.setButtonState(keymap_id, pressed);
}

Component.onCompleted: {
Emu.registerNButton(keymap_id, this);
Connections {
target: Emu
function onButtonStateChanged(id, state) {
if(id === keymap_id)
pressed = state;
}
}

Text {
Expand Down
29 changes: 4 additions & 25 deletions qmlbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,13 @@ QString QMLBridge::getVersion()
return QStringLiteral(STRINGIFY(FB_VERSION));
}

void QMLBridge::keypadStateChanged(int keymap_id, bool state)
void QMLBridge::setButtonState(int id, bool state)
{
int col = keymap_id % KEYPAD_COLS, row = keymap_id / KEYPAD_COLS;
int col = id % KEYPAD_COLS, row = id / KEYPAD_COLS;

::keypad_set_key(row, col, state);
}

static std::multimap<int , QObject *> buttons;

void QMLBridge::registerNButton(unsigned int keymap_id, QVariant button)
{
buttons.insert(std::make_pair(keymap_id, button.value<QObject*>()));
}

void QMLBridge::setTouchpadState(qreal x, qreal y, bool contact, bool down)
{
::touchpad_set_state(x, y, contact, down);
Expand Down Expand Up @@ -464,26 +457,12 @@ bool QMLBridge::getTurboMode()
return turbo_mode;
}

void notifyKeypadStateChanged(int row, int col, bool state)
void QMLBridge::notifyButtonStateChanged(int row, int col, bool state)
{
assert(row < KEYPAD_ROWS);
assert(col < KEYPAD_COLS);

int keymap_id = col + row * KEYPAD_COLS;
auto it = buttons.lower_bound(keymap_id),
end = buttons.upper_bound(keymap_id);

if(it == buttons.end())
{
qWarning() << "Warning: Button " << row*KEYPAD_COLS+col << " not present in keypad!";
return;
}

do
{
QQmlProperty::write(it->second, QStringLiteral("pressed"), state);
}
while (++it != end);
emit buttonStateChanged(col + row * KEYPAD_COLS, state);
}

QObject *qmlBridgeFactory(QQmlEngine *engine, QJSEngine *scriptEngine)
Expand Down
7 changes: 4 additions & 3 deletions qmlbridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class QMLBridge : public QObject
void setTurboMode(bool e);

KitModel *getKitModel() { return &kit_model; }
Q_INVOKABLE void keypadStateChanged(int keymap_id, bool state);
Q_INVOKABLE void registerNButton(unsigned int keymap_id, QVariant button);
Q_INVOKABLE void setButtonState(int id, bool state);

// Coordinates: (0/0) = top left (1/1) = bottom right
Q_INVOKABLE void setTouchpadState(qreal x, qreal y, bool contact, bool down);
Expand Down Expand Up @@ -96,6 +95,7 @@ class QMLBridge : public QObject

void setActive(bool b);

void notifyButtonStateChanged(int row, int col, bool state);
void touchpadStateChanged();

public slots:
Expand Down Expand Up @@ -137,6 +137,8 @@ public slots:

void touchpadStateChanged(qreal x, qreal y, bool contact, bool down);

void buttonStateChanged(int id, bool state);

private:
static void usblink_progress_changed(int percent, void *qml_bridge_p);

Expand All @@ -157,7 +159,6 @@ public slots:

extern QMLBridge *the_qml_bridge;

void notifyKeypadStateChanged(int row, int col, bool state);
QObject *qmlBridgeFactory(QQmlEngine *engine, QJSEngine *scriptEngine);


Expand Down
2 changes: 1 addition & 1 deletion qtkeypadbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void setKeypad(unsigned int keymap_id, bool state)
//assert(col < KEYPAD_COLS); Not needed.

::keypad_set_key(row, col, state);
notifyKeypadStateChanged(row, col, state);
the_qml_bridge->notifyButtonStateChanged(row, col, state);
}

void keyToKeypad(QKeyEvent *event)
Expand Down

0 comments on commit 523afe4

Please sign in to comment.