From 872488d1741afbf0bdfff3b259a46d5774424b67 Mon Sep 17 00:00:00 2001 From: uvos Date: Fri, 11 Feb 2022 11:46:30 +0100 Subject: [PATCH] switch from spaces to tabs --- src/QJoysticks.cpp | 376 +++++++++++++++++----------------- src/QJoysticks.h | 96 ++++----- src/auxitem.cpp | 11 +- src/auxitem.h | 11 +- src/items/auxitem.cpp | 11 +- src/items/auxitem.h | 11 +- src/items/item.cpp | 19 +- src/items/item.h | 31 +-- src/items/itemstore.cpp | 56 ++--- src/items/itemstore.h | 27 +-- src/items/train.cpp | 20 +- src/items/train.h | 20 +- src/items/turnout.cpp | 8 +- src/items/turnout.h | 11 +- src/main.cpp | 145 ++++++------- src/mainobject.cpp | 8 +- src/mainobject.h | 18 +- src/microcontroller.cpp | 184 +++++++++-------- src/microcontroller.h | 70 +++---- src/trainjs.h | 5 +- src/ui/itemcreationdialog.cpp | 22 +- src/ui/itemcreationdialog.h | 19 +- src/ui/itemscrollbox.cpp | 120 +++++------ src/ui/itemscrollbox.h | 21 +- src/ui/itemsettingsdialog.cpp | 254 +++++++++++------------ src/ui/itemsettingsdialog.h | 25 +-- src/ui/itemwidget.cpp | 210 ++++++++++--------- src/ui/itemwidget.h | 43 ++-- src/ui/mainwindow.cpp | 32 +-- src/ui/mainwindow.h | 16 +- 30 files changed, 971 insertions(+), 929 deletions(-) diff --git a/src/QJoysticks.cpp b/src/QJoysticks.cpp index 2deb989..2d6e964 100644 --- a/src/QJoysticks.cpp +++ b/src/QJoysticks.cpp @@ -28,38 +28,38 @@ QJoysticks::QJoysticks() { - /* Initialize input methods */ - m_sdlJoysticks = new SDL_Joysticks(this); - m_virtualJoystick = new VirtualJoystick(this); + /* Initialize input methods */ + m_sdlJoysticks = new SDL_Joysticks(this); + m_virtualJoystick = new VirtualJoystick(this); - /* Configure SDL joysticks */ - connect(sdlJoysticks(), &SDL_Joysticks::POVEvent, this, &QJoysticks::POVEvent); - connect(sdlJoysticks(), &SDL_Joysticks::axisEvent, this, &QJoysticks::axisEvent); - connect(sdlJoysticks(), &SDL_Joysticks::buttonEvent, this, &QJoysticks::buttonEvent); - connect(sdlJoysticks(), &SDL_Joysticks::countChanged, this, &QJoysticks::updateInterfaces); + /* Configure SDL joysticks */ + connect(sdlJoysticks(), &SDL_Joysticks::POVEvent, this, &QJoysticks::POVEvent); + connect(sdlJoysticks(), &SDL_Joysticks::axisEvent, this, &QJoysticks::axisEvent); + connect(sdlJoysticks(), &SDL_Joysticks::buttonEvent, this, &QJoysticks::buttonEvent); + connect(sdlJoysticks(), &SDL_Joysticks::countChanged, this, &QJoysticks::updateInterfaces); - /* Configure virtual joysticks */ - connect(virtualJoystick(), &VirtualJoystick::povEvent, this, &QJoysticks::POVEvent); - connect(virtualJoystick(), &VirtualJoystick::axisEvent, this, &QJoysticks::axisEvent); - connect(virtualJoystick(), &VirtualJoystick::buttonEvent, this, &QJoysticks::buttonEvent); - connect(virtualJoystick(), &VirtualJoystick::enabledChanged, this, &QJoysticks::updateInterfaces); + /* Configure virtual joysticks */ + connect(virtualJoystick(), &VirtualJoystick::povEvent, this, &QJoysticks::POVEvent); + connect(virtualJoystick(), &VirtualJoystick::axisEvent, this, &QJoysticks::axisEvent); + connect(virtualJoystick(), &VirtualJoystick::buttonEvent, this, &QJoysticks::buttonEvent); + connect(virtualJoystick(), &VirtualJoystick::enabledChanged, this, &QJoysticks::updateInterfaces); - /* React to own signals to create QML signals */ - connect(this, &QJoysticks::POVEvent, this, &QJoysticks::onPOVEvent); - connect(this, &QJoysticks::axisEvent, this, &QJoysticks::onAxisEvent); - connect(this, &QJoysticks::buttonEvent, this, &QJoysticks::onButtonEvent); + /* React to own signals to create QML signals */ + connect(this, &QJoysticks::POVEvent, this, &QJoysticks::onPOVEvent); + connect(this, &QJoysticks::axisEvent, this, &QJoysticks::onAxisEvent); + connect(this, &QJoysticks::buttonEvent, this, &QJoysticks::onButtonEvent); - /* Configure the settings */ - m_sortJoyticks = 0; - m_settings = new QSettings(qApp->organizationName(), qApp->applicationName()); - m_settings->beginGroup("Blacklisted Joysticks"); + /* Configure the settings */ + m_sortJoyticks = 0; + m_settings = new QSettings(qApp->organizationName(), qApp->applicationName()); + m_settings->beginGroup("Blacklisted Joysticks"); } QJoysticks::~QJoysticks() { - delete m_settings; - delete m_sdlJoysticks; - delete m_virtualJoystick; + delete m_settings; + delete m_sdlJoysticks; + delete m_virtualJoystick; } /** @@ -67,8 +67,8 @@ QJoysticks::~QJoysticks() */ QJoysticks *QJoysticks::getInstance() { - static QJoysticks joysticks; - return &joysticks; + static QJoysticks joysticks; + return &joysticks; } /** @@ -79,7 +79,7 @@ QJoysticks *QJoysticks::getInstance() */ int QJoysticks::count() const { - return inputDevices().count(); + return inputDevices().count(); } /** @@ -88,13 +88,13 @@ int QJoysticks::count() const */ int QJoysticks::nonBlacklistedCount() { - int cnt = count(); + int cnt = count(); - for (int i = 0; i < count(); ++i) - if (isBlacklisted(i)) - --cnt; + for (int i = 0; i < count(); ++i) + if (isBlacklisted(i)) + --cnt; - return cnt; + return cnt; } /** @@ -105,12 +105,12 @@ int QJoysticks::nonBlacklistedCount() */ QStringList QJoysticks::deviceNames() const { - QStringList names; + QStringList names; - foreach (QJoystickDevice *joystick, inputDevices()) - names.append(joystick->name); + foreach (QJoystickDevice *joystick, inputDevices()) + names.append(joystick->name); - return names; + return names; } /** @@ -118,10 +118,10 @@ QStringList QJoysticks::deviceNames() const */ int QJoysticks::getPOV(const int index, const int pov) { - if (joystickExists(index)) - return getInputDevice(index)->povs.at(pov); + if (joystickExists(index)) + return getInputDevice(index)->povs.at(pov); - return -1; + return -1; } /** @@ -129,10 +129,10 @@ int QJoysticks::getPOV(const int index, const int pov) */ double QJoysticks::getAxis(const int index, const int axis) { - if (joystickExists(index)) - return getInputDevice(index)->axes.at(axis); + if (joystickExists(index)) + return getInputDevice(index)->axes.at(axis); - return 0; + return 0; } /** @@ -140,10 +140,10 @@ double QJoysticks::getAxis(const int index, const int axis) */ bool QJoysticks::getButton(const int index, const int button) { - if (joystickExists(index)) - return getInputDevice(index)->buttons.at(button); + if (joystickExists(index)) + return getInputDevice(index)->buttons.at(button); - return false; + return false; } /** @@ -151,10 +151,10 @@ bool QJoysticks::getButton(const int index, const int button) */ int QJoysticks::getNumAxes(const int index) { - if (joystickExists(index)) - return getInputDevice(index)->axes.count(); + if (joystickExists(index)) + return getInputDevice(index)->axes.count(); - return -1; + return -1; } /** @@ -162,10 +162,10 @@ int QJoysticks::getNumAxes(const int index) */ int QJoysticks::getNumPOVs(const int index) { - if (joystickExists(index)) - return getInputDevice(index)->povs.count(); + if (joystickExists(index)) + return getInputDevice(index)->povs.count(); - return -1; + return -1; } /** @@ -173,10 +173,10 @@ int QJoysticks::getNumPOVs(const int index) */ int QJoysticks::getNumButtons(const int index) { - if (joystickExists(index)) - return getInputDevice(index)->buttons.count(); + if (joystickExists(index)) + return getInputDevice(index)->buttons.count(); - return -1; + return -1; } /** @@ -184,10 +184,10 @@ int QJoysticks::getNumButtons(const int index) */ bool QJoysticks::isBlacklisted(const int index) { - if (joystickExists(index)) - return inputDevices().at(index)->blacklisted; + if (joystickExists(index)) + return inputDevices().at(index)->blacklisted; - return true; + return true; } /** @@ -196,7 +196,7 @@ bool QJoysticks::isBlacklisted(const int index) */ bool QJoysticks::joystickExists(const int index) { - return (index >= 0) && (count() > index); + return (index >= 0) && (count() > index); } /** @@ -204,10 +204,10 @@ bool QJoysticks::joystickExists(const int index) */ QString QJoysticks::getName(const int index) { - if (joystickExists(index)) - return m_devices.at(index)->name; + if (joystickExists(index)) + return m_devices.at(index)->name; - return "Invalid Joystick"; + return "Invalid Joystick"; } /** @@ -217,7 +217,7 @@ QString QJoysticks::getName(const int index) */ SDL_Joysticks *QJoysticks::sdlJoysticks() const { - return m_sdlJoysticks; + return m_sdlJoysticks; } /** @@ -230,7 +230,7 @@ SDL_Joysticks *QJoysticks::sdlJoysticks() const */ VirtualJoystick *QJoysticks::virtualJoystick() const { - return m_virtualJoystick; + return m_virtualJoystick; } /** @@ -238,10 +238,10 @@ VirtualJoystick *QJoysticks::virtualJoystick() const */ QJoystickDevice *QJoysticks::getInputDevice(const int index) { - if (joystickExists(index)) - return inputDevices().at(index); + if (joystickExists(index)) + return inputDevices().at(index); - return Q_NULLPTR; + return Q_NULLPTR; } /** @@ -251,7 +251,7 @@ QJoystickDevice *QJoysticks::getInputDevice(const int index) */ QList QJoysticks::inputDevices() const { - return m_devices; + return m_devices; } /** @@ -260,11 +260,11 @@ QList QJoysticks::inputDevices() const */ void QJoysticks::setSortJoysticksByBlacklistState(bool sort) { - if (m_sortJoyticks != sort) - { - m_sortJoyticks = sort; - updateInterfaces(); - } + if (m_sortJoyticks != sort) + { + m_sortJoyticks = sort; + updateInterfaces(); + } } /** @@ -275,31 +275,31 @@ void QJoysticks::setSortJoysticksByBlacklistState(bool sort) */ void QJoysticks::setBlacklisted(const int index, bool blacklisted) { - Q_ASSERT(joystickExists(index)); + Q_ASSERT(joystickExists(index)); - /* Netrualize the joystick */ - if (blacklisted) - { - for (int i = 0; i < getNumAxes(index); ++i) - emit axisChanged(index, i, 0); + /* Netrualize the joystick */ + if (blacklisted) + { + for (int i = 0; i < getNumAxes(index); ++i) + emit axisChanged(index, i, 0); - for (int i = 0; i < getNumButtons(index); ++i) - emit buttonChanged(index, i, false); + for (int i = 0; i < getNumButtons(index); ++i) + emit buttonChanged(index, i, false); - for (int i = 0; i < getNumPOVs(index); ++i) - emit povChanged(index, i, 0); - } + for (int i = 0; i < getNumPOVs(index); ++i) + emit povChanged(index, i, 0); + } - /* See if blacklist value was actually changed */ - bool changed = m_devices.at(index)->blacklisted != blacklisted; + /* See if blacklist value was actually changed */ + bool changed = m_devices.at(index)->blacklisted != blacklisted; - /* Save settings */ - m_devices.at(index)->blacklisted = blacklisted; - m_settings->setValue(getName(index), blacklisted); + /* Save settings */ + m_devices.at(index)->blacklisted = blacklisted; + m_settings->setValue(getName(index), blacklisted); - /* Re-scan joysticks if blacklist value has changed */ - if (changed) - updateInterfaces(); + /* Re-scan joysticks if blacklist value has changed */ + if (changed) + updateInterfaces(); } /** @@ -307,76 +307,76 @@ void QJoysticks::setBlacklisted(const int index, bool blacklisted) */ void QJoysticks::updateInterfaces() { - m_devices.clear(); + m_devices.clear(); - /* Put blacklisted joysticks at the bottom of the list */ - if (m_sortJoyticks) - { - /* Register non-blacklisted SDL joysticks */ - foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) - { - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - if (!joystick->blacklisted) - addInputDevice(joystick); - } + /* Put blacklisted joysticks at the bottom of the list */ + if (m_sortJoyticks) + { + /* Register non-blacklisted SDL joysticks */ + foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) + { + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + if (!joystick->blacklisted) + addInputDevice(joystick); + } - /* Register the virtual joystick (if its not blacklisted) */ - if (virtualJoystick()->joystickEnabled()) - { - QJoystickDevice *joystick = virtualJoystick()->joystick(); - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + /* Register the virtual joystick (if its not blacklisted) */ + if (virtualJoystick()->joystickEnabled()) + { + QJoystickDevice *joystick = virtualJoystick()->joystick(); + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - if (!joystick->blacklisted) - { - addInputDevice(joystick); - virtualJoystick()->setJoystickID(inputDevices().count() - 1); - } - } + if (!joystick->blacklisted) + { + addInputDevice(joystick); + virtualJoystick()->setJoystickID(inputDevices().count() - 1); + } + } - /* Register blacklisted SDL joysticks */ - foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) - { - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - if (joystick->blacklisted) - addInputDevice(joystick); - } + /* Register blacklisted SDL joysticks */ + foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) + { + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + if (joystick->blacklisted) + addInputDevice(joystick); + } - /* Register the virtual joystick (if its blacklisted) */ - if (virtualJoystick()->joystickEnabled()) - { - QJoystickDevice *joystick = virtualJoystick()->joystick(); - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + /* Register the virtual joystick (if its blacklisted) */ + if (virtualJoystick()->joystickEnabled()) + { + QJoystickDevice *joystick = virtualJoystick()->joystick(); + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - if (joystick->blacklisted) - { - addInputDevice(joystick); - virtualJoystick()->setJoystickID(inputDevices().count() - 1); - } - } - } + if (joystick->blacklisted) + { + addInputDevice(joystick); + virtualJoystick()->setJoystickID(inputDevices().count() - 1); + } + } + } - /* Sort normally */ - else - { - /* Register SDL joysticks */ - foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) - { - addInputDevice(joystick); - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - } + /* Sort normally */ + else + { + /* Register SDL joysticks */ + foreach (QJoystickDevice *joystick, sdlJoysticks()->joysticks()) + { + addInputDevice(joystick); + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + } - /* Register virtual joystick */ - if (virtualJoystick()->joystickEnabled()) - { - QJoystickDevice *joystick = virtualJoystick()->joystick(); - joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); + /* Register virtual joystick */ + if (virtualJoystick()->joystickEnabled()) + { + QJoystickDevice *joystick = virtualJoystick()->joystick(); + joystick->blacklisted = m_settings->value(joystick->name, false).toBool(); - addInputDevice(joystick); - virtualJoystick()->setJoystickID(inputDevices().count() - 1); - } - } + addInputDevice(joystick); + virtualJoystick()->setJoystickID(inputDevices().count() - 1); + } + } - emit countChanged(); + emit countChanged(); } /** @@ -387,7 +387,7 @@ void QJoysticks::updateInterfaces() */ void QJoysticks::setVirtualJoystickRange(qreal range) { - virtualJoystick()->setAxisRange(range); + virtualJoystick()->setAxisRange(range); } /** @@ -395,12 +395,12 @@ void QJoysticks::setVirtualJoystickRange(qreal range) */ void QJoysticks::setVirtualJoystickEnabled(bool enabled) { - virtualJoystick()->setJoystickEnabled(enabled); + virtualJoystick()->setJoystickEnabled(enabled); } void QJoysticks::setVirtualJoystickAxisSensibility(qreal sensibility) { - virtualJoystick()->setAxisSensibility(sensibility); + virtualJoystick()->setAxisSensibility(sensibility); } /** @@ -408,8 +408,8 @@ void QJoysticks::setVirtualJoystickAxisSensibility(qreal sensibility) */ void QJoysticks::resetJoysticks() { - m_devices.clear(); - emit countChanged(); + m_devices.clear(); + emit countChanged(); } /** @@ -417,8 +417,8 @@ void QJoysticks::resetJoysticks() */ void QJoysticks::addInputDevice(QJoystickDevice *device) { - Q_ASSERT(device); - m_devices.append(device); + Q_ASSERT(device); + m_devices.append(device); } /** @@ -427,17 +427,17 @@ void QJoysticks::addInputDevice(QJoystickDevice *device) */ void QJoysticks::onPOVEvent(const QJoystickPOVEvent &e) { - if (e.joystick == nullptr) - return; + if (e.joystick == nullptr) + return; - if (!isBlacklisted(e.joystick->id)) - { - if (e.pov < getInputDevice(e.joystick->id)->povs.count()) - { - getInputDevice(e.joystick->id)->povs[e.pov] = e.angle; - emit povChanged(e.joystick->id, e.pov, e.angle); - } - } + if (!isBlacklisted(e.joystick->id)) + { + if (e.pov < getInputDevice(e.joystick->id)->povs.count()) + { + getInputDevice(e.joystick->id)->povs[e.pov] = e.angle; + emit povChanged(e.joystick->id, e.pov, e.angle); + } + } } /** @@ -446,17 +446,17 @@ void QJoysticks::onPOVEvent(const QJoystickPOVEvent &e) */ void QJoysticks::onAxisEvent(const QJoystickAxisEvent &e) { - if (e.joystick == nullptr) - return; + if (e.joystick == nullptr) + return; - if (!isBlacklisted(e.joystick->id)) - { - if (e.axis < getInputDevice(e.joystick->id)->axes.count()) - { - getInputDevice(e.joystick->id)->axes[e.axis] = e.value; - emit axisChanged(e.joystick->id, e.axis, e.value); - } - } + if (!isBlacklisted(e.joystick->id)) + { + if (e.axis < getInputDevice(e.joystick->id)->axes.count()) + { + getInputDevice(e.joystick->id)->axes[e.axis] = e.value; + emit axisChanged(e.joystick->id, e.axis, e.value); + } + } } /** @@ -465,15 +465,15 @@ void QJoysticks::onAxisEvent(const QJoystickAxisEvent &e) */ void QJoysticks::onButtonEvent(const QJoystickButtonEvent &e) { - if (e.joystick == nullptr) - return; + if (e.joystick == nullptr) + return; - if (!isBlacklisted(e.joystick->id)) - { - if (e.button < getInputDevice(e.joystick->id)->buttons.count()) - { - getInputDevice(e.joystick->id)->buttons[e.button] = e.pressed; - emit buttonChanged(e.joystick->id, e.button, e.pressed); - } - } + if (!isBlacklisted(e.joystick->id)) + { + if (e.button < getInputDevice(e.joystick->id)->buttons.count()) + { + getInputDevice(e.joystick->id)->buttons[e.button] = e.pressed; + emit buttonChanged(e.joystick->id, e.button, e.pressed); + } + } } diff --git a/src/QJoysticks.h b/src/QJoysticks.h index 819e092..6988a82 100644 --- a/src/QJoysticks.h +++ b/src/QJoysticks.h @@ -50,73 +50,73 @@ class VirtualJoystick; */ class QJoysticks : public QObject { - Q_OBJECT - Q_PROPERTY(int count READ count NOTIFY countChanged) - Q_PROPERTY(int nonBlacklistedCount READ nonBlacklistedCount NOTIFY countChanged) - Q_PROPERTY(QStringList deviceNames READ deviceNames NOTIFY countChanged) + Q_OBJECT + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_PROPERTY(int nonBlacklistedCount READ nonBlacklistedCount NOTIFY countChanged) + Q_PROPERTY(QStringList deviceNames READ deviceNames NOTIFY countChanged) - friend class Test_QJoysticks; + friend class Test_QJoysticks; signals: - void countChanged(); - void enabledChanged(const bool enabled); - void POVEvent(const QJoystickPOVEvent &event); - void axisEvent(const QJoystickAxisEvent &event); - void buttonEvent(const QJoystickButtonEvent &event); - void povChanged(const int js, const int pov, const int angle); - void axisChanged(const int js, const int axis, const qreal value); - void buttonChanged(const int js, const int button, const bool pressed); + void countChanged(); + void enabledChanged(const bool enabled); + void POVEvent(const QJoystickPOVEvent &event); + void axisEvent(const QJoystickAxisEvent &event); + void buttonEvent(const QJoystickButtonEvent &event); + void povChanged(const int js, const int pov, const int angle); + void axisChanged(const int js, const int axis, const qreal value); + void buttonChanged(const int js, const int button, const bool pressed); public: - static QJoysticks *getInstance(); + static QJoysticks *getInstance(); - int count() const; - int nonBlacklistedCount(); - QStringList deviceNames() const; + int count() const; + int nonBlacklistedCount(); + QStringList deviceNames() const; - Q_INVOKABLE int getPOV(const int index, const int pov); - Q_INVOKABLE double getAxis(const int index, const int axis); - Q_INVOKABLE bool getButton(const int index, const int button); + Q_INVOKABLE int getPOV(const int index, const int pov); + Q_INVOKABLE double getAxis(const int index, const int axis); + Q_INVOKABLE bool getButton(const int index, const int button); - Q_INVOKABLE int getNumAxes(const int index); - Q_INVOKABLE int getNumPOVs(const int index); - Q_INVOKABLE int getNumButtons(const int index); - Q_INVOKABLE bool isBlacklisted(const int index); - Q_INVOKABLE bool joystickExists(const int index); - Q_INVOKABLE QString getName(const int index); + Q_INVOKABLE int getNumAxes(const int index); + Q_INVOKABLE int getNumPOVs(const int index); + Q_INVOKABLE int getNumButtons(const int index); + Q_INVOKABLE bool isBlacklisted(const int index); + Q_INVOKABLE bool joystickExists(const int index); + Q_INVOKABLE QString getName(const int index); - SDL_Joysticks *sdlJoysticks() const; - VirtualJoystick *virtualJoystick() const; - QJoystickDevice *getInputDevice(const int index); - QList inputDevices() const; + SDL_Joysticks *sdlJoysticks() const; + VirtualJoystick *virtualJoystick() const; + QJoystickDevice *getInputDevice(const int index); + QList inputDevices() const; public slots: - void updateInterfaces(); - void setVirtualJoystickRange(qreal range); - void setVirtualJoystickEnabled(bool enabled); - void setVirtualJoystickAxisSensibility(qreal sensibility); - void setSortJoysticksByBlacklistState(bool sort); - void setBlacklisted(int index, bool blacklisted); + void updateInterfaces(); + void setVirtualJoystickRange(qreal range); + void setVirtualJoystickEnabled(bool enabled); + void setVirtualJoystickAxisSensibility(qreal sensibility); + void setSortJoysticksByBlacklistState(bool sort); + void setBlacklisted(int index, bool blacklisted); protected: - explicit QJoysticks(); - ~QJoysticks(); + explicit QJoysticks(); + ~QJoysticks(); private slots: - void resetJoysticks(); - void addInputDevice(QJoystickDevice *device); - void onPOVEvent(const QJoystickPOVEvent &e); - void onAxisEvent(const QJoystickAxisEvent &e); - void onButtonEvent(const QJoystickButtonEvent &e); + void resetJoysticks(); + void addInputDevice(QJoystickDevice *device); + void onPOVEvent(const QJoystickPOVEvent &e); + void onAxisEvent(const QJoystickAxisEvent &e); + void onButtonEvent(const QJoystickButtonEvent &e); private: - bool m_sortJoyticks; + bool m_sortJoyticks; - QSettings *m_settings; - SDL_Joysticks *m_sdlJoysticks; - VirtualJoystick *m_virtualJoystick; + QSettings *m_settings; + SDL_Joysticks *m_sdlJoysticks; + VirtualJoystick *m_virtualJoystick; - QList m_devices; + QList m_devices; }; #endif diff --git a/src/auxitem.cpp b/src/auxitem.cpp index 8f104c5..860a121 100644 --- a/src/auxitem.cpp +++ b/src/auxitem.cpp @@ -1,18 +1,19 @@ #include "auxitem.h" -AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) +AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, + QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) { } void AuxItem::setValue(uint8_t value) { - Item::setValue(value); - micro_->setAuxPwm(value); + Item::setValue(value); + micro_->setAuxPwm(value); } void AuxItem::store(QJsonObject &json) { - json["Type"] = "Aux"; - Item::store(json); + json["Type"] = "Aux"; + Item::store(json); } diff --git a/src/auxitem.h b/src/auxitem.h index a1e6765..512144f 100644 --- a/src/auxitem.h +++ b/src/auxitem.h @@ -5,16 +5,17 @@ class AuxItem: public Item { - Q_OBJECT + Q_OBJECT private: - Microcontroller* micro_; + Microcontroller* micro_; public slots: - virtual void setValue(uint8_t value); + virtual void setValue(uint8_t value); public: - AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); + AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", + uint8_t value = 0, QObject* parent = nullptr); - virtual void store(QJsonObject& json); + virtual void store(QJsonObject& json); }; diff --git a/src/items/auxitem.cpp b/src/items/auxitem.cpp index 8f104c5..860a121 100644 --- a/src/items/auxitem.cpp +++ b/src/items/auxitem.cpp @@ -1,18 +1,19 @@ #include "auxitem.h" -AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) +AuxItem::AuxItem(Microcontroller* micro, uint32_t itemIdIn, QString name, uint8_t value, + QObject* parent): Item(itemIdIn, name, value, parent), micro_(micro) { } void AuxItem::setValue(uint8_t value) { - Item::setValue(value); - micro_->setAuxPwm(value); + Item::setValue(value); + micro_->setAuxPwm(value); } void AuxItem::store(QJsonObject &json) { - json["Type"] = "Aux"; - Item::store(json); + json["Type"] = "Aux"; + Item::store(json); } diff --git a/src/items/auxitem.h b/src/items/auxitem.h index a1e6765..512144f 100644 --- a/src/items/auxitem.h +++ b/src/items/auxitem.h @@ -5,16 +5,17 @@ class AuxItem: public Item { - Q_OBJECT + Q_OBJECT private: - Microcontroller* micro_; + Microcontroller* micro_; public slots: - virtual void setValue(uint8_t value); + virtual void setValue(uint8_t value); public: - AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", uint8_t value = 0, QObject* parent = nullptr); + AuxItem(Microcontroller* micro, uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "", + uint8_t value = 0, QObject* parent = nullptr); - virtual void store(QJsonObject& json); + virtual void store(QJsonObject& json); }; diff --git a/src/items/item.cpp b/src/items/item.cpp index 36756b9..6a8dd2f 100644 --- a/src/items/item.cpp +++ b/src/items/item.cpp @@ -10,28 +10,29 @@ ItemData::ItemData(uint32_t itemIdIn, QString name, int8_t value): name_(name), QString ItemData::getName() const { - return name_; + return name_; } void ItemData::setName(QString name) { - name_ = name; + name_ = name; } int8_t ItemData::getValue() const { - return value_; + return value_; } uint32_t ItemData::id() const { - return itemId_; + return itemId_; } //item -Item::Item(uint32_t itemIdIn, QString name, int8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, value) +Item::Item(uint32_t itemIdIn, QString name, int8_t value, QObject *parent): QObject(parent), ItemData (itemIdIn, name, + value) { } @@ -47,16 +48,16 @@ Item::~Item() void Item::setFunction(uint8_t function, bool on) { - functionChanged(function, on); + functionChanged(function, on); } void Item::setValue(int8_t value) { - value_ = value; - valueChanged(value_); + value_ = value; + valueChanged(value_); } void Item::informValue(int8_t value) { - Item::setValue(value); + Item::setValue(value); } diff --git a/src/items/item.h b/src/items/item.h index be563d7..9034be5 100644 --- a/src/items/item.h +++ b/src/items/item.h @@ -11,28 +11,34 @@ class Actor; class ItemData { protected: - QString name_; + QString name_; int8_t value_; - uint32_t itemId_; + uint32_t itemId_; public: ItemData(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", int8_t value = 0); - inline bool operator==(const ItemData& in) const{ return itemId_==in.itemId_; } - inline bool operator!=(const ItemData& in) const{ return itemId_!=in.itemId_; } + inline bool operator==(const ItemData& in) const + { + return itemId_==in.itemId_; + } + inline bool operator!=(const ItemData& in) const + { + return itemId_!=in.itemId_; + } - uint32_t id() const; + uint32_t id() const; - void setName(QString name); + void setName(QString name); int8_t getValue() const; - virtual QString getName() const; + virtual QString getName() const; }; class Item: public QObject, public ItemData { - Q_OBJECT + Q_OBJECT private: signals: @@ -43,14 +49,15 @@ signals: public slots: virtual void setValue(int8_t value); - virtual void setFunction(uint8_t funciton, bool value); + virtual void setFunction(uint8_t funciton, bool value); public: - Item(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", int8_t value = 0, QObject *parent = nullptr); - Item(const ItemData& itemData, QObject *parent = nullptr); + Item(uint32_t itemIdIn = QRandomGenerator::global()->generate(), QString name = "Item", int8_t value = 0, + QObject *parent = nullptr); + Item(const ItemData& itemData, QObject *parent = nullptr); - virtual ~Item(); + virtual ~Item(); void informValue(int8_t value); diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index 61b41eb..2a335f8 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -15,12 +15,12 @@ void ItemStore::addItem(std::shared_ptr item) qWarning()<<"invalid item"; return; } - bool mached = false; - for(unsigned i = 0; i < items_.size(); i++ ) - if(*items_[i] == *item) mached = true; - if(!mached) - { - items_.push_back(std::shared_ptr(item)); + bool mached = false; + for(unsigned i = 0; i < items_.size(); i++ ) + if(*items_[i] == *item) mached = true; + if(!mached) + { + items_.push_back(std::shared_ptr(item)); if(dynamic_cast(item.get())) { @@ -36,9 +36,9 @@ void ItemStore::addItem(std::shared_ptr item) } } - itemAdded(std::weak_ptr(items_.back())); - } - qDebug()<<"Got item: "<id()<<" matched: "<(items_.back())); + } + qDebug()<<"Got item: "<id()<<" matched: "<>& itemIn) { - for(unsigned j = 0; j < itemIn.size(); j++) - addItem(itemIn[j]); + for(unsigned j = 0; j < itemIn.size(); j++) + addItem(itemIn[j]); } void ItemStore::removeItem(const ItemData& item) { - for(unsigned j = 0; j < items_.size(); j++) - { - if(item == *items_[j]) - { - items_.erase(items_.begin()+j); - --j; - } - } + for(unsigned j = 0; j < items_.size(); j++) + { + if(item == *items_[j]) + { + items_.erase(items_.begin()+j); + --j; + } + } } void ItemStore::clear() { - for(size_t i = 0; i < items_.size(); ++i) itemDeleted(*items_[i]); - items_.clear(); + for(size_t i = 0; i < items_.size(); ++i) itemDeleted(*items_[i]); + items_.clear(); } void ItemStore::itemStateChanged(const ItemData& item) { - for(unsigned i = 0; i < items_.size(); i++ ) - { - if(items_[i]->operator==(item)) - { + for(unsigned i = 0; i < items_.size(); i++ ) + { + if(items_[i]->operator==(item)) + { - if(items_[i]->getValue() != item.getValue())items_[i]->informValue(item.getValue()); - } + if(items_[i]->getValue() != item.getValue())items_[i]->informValue(item.getValue()); + } - } + } } diff --git a/src/items/itemstore.h b/src/items/itemstore.h index 6148027..8e94404 100644 --- a/src/items/itemstore.h +++ b/src/items/itemstore.h @@ -7,18 +7,21 @@ class ItemStore: public QObject { - Q_OBJECT + Q_OBJECT private: - std::vector< std::shared_ptr > items_; + std::vector< std::shared_ptr > items_; public: - ItemStore(QObject *parent = nullptr); - virtual ~ItemStore(){} + ItemStore(QObject *parent = nullptr); + virtual ~ItemStore() {} - inline std::vector< std::shared_ptr >* getItems(){ return &items_; } + inline std::vector< std::shared_ptr >* getItems() + { + return &items_; + } - void clear(); + void clear(); private slots: @@ -26,13 +29,13 @@ private slots: signals: - void itemDeleted(ItemData item); - void itemAdded(std::weak_ptr Item); + void itemDeleted(ItemData item); + void itemAdded(std::weak_ptr Item); public slots: - void removeItem(const ItemData& item); - void addItem(std::shared_ptr item); - void addItems(const std::vector>& itemsIn); - void itemStateChanged(const ItemData& item); + void removeItem(const ItemData& item); + void addItem(std::shared_ptr item); + void addItems(const std::vector>& itemsIn); + void itemStateChanged(const ItemData& item); }; diff --git a/src/items/train.cpp b/src/items/train.cpp index 09266de..2929df9 100644 --- a/src/items/train.cpp +++ b/src/items/train.cpp @@ -2,8 +2,8 @@ Train::Train(uint8_t id, uint8_t address, uint8_t functionMask, int8_t initalValue): Item(address, QString("Train ")+QString::number(id), initalValue), - functionMask_(functionMask), - train_id_(id) + functionMask_(functionMask), + train_id_(id) { } @@ -11,21 +11,21 @@ Microcontroller *Train::micro = nullptr; void Train::setFunction(uint8_t funciton, bool value) { - Item::setFunction(funciton, value); - if(micro) - micro->trainSetFunction(train_id_, funciton, value); + Item::setFunction(funciton, value); + if(micro) + micro->trainSetFunction(train_id_, funciton, value); } void Train::setValue(int8_t value) { - Item::setValue(value); - if(micro) - micro->trainSetSpeed(train_id_, value); + Item::setValue(value); + if(micro) + micro->trainSetSpeed(train_id_, value); } void Train::reverse() { - if(micro) - micro->trainReverse(train_id_); + if(micro) + micro->trainReverse(train_id_); } diff --git a/src/items/train.h b/src/items/train.h index 41bbf0e..dec8b31 100644 --- a/src/items/train.h +++ b/src/items/train.h @@ -6,21 +6,27 @@ class Train : public Item { - Q_OBJECT - uint8_t functionMask_; - uint8_t train_id_; + Q_OBJECT + uint8_t functionMask_; + uint8_t train_id_; public: - static Microcontroller *micro; + static Microcontroller *micro; Train(uint8_t id = 0, uint8_t address = 0, uint8_t functionMask = 0, int8_t initalValue = 0); - uint8_t getFunctionMask() {return functionMask_;} + uint8_t getFunctionMask() + { + return functionMask_; + } public slots: - void reverse(); + void reverse(); virtual void setFunction(uint8_t function, bool on); virtual void setValue(int8_t value); - uint8_t getTrainId(){return train_id_;} + uint8_t getTrainId() + { + return train_id_; + } }; #endif // TRAIN_H diff --git a/src/items/turnout.cpp b/src/items/turnout.cpp index a2d3133..df82938 100644 --- a/src/items/turnout.cpp +++ b/src/items/turnout.cpp @@ -6,13 +6,13 @@ Turnout::Turnout(uint8_t id, uint8_t address, uint8_t subaddress, int8_t initalV : Item(address | (subaddress << 8), QString("Turnout ")+QString::number(id), initalValue), subaddress_(subaddress), turnoutId_(id) { - itemId_ = address | (subaddress << 8); - name_ = QString("Turnout ")+QString::number(id); + itemId_ = address | (subaddress << 8); + name_ = QString("Turnout ")+QString::number(id); } void Turnout::setValue(int8_t value) { - Item::setValue(value); - if(micro) + Item::setValue(value); + if(micro) micro->tunoutSetDirection(turnoutId_, value > 0); } diff --git a/src/items/turnout.h b/src/items/turnout.h index 27cd975..6f203c2 100644 --- a/src/items/turnout.h +++ b/src/items/turnout.h @@ -6,14 +6,17 @@ class Turnout : public Item { - uint8_t subaddress_; - uint8_t turnoutId_; + uint8_t subaddress_; + uint8_t turnoutId_; public: - static Microcontroller *micro; + static Microcontroller *micro; explicit Turnout(uint8_t id = 0, uint8_t address = 0, uint8_t subaddress = 0, int8_t initalValue = 0); virtual void setValue(int8_t value); - uint8_t getTurnoutId(){return turnoutId_;} + uint8_t getTurnoutId() + { + return turnoutId_; + } }; #endif // TURNOUT_H diff --git a/src/main.cpp b/src/main.cpp index 4464629..8975970 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,94 +18,97 @@ int main(int argc, char *argv[]) { - QApplication a(argc, argv); + QApplication a(argc, argv); - //set info - QCoreApplication::setOrganizationName("UVOS"); - QCoreApplication::setOrganizationDomain("uvos.xyz"); - QCoreApplication::setApplicationName("traincontrollerui"); - QCoreApplication::setApplicationVersion("0.1"); + //set info + QCoreApplication::setOrganizationName("UVOS"); + QCoreApplication::setOrganizationDomain("uvos.xyz"); + QCoreApplication::setApplicationName("traincontrollerui"); + QCoreApplication::setApplicationVersion("0.1"); - QDir::setCurrent(a.applicationDirPath()); + QDir::setCurrent(a.applicationDirPath()); - //parse comand line - QCommandLineParser parser; - parser.setApplicationDescription("Smart Home Interface"); - parser.addHelpOption(); - parser.addVersionOption(); - QCommandLineOption tcpOption(QStringList() << "t" << "tcp", QCoreApplication::translate("main", "Use Tcp connection")); - parser.addOption(tcpOption); - QCommandLineOption hostOption(QStringList() << "H" << "host", QCoreApplication::translate("main", "Set server host ip addres"), "adress"); - parser.addOption(hostOption); - QCommandLineOption portOption(QStringList() << "p" << "port", QCoreApplication::translate("main", "Set server Port in TCP mode or Serial port in serial mode"), "port"); - parser.addOption(portOption); - QCommandLineOption serialOption(QStringList() << "s" << "serial", QCoreApplication::translate("main", "Use serial connection")); - parser.addOption(serialOption); - QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate")); - parser.addOption(baudOption); - parser.process(a); + //parse comand line + QCommandLineParser parser; + parser.setApplicationDescription("Smart Home Interface"); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption tcpOption(QStringList() << "t" << "tcp", QCoreApplication::translate("main", "Use Tcp connection")); + parser.addOption(tcpOption); + QCommandLineOption hostOption(QStringList() << "H" << "host", QCoreApplication::translate("main", + "Set server host ip addres"), "adress"); + parser.addOption(hostOption); + QCommandLineOption portOption(QStringList() << "p" << "port", QCoreApplication::translate("main", + "Set server Port in TCP mode or Serial port in serial mode"), "port"); + parser.addOption(portOption); + QCommandLineOption serialOption(QStringList() << "s" << "serial", QCoreApplication::translate("main", + "Use serial connection")); + parser.addOption(serialOption); + QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate")); + parser.addOption(baudOption); + parser.process(a); - QIODevice* masterIODevice = nullptr; + QIODevice* masterIODevice = nullptr; - if(parser.isSet(tcpOption)) - { - QTcpSocket* microSocket = new QTcpSocket; + if(parser.isSet(tcpOption)) + { + QTcpSocket* microSocket = new QTcpSocket; - int port = 6856; - if(parser.isSet(portOption)) port = parser.value(portOption).toInt(); + int port = 6856; + if(parser.isSet(portOption)) port = parser.value(portOption).toInt(); - QString host("127.0.0.1"); - if(parser.isSet(hostOption)) host = parser.value(hostOption); - std::cout<<"connecting to "<connectToHost(host, port, QIODevice::ReadWrite); - if(!microSocket->waitForConnected(3000)) - { - std::cout<<"Can not connect to to Server.\n"; - QMessageBox::critical(nullptr, "Error", "Can not connect to to Server"); - return 1; - } - masterIODevice = microSocket; - } - else - { - QSerialPort* microPort = new QSerialPort; - if(parser.isSet(portOption)) microPort->setPortName(parser.value(portOption)); - else microPort->setPortName("ttyUSB0"); + QString host("127.0.0.1"); + if(parser.isSet(hostOption)) host = parser.value(hostOption); + std::cout<<"connecting to "<connectToHost(host, port, QIODevice::ReadWrite); + if(!microSocket->waitForConnected(3000)) + { + std::cout<<"Can not connect to to Server.\n"; + QMessageBox::critical(nullptr, "Error", "Can not connect to to Server"); + return 1; + } + masterIODevice = microSocket; + } + else + { + QSerialPort* microPort = new QSerialPort; + if(parser.isSet(portOption)) microPort->setPortName(parser.value(portOption)); + else microPort->setPortName("ttyUSB0"); - if(parser.isSet(portOption)) microPort->setBaudRate(parser.value(baudOption).toInt()); - else microPort->setBaudRate(BAUD); + if(parser.isSet(portOption)) microPort->setBaudRate(parser.value(baudOption).toInt()); + else microPort->setBaudRate(BAUD); - if(!microPort->open(QIODevice::ReadWrite)) - { - QMessageBox::critical(nullptr, "Error", QString("Can not open serial port ")+microPort->portName()); - std::cout<<"Can not open serial port "<portName().toStdString()<<". Continueing in demo mode"<<'\n'; - return 1; - } - masterIODevice = microPort; - } + if(!microPort->open(QIODevice::ReadWrite)) + { + QMessageBox::critical(nullptr, "Error", QString("Can not open serial port ")+microPort->portName()); + std::cout<<"Can not open serial port "<portName().toStdString()<<". Continueing in demo mode"<<'\n'; + return 1; + } + masterIODevice = microPort; + } - Microcontroller micro(masterIODevice); - micro.setPower(true); + Microcontroller micro(masterIODevice); + micro.setPower(true); - TrainJs::init(); + TrainJs::init(); - Train::micro = µ - Turnout::micro = µ - ItemStore items; + Train::micro = µ + Turnout::micro = µ + ItemStore items; - QObject::connect(µ, &Microcontroller::gotItemList, &items, &ItemStore::addItems); + QObject::connect(µ, &Microcontroller::gotItemList, &items, &ItemStore::addItems); QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged); - //mainwindow - MainWindow w(µ, &items); + //mainwindow + MainWindow w(µ, &items); - w.show(); + w.show(); - int retVal = a.exec(); - micro.setPower(false); + int retVal = a.exec(); + micro.setPower(false); - if(masterIODevice) - delete masterIODevice; - return retVal; + if(masterIODevice) + delete masterIODevice; + return retVal; } diff --git a/src/mainobject.cpp b/src/mainobject.cpp index 4af15c0..3a0558b 100644 --- a/src/mainobject.cpp +++ b/src/mainobject.cpp @@ -1,11 +1,11 @@ #include "mainobject.h" MainObject::MainObject(QIODevice* ioDevice) : - masterIODevice(ioDevice), - micro(masterIODevice) + masterIODevice(ioDevice), + micro(masterIODevice) { - //connect item store - QObject::connect(µ, &Microcontroller::gotItemList, &items, &ItemStore::addItems); + //connect item store + QObject::connect(µ, &Microcontroller::gotItemList, &items, &ItemStore::addItems); } MainObject::~MainObject() diff --git a/src/mainobject.h b/src/mainobject.h index 234bd16..62596d8 100644 --- a/src/mainobject.h +++ b/src/mainobject.h @@ -26,22 +26,22 @@ class MainObject : public QObject { - Q_OBJECT + Q_OBJECT public: - //io - QIODevice * const masterIODevice = nullptr; + //io + QIODevice * const masterIODevice = nullptr; - Microcontroller micro; + Microcontroller micro; - const QString settingsPath; + const QString settingsPath; - //items - ItemStore items; + //items + ItemStore items; public: - explicit MainObject(QIODevice* ioDevice); - ~MainObject(); + explicit MainObject(QIODevice* ioDevice); + ~MainObject(); }; #endif // MAINOBJECT_H diff --git a/src/microcontroller.cpp b/src/microcontroller.cpp index 093be50..a162d8d 100644 --- a/src/microcontroller.cpp +++ b/src/microcontroller.cpp @@ -7,83 +7,83 @@ void Microcontroller::trainSetSpeed(uint8_t id, int8_t speed) { - qDebug()<<__func__; - std::stringstream ss; + qDebug()<<__func__; + std::stringstream ss; ss<<"train "<<(unsigned)id<<" speed "<<(int)speed<<'\n'; - write(ss.str().c_str()); + write(ss.str().c_str()); } void Microcontroller::trainReverse(uint8_t id) { - std::stringstream ss; - ss<<"train "<<(unsigned)id<<" reverse\n"; - write(ss.str().c_str()); + std::stringstream ss; + ss<<"train "<<(unsigned)id<<" reverse\n"; + write(ss.str().c_str()); } void Microcontroller::trainSetFunction(uint8_t id, uint8_t function, bool on) { - std::stringstream ss; - ss<<"train "<<(unsigned)id<<" function "<<(unsigned)function<<' '<<(on ? "on" : "off")<<'\n'; - write(ss.str().c_str()); + std::stringstream ss; + ss<<"train "<<(unsigned)id<<" function "<<(unsigned)function<<' '<<(on ? "on" : "off")<<'\n'; + write(ss.str().c_str()); } void Microcontroller::tunoutSetDirection(uint8_t id, bool direction) { - std::stringstream ss; - ss<<"turnout "<<(unsigned)id<<" set "<<(!direction ? "left" : "right")<<'\n'; - write(ss.str().c_str()); + std::stringstream ss; + ss<<"turnout "<<(unsigned)id<<" set "<<(!direction ? "left" : "right")<<'\n'; + write(ss.str().c_str()); } void Microcontroller::setPower(bool on) { - write(on ? "power on\n" : "power off\n"); + write(on ? "power on\n" : "power off\n"); } void Microcontroller::estop() { - write("stop\n"); + write("stop\n"); } void Microcontroller::write(const QByteArray& buffer) { - qDebug()<write(buffer); + qDebug()<write(buffer); _port->waitForBytesWritten(1000); - } + } std::this_thread::sleep_for(std::chrono::milliseconds(40)); } void Microcontroller::write(char* buffer, const size_t length) { - qDebug()<write(buffer, length); + qDebug()<write(buffer, length); //_port->waitForBytesWritten(1000); - } + } std::this_thread::sleep_for(std::chrono::milliseconds(40)); } bool Microcontroller::connected() { - if(_port != nullptr) - return _port->isOpen(); - else return false; + if(_port != nullptr) + return _port->isOpen(); + else return false; } void Microcontroller::requestState() { - write("train list\n"); - write("turnout list\n"); + write("train list\n"); + write("turnout list\n"); } //housekeeping Microcontroller::Microcontroller(QIODevice* port) { - setIODevice(port); + setIODevice(port); } Microcontroller::Microcontroller() @@ -96,54 +96,56 @@ Microcontroller::~Microcontroller() void Microcontroller::setIODevice(QIODevice *port) { - _port = port; - QObject::connect(_port, &QIODevice::readyRead, this, &Microcontroller::isReadyRead); + _port = port; + QObject::connect(_port, &QIODevice::readyRead, this, &Microcontroller::isReadyRead); } std::shared_ptr Microcontroller::processTrainLine(const QString& buffer) { - QStringList bufferList = buffer.split(' '); + QStringList bufferList = buffer.split(' '); if(bufferList.size() >= 14 && buffer.startsWith("TRAIN NUMBER:")) - { - return std::shared_ptr(new Train(bufferList[2].toInt(), bufferList[4].toInt(), bufferList[13].toInt(nullptr, 2), bufferList[9].toInt())); - } - return nullptr; + { + return std::shared_ptr(new Train(bufferList[2].toInt(), bufferList[4].toInt(), bufferList[13].toInt(nullptr, 2), + bufferList[9].toInt())); + } + return nullptr; } std::shared_ptr Microcontroller::processTurnoutLine(const QString& buffer) { - QStringList bufferList = buffer.split(' '); + QStringList bufferList = buffer.split(' '); if(bufferList.size() >= 11 && buffer.startsWith("TURNOUT NUMBER:")) - { - return std::shared_ptr(new Turnout(bufferList[2].toInt(), bufferList[4].toInt(), bufferList[6].toInt(), bufferList[11].toInt())); - } - return nullptr; + { + return std::shared_ptr(new Turnout(bufferList[2].toInt(), bufferList[4].toInt(), bufferList[6].toInt(), + bufferList[11].toInt())); + } + return nullptr; } void Microcontroller::processList(const QString& buffer) { - QStringList bufferList = buffer.split(' '); - qDebug()<<__func__<<" :"<= 10 && buffer.contains("NUMBER:")) - { + { std::shared_ptr item; - if(listMode == TRAIN_LIST) + if(listMode == TRAIN_LIST) item = processTrainLine(buffer); - else if(listMode == TURNOUT_LIST) + else if(listMode == TURNOUT_LIST) item = processTurnoutLine(buffer); if(item) itemList.push_back(item); - } - else - { - listMode = false; - if(!itemList.empty()) - { - qDebug()<<"got item list " << itemList.size(); - gotItemList(itemList); - itemList.clear(); - } - } + } + else + { + listMode = false; + if(!itemList.empty()) + { + qDebug()<<"got item list " << itemList.size(); + gotItemList(itemList); + itemList.clear(); + } + } } void Microcontroller::processItemState(const QString& buffer) @@ -157,43 +159,43 @@ void Microcontroller::processItemState(const QString& buffer) void Microcontroller::processMicroReturn() { - if(listMode) - processList(_buffer); - else - { - if(_buffer.startsWith("Trains:")) - { - listMode = TRAIN_LIST; - itemList.clear(); - } - else if(_buffer.startsWith("Turnouts:")) - { - listMode = TURNOUT_LIST; - } - else if(_buffer.startsWith("TRAIN NUMBER:") || _buffer.startsWith("TURNOUT NUMBER:")) - { - processItemState(_buffer); + if(listMode) + processList(_buffer); + else + { + if(_buffer.startsWith("Trains:")) + { + listMode = TRAIN_LIST; + itemList.clear(); } - else if(_buffer.startsWith("TrainController")) - { - requestState(); - } - } + else if(_buffer.startsWith("Turnouts:")) + { + listMode = TURNOUT_LIST; + } + else if(_buffer.startsWith("TRAIN NUMBER:") || _buffer.startsWith("TURNOUT NUMBER:")) + { + processItemState(_buffer); + } + else if(_buffer.startsWith("TrainController")) + { + requestState(); + } + } } void Microcontroller::isReadyRead() { - char charBuf; - while(_port->getChar(&charBuf)) - { - _buffer.push_back(charBuf); - if( _buffer.endsWith('\n') ) - { - _buffer.remove('\n'); - processMicroReturn(); - textRecived(_buffer); - _buffer.clear(); - } - } + char charBuf; + while(_port->getChar(&charBuf)) + { + _buffer.push_back(charBuf); + if( _buffer.endsWith('\n') ) + { + _buffer.remove('\n'); + processMicroReturn(); + textRecived(_buffer); + _buffer.clear(); + } + } } diff --git a/src/microcontroller.h b/src/microcontroller.h index cddfe2e..c9ad9e2 100644 --- a/src/microcontroller.h +++ b/src/microcontroller.h @@ -20,64 +20,64 @@ class Microcontroller : public QObject { - Q_OBJECT + Q_OBJECT public: - static constexpr char AMP_RELAY = 0; - static constexpr char SENSOR_TEMPERATURE = 1; - static constexpr char SENSOR_DOOR = 0 ; + static constexpr char AMP_RELAY = 0; + static constexpr char SENSOR_TEMPERATURE = 1; + static constexpr char SENSOR_DOOR = 0 ; private: - static constexpr int TRAIN_LIST = 1; - static constexpr int TURNOUT_LIST = 2; + static constexpr int TRAIN_LIST = 1; + static constexpr int TURNOUT_LIST = 2; - int listMode = 0; + int listMode = 0; - //uint8_t _auxState = 0; + //uint8_t _auxState = 0; - QIODevice* _port = nullptr; + QIODevice* _port = nullptr; - std::vector< std::shared_ptr > itemList; + std::vector< std::shared_ptr > itemList; - QScopedPointer loop; - QString _buffer; + QScopedPointer loop; + QString _buffer; - void processMicroReturn(); - void processList(const QString& buffer); - void processItemState(const QString& buffer); - std::shared_ptr processTrainLine(const QString& buffer); - std::shared_ptr processTurnoutLine(const QString& buffer); + void processMicroReturn(); + void processList(const QString& buffer); + void processItemState(const QString& buffer); + std::shared_ptr processTrainLine(const QString& buffer); + std::shared_ptr processTurnoutLine(const QString& buffer); - void write(char *buffer, const size_t length); - void write(const QByteArray& buffer); + void write(char *buffer, const size_t length); + void write(const QByteArray& buffer); public: - Microcontroller(QIODevice* port); - Microcontroller(); - ~Microcontroller(); - bool connected(); - void setIODevice(QIODevice* port); + Microcontroller(QIODevice* port); + Microcontroller(); + ~Microcontroller(); + bool connected(); + void setIODevice(QIODevice* port); public slots: - void requestState(); + void requestState(); void trainSetSpeed(uint8_t id, int8_t speed); - void trainReverse(uint8_t id); - void trainSetFunction(uint8_t id, uint8_t function, bool on); - void tunoutSetDirection(uint8_t id, bool direction); - void estop(); - void setPower(bool on); + void trainReverse(uint8_t id); + void trainSetFunction(uint8_t id, uint8_t function, bool on); + void tunoutSetDirection(uint8_t id, bool direction); + void estop(); + void setPower(bool on); private slots: - void isReadyRead(); + void isReadyRead(); signals: - void textRecived(const QString string); - void itemChanged(ItemData relay); - void auxStateChanged(int value); - void gotItemList(std::vector< std::shared_ptr >&); + void textRecived(const QString string); + void itemChanged(ItemData relay); + void auxStateChanged(int value); + void gotItemList(std::vector< std::shared_ptr >&); }; #endif // MICROCONTROLLER_H diff --git a/src/trainjs.h b/src/trainjs.h index fb5f94f..f8a325b 100644 --- a/src/trainjs.h +++ b/src/trainjs.h @@ -41,7 +41,10 @@ public: std::weak_ptr getItem(); void setItem(std::weak_ptr); bool itemIsSet(); - bool getWantsNewItem() {return wantsNewItem;} + bool getWantsNewItem() + { + return wantsNewItem; + } }; #endif // TRAINJS_H diff --git a/src/ui/itemcreationdialog.cpp b/src/ui/itemcreationdialog.cpp index 55b26ca..de0d51d 100644 --- a/src/ui/itemcreationdialog.cpp +++ b/src/ui/itemcreationdialog.cpp @@ -2,29 +2,29 @@ #include "ui_itemcreationdialog.h" ItemCreationDialog::ItemCreationDialog(QWidget *parent) : - QDialog(parent), - ui(new Ui::ItemCreationDialog) + QDialog(parent), + ui(new Ui::ItemCreationDialog) { - ui->setupUi(this); - connect(ui->comboBox, &QComboBox::currentTextChanged, this, &ItemCreationDialog::itemTypeChanged); - connect(ui->lineEdit, &QLineEdit::textChanged, this, &ItemCreationDialog::itemNameChanged); + ui->setupUi(this); + connect(ui->comboBox, &QComboBox::currentTextChanged, this, &ItemCreationDialog::itemTypeChanged); + connect(ui->lineEdit, &QLineEdit::textChanged, this, &ItemCreationDialog::itemNameChanged); } ItemCreationDialog::~ItemCreationDialog() { - delete ui; + delete ui; } void ItemCreationDialog::itemTypeChanged(const QString& type) { - ui->verticalLayout->removeWidget(widget); + ui->verticalLayout->removeWidget(widget); } void ItemCreationDialog::itemNameChanged(const QString& name) { - if(item) - { - item->setName(name); - } + if(item) + { + item->setName(name); + } } diff --git a/src/ui/itemcreationdialog.h b/src/ui/itemcreationdialog.h index 57421ae..6e993d3 100644 --- a/src/ui/itemcreationdialog.h +++ b/src/ui/itemcreationdialog.h @@ -5,29 +5,30 @@ #include #include "../items/item.h" -namespace Ui { +namespace Ui +{ class ItemCreationDialog; } class ItemCreationDialog : public QDialog { - Q_OBJECT + Q_OBJECT - QWidget* widget; + QWidget* widget; public: - explicit ItemCreationDialog(QWidget *parent = nullptr); - ~ItemCreationDialog(); + explicit ItemCreationDialog(QWidget *parent = nullptr); + ~ItemCreationDialog(); - std::shared_ptr item; + std::shared_ptr item; private slots: - void itemTypeChanged(const QString& type); - void itemNameChanged(const QString& name); + void itemTypeChanged(const QString& type); + void itemNameChanged(const QString& name); private: - Ui::ItemCreationDialog *ui; + Ui::ItemCreationDialog *ui; }; #endif // ITEMCREATIONDIALOG_H diff --git a/src/ui/itemscrollbox.cpp b/src/ui/itemscrollbox.cpp index a83b7e0..3d92249 100644 --- a/src/ui/itemscrollbox.cpp +++ b/src/ui/itemscrollbox.cpp @@ -6,79 +6,79 @@ #include "../trainjs.h" ItemScrollBox::ItemScrollBox(QWidget *parent) : - QWidget(parent), - ui(new Ui::RelayScrollBox) + QWidget(parent), + ui(new Ui::RelayScrollBox) { - ui->setupUi(this); - QScroller::grabGesture(ui->scrollArea, QScroller::TouchGesture); - QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture); + ui->setupUi(this); + QScroller::grabGesture(ui->scrollArea, QScroller::TouchGesture); + QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture); } ItemScrollBox::~ItemScrollBox() { - delete ui; + delete ui; } void ItemScrollBox::addItem(std::weak_ptr item) { - if(auto workItem = item.lock()) - { - widgets_.push_back(new ItemWidget(item, this)); - Train* train = dynamic_cast(workItem.get()); - Turnout* turnout = dynamic_cast(workItem.get()); - if(train) - { - if(train->getTrainId() == 0) - widgets_.back()->setShortcuts(QKeySequence(Qt::Key_Q), QKeySequence(Qt::Key_A), QKeySequence(Qt::Key_Z)); - else if(train->getTrainId() == 1) - widgets_.back()->setShortcuts(QKeySequence(Qt::Key_W), QKeySequence(Qt::Key_S), QKeySequence(Qt::Key_X)); - else if(train->getTrainId() == 2) - widgets_.back()->setShortcuts(QKeySequence(Qt::Key_E), QKeySequence(Qt::Key_D), QKeySequence(Qt::Key_C)); - else if(train->getTrainId() == 3) - widgets_.back()->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V)); - else if(train->getTrainId() == 4) - widgets_.back()->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B)); - } - else if(turnout) - { - if(turnout->getTurnoutId() == 0) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_1)); - else if(turnout->getTurnoutId() == 1) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_2)); - else if(turnout->getTurnoutId() == 2) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_3)); - else if(turnout->getTurnoutId() == 3) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_4)); - else if(turnout->getTurnoutId() == 4) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_5)); - else if(turnout->getTurnoutId() == 5) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_6)); - else if(turnout->getTurnoutId() == 6) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_7)); - else if(turnout->getTurnoutId() == 7) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_8)); - else if(turnout->getTurnoutId() == 8) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_9)); - else if(turnout->getTurnoutId() == 9) - widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_0)); - } - ui->relayWidgetVbox->addWidget(widgets_.back()); - connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); - connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); - } + if(auto workItem = item.lock()) + { + widgets_.push_back(new ItemWidget(item, this)); + Train* train = dynamic_cast(workItem.get()); + Turnout* turnout = dynamic_cast(workItem.get()); + if(train) + { + if(train->getTrainId() == 0) + widgets_.back()->setShortcuts(QKeySequence(Qt::Key_Q), QKeySequence(Qt::Key_A), QKeySequence(Qt::Key_Z)); + else if(train->getTrainId() == 1) + widgets_.back()->setShortcuts(QKeySequence(Qt::Key_W), QKeySequence(Qt::Key_S), QKeySequence(Qt::Key_X)); + else if(train->getTrainId() == 2) + widgets_.back()->setShortcuts(QKeySequence(Qt::Key_E), QKeySequence(Qt::Key_D), QKeySequence(Qt::Key_C)); + else if(train->getTrainId() == 3) + widgets_.back()->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V)); + else if(train->getTrainId() == 4) + widgets_.back()->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B)); + } + else if(turnout) + { + if(turnout->getTurnoutId() == 0) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_1)); + else if(turnout->getTurnoutId() == 1) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_2)); + else if(turnout->getTurnoutId() == 2) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_3)); + else if(turnout->getTurnoutId() == 3) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_4)); + else if(turnout->getTurnoutId() == 4) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_5)); + else if(turnout->getTurnoutId() == 5) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_6)); + else if(turnout->getTurnoutId() == 6) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_7)); + else if(turnout->getTurnoutId() == 7) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_8)); + else if(turnout->getTurnoutId() == 8) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_9)); + else if(turnout->getTurnoutId() == 9) + widgets_.back()->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_0)); + } + ui->relayWidgetVbox->addWidget(widgets_.back()); + connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest); + connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem); + } } void ItemScrollBox::removeItem(const ItemData& item) { - for(unsigned i = 0; i < widgets_.size(); i++) - { - if(widgets_[i]->controles(item)) - { - ui->relayWidgetVbox->removeWidget(widgets_[i]); - delete widgets_[i]; - widgets_.erase(widgets_.begin()+i); - } - } + for(unsigned i = 0; i < widgets_.size(); i++) + { + if(widgets_[i]->controles(item)) + { + ui->relayWidgetVbox->removeWidget(widgets_[i]); + delete widgets_[i]; + widgets_.erase(widgets_.begin()+i); + } + } } diff --git a/src/ui/itemscrollbox.h b/src/ui/itemscrollbox.h index 2d78f2a..228a315 100644 --- a/src/ui/itemscrollbox.h +++ b/src/ui/itemscrollbox.h @@ -10,32 +10,33 @@ #include "../items/itemstore.h" -namespace Ui { +namespace Ui +{ class RelayScrollBox; } class ItemScrollBox : public QWidget { - Q_OBJECT + Q_OBJECT private: - std::vector< ItemWidget* > widgets_; + std::vector< ItemWidget* > widgets_; signals: - void deleteRequest(const ItemData& item); + void deleteRequest(const ItemData& item); public: - explicit ItemScrollBox(QWidget *parent = nullptr); - ~ItemScrollBox(); + explicit ItemScrollBox(QWidget *parent = nullptr); + ~ItemScrollBox(); - void setItemStore(ItemStore* itemStore); + void setItemStore(ItemStore* itemStore); public slots: - void addItem(std::weak_ptr item); - void removeItem(const ItemData& item); + void addItem(std::weak_ptr item); + void removeItem(const ItemData& item); private: - Ui::RelayScrollBox *ui; + Ui::RelayScrollBox *ui; }; #endif // RELAYSCROLLBOX_H diff --git a/src/ui/itemsettingsdialog.cpp b/src/ui/itemsettingsdialog.cpp index 179ce19..9f2086d 100644 --- a/src/ui/itemsettingsdialog.cpp +++ b/src/ui/itemsettingsdialog.cpp @@ -1,176 +1,176 @@ - #include "itemsettingsdialog.h" +#include "itemsettingsdialog.h" #include "ui_itemsettingsdialog.h" #include ItemSettingsDialog::ItemSettingsDialog(std::shared_ptr item, QWidget *parent) : - QDialog(parent), - item_(item), - ui(new Ui::ItemSettingsDialog) + QDialog(parent), + item_(item), + ui(new Ui::ItemSettingsDialog) { - ui->setupUi(this); + ui->setupUi(this); - setModal(false); + setModal(false); - ui->label_name->setText(item_->getName()); - ui->checkBox_Override->setChecked(item_->getOverride()); + ui->label_name->setText(item_->getName()); + ui->checkBox_Override->setChecked(item_->getOverride()); - if(std::shared_ptr relay = std::dynamic_pointer_cast(item_)) - { - itemSpecificWidget_ = new RelayItemSettingsWidget(relay); - } - else if(std::shared_ptr msgItem = std::dynamic_pointer_cast(item_)) - { - itemSpecificWidget_ = new MessageItemSettingsWidget(msgItem); - } - else if(std::shared_ptr sysItem = std::dynamic_pointer_cast(item_)) - { - itemSpecificWidget_ = new SystemItemSettingsWidget(sysItem); - } + if(std::shared_ptr relay = std::dynamic_pointer_cast(item_)) + { + itemSpecificWidget_ = new RelayItemSettingsWidget(relay); + } + else if(std::shared_ptr msgItem = std::dynamic_pointer_cast(item_)) + { + itemSpecificWidget_ = new MessageItemSettingsWidget(msgItem); + } + else if(std::shared_ptr sysItem = std::dynamic_pointer_cast(item_)) + { + itemSpecificWidget_ = new SystemItemSettingsWidget(sysItem); + } - if(itemSpecificWidget_) - { - ui->verticalLayout_2->addWidget(itemSpecificWidget_); - } + if(itemSpecificWidget_) + { + ui->verticalLayout_2->addWidget(itemSpecificWidget_); + } - connect(ui->pushButton_add, &QPushButton::clicked, this, &ItemSettingsDialog::addActor); - connect(ui->pushButton_remove, &QPushButton::clicked, this, &ItemSettingsDialog::removeActor); - connect(ui->pushButton_edit, &QPushButton::clicked, this, &ItemSettingsDialog::editActor); - connect(ui->checkBox_Override, &QPushButton::clicked, this, &ItemSettingsDialog::changeOverride); + connect(ui->pushButton_add, &QPushButton::clicked, this, &ItemSettingsDialog::addActor); + connect(ui->pushButton_remove, &QPushButton::clicked, this, &ItemSettingsDialog::removeActor); + connect(ui->pushButton_edit, &QPushButton::clicked, this, &ItemSettingsDialog::editActor); + connect(ui->checkBox_Override, &QPushButton::clicked, this, &ItemSettingsDialog::changeOverride); - ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Actor")); - ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Action")); - ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Enabled")); - ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); - ui->tableWidget->horizontalHeader()->resizeSection(1, 60); - ui->tableWidget->horizontalHeader()->resizeSection(2, 75); - loadActorList(); + ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Actor")); + ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("Action")); + ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("Enabled")); + ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + ui->tableWidget->horizontalHeader()->resizeSection(1, 60); + ui->tableWidget->horizontalHeader()->resizeSection(2, 75); + loadActorList(); } ItemSettingsDialog::~ItemSettingsDialog() { - if(itemSpecificWidget_) delete itemSpecificWidget_; - delete ui; + if(itemSpecificWidget_) delete itemSpecificWidget_; + delete ui; } void ItemSettingsDialog::changeOverride() { - item_->setOverride(ui->checkBox_Override->isChecked()); + item_->setOverride(ui->checkBox_Override->isChecked()); } void ItemSettingsDialog::loadActorList() { - //ui->listWidget->clear(); - ui->tableWidget->setRowCount(item_->getActors().size()); + //ui->listWidget->clear(); + ui->tableWidget->setRowCount(item_->getActors().size()); - for(unsigned i = 0; i < item_->getActors().size(); i++) - { - ui->tableWidget->setItem(i, 0, new QTableWidgetItem(item_->getActors()[i]->getName())); - ui->tableWidget->setItem(i, 1, new QTableWidgetItem(item_->getActors()[i]->actionName())); - ui->tableWidget->setItem(i, 2, new QTableWidgetItem(item_->getActors()[i]->isActive() ? "Y" : "N")); - } + for(unsigned i = 0; i < item_->getActors().size(); i++) + { + ui->tableWidget->setItem(i, 0, new QTableWidgetItem(item_->getActors()[i]->getName())); + ui->tableWidget->setItem(i, 1, new QTableWidgetItem(item_->getActors()[i]->actionName())); + ui->tableWidget->setItem(i, 2, new QTableWidgetItem(item_->getActors()[i]->isActive() ? "Y" : "N")); + } } void ItemSettingsDialog::addActor() { - ActorSettingsDialog* dialog = nullptr; - std::shared_ptr actor = nullptr; + ActorSettingsDialog* dialog = nullptr; + std::shared_ptr actor = nullptr; - if(ui->comboBox->currentText() == "Alarm") - { - std::shared_ptr alarm = std::shared_ptr(new AlarmTime); - actor = alarm; - dialog = new ActorSettingsDialog(alarm, this); - } - else if(ui->comboBox->currentText() == "Sensor") - { - std::shared_ptr sensorActor = std::shared_ptr(new SensorActor); - actor = sensorActor; - dialog = new ActorSettingsDialog(sensorActor, this); - } - else if(ui->comboBox->currentText() == "Timer" ) - { - std::shared_ptr timerActor = std::shared_ptr(new TimerActor); - actor = timerActor; - dialog = new ActorSettingsDialog(timerActor, this); - } - else if(ui->comboBox->currentText() == "Regulator") - { - std::shared_ptr regulator = std::shared_ptr(new Regulator); - actor = regulator; - dialog = new ActorSettingsDialog(regulator, this); - } + if(ui->comboBox->currentText() == "Alarm") + { + std::shared_ptr alarm = std::shared_ptr(new AlarmTime); + actor = alarm; + dialog = new ActorSettingsDialog(alarm, this); + } + else if(ui->comboBox->currentText() == "Sensor") + { + std::shared_ptr sensorActor = std::shared_ptr(new SensorActor); + actor = sensorActor; + dialog = new ActorSettingsDialog(sensorActor, this); + } + else if(ui->comboBox->currentText() == "Timer" ) + { + std::shared_ptr timerActor = std::shared_ptr(new TimerActor); + actor = timerActor; + dialog = new ActorSettingsDialog(timerActor, this); + } + else if(ui->comboBox->currentText() == "Regulator") + { + std::shared_ptr regulator = std::shared_ptr(new Regulator); + actor = regulator; + dialog = new ActorSettingsDialog(regulator, this); + } - else if(ui->comboBox->currentText() == "Polynomal") - { - std::shared_ptr polynomalActor = std::shared_ptr(new PolynomalActor); - actor = polynomalActor; - dialog = new ActorSettingsDialog(polynomalActor, this); - } + else if(ui->comboBox->currentText() == "Polynomal") + { + std::shared_ptr polynomalActor = std::shared_ptr(new PolynomalActor); + actor = polynomalActor; + dialog = new ActorSettingsDialog(polynomalActor, this); + } - else if(ui->comboBox->currentText() == "Multi Factor") - { - std::shared_ptr polynomalActor = std::shared_ptr(new MultiFactorActor); - actor = polynomalActor; - dialog = new ActorSettingsDialog(polynomalActor, this); - } + else if(ui->comboBox->currentText() == "Multi Factor") + { + std::shared_ptr polynomalActor = std::shared_ptr(new MultiFactorActor); + actor = polynomalActor; + dialog = new ActorSettingsDialog(polynomalActor, this); + } - if(dialog != nullptr) - { - dialog->setParent(this); - dialog->show(); - if(dialog->exec() == QDialog::Accepted) - { - item_->addActor(actor); - loadActorList(); - } - delete dialog; - } + if(dialog != nullptr) + { + dialog->setParent(this); + dialog->show(); + if(dialog->exec() == QDialog::Accepted) + { + item_->addActor(actor); + loadActorList(); + } + delete dialog; + } } void ItemSettingsDialog::removeActor() { - if(item_->getActors().size() > ui->tableWidget->currentRow()) - { - item_->removeActor(item_->getActors().at(ui->tableWidget->currentRow())); - loadActorList(); - } + if(item_->getActors().size() > ui->tableWidget->currentRow()) + { + item_->removeActor(item_->getActors().at(ui->tableWidget->currentRow())); + loadActorList(); + } } void ItemSettingsDialog::editActor() { - if(item_->getActors().size() > ui->tableWidget->currentRow()) - { - std::shared_ptr actor = item_->getActors()[ui->tableWidget->currentRow()]; + if(item_->getActors().size() > ui->tableWidget->currentRow()) + { + std::shared_ptr actor = item_->getActors()[ui->tableWidget->currentRow()]; - std::shared_ptr alarmTime = std::dynamic_pointer_cast(actor); - std::shared_ptr regulator = std::dynamic_pointer_cast(actor); - std::shared_ptr sensorActor = std::dynamic_pointer_cast(actor); - std::shared_ptr timerActor = std::dynamic_pointer_cast(actor); - std::shared_ptr polynomalActor = std::dynamic_pointer_cast(actor); - std::shared_ptr factorActor = std::dynamic_pointer_cast(actor); + std::shared_ptr alarmTime = std::dynamic_pointer_cast(actor); + std::shared_ptr regulator = std::dynamic_pointer_cast(actor); + std::shared_ptr sensorActor = std::dynamic_pointer_cast(actor); + std::shared_ptr timerActor = std::dynamic_pointer_cast(actor); + std::shared_ptr polynomalActor = std::dynamic_pointer_cast(actor); + std::shared_ptr factorActor = std::dynamic_pointer_cast(actor); - ActorSettingsDialog* dialog; + ActorSettingsDialog* dialog; - if(alarmTime) dialog = new ActorSettingsDialog(alarmTime, this); - else if(regulator) dialog = new ActorSettingsDialog(regulator, this); - else if(sensorActor) dialog = new ActorSettingsDialog(sensorActor, this); - else if(timerActor) dialog = new ActorSettingsDialog(timerActor, this); - else if(polynomalActor) dialog = new ActorSettingsDialog(polynomalActor, this); - else if(factorActor) dialog = new ActorSettingsDialog(factorActor, this); - else dialog = new ActorSettingsDialog(actor, this); - dialog->setParent(this); - dialog->show(); - dialog->exec(); + if(alarmTime) dialog = new ActorSettingsDialog(alarmTime, this); + else if(regulator) dialog = new ActorSettingsDialog(regulator, this); + else if(sensorActor) dialog = new ActorSettingsDialog(sensorActor, this); + else if(timerActor) dialog = new ActorSettingsDialog(timerActor, this); + else if(polynomalActor) dialog = new ActorSettingsDialog(polynomalActor, this); + else if(factorActor) dialog = new ActorSettingsDialog(factorActor, this); + else dialog = new ActorSettingsDialog(actor, this); + dialog->setParent(this); + dialog->show(); + dialog->exec(); - for(int i = 0; i < ui->tableWidget->rowCount() && i < item_->getActors().size(); ++i) - { - ui->tableWidget->item(i, 0)->setText(item_->getActors()[i]->getName()); - ui->tableWidget->item(i, 1)->setText(item_->getActors()[i]->actionName()); - ui->tableWidget->item(i, 2)->setText(item_->getActors()[i]->isActive() ? "Y" : "N"); - } - } + for(int i = 0; i < ui->tableWidget->rowCount() && i < item_->getActors().size(); ++i) + { + ui->tableWidget->item(i, 0)->setText(item_->getActors()[i]->getName()); + ui->tableWidget->item(i, 1)->setText(item_->getActors()[i]->actionName()); + ui->tableWidget->item(i, 2)->setText(item_->getActors()[i]->isActive() ? "Y" : "N"); + } + } } diff --git a/src/ui/itemsettingsdialog.h b/src/ui/itemsettingsdialog.h index ca12f21..5ec31a2 100644 --- a/src/ui/itemsettingsdialog.h +++ b/src/ui/itemsettingsdialog.h @@ -6,32 +6,33 @@ #include #include "../items/item.h" -namespace Ui { +namespace Ui +{ class ItemSettingsDialog; } class ItemSettingsDialog : public QDialog { - Q_OBJECT - std::shared_ptr item_; - QWidget* itemSpecificWidget_ = nullptr; + Q_OBJECT + std::shared_ptr item_; + QWidget* itemSpecificWidget_ = nullptr; private: - void loadActorList(); + void loadActorList(); public: - explicit ItemSettingsDialog(std::shared_ptr item, QWidget *parent = nullptr); - ~ItemSettingsDialog(); + explicit ItemSettingsDialog(std::shared_ptr item, QWidget *parent = nullptr); + ~ItemSettingsDialog(); private slots: - void removeActor(); - void addActor(); - void editActor(); - void changeOverride(); + void removeActor(); + void addActor(); + void editActor(); + void changeOverride(); private: - Ui::ItemSettingsDialog *ui; + Ui::ItemSettingsDialog *ui; }; #endif // RELAYSETTINGSDIALOG_H diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index af6426e..f56cdc7 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -8,68 +8,74 @@ #include "../items/turnout.h" ItemWidget::ItemWidget(std::weak_ptr item, QWidget *parent) : - QWidget(parent), - item_(item), - ui(new Ui::ItemWidget) + QWidget(parent), + item_(item), + ui(new Ui::ItemWidget) { - ui->setupUi(this); + ui->setupUi(this); if(auto workingItem = item_.lock()) - { + { ui->label->setText(workingItem->getName()); connect(ui->slider, &QSlider::valueChanged, this, &ItemWidget::setValue); - connect(ui->checkBox_f1, &QCheckBox::stateChanged, this, &ItemWidget::f1); - connect(ui->checkBox_f2, &QCheckBox::stateChanged, this, &ItemWidget::f2); - connect(ui->checkBox_f3, &QCheckBox::stateChanged, this, &ItemWidget::f3); - connect(ui->checkBox_f4, &QCheckBox::stateChanged, this, &ItemWidget::f4); - connect(ui->pushButton_reverse, &QPushButton::clicked, this, &ItemWidget::reverse); - connect(ui->radioButton_left, &QRadioButton::clicked, this, [this](){setValue(0);}); - connect(ui->radioButton_right, &QRadioButton::clicked, this, [this](){setValue(1);}); + connect(ui->checkBox_f1, &QCheckBox::stateChanged, this, &ItemWidget::f1); + connect(ui->checkBox_f2, &QCheckBox::stateChanged, this, &ItemWidget::f2); + connect(ui->checkBox_f3, &QCheckBox::stateChanged, this, &ItemWidget::f3); + connect(ui->checkBox_f4, &QCheckBox::stateChanged, this, &ItemWidget::f4); + connect(ui->pushButton_reverse, &QPushButton::clicked, this, &ItemWidget::reverse); + connect(ui->radioButton_left, &QRadioButton::clicked, this, [this]() + { + setValue(0); + }); + connect(ui->radioButton_right, &QRadioButton::clicked, this, [this]() + { + setValue(1); + }); connect(workingItem.get(), &Item::valueChanged, this, &ItemWidget::moveToValue); Train* train = dynamic_cast(workingItem.get()); Turnout* turnout = dynamic_cast(workingItem.get()); - if(turnout) - { - ui->checkBox_f1->hide(); - ui->checkBox_f2->hide(); - ui->checkBox_f3->hide(); - ui->checkBox_f4->hide(); - ui->slider->hide(); - } + if(turnout) + { + ui->checkBox_f1->hide(); + ui->checkBox_f2->hide(); + ui->checkBox_f3->hide(); + ui->checkBox_f4->hide(); + ui->slider->hide(); + } - if(!train) - { - ui->pushButton_reverse->hide(); - } - else - { - ui->radioButton_left->hide(); - ui->radioButton_right->hide(); - uint8_t functionMask = train->getFunctionMask(); + if(!train) + { + ui->pushButton_reverse->hide(); + } + else + { + ui->radioButton_left->hide(); + ui->radioButton_right->hide(); + uint8_t functionMask = train->getFunctionMask(); qDebug()<<"functionMask: "<<(int)functionMask; - if(!(functionMask & (1 << 0))) - ui->checkBox_f1->hide(); - if(!(functionMask & (1 << 1))) - ui->checkBox_f2->hide(); - if(!(functionMask & (1 << 2))) - ui->checkBox_f3->hide(); - if(!(functionMask & (1 << 3))) - ui->checkBox_f4->hide(); - } - } - else disable(); + if(!(functionMask & (1 << 0))) + ui->checkBox_f1->hide(); + if(!(functionMask & (1 << 1))) + ui->checkBox_f2->hide(); + if(!(functionMask & (1 << 2))) + ui->checkBox_f3->hide(); + if(!(functionMask & (1 << 3))) + ui->checkBox_f4->hide(); + } + } + else disable(); } void ItemWidget::deleteItem() { - if(auto workingItem = item_.lock()) - { - deleteRequest(*workingItem); - } + if(auto workingItem = item_.lock()) + { + deleteRequest(*workingItem); + } } void ItemWidget::setValue(int8_t value) @@ -82,85 +88,85 @@ void ItemWidget::setValue(int8_t value) void ItemWidget::moveToValue(int8_t value) { - ui->slider->blockSignals(true); - ui->radioButton_left->blockSignals(true); - ui->radioButton_right->blockSignals(true); + ui->slider->blockSignals(true); + ui->radioButton_left->blockSignals(true); + ui->radioButton_right->blockSignals(true); ui->pushButton_reverse->setText(value == 0 ? "Reverse" : "Stop"); - ui->slider->setValue(value); - ui->radioButton_left->setChecked(!value); - ui->radioButton_right->setChecked(value); + ui->slider->setValue(value); + ui->radioButton_left->setChecked(!value); + ui->radioButton_right->setChecked(value); - ui->slider->blockSignals(false); - ui->radioButton_left->blockSignals(false); - ui->radioButton_right->blockSignals(false); + ui->slider->blockSignals(false); + ui->radioButton_left->blockSignals(false); + ui->radioButton_right->blockSignals(false); } void ItemWidget::f1(int value) { - if(auto workingItem = item_.lock()) - workingItem->setFunction(0, value == Qt::Checked); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setFunction(0, value == Qt::Checked); + else disable(); } void ItemWidget::f2(int value) { - if(auto workingItem = item_.lock()) - workingItem->setFunction(1, value == Qt::Checked); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setFunction(1, value == Qt::Checked); + else disable(); } void ItemWidget::f3(int value) { - if(auto workingItem = item_.lock()) - workingItem->setFunction(2, value == Qt::Checked); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setFunction(2, value == Qt::Checked); + else disable(); } void ItemWidget::f4(int value) { - if(auto workingItem = item_.lock()) - workingItem->setFunction(3, value == Qt::Checked); - else disable(); + if(auto workingItem = item_.lock()) + workingItem->setFunction(3, value == Qt::Checked); + else disable(); } void ItemWidget::reverse() { - if(auto workingItem = item_.lock()) - { - Train* train = dynamic_cast(workingItem.get()); - if(train && workingItem->getValue() == 0) - train->reverse(); - else + if(auto workingItem = item_.lock()) + { + Train* train = dynamic_cast(workingItem.get()); + if(train && workingItem->getValue() == 0) + train->reverse(); + else { setValue(!((bool)workingItem->getValue())); - } - } - else disable(); + } + } + else disable(); } void ItemWidget::disable() { - ui->checkBox_f1->setEnabled(false); - ui->checkBox_f2->setEnabled(false); - ui->checkBox_f3->setEnabled(false); - ui->checkBox_f4->setEnabled(false); - ui->label->setEnabled(false); - ui->slider->setEnabled(false); - ui->pushButton_reverse->setEnabled(false); + ui->checkBox_f1->setEnabled(false); + ui->checkBox_f2->setEnabled(false); + ui->checkBox_f3->setEnabled(false); + ui->checkBox_f4->setEnabled(false); + ui->label->setEnabled(false); + ui->slider->setEnabled(false); + ui->pushButton_reverse->setEnabled(false); } bool ItemWidget::controles(const ItemData& relay) { - if(auto workingRelay = item_.lock()) - { - if(relay == *workingRelay) - return true; - else - return false; - } - return true; + if(auto workingRelay = item_.lock()) + { + if(relay == *workingRelay) + return true; + else + return false; + } + return true; } void ItemWidget::stepUp() @@ -175,29 +181,29 @@ void ItemWidget::stepDown() void ItemWidget::setShortcuts(QKeySequence up, QKeySequence down, QKeySequence rev) { - shortcuts_.clear(); - shortcuts_.push_back(std::unique_ptr(new QShortcut(up, this))); - connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::stepUp); - shortcuts_.push_back(std::unique_ptr(new QShortcut(down, this))); - connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::stepDown); - shortcuts_.push_back(std::unique_ptr(new QShortcut(rev, this))); - connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::reverse); + shortcuts_.clear(); + shortcuts_.push_back(std::unique_ptr(new QShortcut(up, this))); + connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::stepUp); + shortcuts_.push_back(std::unique_ptr(new QShortcut(down, this))); + connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::stepDown); + shortcuts_.push_back(std::unique_ptr(new QShortcut(rev, this))); + connect(shortcuts_.back().get(), &QShortcut::activated, this, &ItemWidget::reverse); } std::weak_ptr ItemWidget::getItem() { - return item_; + return item_; } void ItemWidget::stateChanged(int state) { - qDebug()<<"widget got state "<slider->blockSignals(true); - ui->slider->setValue(state); - ui->slider->blockSignals(false); + qDebug()<<"widget got state "<slider->blockSignals(true); + ui->slider->setValue(state); + ui->slider->blockSignals(false); } ItemWidget::~ItemWidget() { - delete ui; + delete ui; } diff --git a/src/ui/itemwidget.h b/src/ui/itemwidget.h index c0eb45b..465fed6 100644 --- a/src/ui/itemwidget.h +++ b/src/ui/itemwidget.h @@ -7,51 +7,52 @@ #include "itemsettingsdialog.h" #include "../items/item.h" -namespace Ui { +namespace Ui +{ class ItemWidget; } class ItemWidget : public QWidget { - Q_OBJECT + Q_OBJECT private: - std::weak_ptr item_; + std::weak_ptr item_; - std::vector< std::unique_ptr > shortcuts_; + std::vector< std::unique_ptr > shortcuts_; - void disable(); + void disable(); signals: - void deleteRequest(const ItemData& item); + void deleteRequest(const ItemData& item); private slots: void setValue(int8_t value); void moveToValue(int8_t value); - void deleteItem(); + void deleteItem(); - void stepUp(); - void stepDown(); + void stepUp(); + void stepDown(); - void f1(int state); - void f2(int state); - void f3(int state); - void f4(int state); - void reverse(); + void f1(int state); + void f2(int state); + void f3(int state); + void f4(int state); + void reverse(); public: - explicit ItemWidget(std::weak_ptr item, QWidget *parent); - std::weak_ptr getItem(); - bool controles(const ItemData& relay); - ~ItemWidget(); - void setShortcuts(QKeySequence up, QKeySequence down, QKeySequence rev); + explicit ItemWidget(std::weak_ptr item, QWidget *parent); + std::weak_ptr getItem(); + bool controles(const ItemData& relay); + ~ItemWidget(); + void setShortcuts(QKeySequence up, QKeySequence down, QKeySequence rev); public slots: - void stateChanged(int state); + void stateChanged(int state); private: - Ui::ItemWidget *ui; + Ui::ItemWidget *ui; }; #endif // RELAYWIDGET_H diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index 93187a4..7f7d361 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -5,31 +5,31 @@ #include "itemcreationdialog.h" MainWindow::MainWindow(Microcontroller * const micro, ItemStore* items, QWidget *parent) : - QMainWindow(parent), - stopShort(QKeySequence(Qt::Key_Space), this), - ui(new Ui::MainWindow), - _micro(micro) + QMainWindow(parent), + stopShort(QKeySequence(Qt::Key_Space), this), + ui(new Ui::MainWindow), + _micro(micro) { - ui->setupUi(this); + ui->setupUi(this); - connect(&stopShort, &QShortcut::activated, _micro, &Microcontroller::estop); - connect(ui->pushButton_stop, &QPushButton::clicked, _micro, &Microcontroller::estop); - connect(ui->pushButton_refesh, &QPushButton::clicked, _micro, &Microcontroller::requestState); + connect(&stopShort, &QShortcut::activated, _micro, &Microcontroller::estop); + connect(ui->pushButton_stop, &QPushButton::clicked, _micro, &Microcontroller::estop); + connect(ui->pushButton_refesh, &QPushButton::clicked, _micro, &Microcontroller::requestState); - connect(items, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem); - connect(items, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem); + connect(items, &ItemStore::itemAdded, ui->relayList, &ItemScrollBox::addItem); + connect(items, &ItemStore::itemDeleted, ui->relayList, &ItemScrollBox::removeItem); - for(size_t i = 0; i < items->getItems()->size(); ++i) - { - ui->relayList->addItem(items->getItems()->at(i)); - } + for(size_t i = 0; i < items->getItems()->size(); ++i) + { + ui->relayList->addItem(items->getItems()->at(i)); + } - connect(ui->relayList, &ItemScrollBox::deleteRequest, items, &ItemStore::removeItem); + connect(ui->relayList, &ItemScrollBox::deleteRequest, items, &ItemStore::removeItem); } MainWindow::~MainWindow() { - delete ui; + delete ui; } /* diff --git a/src/ui/mainwindow.h b/src/ui/mainwindow.h index 890b84b..17a9071 100644 --- a/src/ui/mainwindow.h +++ b/src/ui/mainwindow.h @@ -20,25 +20,25 @@ class MainWindow; class MainWindow : public QMainWindow { - Q_OBJECT - QShortcut stopShort; + Q_OBJECT + QShortcut stopShort; public: - explicit MainWindow(Microcontroller * const micro, ItemStore* items, QWidget *parent = nullptr); - ~MainWindow(); + explicit MainWindow(Microcontroller * const micro, ItemStore* items, QWidget *parent = nullptr); + ~MainWindow(); private: - Ui::MainWindow *ui; + Ui::MainWindow *ui; - Microcontroller *_micro; + Microcontroller *_micro; signals: - void createdItem(std::shared_ptr item); + void createdItem(std::shared_ptr item); private slots: - //void showItemCreationDialog(); + //void showItemCreationDialog(); };