diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 21cbf5c5..720987df 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -50,6 +50,8 @@ file(GLOB SOURCES "src/widget/*.cpp" "src/widget/object/*.hpp" "src/widget/object/*.cpp" + "src/widget/objectlist/*.hpp" + "src/widget/objectlist/*.cpp" "src/widget/throttle/*.hpp" "src/widget/throttle/*.cpp" "../shared/src/traintastic/locale/locale.cpp" diff --git a/client/src/widget/commandstationlistwidget.cpp b/client/src/widget/commandstationlistwidget.cpp deleted file mode 100644 index 9bf37429..00000000 --- a/client/src/widget/commandstationlistwidget.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/** - * client/src/widget/commandstationlistwidget.cpp - * - * 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. - */ - -#include "commandstationlistwidget.hpp" - -CommandStationListWidget::CommandStationListWidget(const ObjectPtr& object, QWidget* parent) : - ObjectListWidget(object, parent) -{ - //addActionAdd(); - addActionEdit(); - addActionDelete(); -} diff --git a/client/src/widget/commandstationlistwidget.hpp b/client/src/widget/commandstationlistwidget.hpp deleted file mode 100644 index 172c6440..00000000 --- a/client/src/widget/commandstationlistwidget.hpp +++ /dev/null @@ -1,36 +0,0 @@ -/** - * client/src/widget/commandstationlistwidget.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_CLIENT_WIDGET_COMMANDSTATIONLISTWIDGET_HPP -#define TRAINTASTIC_CLIENT_WIDGET_COMMANDSTATIONLISTWIDGET_HPP - -#include "objectlistwidget.hpp" - -class CommandStationListWidget : public ObjectListWidget -{ - Q_OBJECT - - public: - CommandStationListWidget(const ObjectPtr& object, QWidget* parent = nullptr); -}; - -#endif diff --git a/client/src/widget/createwidget.cpp b/client/src/widget/createwidget.cpp index 8cae2a44..883d8c85 100644 --- a/client/src/widget/createwidget.cpp +++ b/client/src/widget/createwidget.cpp @@ -21,10 +21,7 @@ */ #include "createwidget.hpp" -#include "commandstationlistwidget.hpp" -#include "decoderlistwidget.hpp" -#include "inputlistwidget.hpp" -#include "luascriptlistwidget.hpp" +#include "objectlist/throttleobjectlistwidget.hpp" #include "object/luascripteditwidget.hpp" #include "object/objecteditwidget.hpp" #include "object/itemseditwidget.hpp" @@ -42,17 +39,17 @@ QWidget* createWidgetIfCustom(const ObjectPtr& object, QWidget* parent) const QString& classId = object->classId(); if(classId == "command_station_list") - return new CommandStationListWidget(object, parent); // todo remove + return new ObjectListWidget(object, parent); // todo remove else if(classId == "decoder_list") - return new DecoderListWidget(object, parent); // todo remove + return new ThrottleObjectListWidget(object, parent); // todo remove else if(classId == "input_list") - return new InputListWidget(object, parent); // todo remove + return new ObjectListWidget(object, parent); // todo remove else if(classId == "controller_list") return new ObjectListWidget(object, parent); // todo remove else if(classId == "rail_vehicle_list") return new ObjectListWidget(object, parent); // todo remove else if(classId == "lua.script_list") - return new LuaScriptListWidget(object, parent); // todo remove + return new ObjectListWidget(object, parent); // todo remove else if(classId == "world_list") return new ObjectListWidget(object, parent); else if(classId == "lua.script") diff --git a/client/src/widget/inputlistwidget.cpp b/client/src/widget/inputlistwidget.cpp deleted file mode 100644 index 2c3683ca..00000000 --- a/client/src/widget/inputlistwidget.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/** - * client/src/widget/decoderlistwidget.cpp - * - * 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. - */ - -#include "inputlistwidget.hpp" - -InputListWidget::InputListWidget(const ObjectPtr& object, QWidget* parent) : - ObjectListWidget(object, parent) -{ - //addActionAdd(); - addActionEdit(); - addActionDelete(); -} - -void InputListWidget::add() -{ -} diff --git a/client/src/widget/inputlistwidget.hpp b/client/src/widget/inputlistwidget.hpp deleted file mode 100644 index d0b18c01..00000000 --- a/client/src/widget/inputlistwidget.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * client/src/widget/decoderlistwidget.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_CLIENT_WIDGET_INPUTLISTWIDGET_HPP -#define TRAINTASTIC_CLIENT_WIDGET_INPUTLISTWIDGET_HPP - -#include "objectlistwidget.hpp" - -class InputListWidget : public ObjectListWidget -{ - Q_OBJECT - - protected: - void add() final; - - public: - InputListWidget(const ObjectPtr& object, QWidget* parent = nullptr); -}; - -#endif diff --git a/client/src/widget/luascriptlistwidget.cpp b/client/src/widget/luascriptlistwidget.cpp deleted file mode 100644 index 96a8dcd4..00000000 --- a/client/src/widget/luascriptlistwidget.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/** - * client/src/widget/luascriptlistwidget.cpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 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. - */ - -#include "luascriptlistwidget.hpp" -#include "../network/connection.hpp" - -LuaScriptListWidget::LuaScriptListWidget(const ObjectPtr& object, QWidget* parent) : - ObjectListWidget(object, parent) -{ - //addActionAdd(); - //addActionEdit(); - //addActionDelete(); -} - -void LuaScriptListWidget::add() -{ - //Client::instance->createObject("lua.script"); -} diff --git a/client/src/widget/luascriptlistwidget.hpp b/client/src/widget/luascriptlistwidget.hpp deleted file mode 100644 index 3e0034c8..00000000 --- a/client/src/widget/luascriptlistwidget.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/** - * client/src/widget/luascriptlistwidget.hpp - * - * This file is part of the traintastic source code. - * - * Copyright (C) 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_CLIENT_WIDGET_LUASCRIPTLISTWIDGET_HPP -#define TRAINTASTIC_CLIENT_WIDGET_LUASCRIPTLISTWIDGET_HPP - -#include "objectlistwidget.hpp" - -class LuaScriptListWidget : public ObjectListWidget -{ - Q_OBJECT - - protected: - void add() final; - - public: - LuaScriptListWidget(const ObjectPtr& object, QWidget* parent = nullptr); -}; - -#endif diff --git a/client/src/widget/objectlistwidget.cpp b/client/src/widget/objectlist/objectlistwidget.cpp similarity index 72% rename from client/src/widget/objectlistwidget.cpp rename to client/src/widget/objectlist/objectlistwidget.cpp index 94c71645..20ab98f0 100644 --- a/client/src/widget/objectlistwidget.cpp +++ b/client/src/widget/objectlist/objectlistwidget.cpp @@ -1,9 +1,9 @@ /** - * client/src/widget/objectlistwidget.cpp + * client/src/widget/objectlist/objectlistwidget.cpp * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2020 Reinder Feenstra + * Copyright (C) 2019-2021 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -26,17 +26,19 @@ #include #include #include -#include "tablewidget.hpp" -#include "../network/connection.hpp" -#include "../network/object.hpp" -#include "../network/tablemodel.hpp" -#include "../network/method.hpp" -#include "../network/utils.hpp" -#include "../widget/alertwidget.hpp" -#include "../theme/theme.hpp" +#include "../tablewidget.hpp" +#include "../../network/connection.hpp" +#include "../../network/object.hpp" +#include "../../network/tablemodel.hpp" +#include "../../network/method.hpp" +#include "../../network/utils.hpp" +#include "../../network/callmethod.hpp" +#include "../alertwidget.hpp" +#include "../../theme/theme.hpp" +#include "../../misc/methodaction.hpp" -#include "../mainwindow.hpp" +#include "../../mainwindow.hpp" @@ -55,6 +57,8 @@ ObjectListWidget::ObjectListWidget(const ObjectPtr& object, QWidget* parent) : m_actionDelete{nullptr}, m_tableWidget{new TableWidget()} { + m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); + QVBoxLayout* layout = new QVBoxLayout(); layout->setMargin(0); layout->addWidget(m_toolbar); @@ -73,6 +77,12 @@ ObjectListWidget::ObjectListWidget(const ObjectPtr& object, QWidget* parent) : m_tableWidget->setTableModel(tableModel); connect(m_tableWidget, &TableWidget::doubleClicked, this, &ObjectListWidget::tableDoubleClicked); + connect(m_tableWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, + [this](const QItemSelection&, const QItemSelection&) + { + tableSelectionChanged(); + }); + tableSelectionChanged(); delete spinner; } @@ -159,13 +169,24 @@ ObjectListWidget::ObjectListWidget(const ObjectPtr& object, QWidget* parent) : Q_ASSERT(false); // unsupported method prototype } - m_actionEdit = m_toolbar->addAction(Theme::getIcon("edit"), tr("Edit")); + m_actionEdit = m_toolbar->addAction(Theme::getIcon("edit"), Locale::tr("object_list:edit"), + [this]() + { + for(const QString& id : getSelectedObjectIds()) + MainWindow::instance->showObject(id); + }); m_actionEdit->setEnabled(false); if(Method* method = m_object->getMethod("remove")) { - m_actionDelete = m_toolbar->addAction(Theme::getIcon("delete"), method->displayName()); - //m_actionDelete->setEnabled(false); + m_actionDelete = new MethodAction(Theme::getIcon("delete"), *method, + [this]() + { + for(const QString& id : getSelectedObjectIds()) + callMethod(m_actionDelete->method(), nullptr, id); + }); + m_actionDelete->setForceDisabled(true); + m_toolbar->addAction(m_actionDelete); } } @@ -174,17 +195,6 @@ ObjectListWidget::~ObjectListWidget() m_object->connection()->cancelRequest(m_requestId); } -void ObjectListWidget::addActionEdit() -{ - // Q_ASSERT(!m_actionEdit); - -} - -void ObjectListWidget::addActionDelete() -{ - // Q_ASSERT(!m_actionDelete); -} - void ObjectListWidget::objectDoubleClicked(const QString& id) { MainWindow::instance->showObject(id); @@ -197,5 +207,27 @@ void ObjectListWidget::tableDoubleClicked(const QModelIndex& index) objectDoubleClicked(id); } +QStringList ObjectListWidget::getSelectedObjectIds() const +{ + QStringList ids; + if(auto* model = m_tableWidget->selectionModel(); model && model->hasSelection()) + for(const auto& index : model->selectedRows()) + if(QString id = m_tableWidget->getRowObjectId(index.row()); !id.isEmpty()) + ids.append(id); + return ids; +} + +void ObjectListWidget::tableSelectionChanged() +{ + tableSelectionChanged(m_tableWidget->selectionModel() && m_tableWidget->selectionModel()->hasSelection()); +} + +void ObjectListWidget::tableSelectionChanged(bool hasSelection) +{ + if(m_actionEdit) + m_actionEdit->setEnabled(hasSelection); + if(m_actionDelete) + m_actionDelete->setForceDisabled(!hasSelection); +} \ No newline at end of file diff --git a/client/src/widget/objectlistwidget.hpp b/client/src/widget/objectlist/objectlistwidget.hpp similarity index 70% rename from client/src/widget/objectlistwidget.hpp rename to client/src/widget/objectlist/objectlistwidget.hpp index f5587707..07b45032 100644 --- a/client/src/widget/objectlistwidget.hpp +++ b/client/src/widget/objectlist/objectlistwidget.hpp @@ -1,9 +1,9 @@ /** - * client/src/widget/objectlistwidget.hpp + * client/src/widget/objectlist/objectlistwidget.hpp * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2020 Reinder Feenstra + * Copyright (C) 2019-2021 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -20,42 +20,45 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef TRAINTASTIC_CLIENT_WIDGET_OBJECTLISTWIDGET_HPP -#define TRAINTASTIC_CLIENT_WIDGET_OBJECTLISTWIDGET_HPP +#ifndef TRAINTASTIC_CLIENT_WIDGET_OBJECTLIST_OBJECTLISTWIDGET_HPP +#define TRAINTASTIC_CLIENT_WIDGET_OBJECTLIST_OBJECTLISTWIDGET_HPP #include -#include "../network/objectptr.hpp" +#include "../../network/objectptr.hpp" class QToolBar; class QToolButton; class TableWidget; +class MethodAction; class ObjectListWidget : public QWidget { Q_OBJECT private: + //const QString m_id; + int m_requestId; + int m_requestIdAdd; + ObjectPtr m_object; + QToolBar* m_toolbar; QToolButton* m_buttonAdd; + QAction* m_actionAdd; + QAction* m_actionEdit; + MethodAction* m_actionDelete; + TableWidget* m_tableWidget; + + void tableSelectionChanged(); private slots: void tableDoubleClicked(const QModelIndex& index); protected: - const QString m_id; - int m_requestId; - int m_requestIdAdd; - ObjectPtr m_object; - QToolBar* m_toolbar; - QAction* m_actionAdd; - QAction* m_actionEdit; - QAction* m_actionDelete; - TableWidget* m_tableWidget; + const ObjectPtr& object() { return m_object; } + QToolBar* toolbar() { return m_toolbar; } - void addActionEdit(); - void addActionDelete(); - - virtual void add() { Q_ASSERT(false); } + virtual void tableSelectionChanged(bool hasSelection); virtual void objectDoubleClicked(const QString& id); + QStringList getSelectedObjectIds() const; public: explicit ObjectListWidget(const ObjectPtr& object, QWidget* parent = nullptr); diff --git a/client/src/widget/decoderlistwidget.cpp b/client/src/widget/objectlist/throttleobjectlistwidget.cpp similarity index 52% rename from client/src/widget/decoderlistwidget.cpp rename to client/src/widget/objectlist/throttleobjectlistwidget.cpp index c285078e..8adb7422 100644 --- a/client/src/widget/decoderlistwidget.cpp +++ b/client/src/widget/objectlist/throttleobjectlistwidget.cpp @@ -1,9 +1,9 @@ /** - * client/src/widget/decoderlistwidget.cpp + * client/src/widget/objectlist/throttleobjectlistwidget.cpp * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2020 Reinder Feenstra + * Copyright (C) 2021 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -20,25 +20,36 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "decoderlistwidget.hpp" -#include "tablewidget.hpp" -#include "../network/connection.hpp" -#include "../network/object.hpp" -#include "../network/abstractproperty.hpp" -#include "../mainwindow.hpp" +#include "throttleobjectlistwidget.hpp" +#include +#include "../../network/connection.hpp" +#include "../../network/object.hpp" +#include "../../network/abstractproperty.hpp" +#include "../../mainwindow.hpp" -DecoderListWidget::DecoderListWidget(const ObjectPtr& object, QWidget* parent) : - ObjectListWidget(object, parent) +ThrottleObjectListWidget::ThrottleObjectListWidget(const ObjectPtr& object, QWidget* parent) + : ObjectListWidget(object, parent) { - addActionEdit(); - addActionDelete(); + toolbar()->addSeparator(); + m_actionThrottle = toolbar()->addAction("throttle_object_list:throttle", + [this]() + { + for(const QString& id : getSelectedObjectIds()) + MainWindow::instance->showObject(id, "", SubWindowType::Throttle); + }); + m_actionThrottle->setEnabled(false); } -void DecoderListWidget::objectDoubleClicked(const QString& id) +void ThrottleObjectListWidget::tableSelectionChanged(bool hasSelection) { - if(m_object) + m_actionThrottle->setEnabled(hasSelection); +} + +void ThrottleObjectListWidget::objectDoubleClicked(const QString& id) +{ + if(object()) { - if(const auto& c = m_object->connection()) + if(const auto& c = object()->connection()) { if(const auto& w = c->world()) { diff --git a/client/src/widget/decoderlistwidget.hpp b/client/src/widget/objectlist/throttleobjectlistwidget.hpp similarity index 64% rename from client/src/widget/decoderlistwidget.hpp rename to client/src/widget/objectlist/throttleobjectlistwidget.hpp index bf8abf3c..51572269 100644 --- a/client/src/widget/decoderlistwidget.hpp +++ b/client/src/widget/objectlist/throttleobjectlistwidget.hpp @@ -1,9 +1,9 @@ /** - * client/src/widget/decoderlistwidget.hpp + * client/src/widget/objectlist/throttleobjectlistwidget.hpp * * This file is part of the traintastic source code. * - * Copyright (C) 2019-2020 Reinder Feenstra + * Copyright (C) 2021 Reinder Feenstra * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -20,20 +20,22 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef TRAINTASTIC_CLIENT_WIDGET_DECODERLISTWIDGET_HPP -#define TRAINTASTIC_CLIENT_WIDGET_DECODERLISTWIDGET_HPP +#ifndef TRAINTASTIC_CLIENT_WIDGET_OBJECTLIST_THROTTLEOBJECTLISTWIDGET_HPP +#define TRAINTASTIC_CLIENT_WIDGET_OBJECTLIST_THROTTLEOBJECTLISTWIDGET_HPP #include "objectlistwidget.hpp" -class DecoderListWidget : public ObjectListWidget +class ThrottleObjectListWidget : public ObjectListWidget { - Q_OBJECT + private: + QAction* m_actionThrottle; protected: + void tableSelectionChanged(bool hasSelection) final; void objectDoubleClicked(const QString& id) final; public: - explicit DecoderListWidget(const ObjectPtr& object, QWidget* parent = nullptr); + explicit ThrottleObjectListWidget(const ObjectPtr& object, QWidget* parent = nullptr); }; #endif