src/libraries.cpp

changeset 259
c27612f0eac0
parent 230
a1f3f7d9078b
child 264
76a025db4948
--- a/src/libraries.cpp	Wed Jun 22 23:51:06 2022 +0300
+++ b/src/libraries.cpp	Sun Jun 26 19:44:45 2022 +0300
@@ -30,24 +30,6 @@
 }
 
 /**
- * @brief Yields a begin-terator for the libraries
- * @return iterator
- */
-QVector<Library>::const_iterator LibrariesModel::begin() const
-{
-	return this->libraries.begin();
-}
-
-/**
- * @brief Yields an end-iterator for the libraries
- * @return iterator
- */
-QVector<Library>::const_iterator LibrariesModel::end() const
-{
-	return this->libraries.end();
-}
-
-/**
  * @brief Searches the libraries for the specified file name.
  * @param fileName File to search for
  * @return Full path to the file, or empty string if not found.
@@ -63,7 +45,7 @@
 		{
 			QDir directory = library.path;
 			directory.cd(subdirectory);
-			QFileInfo fileInfo = directory.absoluteFilePath(fileName);
+			QFileInfo fileInfo{directory.absoluteFilePath(fileName)};
 			if (fileInfo.exists() && fileInfo.isFile())
 			{
 				path = fileInfo.absoluteFilePath();
@@ -86,7 +68,7 @@
 void LibrariesModel::addLibrary(const Library& library)
 {
 	Q_EMIT layoutAboutToBeChanged();
-	libraries.append(library);
+	this->libraries.push_back(library);
 	Q_EMIT layoutChanged();
 }
 
@@ -94,13 +76,13 @@
  * @brief Removes a library by index. Does nothing if the index is not valid.
  * @param libraryIndex Index of the library
  */
-void LibrariesModel::removeLibrary(const int libraryIndex)
+void LibrariesModel::removeLibrary(const std::size_t libraryIndex)
 {
 	Q_ASSERT(isValidIndex(libraryIndex));
 	if (isValidIndex(libraryIndex))
 	{
 		Q_EMIT layoutAboutToBeChanged();
-		libraries.remove(libraryIndex);
+		this->libraries.erase(this->libraries.begin() + static_cast<long>(libraryIndex));
 		Q_EMIT layoutChanged();
 	}
 }
@@ -111,7 +93,7 @@
  * @param libraryIndex Index of the library
  * @return const reference
  */
-const Library& LibrariesModel::library(int libraryIndex) const
+const Library& LibrariesModel::library(std::size_t libraryIndex) const
 {
 	Q_ASSERT(isValidIndex(libraryIndex));
 	return this->libraries[libraryIndex];
@@ -122,7 +104,7 @@
  * @param libraryIndex Index of the library
  * @param path New path
  */
-void LibrariesModel::setLibraryPath(int libraryIndex, const QDir& path)
+void LibrariesModel::setLibraryPath(std::size_t libraryIndex, const QDir& path)
 {
 	if (this->isValidIndex(libraryIndex))
 	{
@@ -136,7 +118,7 @@
  * @param libraryIndex Index of the library
  * @param role New role
  */
-void LibrariesModel::setLibraryRole(int libraryIndex, const Library::Role role)
+void LibrariesModel::setLibraryRole(std::size_t libraryIndex, const Library::Role role)
 {
 	if (this->isValidIndex(libraryIndex))
 	{
@@ -167,12 +149,12 @@
 /**
  * @returns the amount of libraries
  */
-int LibrariesModel::count() const
+std::size_t LibrariesModel::count() const
 {
 	return this->libraries.size();
 }
 
-void LibrariesModel::moveLibrary(const int libraryFromIndex, const int libraryToIndex)
+void LibrariesModel::moveLibrary(const std::size_t libraryFromIndex, const std::size_t libraryToIndex)
 {
 	if (isValidIndex(libraryFromIndex) and
 		(isValidIndex(libraryToIndex) or libraryToIndex == count()) and
@@ -180,15 +162,17 @@
 	{
 		Q_EMIT layoutAboutToBeChanged();
 		const Library library = this->library(libraryFromIndex);
-		if (libraryToIndex > libraryFromIndex)
+		const auto from = this->libraries.begin() + signed_cast(libraryFromIndex);
+		const auto to = this->libraries.begin() + signed_cast(libraryToIndex);
+		if (to > from)
 		{
-			this->libraries.insert(libraryToIndex, library);
-			this->libraries.removeAt(libraryFromIndex);
+			this->libraries.insert(to, library);
+			this->libraries.erase(from);
 		}
 		else if (libraryToIndex < libraryFromIndex)
 		{
-			this->libraries.removeAt(libraryFromIndex);
-			this->libraries.insert(libraryToIndex, library);
+			this->libraries.erase(from);
+			this->libraries.insert(to, library);
 		}
 		Q_EMIT layoutChanged();
 	}
@@ -199,7 +183,7 @@
  * @param libraryIndex Index to check
  * @returns whether or not it is valid
  */
-bool LibrariesModel::isValidIndex(const int libraryIndex) const
+bool LibrariesModel::isValidIndex(const std::size_t libraryIndex) const
 {
 	return libraryIndex >= 0 && libraryIndex < this->libraries.size();
 }
@@ -275,7 +259,7 @@
 	{
 		const int row = index.row();
 		const Column column = static_cast<Column>(index.column());
-		const Library& library = this->library(row);
+		const Library& library = this->library(static_cast<std::size_t>(row));
 		switch (column)
 		{
 		case RoleColumn:
@@ -320,7 +304,7 @@
  */
 int LibrariesModel::rowCount(const QModelIndex&) const
 {
-	return this->count();
+	return static_cast<int>(this->count());
 }
 
 /**
@@ -337,11 +321,11 @@
  * to update the table widget know when libraries are changed.
  * @param libraryIndex Index of the library.
  */
-void LibrariesModel::signalLibraryChange(int libraryIndex)
+void LibrariesModel::signalLibraryChange(std::size_t libraryIndex)
 {
 	Q_ASSERT(isValidIndex(libraryIndex));
-	const QModelIndex topLeft = this->index(libraryIndex, 0);
-	const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1);
+	const QModelIndex topLeft = this->index(static_cast<int>(libraryIndex), 0);
+	const QModelIndex bottomRight = this->index(static_cast<int>(libraryIndex), columnCount({}) - 1);
 	Q_EMIT dataChanged(topLeft, bottomRight);
 }
 
@@ -378,3 +362,26 @@
 {
 	return one.role == other.role and one.path == other.path;
 }
+
+/*
+QDataStream &operator<<(QDataStream& stream, const Libraries& libs)
+{
+	const auto size = static_cast<std::uint32_t>(libs.size());
+	stream << size;
+	for (std::uint32_t i = 0; i < size; ++i) {
+		stream << libs[i];
+	}
+	return stream;
+}
+
+QDataStream &operator>>(QDataStream& stream, Libraries& libs)
+{
+	std::uint32_t size;
+	stream >> size;
+	libs.resize(size);
+	for (std::uint32_t i = 0; i < size; ++i) {
+		stream >> libs[i];
+	}
+	return stream;
+}
+*/

mercurial