Implemented ClassList attribute
Dieser Commit ist enthalten in:
Ursprung
ab17ebe666
Commit
35f1cfc263
@ -25,6 +25,7 @@
|
||||
#include <QToolBar>
|
||||
#include <QTableView>
|
||||
#include <QtWaitingSpinner/waitingspinnerwidget.h>
|
||||
#include <traintastic/locale/locale.hpp>
|
||||
#include "tablewidget.hpp"
|
||||
#include "../network/connection.hpp"
|
||||
#include "../network/object.hpp"
|
||||
@ -116,83 +117,34 @@ ObjectListWidget::ObjectListWidget(const ObjectPtr& object, QWidget* parent) :
|
||||
m_buttonAdd->setPopupMode(QToolButton::InstantPopup);
|
||||
|
||||
QMenu* menu = new QMenu(m_buttonAdd);
|
||||
menu->addAction("li10x");
|
||||
menu->addAction("z21");
|
||||
menu->addAction("loconet_serial",
|
||||
[this, method]()
|
||||
{
|
||||
if(m_requestIdAdd != Connection::invalidRequestId)
|
||||
m_object->connection()->cancelRequest(m_requestIdAdd);
|
||||
|
||||
m_requestIdAdd = method->call("hardware.command_station.loconet_serial",
|
||||
[this](const ObjectPtr& object, Message::ErrorCode /*ec*/)
|
||||
{
|
||||
m_requestIdAdd = Connection::invalidRequestId;
|
||||
if(object)
|
||||
{
|
||||
MainWindow::instance->showObject(object);
|
||||
}
|
||||
// TODO: show error
|
||||
});
|
||||
});
|
||||
menu->addAction("locomotive",
|
||||
[this, method]()
|
||||
{
|
||||
if(m_requestIdAdd != Connection::invalidRequestId)
|
||||
m_object->connection()->cancelRequest(m_requestIdAdd);
|
||||
QStringList classList = method->getAttribute(AttributeName::ClassList, QVariant()).toStringList();
|
||||
for(const QString& classId : classList)
|
||||
{
|
||||
QAction* action = menu->addAction(Locale::tr("class_id:" + classId));
|
||||
action->setData(classId);
|
||||
connect(action, &QAction::triggered, this,
|
||||
[this, method, action]()
|
||||
{
|
||||
if(m_requestIdAdd != Connection::invalidRequestId)
|
||||
m_object->connection()->cancelRequest(m_requestIdAdd);
|
||||
|
||||
m_requestIdAdd = method->call("vehicle.rail.locomotive",
|
||||
[this](const ObjectPtr& object, Message::ErrorCode /*ec*/)
|
||||
{
|
||||
m_requestIdAdd = Connection::invalidRequestId;
|
||||
if(object)
|
||||
{
|
||||
MainWindow::instance->showObject(object);
|
||||
}
|
||||
// TODO: show error
|
||||
});
|
||||
});
|
||||
menu->addAction("locomotive",
|
||||
[this, method]()
|
||||
{
|
||||
if(m_requestIdAdd != Connection::invalidRequestId)
|
||||
m_object->connection()->cancelRequest(m_requestIdAdd);
|
||||
|
||||
m_requestIdAdd = method->call("vehicle.rail.freight_car",
|
||||
[this](const ObjectPtr& object, Message::ErrorCode /*ec*/)
|
||||
{
|
||||
m_requestIdAdd = Connection::invalidRequestId;
|
||||
if(object)
|
||||
{
|
||||
MainWindow::instance->showObject(object);
|
||||
}
|
||||
// TODO: show error
|
||||
});
|
||||
});
|
||||
|
||||
m_buttonAdd->setMenu(menu);
|
||||
|
||||
m_toolbar->addWidget(m_buttonAdd);
|
||||
|
||||
|
||||
/*
|
||||
m_requestIdAdd = method->call("hardware.command_station.li10x",
|
||||
[this](const ObjectPtr& object, Message::ErrorCode /*ec*//*)
|
||||
m_requestIdAdd = method->call(action->data().toString(),
|
||||
[this](const ObjectPtr& object, Message::ErrorCode /*ec*/)
|
||||
{
|
||||
m_requestIdAdd = Connection::invalidRequestId;
|
||||
|
||||
if(object)
|
||||
{
|
||||
MainWindow::instance->showObject(object);
|
||||
}
|
||||
|
||||
// TODO: show error
|
||||
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
*/
|
||||
m_buttonAdd->setMenu(menu);
|
||||
|
||||
m_toolbar->addWidget(m_buttonAdd);
|
||||
|
||||
m_buttonAdd->setEnabled(method->getAttributeBool(AttributeName::Enabled, true));
|
||||
connect(method, &Method::attributeChanged,
|
||||
|
||||
@ -32,6 +32,12 @@ struct Attributes
|
||||
item.addAttribute(AttributeName::Category, value);
|
||||
}
|
||||
|
||||
template<size_t N>
|
||||
static inline void addClassList(InterfaceItem& item, const std::array<std::string_view, N>& classList)
|
||||
{
|
||||
item.addAttribute(AttributeName::ClassList, classList);
|
||||
}
|
||||
|
||||
static inline void addEnabled(InterfaceItem& item, bool value)
|
||||
{
|
||||
item.addAttribute(AttributeName::Enabled, value);
|
||||
|
||||
@ -99,6 +99,10 @@ To to(const From& value)
|
||||
{
|
||||
return std::to_string(value);
|
||||
}
|
||||
else if constexpr(std::is_same_v<To, std::string> && std::is_same_v<From, std::string_view>)
|
||||
{
|
||||
return std::string(value);
|
||||
}
|
||||
else if constexpr(std::is_same_v<From, nlohmann::json>)
|
||||
{
|
||||
if constexpr(std::is_enum_v<To>)
|
||||
|
||||
@ -49,6 +49,7 @@ CommandStationList::CommandStationList(Object& _parent, const std::string& paren
|
||||
const bool editable = world && contains(world->state.value(), WorldState::Edit);
|
||||
|
||||
Attributes::addEnabled(add, editable);
|
||||
Attributes::addClassList(add, CommandStations::classList);
|
||||
m_interfaceItems.add(add);
|
||||
|
||||
Attributes::addEnabled(remove, editable);
|
||||
|
||||
@ -21,27 +21,6 @@
|
||||
*/
|
||||
|
||||
#include "commandstations.hpp"
|
||||
#include "loconetserial.hpp"
|
||||
#ifndef DISABLE_USB_XPRESSNET_INTERFACE
|
||||
#include "usbxpressnetinterface.hpp"
|
||||
#endif
|
||||
#include "xpressnetserial.hpp"
|
||||
#include "rocoz21.hpp"
|
||||
#include "virtualcommandstation.hpp"
|
||||
|
||||
const std::vector<std::string_view>& CommandStations::classList()
|
||||
{
|
||||
static std::vector<std::string_view> list({
|
||||
LocoNetSerial::classId,
|
||||
#ifndef DISABLE_USB_XPRESSNET_INTERFACE
|
||||
USBXpressNetInterface::classId,
|
||||
#endif
|
||||
XpressNetSerial::classId,
|
||||
RocoZ21::classId,
|
||||
VirtualCommandStation::classId,
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
std::shared_ptr<CommandStation> CommandStations::create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id)
|
||||
{
|
||||
|
||||
@ -24,12 +24,29 @@
|
||||
#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_COMMANDSTATIONS_HPP
|
||||
|
||||
#include "commandstation.hpp"
|
||||
#include "../../utils/makearray.hpp"
|
||||
|
||||
#include "loconetserial.hpp"
|
||||
#ifndef DISABLE_USB_XPRESSNET_INTERFACE
|
||||
#include "usbxpressnetinterface.hpp"
|
||||
#endif
|
||||
#include "xpressnetserial.hpp"
|
||||
#include "rocoz21.hpp"
|
||||
#include "virtualcommandstation.hpp"
|
||||
|
||||
struct CommandStations
|
||||
{
|
||||
static constexpr std::string_view classIdPrefix = "command_station.";
|
||||
|
||||
static const std::vector<std::string_view>& classList();
|
||||
static constexpr auto classList = makeArray(
|
||||
LocoNetSerial::classId,
|
||||
#ifndef DISABLE_USB_XPRESSNET_INTERFACE
|
||||
USBXpressNetInterface::classId,
|
||||
#endif
|
||||
XpressNetSerial::classId,
|
||||
RocoZ21::classId,
|
||||
VirtualCommandStation::classId
|
||||
);
|
||||
|
||||
static std::shared_ptr<CommandStation> create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id);
|
||||
};
|
||||
|
||||
@ -30,23 +30,24 @@
|
||||
ControllerList::ControllerList(Object& _parent, const std::string& parentPropertyName) :
|
||||
ObjectList<Controller>(_parent, parentPropertyName),
|
||||
add{*this, "add",
|
||||
[this]()
|
||||
{
|
||||
auto world = getWorld(&this->parent());
|
||||
if(!world)
|
||||
return std::shared_ptr<Controller>();
|
||||
auto controller = Controllers::create(world, "controller.wlanmaus", world->getUniqueId("controller"));
|
||||
if(auto* cs = dynamic_cast<CommandStation*>(&this->parent()))
|
||||
controller->commandStation = cs->shared_ptr<CommandStation>();
|
||||
//else if(world->commandStations->length() == 1)
|
||||
// decoder->commandStation = cs->shared_ptr<CommandStation>();
|
||||
return controller;
|
||||
}}
|
||||
[this](std::string_view classId)
|
||||
{
|
||||
auto world = getWorld(&this->parent());
|
||||
if(!world)
|
||||
return std::shared_ptr<Controller>();
|
||||
auto controller = Controllers::create(world, classId, world->getUniqueId("controller"));
|
||||
if(auto* cs = dynamic_cast<CommandStation*>(&this->parent()))
|
||||
controller->commandStation = cs->shared_ptr<CommandStation>();
|
||||
//else if(world->commandStations->length() == 1)
|
||||
// decoder->commandStation = cs->shared_ptr<CommandStation>();
|
||||
return controller;
|
||||
}}
|
||||
{
|
||||
auto world = getWorld(&_parent);
|
||||
const bool editable = world && contains(world->state.value(), WorldState::Edit);
|
||||
|
||||
Attributes::addEnabled(add, editable);
|
||||
Attributes::addClassList(add, Controllers::classList);
|
||||
m_interfaceItems.add(add);
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +37,7 @@ class ControllerList : public ObjectList<Controller>
|
||||
public:
|
||||
CLASS_ID("controller_list")
|
||||
|
||||
Method<std::shared_ptr<Controller>()> add;
|
||||
Method<std::shared_ptr<Controller>(std::string_view)> add;
|
||||
|
||||
ControllerList(Object& _parent, const std::string& parentPropertyName);
|
||||
|
||||
|
||||
@ -21,15 +21,6 @@
|
||||
*/
|
||||
|
||||
#include "controllers.hpp"
|
||||
#include "wlanmaus.hpp"
|
||||
|
||||
const std::vector<std::string_view>& Controllers::classList()
|
||||
{
|
||||
static std::vector<std::string_view> list({
|
||||
WLANmaus::classId,
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
std::shared_ptr<Controller> Controllers::create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id)
|
||||
{
|
||||
|
||||
@ -24,12 +24,17 @@
|
||||
#define TRAINTASTIC_SERVER_HARDWARE_CONTROLLER_CONTROLLERS_HPP
|
||||
|
||||
#include "controller.hpp"
|
||||
#include "../../utils/makearray.hpp"
|
||||
|
||||
#include "wlanmaus.hpp"
|
||||
|
||||
struct Controllers
|
||||
{
|
||||
static constexpr std::string_view classIdPrefix = "controller.";
|
||||
|
||||
static const std::vector<std::string_view>& classList();
|
||||
static constexpr auto classList = makeArray(
|
||||
WLANmaus::classId
|
||||
);
|
||||
|
||||
static std::shared_ptr<Controller> create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id);
|
||||
};
|
||||
|
||||
36
server/src/utils/makearray.hpp
Normale Datei
36
server/src/utils/makearray.hpp
Normale Datei
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* server/src/utils/makearray.hpp
|
||||
*
|
||||
* This file is part of the traintastic source code.
|
||||
*
|
||||
* Copyright (C) 2019-2020 Reinder Feenstra
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef TRAINTASTIC_SERVER_UTILS_MAKEARRAY_HPP
|
||||
#define TRAINTASTIC_SERVER_UTILS_MAKEARRAY_HPP
|
||||
|
||||
#include <array>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
template<typename T, typename... Ts>
|
||||
constexpr std::array<std::decay_t<T>, 1 + sizeof...(Ts)> makeArray(T&& t, Ts&&... ts) noexcept(noexcept(std::is_nothrow_constructible<std::array<std::decay_t<T>, 1 + sizeof... (Ts)>, T&&, Ts&&...>::value))
|
||||
{
|
||||
return {{std::forward<T>(t), std::forward<Ts>(ts)...}};
|
||||
}
|
||||
|
||||
#endif
|
||||
@ -41,6 +41,7 @@ RailVehicleList::RailVehicleList(Object& _parent, const std::string& parentPrope
|
||||
const bool editable = world && contains(world->state.value(), WorldState::Edit);
|
||||
|
||||
Attributes::addEnabled(add, editable);
|
||||
Attributes::addClassList(add, RailVehicles::classList);
|
||||
m_interfaceItems.add(add);
|
||||
}
|
||||
|
||||
|
||||
@ -21,17 +21,6 @@
|
||||
*/
|
||||
|
||||
#include "railvehicles.hpp"
|
||||
#include "locomotive.hpp"
|
||||
#include "freightcar.hpp"
|
||||
|
||||
const std::vector<std::string_view>& RailVehicles::classList()
|
||||
{
|
||||
static std::vector<std::string_view> list({
|
||||
Locomotive::classId,
|
||||
FreightCar::classId,
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
std::shared_ptr<RailVehicle> RailVehicles::create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id)
|
||||
{
|
||||
|
||||
@ -24,12 +24,19 @@
|
||||
#define TRAINTASTIC_SERVER_VEHICLE_RAIL_RAILVEHICLES_HPP
|
||||
|
||||
#include "railvehicle.hpp"
|
||||
#include "../../utils/makearray.hpp"
|
||||
|
||||
#include "locomotive.hpp"
|
||||
#include "freightcar.hpp"
|
||||
|
||||
struct RailVehicles
|
||||
{
|
||||
static constexpr std::string_view classIdPrefix = "vehicle.rail.";
|
||||
|
||||
static const std::vector<std::string_view>& classList();
|
||||
static constexpr auto classList = makeArray(
|
||||
Locomotive::classId,
|
||||
FreightCar::classId
|
||||
);
|
||||
|
||||
static std::shared_ptr<RailVehicle> create(const std::weak_ptr<World>& world, std::string_view classId, std::string_view id);
|
||||
};
|
||||
|
||||
@ -35,6 +35,7 @@ enum class AttributeName : uint16_t
|
||||
ObjectEditor = 5,
|
||||
Values = 6,
|
||||
SubObject = 7,
|
||||
ClassList = 8,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Laden…
x
In neuem Issue referenzieren
Einen Benutzer sperren