Thu, 15 Feb 2018 14:40:59 +0200
convert MainWindow::deleteSelection to mvc
CMakeLists.txt | file | annotate | diff | comparison | revisions | |
src/generics/reverse.h | file | annotate | diff | comparison | revisions | |
src/main.cpp | file | annotate | diff | comparison | revisions | |
src/mainwindow.cpp | file | annotate | diff | comparison | revisions | |
src/model.cpp | file | annotate | diff | comparison | revisions | |
src/model.h | file | annotate | diff | comparison | revisions |
--- a/CMakeLists.txt Thu Feb 15 12:19:10 2018 +0200 +++ b/CMakeLists.txt Thu Feb 15 14:40:59 2018 +0200 @@ -135,6 +135,7 @@ src/editmodes/magicWandMode.h src/editmodes/rectangleMode.h src/editmodes/selectMode.h + src/generics/reverse.h src/linetypes/comment.h src/linetypes/conditionaledge.h src/linetypes/edgeline.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/generics/reverse.h Thu Feb 15 14:40:59 2018 +0200 @@ -0,0 +1,128 @@ +/* + * LDForge: LDraw parts authoring CAD + * Copyright (C) 2013 - 2017 Teemu Piippo + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#pragma once +#include <type_traits> + +/* + * reverse(container) + * + * Returns a container class that, when iterated, iterates over the container in reverse order. + * Can be used for const and mutable containers, arrays as well as objects. + */ + +template<typename T> +class ReverseIterator +{ +public: + ReverseIterator(T iterator) : + iterator {iterator} {} + + ReverseIterator& operator++() + { + --this->iterator; + return *this; + } + + decltype(*T{}) operator*() const + { + return *this->iterator; + } + + bool operator!=(const ReverseIterator<T>& other) + { + return this->iterator != other.iterator; + } + +private: + T iterator; +}; + +template<typename T> +struct GenericIterator +{ + using type = decltype(std::begin(T{})); +}; + +template<typename T> +struct GenericConstIterator +{ + using type = decltype(std::begin((const T&) T{})); +}; + +template<typename T> +class ConstReverser +{ +public: + ConstReverser(const T& container) : + container {container} {} + + using Iterator = ReverseIterator<typename GenericConstIterator<T>::type>; + + Iterator begin() const + { + auto result = std::end(this->container); + return Iterator {--result}; + } + + Iterator end() const + { + auto result = std::begin(this->container); + return Iterator {--result}; + } + +private: + const T& container; +}; + +template<typename T> +class MutableReverser +{ +public: + MutableReverser(T& container) : + container {container} {} + + using Iterator = ReverseIterator<typename GenericIterator<T>::type>; + + Iterator begin() const + { + auto result = std::end(this->container); + return Iterator {--result}; + } + + Iterator end() const + { + auto result = std::begin(this->container); + return Iterator {--result}; + } + +private: + T& container; +}; + +template<typename T> +ConstReverser<T> reverse(const T& container) +{ + return {container}; +} + +template<typename T> +MutableReverser<T> reverse(T& container) +{ + return {container}; +}
--- a/src/main.cpp Thu Feb 15 12:19:10 2018 +0200 +++ b/src/main.cpp Thu Feb 15 14:40:59 2018 +0200 @@ -21,6 +21,7 @@ #include "ldpaths.h" #include "documentmanager.h" #include "mainwindow.h" +#include "generics/reverse.h" int main (int argc, char* argv[]) {
--- a/src/mainwindow.cpp Thu Feb 15 12:19:10 2018 +0200 +++ b/src/mainwindow.cpp Thu Feb 15 14:40:59 2018 +0200 @@ -22,6 +22,7 @@ #include <QFileDialog> #include <QPushButton> #include <QSettings> +#include "generics/reverse.h" #include "main.h" #include "canvas.h" #include "mainwindow.h" @@ -316,17 +317,19 @@ // int MainWindow::deleteSelection() { - if (selectedObjects().isEmpty()) - return 0; - - QSet<LDObject*> selectionCopy = selectedObjects(); + int count = 0; + QModelIndexList thingsToRemove = ui.objectList->selectionModel()->selectedIndexes(); - // Delete the objects that were being selected - for (LDObject* object : selectionCopy) - m_currentDocument->remove(object); + for (QModelIndex index : reverse(thingsToRemove)) + { + if (m_currentDocument->hasIndex(index.row(), index.column())) + { + m_currentDocument->removeAt(index); + count += 1; + } + } - refresh(); - return countof(selectionCopy); + return count; } // ---------------------------------------------------------------------------------------------------------------------
--- a/src/model.cpp Thu Feb 15 12:19:10 2018 +0200 +++ b/src/model.cpp Thu Feb 15 14:40:59 2018 +0200 @@ -144,6 +144,11 @@ delete object; } +void Model::removeAt(const QModelIndex& index) +{ + removeAt(index.row()); +} + /* * Replaces the given object with the contents of a model. */
--- a/src/model.h Thu Feb 15 12:19:10 2018 +0200 +++ b/src/model.h Thu Feb 15 14:40:59 2018 +0200 @@ -95,6 +95,7 @@ template<typename T, typename... Args> T* emplaceReplacement(LDObject* object, Args&& ...args); template<typename T, typename... Args> T* emplaceReplacementAt(int position, Args&& ...args); void removeAt(int position); + void removeAt(const QModelIndex& index); void remove(LDObject* object); void replace(LDObject *object, Model& model); void clear();