diff --git a/src/items/itemstore.cpp b/src/items/itemstore.cpp index 4c156dc..db0d139 100644 --- a/src/items/itemstore.cpp +++ b/src/items/itemstore.cpp @@ -1,6 +1,8 @@ #include "itemstore.h" #include #include +#include "train.h" +#include "../trainjs.h" ItemStore::ItemStore(QObject *parent): QObject(parent) { @@ -19,11 +21,54 @@ void ItemStore::addItem(std::shared_ptr item) if(!mached) { items_.push_back(std::shared_ptr(item)); + + if(dynamic_cast(item.get())) + { + std::vector> joysticks = TrainJs::getJsDevices(); + for(auto joystick: joysticks) + { + if(!joystick->itemIsSet()) + { + joystick->setItem(item); + connect(joystick.get(), &TrainJs::reqNewItem, this, &ItemStore::jsReqNewItem); + break; + } + } + } + itemAdded(std::weak_ptr(items_.back())); } qDebug()<<"Got item: "<id()<<" matched: "<> joysticks = TrainJs::getJsDevices(); + for(auto joystick: joysticks) + { + if(joystick->getWantsNewItem()) + { + std::shared_ptr oldItem = joystick->getItem().lock(); + for(size_t i = 0; i < items_.size(); ++i) + { + if(!oldItem || *items_[i] == *oldItem) + { + if(i+1 < items_.size()) + joystick->setItem(items_[i+1]); + else + joystick->setItem(items_[0]); + break; + } + } + } + } +} + void ItemStore::addItems(const std::vector>& itemIn) { for(unsigned j = 0; j < itemIn.size(); j++) diff --git a/src/items/itemstore.h b/src/items/itemstore.h index fa0b004..6148027 100644 --- a/src/items/itemstore.h +++ b/src/items/itemstore.h @@ -20,6 +20,10 @@ public: void clear(); +private slots: + + void jsReqNewItem(); + signals: void itemDeleted(ItemData item); diff --git a/src/items/turnout.cpp b/src/items/turnout.cpp index 23afcea..a2d3133 100644 --- a/src/items/turnout.cpp +++ b/src/items/turnout.cpp @@ -14,5 +14,5 @@ void Turnout::setValue(int8_t value) { Item::setValue(value); if(micro) - micro->tunoutSetDirection(turnoutId_, value); + micro->tunoutSetDirection(turnoutId_, value > 0); } diff --git a/src/trainjs.cpp b/src/trainjs.cpp index 0937998..5a55aef 100644 --- a/src/trainjs.cpp +++ b/src/trainjs.cpp @@ -5,13 +5,9 @@ TrainJs::TrainJs(int id, int axis): id_(id), axis_(axis) { - longpressTimer_.setSingleShot(true); - longpressTimer_.setInterval(LONGPRESS_TIME); - QJoysticks* jsmanager = QJoysticks::getInstance(); connect(jsmanager, &QJoysticks::axisChanged, this, &TrainJs::axisChanged); connect(jsmanager, &QJoysticks::buttonChanged, this, &TrainJs::buttonChanged); - connect(&longpressTimer_, &QTimer::timeout, this, &TrainJs::longpressTimeout); } TrainJs::~TrainJs() @@ -53,6 +49,8 @@ std::weak_ptr TrainJs::getItem() void TrainJs::setItem(std::weak_ptr item) { item_ = item; + wantsNewItem = false; + inhibitUntillZero = true; } bool TrainJs::itemIsSet() @@ -67,11 +65,14 @@ void TrainJs::axisChanged(const int id, const int axis, const qreal value) if(std::shared_ptr workitem = item_.lock()) { int8_t newValue = value*14; - if(newValue != workitem->getValue()) + if(newValue == 0) + inhibitUntillZero = false; + if(newValue != workitem->getValue() && !inhibitUntillZero) workitem->setValue(newValue); } else { + wantsNewItem = true; reqNewItem(); } } @@ -85,32 +86,14 @@ void TrainJs::buttonChanged(const int id, const int button, const bool pressed) if(pressed) { handleRelease = true; - longpressTimer_.start(); } else if(handleRelease) { - longpressTimer_.stop(); - if(std::shared_ptr workitem = item_.lock()) - { - if(Train* train = dynamic_cast(workitem.get())) - { - if(train->getValue() == 0) - train->reverse(); - } - } - else - { - reqNewItem(); - } + wantsNewItem = true; + reqNewItem(); handleRelease = false; } } } -void TrainJs::longpressTimeout() -{ - reqNewItem(); - handleRelease = false; -} - diff --git a/src/trainjs.h b/src/trainjs.h index 5e3efbd..fb5f94f 100644 --- a/src/trainjs.h +++ b/src/trainjs.h @@ -13,15 +13,15 @@ class TrainJs: public QObject Q_OBJECT private: static constexpr char JOYSTICK_NAME[] = "UVOS UsbJoy"; - static constexpr int LONGPRESS_TIME = 500; inline static std::vector> js_; int id_ = -1; int axis_ = -1; - QTimer longpressTimer_; bool handleRelease = false; + bool wantsNewItem = true; + bool inhibitUntillZero = true; std::weak_ptr item_; TrainJs(int id, int axis); @@ -30,7 +30,6 @@ private: private slots: void axisChanged(const int id, const int axis, const qreal value); void buttonChanged(const int id, const int button, const bool pressed); - void longpressTimeout(); signals: void reqNewItem(); @@ -42,6 +41,7 @@ public: std::weak_ptr getItem(); void setItem(std::weak_ptr); bool itemIsSet(); + bool getWantsNewItem() {return wantsNewItem;} }; #endif // TRAINJS_H diff --git a/src/ui/itemscrollbox.cpp b/src/ui/itemscrollbox.cpp index ce94f93..a83b7e0 100644 --- a/src/ui/itemscrollbox.cpp +++ b/src/ui/itemscrollbox.cpp @@ -38,16 +38,6 @@ void ItemScrollBox::addItem(std::weak_ptr item) 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)); - - std::vector> joysticks = TrainJs::getJsDevices(); - for(auto joystick: joysticks) - { - if(!joystick->itemIsSet()) - { - joystick->setItem(item); - break; - } - } } else if(turnout) { diff --git a/src/ui/itemscrollbox.h b/src/ui/itemscrollbox.h index c95a7dc..2d78f2a 100644 --- a/src/ui/itemscrollbox.h +++ b/src/ui/itemscrollbox.h @@ -23,7 +23,6 @@ private: signals: void deleteRequest(const ItemData& item); - public: explicit ItemScrollBox(QWidget *parent = nullptr); ~ItemScrollBox(); diff --git a/src/ui/itemwidget.cpp b/src/ui/itemwidget.cpp index 6cf34ff..1a2747b 100644 --- a/src/ui/itemwidget.cpp +++ b/src/ui/itemwidget.cpp @@ -73,7 +73,6 @@ void ItemWidget::deleteItem() void ItemWidget::setValue(int8_t value) { - moveToValue(value); if(auto workingItem = item_.lock()) workingItem->setValue(value); else