more selection rework

Sat, 03 Mar 2018 15:14:07 +0200

author
Santeri Piippo
date
Sat, 03 Mar 2018 15:14:07 +0200
changeset 1257
0d42a1ebd954
parent 1256
3fea6bc1519b
child 1258
f5921a474d57

more selection rework

src/canvas.cpp file | annotate | diff | comparison | revisions
src/lddocument.cpp file | annotate | diff | comparison | revisions
src/lddocument.h file | annotate | diff | comparison | revisions
src/mainwindow.cpp file | annotate | diff | comparison | revisions
src/mainwindow.h file | annotate | diff | comparison | revisions
src/toolsets/basictoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/filetoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/canvas.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/canvas.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -256,9 +256,10 @@
 	{
 		PrimitiveTreeItem* item = static_cast<PrimitiveTreeItem*> (m_window->getPrimitivesTree()->currentItem());
 		QString primitiveName = item->primitive()->name;
-		LDSubfileReference* reference = currentDocument()->emplaceAt<LDSubfileReference>(m_window->suggestInsertPoint());
-		reference->setFileInfo (m_documents->getDocumentByName(primitiveName));
-		currentDocument()->addToSelection(reference);
+		int position = m_window->suggestInsertPoint();
+		LDSubfileReference* reference = currentDocument()->emplaceAt<LDSubfileReference>(position);
+		reference->setFileInfo(m_documents->getDocumentByName(primitiveName));
+		m_window->select(currentDocument()->index(position));
 		update();
 		event->acceptProposedAction();
 	}
--- a/src/lddocument.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/lddocument.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -320,7 +320,6 @@
 		m_objectVertices.remove(object);
 	}
 
-	m_selection.remove(object);
 	return Model::withdrawAt(position);
 }
 
@@ -432,29 +431,6 @@
 			model.addFromString(object->asText());
 	}
 }
-
-// =============================================================================
-//
-void LDDocument::addToSelection (LDObject* obj) // [protected]
-{
-	if (not m_selection.contains(obj) and obj->model() == this)
-	{
-		m_selection.insert(obj);
-		emit objectModified(obj);
-	}
-}
-
-// =============================================================================
-//
-void LDDocument::removeFromSelection (LDObject* obj) // [protected]
-{
-	if (m_selection.contains(obj))
-	{
-		m_selection.remove(obj);
-		emit objectModified(obj);
-	}
-}
-
 // =============================================================================
 //
 bool LDDocument::swapObjects (LDObject* one, LDObject* other)
--- a/src/lddocument.h	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/lddocument.h	Sat Mar 03 15:14:07 2018 +0200
@@ -48,7 +48,6 @@
 
 	void addHistoryStep();
 	void addToHistory (AbstractHistoryEntry* entry);
-	void addToSelection (LDObject* obj);
 	void clearHistory();
 	void close();
 	QString defaultName() const;
@@ -70,7 +69,6 @@
 	void redo();
 	void redoVertices();
 	void reloadAllSubfiles();
-	void removeFromSelection (LDObject* obj);
 	bool save (QString path = "", qint64* sizeptr = nullptr);
 	long savePosition() const;
 	void setDefaultName (QString value);
@@ -107,7 +105,6 @@
 	QList<LDPolygon> m_polygonData;
 	QMap<LDObject*, QSet<Vertex>> m_objectVertices;
 	QSet<Vertex> m_vertices;
-	QSet<LDObject*> m_selection;
 	DocumentManager* m_manager;
 };
 
--- a/src/mainwindow.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/mainwindow.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -1104,6 +1104,22 @@
 	m_selections[m_currentDocument]->clear();
 }
 
+void MainWindow::select(const QModelIndex &objectIndex)
+{
+	if (objectIndex.isValid() and objectIndex.model() == m_currentDocument)
+		m_selections[m_currentDocument]->select(objectIndex, QItemSelectionModel::Select);
+}
+
+QItemSelectionModel* MainWindow::currentSelectionModel()
+{
+	return m_selections[m_currentDocument];
+}
+
+void MainWindow::replaceSelection(const QItemSelection& selection)
+{
+	m_selections[m_currentDocument]->select(selection, QItemSelectionModel::ClearAndSelect);
+}
+
 // ---------------------------------------------------------------------------------------------------------------------
 //
 ColorToolbarItem::ColorToolbarItem (LDColor color, QToolButton* toolButton) :
--- a/src/mainwindow.h	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/mainwindow.h	Sat Mar 03 15:14:07 2018 +0200
@@ -73,6 +73,7 @@
 	void createBlankDocument();
 	LDDocument* currentDocument();
 	void currentDocumentClosed();
+	QItemSelectionModel* currentSelectionModel();
 	QKeySequence defaultShortcut (QAction* act);
 	void deleteByColor (LDColor color);
 	int deleteSelection();
@@ -95,10 +96,12 @@
 	PrimitiveManager* primitives();
 	Canvas* renderer();
 	void refresh();
+	void replaceSelection(const QItemSelection& selection);
 	bool ringToolHiRes() const;
 	int ringToolSegments() const;
 	bool save (LDDocument* doc, bool saveAs);
 	void saveShortcuts();
+	void select(const QModelIndex& objectIndex);
 	QModelIndexList selectedIndexes() const;
 	QSet<LDObject*> selectedObjects() const;
 	void setQuickColors (const QVector<ColorToolbarItem> &colors);
--- a/src/toolsets/basictoolset.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/toolsets/basictoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -70,15 +70,16 @@
 void BasicToolset::paste()
 {
 	const QString clipboardText = qApp->clipboard()->text();
-	int idx = m_window->suggestInsertPoint();
+	int row = m_window->suggestInsertPoint();
 	mainWindow()->clearSelection();
 	int count = 0;
 
 	for (QString line : clipboardText.split("\n"))
 	{
-		LDObject* pasted = currentDocument()->insertFromString(idx++, line);
-		currentDocument()->addToSelection(pasted);
-		++count;
+		currentDocument()->insertFromString(row, line);
+		mainWindow()->select(currentDocument()->index(row));
+		row += 1;
+		count += 1;
 	}
 
 	print(tr("%1 objects pasted"), count);
@@ -97,9 +98,10 @@
 	{
 		// Get the index of the subfile so we know where to insert the
 		// inlined contents.
-		int idx = reference->lineNumber();
+		QPersistentModelIndex referenceIndex = currentDocument()->index(reference->lineNumber());
+		int row = referenceIndex.row();
 
-		if (idx != -1)
+		if (referenceIndex.isValid())
 		{
 			Model inlined {m_documents};
 			reference->inlineContents(inlined, deep, false);
@@ -107,12 +109,13 @@
 			// Merge in the inlined objects
 			for (LDObject* inlinedObject : inlined.objects())
 			{
-				currentDocument()->insertObject (idx++, inlinedObject);
-				currentDocument()->addToSelection(inlinedObject);
+				currentDocument()->insertObject(row, inlinedObject);
+				mainWindow()->select(currentDocument()->index(row));
+				row += 1;
 			}
 
 			// Delete the subfile now as it's been inlined.
-			currentDocument()->remove(reference);
+			currentDocument()->removeRow(referenceIndex.row());
 		}
 	}
 
@@ -162,7 +165,7 @@
 
 void BasicToolset::insertRaw()
 {
-	int position = m_window->suggestInsertPoint();
+	int row = m_window->suggestInsertPoint();
 
 	QDialog* const dlg = new QDialog;
 	QVBoxLayout* const layout = new QVBoxLayout;
@@ -183,9 +186,9 @@
 
 	for (QString line : QString (inputbox->toPlainText()).split ("\n"))
 	{
-		LDObject* object = currentDocument()->insertFromString(position, line);
-		currentDocument()->addToSelection(object);
-		position += 1;
+		currentDocument()->insertFromString(row, line);
+		mainWindow()->select(currentDocument()->index(row));
+		row += 1;
 	}
 
 	m_window->refresh();
--- a/src/toolsets/filetoolset.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/toolsets/filetoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -124,8 +124,8 @@
 			for (LDObject* object : model.objects())
 			{
 				currentDocument()->insertObject (position, object);
-				currentDocument()->addToSelection(object);
-				position++;
+				mainWindow()->select(currentDocument()->index(position));
+				position += 1;
 			}
 
 			m_window->refresh();
--- a/src/toolsets/viewtoolset.cpp	Fri Mar 02 22:23:53 2018 +0200
+++ b/src/toolsets/viewtoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
@@ -34,8 +34,13 @@
 
 void ViewToolset::selectAll()
 {
-	for (LDObject* obj : currentDocument()->objects())
-		currentDocument()->addToSelection(obj);
+	if (currentDocument()->size() >= 1)
+	{
+		QModelIndex top = currentDocument()->index(0);
+		QModelIndex bottom = currentDocument()->index(currentDocument()->size() - 1);
+		QItemSelection selection {top, bottom};
+		mainWindow()->replaceSelection(selection);
+	}
 }
 
 void ViewToolset::selectByColor()
@@ -51,13 +56,15 @@
 			colors << obj->color();
 	}
 
-	mainWindow()->clearSelection();
+	QItemSelection selection;
 
-	for (LDObject* obj : currentDocument()->objects())
+	for (QModelIndex index : currentDocument()->indices())
 	{
-		if (colors.contains (obj->color()))
-			currentDocument()->addToSelection(obj);
+		if (colors.contains(currentDocument()->lookup(index)->color()))
+			selection.select(index, index);
 	}
+
+	mainWindow()->replaceSelection(selection);
 }
 
 void ViewToolset::selectByType()
@@ -76,10 +83,11 @@
 			subfilenames << static_cast<LDSubfileReference*> (obj)->fileInfo()->name();
 	}
 
-	mainWindow()->clearSelection();
+	QItemSelection selection;
 
-	for (LDObject* obj : currentDocument()->objects())
+	for (QModelIndex index : currentDocument()->indices())
 	{
+		LDObject* obj = currentDocument()->lookup(index);
 		LDObjectType type = obj->type();
 
 		if (not types.contains (type))
@@ -92,8 +100,10 @@
 			continue;
 		}
 
-		currentDocument()->addToSelection(obj);
+		selection.select(index, index);
 	}
+
+	mainWindow()->replaceSelection(selection);
 }
 
 void ViewToolset::resetView()
@@ -259,7 +269,7 @@
 		if (object.isValid() and object.row() < currentDocument()->size())
 		{
 			mainWindow()->clearSelection();
-			currentDocument()->addToSelection(currentDocument()->lookup(object));
+			mainWindow()->select(object);
 		}
 	}
 }

mercurial