src/libraries.h

Thu, 15 Jun 2023 16:18:03 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 15 Jun 2023 16:18:03 +0300
changeset 383
530d23cd4e97
parent 381
80bea7a6e84f
permissions
-rw-r--r--

Refactor, make selecting elements from the model select the corresponding line from the editor as well

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2020 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once
#include <QDir>
#include <QAbstractTableModel>
#include "src/basics.h"
#include "src/colors.h"

class QSettings;

enum class library_role_e
{
	official,
	unofficial,
	working,
};

constexpr static const library_role_e all_library_roles[] =
{
	library_role_e::official,
	library_role_e::unofficial,
	library_role_e::working
};

struct Library
{
	library_role_e role;
	QDir path;
};

QString library_role_name(const library_role_e role);
bool operator==(const Library& one, const Library& other);

Q_DECLARE_METATYPE(Library)
Q_DECLARE_METATYPE(library_role_e)
QDataStream &operator<<(QDataStream&, const Library&);
QDataStream &operator>>(QDataStream&, Library&);
using Libraries = QVector<Library>;

class LibrariesModel : public QAbstractTableModel
{
	Q_OBJECT
public:
	LibrariesModel(QObject* parent = nullptr);
	auto begin() const { return this->libraries.begin(); }
	auto end() const { return this->libraries.end(); }
	QFileInfo findFile(QString fileName) const;
	void addLibrary(const Library& library);
	void removeLibrary(const index_t libraryIndex);
	const Library& library(index_t libraryIndex) const;
	void setLibraryPath(index_t libraryIndex, const QDir& path);
	void setLibraryRole(index_t libraryIndex, const library_role_e role);
	void restoreFromSettings();
	void storeToSettings();
	index_t count() const;
	void moveLibrary(const index_t libraryFromIndex, const index_t libraryToIndex);
	// Definitions for QAbstractTableModel
	Qt::ItemFlags flags(const QModelIndex& index) const override;
	QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
	QVariant headerData(
		int section,
		Qt::Orientation orientation,
		int role = Qt::DisplayRole) const override;
	int rowCount(const QModelIndex&) const override;
	int columnCount(const QModelIndex&) const override;
	bool isValidIndex(const index_t libraryIndex) const;
	ColorTable loadColorTable(QTextStream& errors) const;
private:
	enum Column
	{
		RoleColumn,
		PathColumn
	};
	void signalLibraryChange(const index_t library);
	Libraries libraries;
};

mercurial