removed LDObject::lineNumber

Sat, 03 Mar 2018 16:53:56 +0200

author
Santeri Piippo
date
Sat, 03 Mar 2018 16:53:56 +0200
changeset 1258
f5921a474d57
parent 1257
0d42a1ebd954
child 1259
0384b1d49786

removed LDObject::lineNumber

src/lddocument.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.cpp file | annotate | diff | comparison | revisions
src/linetypes/modelobject.h 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
src/toolsets/algorithmtoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/basictoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/movetoolset.cpp file | annotate | diff | comparison | revisions
src/toolsets/viewtoolset.cpp file | annotate | diff | comparison | revisions
--- a/src/lddocument.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/lddocument.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -303,11 +303,11 @@
 void LDDocument::objectChanged(QString before, QString after)
 {
 	LDObject* object = static_cast<LDObject*>(sender());
-	addToHistory(new EditHistoryEntry {object->lineNumber(), before, after});
+	QModelIndex index = this->indexOf(object);
+	addToHistory(new EditHistoryEntry {index.row(), before, after});
 	redoVertices();
 	emit objectModified(object);
-	int linenumber = object->lineNumber();
-	emit dataChanged(index(linenumber), index(linenumber));
+	emit dataChanged(index, index);
 }
 
 LDObject* LDDocument::withdrawAt(int position)
--- a/src/linetypes/modelobject.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/linetypes/modelobject.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -271,38 +271,11 @@
 
 // =============================================================================
 //
-// Index (i.e. line number) of this object
-//
-int LDObject::lineNumber() const
-{
-	if (model())
-	{
-		for (int i = 0; i < model()->size(); ++i)
-		{
-			if (model()->getObject(i) == this)
-				return i;
-		}
-	}
-
-	return -1;
-}
-
-// =============================================================================
-//
 // Object after this in the current file
 //
-LDObject* LDObject::next() const
+LDObject* LDObject::next()
 {
-	return model()->getObject(lineNumber() + 1);
-}
-
-// =============================================================================
-//
-// Object prior to this in the current file
-//
-LDObject* LDObject::previous() const
-{
-	return model()->getObject(lineNumber() - 1);
+	return model()->getObject(model()->indexOf(this).row() + 1);
 }
 
 // =============================================================================
--- a/src/linetypes/modelobject.h	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/linetypes/modelobject.h	Sat Mar 03 16:53:56 2018 +0200
@@ -72,13 +72,11 @@
 	bool isHidden() const;
 	virtual bool isScemantic() const; // Does this object have meaning in the part model?
 	bool isSelected() const;
-	int lineNumber() const;
 	void move (Vertex vect);
-	LDObject* next() const;
+	LDObject* next();
 	virtual int numVertices() const;
 	virtual int numPolygonVertices() const;
 	virtual QString objectListText() const;
-	LDObject* previous() const;
 	QColor randomColor() const;
 	void setColor (LDColor color);
 	void setHidden (bool value);
--- a/src/mainwindow.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/mainwindow.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -379,8 +379,8 @@
 {
 	// If we have a selection, put the item after it.
 	// TODO: fix this properly!
-	if (not selectedObjects().isEmpty())
-		return (*(selectedObjects().end() - 1))->lineNumber() + 1;
+	if (not selectedIndexes().isEmpty())
+		return (*(selectedIndexes().end() - 1)).row() + 1;
 
 	// Otherwise place the object at the end.
 	return m_currentDocument->size();
--- a/src/model.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/model.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -130,7 +130,8 @@
  */
 void Model::remove(LDObject* object)
 {
-	int position = object->lineNumber();
+	int position = indexOf(object).row();
+
 	if (_objects[position] == object)
 		removeAt(position);
 }
@@ -152,16 +153,16 @@
 /*
  * Replaces the given object with the contents of a model.
  */
-void Model::replace(LDObject *object, Model &model)
+void Model::replace(LDObject* object, Model& model)
 {
-	if (object->model() == this)
+	QModelIndex index = this->indexOf(object);
+
+	if (index.isValid())
 	{
-		int position = object->lineNumber();
+		for (int i = countof(model.objects()) - 1; i >= 1; i -= 1)
+			insertObject(index.row() + i, model.objects()[i]);
 
-		for (int i = countof(model.objects()) - 1; i >= 1; i -= 1)
-			insertObject(position + i, model.objects()[i]);
-
-		setObjectAt(position, model.objects()[0]);
+		setObjectAt(index.row(), model.objects()[0]);
 	}
 }
 
@@ -248,13 +249,10 @@
  */
 void Model::withdraw(LDObject* object)
 {
-	if (object->model() == this)
-	{
-		int position = object->lineNumber();
+	QModelIndex index = this->indexOf(object);
 
-		if (_objects[position] == object)
-			withdrawAt(position);
-	}
+	if (index.isValid())
+		withdrawAt(index.row());
 }
 
 /*
@@ -280,6 +278,20 @@
 }
 
 /*
+ * Returns an index that corresponds to the given object
+ */
+QModelIndex Model::indexOf(LDObject* object) const
+{
+	for (int i = 0; i < this->size(); ++i)
+	{
+		if (this->getObject(i) == object)
+			return index(i);
+	}
+
+	return {};
+}
+
+/*
  * Returns the model's associated document manager. This pointer is used to resolve subfile references.
  */
 DocumentManager* Model::documentManager() const
@@ -524,11 +536,12 @@
  */
 LDObject* Model::replaceWithFromString(LDObject* object, QString line)
 {
-	if (object and object->model() == this)
+	QModelIndex index = this->indexOf(object);
+
+	if (index.isValid())
 	{
-		int position = object->lineNumber();
-		removeAt(position);
-		return insertFromString(position, line);
+		removeAt(index.row());
+		return insertFromString(index.row(), line);
 	}
 	else
 		return nullptr;
--- a/src/model.h	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/model.h	Sat Mar 03 16:53:56 2018 +0200
@@ -107,6 +107,7 @@
 	int triangleCount() const;
 	QVector<LDObject*>::iterator begin();
 	QVector<LDObject*>::iterator end();
+	QModelIndex indexOf(LDObject* object) const;
 	bool isEmpty() const;
 	class DocumentManager* documentManager() const;
 	LDObject* insertFromString(int position, QString line);
@@ -180,9 +181,9 @@
 {
 	if (object->model() == this)
 	{
-		int position = object->lineNumber();
+		QModelIndex position = indexOf(object);
 		T* replacement = constructObject<T>(args...);
-		setObjectAt(position, replacement);
+		setObjectAt(position.row(), replacement);
 		return replacement;
 	}
 	else
--- a/src/toolsets/algorithmtoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/toolsets/algorithmtoolset.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -50,8 +50,10 @@
 {
 	int count = 0;
 
-	for (LDObject* object : selectedObjects().toList())
+	for (LDObject* object : selectedObjects())
 	{
+		QModelIndex index = currentDocument()->indexOf(object);
+
 		if (object->numVertices() != 4)
 			continue;
 
@@ -61,19 +63,14 @@
 		Vertex v3 = object->vertex(3);
 		LDColor color = object->color();
 
-		// Find the index of this quad
-		int index = object->lineNumber();
-		if (index == -1)
-			continue;
-
 		// Create the two triangles based on this quadrilateral:
 		// 0───3       0───3    3
 		// │   │  --→  │  ╱    ╱│
 		// │   │  --→  │ ╱    ╱ │
 		// │   │  --→  │╱    ╱  │
 		// 1───2       1    1───2
-		LDTriangle* triangle1 = currentDocument()->emplaceReplacementAt<LDTriangle>(index, v0, v1, v3);
-		LDTriangle* triangle2 = currentDocument()->emplaceAt<LDTriangle>(index + 1, v1, v2, v3);
+		LDTriangle* triangle1 = currentDocument()->emplaceReplacementAt<LDTriangle>(index.row(), v0, v1, v3);
+		LDTriangle* triangle2 = currentDocument()->emplaceAt<LDTriangle>(index.row() + 1, v1, v2, v3);
 
 		// The triangles also inherit the quad's color
 		triangle1->setColor(color);
@@ -142,7 +139,7 @@
 		}
 
 		count += countof(lines.objects());
-		currentDocument()->merge(lines, object->lineNumber() + 1);
+		currentDocument()->merge(lines, currentDocument()->indexOf(object).row() + 1);
 	}
 
 	print(tr("Added %1 border lines"), count);
@@ -368,7 +365,7 @@
 		prevIsHistory = ishistory;
 	}
 
-	int idx = obj ? obj->lineNumber() : 0;
+	int idx = obj ? currentDocument()->indexOf(obj).row() : 0;
 
 	// Create the comment object based on input
 	currentDocument()->emplaceAt<LDComment>(idx++, format("!HISTORY %1 [%2] %3",
@@ -541,7 +538,7 @@
 	{
 		// Where to insert the subfile reference?
 		// TODO: the selection really should be sorted by position...
-		int referencePosition = (*selectedObjects().begin())->lineNumber();
+		int referencePosition = m_window->selectedIndexes().begin()->row();
 
 		// Save was successful. Delete the original selection now from the
 		// main document.
--- a/src/toolsets/basictoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/toolsets/basictoolset.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -98,7 +98,7 @@
 	{
 		// Get the index of the subfile so we know where to insert the
 		// inlined contents.
-		QPersistentModelIndex referenceIndex = currentDocument()->index(reference->lineNumber());
+		QPersistentModelIndex referenceIndex = currentDocument()->indexOf(reference);
 		int row = referenceIndex.row();
 
 		if (referenceIndex.isValid())
--- a/src/toolsets/movetoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/toolsets/movetoolset.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -47,7 +47,7 @@
 	{
 		LDObject* obj = objs[i];
 
-		int idx = obj->lineNumber();
+		int idx = model->indexOf(obj).row();
 		int target = idx + (up ? -1 : 1);
 
 		if ((up and idx == 0) or (not up and idx == countof(model->objects()) - 1))
--- a/src/toolsets/viewtoolset.cpp	Sat Mar 03 15:14:07 2018 +0200
+++ b/src/toolsets/viewtoolset.cpp	Sat Mar 03 16:53:56 2018 +0200
@@ -248,8 +248,8 @@
 	bool ok;
 	int defaultValue = 0;
 
-	if (countof(selectedObjects()) == 1)
-		defaultValue = (*selectedObjects().begin())->lineNumber();
+	if (countof(m_window->selectedIndexes()) == 1)
+		defaultValue = (*m_window->selectedIndexes().begin()).row();
 
 	int row = QInputDialog::getInt(
 		nullptr, /* parent */

mercurial