src/librariesmodel.cpp

changeset 1308
dcc8c02530c2
child 1326
69a90bd2dba2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/librariesmodel.cpp	Fri Mar 23 12:51:18 2018 +0200
@@ -0,0 +1,156 @@
+#include "librariesmodel.h"
+#include "generics/migrate.h"
+
+LibrariesModel::LibrariesModel(Libraries& libraries, QObject* parent) :
+	QAbstractTableModel {parent},
+	libraries {libraries} {}
+
+QString libraryRoleName(decltype(Library::role) role)
+{
+	switch (role)
+	{
+	case Library::ReadOnlyStorage:
+		return QObject::tr("Storage");
+
+	case Library::UnofficialFiles:
+		return QObject::tr("Unofficial files");
+
+	case Library::WorkingDirectory:
+		return QObject::tr("Working directory");
+
+	default:
+		return "";
+	}
+}
+
+int LibrariesModel::rowCount(const QModelIndex&) const
+{
+	return this->libraries.size();
+}
+
+int LibrariesModel::columnCount(const QModelIndex&) const
+{
+	return 2;
+}
+
+QVariant LibrariesModel::data(const QModelIndex& index, int role) const
+{
+	Column column = static_cast<Column>(index.column());
+
+	if (index.row() >= 0 and index.row() < this->rowCount())
+	{
+		const Library& library = this->libraries[index.row()];
+
+		switch (column)
+		{
+		case PathColumn:
+			switch (role)
+			{
+			case Qt::DisplayRole:
+			case Qt::EditRole:
+				return library.path;
+			}
+			break;
+
+		case RoleColumn:
+			switch (role)
+			{
+			case Qt::DisplayRole:
+				return libraryRoleName(library.role);
+
+			case Qt::EditRole:
+				return library.role;
+			}
+			break;
+		}
+	}
+
+	return {};
+}
+
+bool LibrariesModel::setData(const QModelIndex& index, const QVariant& value, int role)
+{
+	if (index.row() >= 0 and index.row() < this->rowCount({}) and role == Qt::EditRole)
+	{
+		Library& library = this->libraries[index.row()];
+		Column column = static_cast<Column>(index.column());
+
+		switch (column)
+		{
+		case PathColumn:
+			library.path = value.toString();
+			return true;
+
+		case RoleColumn:
+			{
+				int intValue = value.toInt();
+
+				if (intValue >= 0 and intValue < 3)
+				{
+					library.role = static_cast<decltype(Library::role)>(intValue);
+					return true;
+				}
+			}
+			break;
+		}
+	}
+
+	return false;
+}
+
+Qt::ItemFlags LibrariesModel::flags(const QModelIndex& index) const
+{
+	Qt::ItemFlags flags = QAbstractTableModel::flags(index);
+
+	if (index.isValid())
+		flags |= Qt::ItemIsEditable;
+
+	return flags;
+}
+
+bool LibrariesModel::moveRows(
+	const QModelIndex&,
+	int sourceRow,
+	int count,
+	const QModelIndex&,
+	int destinationRow
+) {
+	int sourceRowLast = sourceRow + count - 1;
+	this->beginMoveRows({}, sourceRow, sourceRowLast, {}, destinationRow);
+	::migrate(this->libraries, sourceRow, sourceRowLast, destinationRow);
+	this->endMoveRows();
+	return true;
+}
+
+bool LibrariesModel::removeRows(int row, int count, const QModelIndex&)
+{
+	if (row >= 0 and row + count - 1 < this->rowCount())
+	{
+		this->beginRemoveRows({}, row, row + count - 1);
+		this->libraries.remove(row, count);
+		this->endRemoveRows();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+bool LibrariesModel::insertRows(int startRow, int count, const QModelIndex&)
+{
+	if (startRow >= 0 and startRow <= this->rowCount())
+	{
+		this->beginInsertRows({}, startRow, startRow + count - 1);
+
+		for (int row : range(startRow, startRow + 1, startRow + count - 1))
+			this->libraries.insert(row, {});
+
+		this->endInsertRows();
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}

mercurial