src/model.cpp

changeset 250
2837b549e616
parent 242
16855456992d
child 251
94b0a30a1886
--- a/src/model.cpp	Wed Jun 22 16:13:15 2022 +0300
+++ b/src/model.cpp	Wed Jun 22 16:53:35 2022 +0300
@@ -47,9 +47,9 @@
 	if (circ.fraction.divisions != 16) {
 		result += QString::number(circ.fraction.divisions) + QStringLiteral("\\");
 	}
-	const int factor = gcd(circ.fraction.segments, circ.fraction.divisions);
-	int num = circ.fraction.segments / factor;
-	int denom = circ.fraction.divisions / factor;
+	const unsigned int factor = gcd(circ.fraction.segments, circ.fraction.divisions);
+	unsigned int num = circ.fraction.segments / factor;
+	unsigned int denom = circ.fraction.divisions / factor;
 	if (denom < 4) {
 		num *= 4 / denom;
 		denom = 4;
@@ -175,30 +175,31 @@
 
 ModelId Model::append(const ModelElement &value)
 {
-	const int position = static_cast<int>(this->body.size());
+	const std::size_t position = this->size();
 	const ModelId id = this->runningId;
 	this->runningId.value += 1;
-	Q_EMIT this->beginInsertRows({}, position, position);
+	const int row = narrow<int>(signed_cast(this->size()));
+	Q_EMIT this->beginInsertRows({}, row, row);
 	this->body.push_back({value, id});
 	this->positions[id] = position;
 	Q_EMIT this->endInsertRows();
 	return id;
 }
 
-const ModelElement &Model::at(int position) const
+const ModelElement &Model::at(std::size_t position) const
 {
 	return this->body[position].data;
 }
 
-ModelId Model::idAt(int position) const
+ModelId Model::idAt(std::size_t position) const
 {
 	return this->body[position].id;
 }
 
-void Model::assignAt(int position, const ModelElement &element)
+void Model::assignAt(std::size_t position, const ModelElement &element)
 {
 	this->body[position].data = element;
-	const QModelIndex index = this->index(position);
+	const QModelIndex index = this->index(narrow<int>(signed_cast(position)));
 	Q_EMIT this->dataChanged(index, index);
 }
 
@@ -216,13 +217,14 @@
 	}
 }
 
-void Model::remove(int index)
+void Model::remove(const std::size_t index)
 {
-	if (index >= 0 and index < this->size()) {
-		Q_EMIT this->beginRemoveRows({}, index, index);
+	if (index <  this->body.size()) {
+		const int row = narrow<int>(signed_cast(index));
+		Q_EMIT this->beginRemoveRows({}, row, row);
 		removeFromMap(this->positions, this->body[index].id);
-		this->body.erase(this->body.begin() + index);
-		for (int i = index; i < this->size(); ++i) {
+		this->body.erase(this->body.begin() + row);
+		for (std::size_t i = index; i < this->body.size(); ++i) {
 			this->positions[this->body[i].id] = i;
 		}
 		Q_EMIT this->endRemoveRows();
@@ -231,12 +233,12 @@
 
 int Model::rowCount(const QModelIndex &) const
 {
-	return this->size();
+	return narrow<int>(signed_cast(this->size()));
 }
 
 QVariant Model::data(const QModelIndex &index, int role) const
 {
-	const int i = index.row();
+	const std::size_t i = unsigned_cast(index.row());
 	const ModelElement& element = this->body[i].data;
 	switch(role)
 	{
@@ -257,12 +259,12 @@
 	}
 }
 
-const ModelElement &Model::operator[](int index) const
+const ModelElement &Model::operator[](std::size_t index) const
 {
 	return this->body[index].data;
 }
 
-int Model::size() const
+std::size_t Model::size() const
 {
 	return this->body.size();
 }
@@ -270,7 +272,7 @@
 void save(const Model &model, QIODevice *device)
 {
 	QTextStream out{device};
-	for (int i = 0; i < model.size(); ++i) {
+	for (std::size_t i = 0; i < model.size(); ++i) {
 		out << modelElementToString(model[i]) << "\r\n";
 	}
 }

mercurial