From 00178a0f066e41ecef48e04cc7ba32f285837ebb Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Wed, 31 Oct 2018 17:26:09 +0100 Subject: [PATCH] WIP move up and down : - groups are blocked. Need a way to treat differently full selected groups . --- Polyhedron/demo/Polyhedron/Scene.cpp | 160 ++++++++++++++++++++------- Polyhedron/demo/Polyhedron/Scene.h | 3 +- 2 files changed, 119 insertions(+), 44 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/Scene.cpp b/Polyhedron/demo/Polyhedron/Scene.cpp index ef833414f61..aa135a7a301 100644 --- a/Polyhedron/demo/Polyhedron/Scene.cpp +++ b/Polyhedron/demo/Polyhedron/Scene.cpp @@ -1074,88 +1074,162 @@ bool Scene::dropMimeData(const QMimeData * /*data*/, return true; } -void Scene::moveRowUp() -{ - QList to_select; - QList sorted_list = selectionIndices(); - std::sort(sorted_list.begin(), sorted_list.end(), + +bool Scene::sort_lists(QVector >&sorted_lists, bool up) +{ + QVector group_found; + Q_FOREACH(int i, selectionIndices()) + { + Scene_item* item = this->item(i); + if(item->has_group == 0) + { + sorted_lists.first().push_back(i); + } + else + { + int group_id = item_id(item->parentGroup()); + if(group_found.contains(group_id)) + sorted_lists[group_id].push_back(i); + else + { + group_found.push_back(group_id); + if(sorted_lists.size() < group_id+1) + sorted_lists.resize(group_id+1); + sorted_lists[group_id].push_back(i); + } + } + } + std::sort(sorted_lists.first().begin(), sorted_lists.first().end(), [this](int a, int b) { return children.indexOf(a) < children.indexOf(b); }); - if( children.indexOf(sorted_list.first()) == 0) - return; - for(int i=0; i& list = sorted_lists[i]; + if(list.isEmpty()) + continue; + Scene_group_item* group = qobject_cast(this->item(i)); + if(!group) + continue; + std::sort(list.begin(), list.end(), + [this, group](int a, int b) { + return group->getChildren().indexOf(a) < group->getChildren().indexOf(b); + }); + if(up && group->getChildren().indexOf(list.first()) == 0) + return false; + else if(!up && group->getChildren().indexOf(list.last()) == group->getChildren().size()-1) + return false; + } + return true; +} +void Scene::moveRowUp() +{ + QVector >sorted_lists(1); + QList to_select; + //sort lists according to the indices of each item in its container (scene or group) + //if moving one up would put it out of range, then we stop and do nothing. + if(!sort_lists(sorted_lists, true)) + return; + + for(int i=0; i 0) { - if(item(selected_id)->has_group >0) + //if not in group + QModelIndex baseId = index_map.key(selected_id); + int newId = children.indexOf( + index_map.value(index(baseId.row()-1, baseId.column(),baseId.parent()))) ; + children.move(children.indexOf(selected_id), newId); + redraw_model(); + to_select.append(m_entries.indexOf(selected_item)); + } + } + for(int i=1; i 0) { Scene_group_item* group = selected_item->parentGroup(); if(group) { int id = group->getChildren().indexOf(item_id(selected_item)); group->moveUp(id); + redraw_model(); + to_select.append(m_entries.indexOf(selected_item)); } } - else - { - //if not in group - QModelIndex baseId = index_map.key(selected_id); - int newId = children.indexOf( - index_map.value(index(baseId.row()-1, baseId.column(),baseId.parent()))) ; - children.move(children.indexOf(selected_id), newId); - } - redraw_model(); - to_select.append(m_entries.indexOf(selected_item)); } } - selectionChanged(to_select); + if(!to_select.isEmpty()) + selectionChanged(to_select); } void Scene::moveRowDown() { + QVector >sorted_lists(1); QList to_select; - QList sorted_list = selectionIndices(); - std::sort(sorted_list.begin(), sorted_list.end(), - [this](int a, int b) { - return children.indexOf(a) < children.indexOf(b); -}); - if( children.indexOf(sorted_list.last()) == children.size() -1) + //sort lists according to the indices of each item in its container (scene or group) + //if moving one up would put it out of range, then we stop and do nothing. + if(!sort_lists(sorted_lists, false)) return; - for(int i=sorted_list.size()-1; i>=0; --i) + for(int i=sorted_lists.first().size()-1; i>=0; --i) { - Item_id selected_id = sorted_list[i]; + Item_id selected_id = sorted_lists.first()[i]; Scene_item* selected_item = item(selected_id); if(!selected_item) - return; if(index_map.key(selected_id).row() < rowCount(index_map.key(selected_id).parent())-1) { - if(item(selected_id)->has_group >0) - { - Scene_group_item* group = selected_item->parentGroup(); - if(group) - { - int id = group->getChildren().indexOf(item_id(selected_item)); - group->moveDown(id); - } - } - else - { //if not in group QModelIndex baseId = index_map.key(selected_id); int newId = children.indexOf( index_map.value(index(baseId.row()+1, baseId.column(),baseId.parent()))) ; children.move(children.indexOf(selected_id), newId); - } + redraw_model(); to_select.prepend(m_entries.indexOf(selected_item)); } } - + for(int i=1; i=0; --j) + { + Item_id selected_id = sorted_lists[i][j]; + Scene_item* selected_item = item(selected_id); + if(!selected_item) + return; + if(index_map.key(selected_id).row() < rowCount(index_map.key(selected_id).parent())-1) + { + if(item(selected_id)->has_group >0) + { + Scene_group_item* group = selected_item->parentGroup(); + if(group) + { + int id = group->getChildren().indexOf(item_id(selected_item)); + group->moveDown(id); + } + } + redraw_model(); + to_select.prepend(m_entries.indexOf(selected_item)); + } + } + } selectionChanged(to_select); } Scene::Item_id Scene::mainSelectionIndex() const { diff --git a/Polyhedron/demo/Polyhedron/Scene.h b/Polyhedron/demo/Polyhedron/Scene.h index 8ba07e17898..753bfc9e13f 100644 --- a/Polyhedron/demo/Polyhedron/Scene.h +++ b/Polyhedron/demo/Polyhedron/Scene.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -286,7 +287,7 @@ private: QOpenGLVertexArrayObject* vao; mutable QOpenGLBuffer vbo[2]; bool visibility_recentering_enabled; - + bool sort_lists(QVector >&sorted_lists, bool up); }; // end class Scene class QAbstractProxyModel;