141 lines
4.5 KiB
C++
141 lines
4.5 KiB
C++
#include "itemscrollbox.h"
|
|
#include "ui_relayscrollbox.h"
|
|
#include "ui_relayscrollbox.h"
|
|
#include "train.h"
|
|
#include "turnout.h"
|
|
#include "trainsignal.h"
|
|
#include "trainjs.h"
|
|
#include "trainwidget.h"
|
|
#include "signalwidget.h"
|
|
|
|
ItemScrollBox::ItemScrollBox(QWidget *parent) :
|
|
QWidget(parent),
|
|
ui(new Ui::RelayScrollBox)
|
|
{
|
|
ui->setupUi(this);
|
|
QScroller::grabGesture(ui->scrollArea, QScroller::TouchGesture);
|
|
QScroller::grabGesture(ui->scrollArea, QScroller::LeftMouseButtonGesture);
|
|
}
|
|
|
|
ItemScrollBox::~ItemScrollBox()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void ItemScrollBox::addItem(std::weak_ptr<Item> item)
|
|
{
|
|
if(auto workItem = item.lock())
|
|
{
|
|
Train* train = dynamic_cast<Train*>(workItem.get());
|
|
Turnout* turnout = dynamic_cast<Turnout*>(workItem.get());
|
|
Signal* signal = dynamic_cast<Signal*>(workItem.get());
|
|
if(train)
|
|
{
|
|
TrainWidget *widget = new TrainWidget(item, this);
|
|
widgets_.push_back(widget);
|
|
if(train->getTrainId() == 0)
|
|
widget->setShortcuts(QKeySequence(Qt::Key_Q), QKeySequence(Qt::Key_A), QKeySequence(Qt::Key_Z));
|
|
else if(train->getTrainId() == 1)
|
|
widget->setShortcuts(QKeySequence(Qt::Key_W), QKeySequence(Qt::Key_S), QKeySequence(Qt::Key_X));
|
|
else if(train->getTrainId() == 2)
|
|
widget->setShortcuts(QKeySequence(Qt::Key_E), QKeySequence(Qt::Key_D), QKeySequence(Qt::Key_C));
|
|
else if(train->getTrainId() == 3)
|
|
widget->setShortcuts(QKeySequence(Qt::Key_R), QKeySequence(Qt::Key_F), QKeySequence(Qt::Key_V));
|
|
else if(train->getTrainId() == 4)
|
|
widget->setShortcuts(QKeySequence(Qt::Key_T), QKeySequence(Qt::Key_G), QKeySequence(Qt::Key_B));
|
|
|
|
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
|
|
for(auto joystick: joysticks)
|
|
{
|
|
if(!joystick->itemIsSet())
|
|
{
|
|
joystick->setItem(item);
|
|
connect(joystick.get(), &TrainJs::reqNewItem, this, &ItemScrollBox::jsReqNewItem);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if(turnout)
|
|
{
|
|
TrainWidget *widget = new TrainWidget(item, this);
|
|
widgets_.push_back(widget);
|
|
if(turnout->getTurnoutId() == 0)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_1));
|
|
else if(turnout->getTurnoutId() == 1)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_2));
|
|
else if(turnout->getTurnoutId() == 2)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_3));
|
|
else if(turnout->getTurnoutId() == 3)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_4));
|
|
else if(turnout->getTurnoutId() == 4)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_5));
|
|
else if(turnout->getTurnoutId() == 5)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_6));
|
|
else if(turnout->getTurnoutId() == 6)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_7));
|
|
else if(turnout->getTurnoutId() == 7)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_8));
|
|
else if(turnout->getTurnoutId() == 8)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_9));
|
|
else if(turnout->getTurnoutId() == 9)
|
|
widget->setShortcuts(QKeySequence(), QKeySequence(), QKeySequence(Qt::Key_0));
|
|
}
|
|
else if(signal)
|
|
{
|
|
SignalWidget *widget = new SignalWidget(item, this);
|
|
widgets_.push_back(widget);
|
|
}
|
|
ui->relayWidgetVbox->addWidget(widgets_.back());
|
|
connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::deleteRequest);
|
|
connect(widgets_.back(), &ItemWidget::deleteRequest, this, &ItemScrollBox::removeItem);
|
|
}
|
|
}
|
|
|
|
void ItemScrollBox::jsReqNewItem()
|
|
{
|
|
if(widgets_.empty())
|
|
return;
|
|
|
|
std::vector<std::shared_ptr<TrainJs>> joysticks = TrainJs::getJsDevices();
|
|
for(auto joystick: joysticks)
|
|
{
|
|
if(joystick->getWantsNewItem())
|
|
{
|
|
std::shared_ptr<Item> oldItem = joystick->getItem().lock();
|
|
for(size_t i = 0; i < widgets_.size(); ++i)
|
|
{
|
|
std::shared_ptr<Item> item = widgets_[i]->getItem().lock();
|
|
if(item && (!oldItem || *item == *oldItem))
|
|
{
|
|
for(size_t j = 1; j < widgets_.size(); ++j)
|
|
{
|
|
ItemWidget* widget = widgets_[(i+j) % widgets_.size()];
|
|
std::shared_ptr<Item> item = widgets_[i]->getItem().lock();
|
|
if(item && dynamic_cast<Train*>(item.get()))
|
|
{
|
|
joystick->setItem(item);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
|