# HG changeset patch # User Teemu Piippo # Date 1610285324 -7200 # Node ID 165777a20dc78b5394d187a528fef52de349e7d7 # Parent 06a1aef170aae94d9f9fe102d4874754db7087ab added tool base code diff -r 06a1aef170aa -r 165777a20dc7 CMakeLists.txt --- a/CMakeLists.txt Thu Nov 05 14:29:58 2020 +0200 +++ b/CMakeLists.txt Sun Jan 10 15:28:44 2021 +0200 @@ -25,6 +25,7 @@ source_group("3.1 Settings editor" REGULAR_EXPRESSION "src/settingseditor/.+\\.(cpp|h|ui)") source_group("3 User interface" REGULAR_EXPRESSION "src/(mainwindow|document|documentmanager|uiutilities)\\.(cpp|h|ui)") source_group("2 Model handling" REGULAR_EXPRESSION "src/(model|modeleditcontext|libraries|colors|parser)\\.(cpp|h|ui)") +source_group("6 Editing tools" REGULAR_EXPRESSION "src/tools/.+\\.(cpp|h|ui)") set (LDFORGE_SOURCES src/colors.cpp @@ -67,6 +68,9 @@ src/widgets/doublespinbox.cpp src/widgets/matrixeditor.cpp src/widgets/vec3editor.cpp + src/tools/basetool.cpp + src/tools/selecttool.cpp + src/tools/drawtool.cpp ) set (LDFORGE_HEADERS src/basics.h @@ -118,6 +122,9 @@ src/widgets/doublespinbox.h src/widgets/matrixeditor.h src/widgets/vec3editor.h + src/tools/selecttool.h + src/tools/basetool.h + src/tools/drawtool.h ) set (LDFORGE_FORMS src/document.ui diff -r 06a1aef170aa -r 165777a20dc7 locale/fi.ts --- a/locale/fi.ts Thu Nov 05 14:29:58 2020 +0200 +++ b/locale/fi.ts Sun Jan 10 15:28:44 2021 +0200 @@ -2,6 +2,34 @@ + ColorSelectDialog + + + Choose colour + + + + + Search... + + + + + TextLabel + + + + + Colour index: + + + + + Direct colour... + + + + Document @@ -10,6 +38,19 @@ + DrawTool + + + Draw + + + + + Draw new elements into the model. + + + + KeyboardShortcutsEditor @@ -137,76 +178,91 @@ + toolBar + + + + Quit Lopeta - + Open… Avaa... - + Ctrl+O Ctrl+O - + New Uusi - + Ctrl+N Ctrl+N - + Preferences… Asetukset... - + Normal colours Perusvärit - + BFC color coding BFC-värikoodaus - + Random colours Satunnaiset värit - + Pick scene colours - + + Error + + + + + Unable to construct %1 + + + + Open model Avaa malli - + LDraw models (*.ldr *.dat) LDraw-mallit (*.ldr *.dat) - + Problem loading references Ongelma viitteiden lataamisessa - + Problem opening file Ongelma tiedoston avaamisessa - + Could not open %1: %2 Ei voitu avata %1: %2 @@ -299,6 +355,19 @@ + SelectTool + + + Select + + + + + Select elements from the model. + + + + SettingsEditor diff -r 06a1aef170aa -r 165777a20dc7 locale/sv.ts --- a/locale/sv.ts Thu Nov 05 14:29:58 2020 +0200 +++ b/locale/sv.ts Sun Jan 10 15:28:44 2021 +0200 @@ -2,6 +2,29 @@ + ColorSelectDialog + + Choose colour + + + + Search... + + + + TextLabel + + + + Colour index: + + + + Direct colour... + + + + Dialog System language @@ -16,6 +39,17 @@ + DrawTool + + Draw + + + + Draw new elements into the model. + + + + KeyboardShortcutsEditor Action @@ -228,6 +262,18 @@ Pick scene colours + + Error + + + + Unable to construct %1 + + + + toolBar + + MatrixEditor @@ -306,6 +352,17 @@ + SelectTool + + Select + + + + Select elements from the model. + + + + SettingsEditor Dialog diff -r 06a1aef170aa -r 165777a20dc7 src/basics.h --- a/src/basics.h Thu Nov 05 14:29:58 2020 +0200 +++ b/src/basics.h Sun Jan 10 15:28:44 2021 +0200 @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -162,9 +163,26 @@ return {point.x(), point.y()}; } -constexpr float PIf = static_cast(M_PI); -constexpr double PI = M_PI; -constexpr long double PIl = M_PIl; +/* + * coalesce(arg1, arg2, ..., argn) + * Returns the first of the given arguments that evaluates to true. + */ +template +T coalesce(T&& arg) +{ + // recursion base: 1 argument + return arg; +} + +template +std::common_type_t coalesce(T&& arg, Rest&&... rest) +{ + // general case: n arguments + return arg ? arg : coalesce(rest...); +} + +template +constexpr std::enable_if_t, T> PI = static_cast(M_PIl); Q_DECLARE_METATYPE(glm::vec3) Q_DECLARE_METATYPE(glm::mat4) diff -r 06a1aef170aa -r 165777a20dc7 src/main.h --- a/src/main.h Thu Nov 05 14:29:58 2020 +0200 +++ b/src/main.h Sun Jan 10 15:28:44 2021 +0200 @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "basics.h" #include "utility.h" diff -r 06a1aef170aa -r 165777a20dc7 src/mainwindow.cpp --- a/src/mainwindow.cpp Thu Nov 05 14:29:58 2020 +0200 +++ b/src/mainwindow.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -28,6 +28,14 @@ #include "document.h" #include "uiutilities.h" #include "widgets/colorselectdialog.h" +#include "tools/basetool.h" +#include "tools/drawtool.h" +#include "tools/selecttool.h" + +static const QMetaObject* const toolMetaObjects[] = { + &SelectTool::staticMetaObject, + &DrawTool::staticMetaObject, +}; template struct MemberData @@ -78,6 +86,22 @@ this->restoreSettings(); this->updateRenderPreferences(); this->newModel(); + + for (const QMetaObject* const metaObject : ::toolMetaObjects) + { + QObject* const objectInstance = metaObject->newInstance(Q_ARG(QObject*, this)); + BaseTool* const toolInstance = qobject_cast(objectInstance); + if (toolInstance) + { + this->selectedTool = coalesce(this->selectedTool, toolInstance); + this->tools.append(toolInstance); + qInfo() << toolInstance->name(); + } + else + { + QMessageBox::critical(this, tr("Error"), tr("Unable to construct %1").arg(metaObject->className())); + } + } } // MainWindow needs a destructor even if it is empty because otherwise the destructor of the diff -r 06a1aef170aa -r 165777a20dc7 src/mainwindow.h --- a/src/mainwindow.h Thu Nov 05 14:29:58 2020 +0200 +++ b/src/mainwindow.h Sun Jan 10 15:28:44 2021 +0200 @@ -58,6 +58,8 @@ QStringList recentlyOpenedFiles; ldraw::ColorTable colorTable; gl::RenderPreferences renderPreferences; + QVector tools; + BaseTool* selectedTool = nullptr; void updateTitle(); void updateRenderPreferences(); void saveSettings(); diff -r 06a1aef170aa -r 165777a20dc7 src/mainwindow.ui --- a/src/mainwindow.ui Thu Nov 05 14:29:58 2020 +0200 +++ b/src/mainwindow.ui Sun Jan 10 15:28:44 2021 +0200 @@ -26,7 +26,7 @@ 0 0 800 - 24 + 26 @@ -59,6 +59,17 @@ + + + toolBar + + + LeftToolBarArea + + + false + + Quit diff -r 06a1aef170aa -r 165777a20dc7 src/tools/basetool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/basetool.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,4 @@ +#include "basetool.h" + +BaseTool::BaseTool(QObject* parent) : + QObject{parent} {} diff -r 06a1aef170aa -r 165777a20dc7 src/tools/basetool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/basetool.h Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,26 @@ +#pragma once +#include "../main.h" + +class BaseTool : public QObject +{ + Q_OBJECT + +public: + struct MouseEventData + { + QMouseEvent* ev; + Qt::KeyboardModifiers keymods; + bool mouseMoved; + Qt::MouseButtons releasedButtons; + }; + + BaseTool(QObject* parent = nullptr); + + virtual QString name() const = 0; + virtual QString toolTip() const = 0; + virtual bool mousePressed(QMouseEvent*) { return false; } + virtual bool mouseReleased(MouseEventData const&) { return false; } + virtual bool mouseDoubleClicked(QMouseEvent*) { return false; } + virtual bool mouseMoved(QMouseEvent*) { return false; } + virtual bool keyReleased(QKeyEvent*) { return false; } +}; diff -r 06a1aef170aa -r 165777a20dc7 src/tools/drawtool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/drawtool.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,16 @@ +#include "drawtool.h" + +DrawTool::DrawTool(QObject* parent) : + BaseTool{parent} {} + +QString DrawTool::name() const +{ + static const QString result = tr("Draw"); + return result; +} + +QString DrawTool::toolTip() const +{ + static const QString result = tr("Draw new elements into the model."); + return result; +} diff -r 06a1aef170aa -r 165777a20dc7 src/tools/drawtool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/drawtool.h Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,13 @@ +#pragma once +#include "basetool.h" + +class DrawTool : public BaseTool +{ + Q_OBJECT + +public: + Q_INVOKABLE DrawTool(QObject* parent = nullptr); + + QString name() const; + QString toolTip() const; +}; diff -r 06a1aef170aa -r 165777a20dc7 src/tools/selecttool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/selecttool.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,16 @@ +#include "selecttool.h" + +SelectTool::SelectTool(QObject* parent) : + BaseTool{parent} {} + +QString SelectTool::name() const +{ + static const QString result = tr("Select"); + return result; +} + +QString SelectTool::toolTip() const +{ + static const QString result = tr("Select elements from the model."); + return result; +} diff -r 06a1aef170aa -r 165777a20dc7 src/tools/selecttool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/selecttool.h Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,13 @@ +#pragma once +#include "basetool.h" + +class SelectTool : public BaseTool +{ + Q_OBJECT + +public: + Q_INVOKABLE SelectTool(QObject* parent = nullptr); + + QString name() const; + QString toolTip() const; +}; diff -r 06a1aef170aa -r 165777a20dc7 src/ui/canvas.cpp --- a/src/ui/canvas.cpp Thu Nov 05 14:29:58 2020 +0200 +++ b/src/ui/canvas.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -69,12 +69,12 @@ if (angle_x < angle_y) { this->newStatusText("rotate by X axis"); - this->gridMatrix = glm::rotate(this->gridMatrix, PIf / 2, glm::vec3{1, 0, 0}); + this->gridMatrix = glm::rotate(this->gridMatrix, PI / 2, glm::vec3{1, 0, 0}); } else { this->newStatusText("rotate by Y axis"); - this->gridMatrix = glm::rotate(this->gridMatrix, PIf / 2, glm::vec3{0, 1, 0}); + this->gridMatrix = glm::rotate(this->gridMatrix, PI / 2, glm::vec3{0, 1, 0}); } this->updateGridMatrix(); this->update(); diff -r 06a1aef170aa -r 165777a20dc7 src/widgets/colorinput.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/colorinput.cpp Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,14 @@ +#include "colorinput.h" +#include "ui_colorinput.h" + +ColorInput::ColorInput(QWidget *parent) : + QWidget(parent), + ui(new Ui::ColorInput) +{ + ui->setupUi(this); +} + +ColorInput::~ColorInput() +{ + delete ui; +} diff -r 06a1aef170aa -r 165777a20dc7 src/widgets/colorinput.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/colorinput.h Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,22 @@ +#ifndef COLORINPUT_H +#define COLORINPUT_H + +#include + +namespace Ui { +class ColorInput; +} + +class ColorInput : public QWidget +{ + Q_OBJECT + +public: + explicit ColorInput(QWidget *parent = nullptr); + ~ColorInput(); + +private: + Ui::ColorInput *ui; +}; + +#endif // COLORINPUT_H diff -r 06a1aef170aa -r 165777a20dc7 src/widgets/colorinput.ui --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/widgets/colorinput.ui Sun Jan 10 15:28:44 2021 +0200 @@ -0,0 +1,21 @@ + + + + + ColorInput + + + + 0 + 0 + 400 + 300 + + + + Form + + + + +