# HG changeset patch # User Teemu Piippo # Date 1627080638 -10800 # Node ID 02f142b399b1a13d45802d0ac7c016a8b5300101 # Parent 128efb9d148be5961d23bbd2b48d4085574230d1 Move selection logic into select tool diff -r 128efb9d148b -r 02f142b399b1 src/gl/partrenderer.cpp --- a/src/gl/partrenderer.cpp Tue Jul 20 01:22:01 2021 +0300 +++ b/src/gl/partrenderer.cpp Sat Jul 24 01:50:38 2021 +0300 @@ -382,3 +382,11 @@ emit this->renderPreferencesChanged(); this->update(); } + +/** + * @return the currently highlighted object + */ +ldraw::id_t PartRenderer::getHighlightedObject() const +{ + return this->highlighted; +} diff -r 128efb9d148b -r 02f142b399b1 src/gl/partrenderer.h --- a/src/gl/partrenderer.h Tue Jul 20 01:22:01 2021 +0300 +++ b/src/gl/partrenderer.h Sat Jul 24 01:50:38 2021 +0300 @@ -22,6 +22,7 @@ QWidget* parent = nullptr); ~PartRenderer() override; void setRenderPreferences(const gl::RenderPreferences& newPreferences); + ldraw::id_t getHighlightedObject() const; protected: ldraw::id_t pick(const QPoint& where); void initializeGL() override; diff -r 128efb9d148b -r 02f142b399b1 src/main.h --- a/src/main.h Tue Jul 20 01:22:01 2021 +0300 +++ b/src/main.h Sat Jul 24 01:50:38 2021 +0300 @@ -92,6 +92,12 @@ } template + inline bool operator!=(Id one, decltype(NULL_ID)) + { + return one.value != 0; + } + + template inline bool operator<(Id one, decltype(NULL_ID)) { return one.value < 0; diff -r 128efb9d148b -r 02f142b399b1 src/tools/selecttool.cpp --- a/src/tools/selecttool.cpp Tue Jul 20 01:22:01 2021 +0300 +++ b/src/tools/selecttool.cpp Sat Jul 24 01:50:38 2021 +0300 @@ -14,3 +14,14 @@ static const QString result = tr("Select elements from the model."); return result; } + +bool SelectTool::mouseClick(const Canvas::MouseClickInfo& info) +{ + const ldraw::id_t highlighted = info.invoker->getHighlightedObject(); + info.invoker->clearSelection(); + if (highlighted != ldraw::NULL_ID) + { + info.invoker->addToSelection(highlighted); + } + return true; +} diff -r 128efb9d148b -r 02f142b399b1 src/tools/selecttool.h --- a/src/tools/selecttool.h Tue Jul 20 01:22:01 2021 +0300 +++ b/src/tools/selecttool.h Sat Jul 24 01:50:38 2021 +0300 @@ -8,6 +8,7 @@ public: Q_INVOKABLE SelectTool(QObject* parent = nullptr); - QString name() const; - QString toolTip() const; + QString name() const override; + QString toolTip() const override; + bool mouseClick(const Canvas::MouseClickInfo &info) override; }; diff -r 128efb9d148b -r 02f142b399b1 src/ui/canvas.cpp --- a/src/ui/canvas.cpp Tue Jul 20 01:22:01 2021 +0300 +++ b/src/ui/canvas.cpp Sat Jul 24 01:50:38 2021 +0300 @@ -91,17 +91,6 @@ { if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) { - if (this->highlighted == ldraw::NULL_ID) - { - this->selection = {}; - } - else - { - this->selection = {this->highlighted}; - } - this->compiler->setSelectedObjects(this->selection); - emit selectionChanged(this->selection); - this->update(); MouseClickInfo info; info.worldPosition = this->worldPosition; info.invoker = this; @@ -297,3 +286,19 @@ { return previewLayers[static_cast(name)]; } + +void Canvas::clearSelection() +{ + this->selection.clear(); + this->compiler->setSelectedObjects(this->selection); + emit selectionChanged(this->selection); + this->update(); +} + +void Canvas::addToSelection(ldraw::id_t id) +{ + this->selection.insert(id); + this->compiler->setSelectedObjects(this->selection); + emit selectionChanged(this->selection); + this->update(); +} diff -r 128efb9d148b -r 02f142b399b1 src/ui/canvas.h --- a/src/ui/canvas.h Tue Jul 20 01:22:01 2021 +0300 +++ b/src/ui/canvas.h Sat Jul 24 01:50:38 2021 +0300 @@ -26,6 +26,8 @@ QWidget* parent = nullptr); const PreviewLayer& getPreviewLayer(PreviewLayerName name) const; PreviewLayer& modifyPreviewLayer(PreviewLayerName name); + void clearSelection(); + void addToSelection(ldraw::id_t id); public slots: void handleSelectionChange(const QSet& selectedIds, const QSet& deselectedIds); protected: