Worked more on the library collection thing

Sun, 07 May 2017 13:29:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 07 May 2017 13:29:58 +0300
changeset 1212
851ab72bb850
parent 1211
83b8ed708909
child 1360
c20bd2837e9d

Worked more on the library collection thing

src/basics.h file | annotate | diff | comparison | revisions
src/dialogs/configdialog.ui file | annotate | diff | comparison | revisions
src/documentmanager.cpp file | annotate | diff | comparison | revisions
src/librarycollection.cpp file | annotate | diff | comparison | revisions
src/librarycollection.h file | annotate | diff | comparison | revisions
--- a/src/basics.h	Mon Mar 27 14:56:05 2017 +0300
+++ b/src/basics.h	Sun May 07 13:29:58 2017 +0300
@@ -36,6 +36,7 @@
 
 template<typename T, typename R>
 using Pair = std::pair<T, R>;
+using std::vector;
 
 enum Axis
 {
--- a/src/dialogs/configdialog.ui	Mon Mar 27 14:56:05 2017 +0300
+++ b/src/dialogs/configdialog.ui	Sun May 07 13:29:58 2017 +0300
@@ -51,6 +51,11 @@
        </item>
        <item>
         <property name="text">
+         <string>Libraries</string>
+        </property>
+       </item>
+       <item>
+        <property name="text">
          <string>Shortcuts</string>
         </property>
        </item>
@@ -79,7 +84,7 @@
      <item>
       <widget class="QStackedWidget" name="m_pages">
        <property name="currentIndex">
-        <number>0</number>
+        <number>3</number>
        </property>
        <widget class="QWidget" name="page_3">
         <layout class="QVBoxLayout" name="verticalLayout_13">
@@ -533,6 +538,22 @@
          </item>
         </layout>
        </widget>
+       <widget class="QWidget" name="page_2">
+        <layout class="QVBoxLayout" name="verticalLayout_11">
+         <item>
+          <widget class="QGroupBox" name="groupBox_8">
+           <property name="title">
+            <string>Libraries</string>
+           </property>
+           <layout class="QVBoxLayout" name="verticalLayout_12">
+            <item>
+             <widget class="QTableView" name="libraries"/>
+            </item>
+           </layout>
+          </widget>
+         </item>
+        </layout>
+       </widget>
        <widget class="QWidget" name="page_5">
         <layout class="QVBoxLayout" name="verticalLayout_15">
          <item>
--- a/src/documentmanager.cpp	Mon Mar 27 14:56:05 2017 +0300
+++ b/src/documentmanager.cpp	Sun May 07 13:29:58 2017 +0300
@@ -41,7 +41,7 @@
 	m_logoedStud (nullptr),
 	m_logoedStud2 (nullptr)
 {
-	m_libraries.addLibrary(configuration().lDrawPath(), "LDraw library");
+	m_libraries.addLibrary(configuration().lDrawPath(), "LDraw library", true);
 }
 
 DocumentManager::~DocumentManager()
--- a/src/librarycollection.cpp	Mon Mar 27 14:56:05 2017 +0300
+++ b/src/librarycollection.cpp	Sun May 07 13:29:58 2017 +0300
@@ -20,9 +20,9 @@
 
 const QStringList LibraryCollection::directoryNames = {"parts", "p"};
 
-void LibraryCollection::addLibrary(QDir dir, QString name)
+void LibraryCollection::addLibrary(QDir dir, QString name, bool frozen)
 {
-	Super::append({dir, name});
+	_libraries.emplace_back(dir, name, frozen);
 }
 
 /*
@@ -34,11 +34,11 @@
 	// slashes so that Qt understands them on all platforms.
 	relativePath.replace('\\', '/');
 
-	for (const Library& library : *this)
+	for (const Library& library : _libraries)
 	{
 		for (const QString& directoryName : directoryNames)
 		{
-			QDir dir = library.path;
+			QDir dir = library.path();
 
 			if (dir.exists() and dir.cd(directoryName) and dir.exists(relativePath))
 				return QDir::cleanPath(dir.absoluteFilePath(relativePath));
@@ -63,7 +63,7 @@
  */
 QString LibraryCollection::find(const FileSpec& spec) const
 {
-	for (const Library& library : *this)
+	for (const Library& library : _libraries)
 	{
 		try
 		{
@@ -78,14 +78,22 @@
 }
 
 /*
+ * Constructs a new library.
+ */
+Library::Library(const QDir& path, const QString& name, bool frozen) :
+    _path {path},
+    _name {name},
+    _frozen {frozen} {}
+
+/*
  * Returns the appropriate subdirectory for the given file type.
  */
 QDir Library::subdirectory(FileType type) const
 {
 	QString subdirectory = subdirectoryName(type);
 
-	if (path.exists(subdirectory))
-		return path.absolutePath() + "/" + subdirectory;
+	if (path().exists(subdirectory))
+		return path().absolutePath() + "/" + subdirectory;
 	else
 		throw SubdirectoryNotFoundError {};
 }
@@ -115,3 +123,25 @@
 
 	return "";
 }
+
+QDir Library::path() const
+{
+	return _path;
+}
+
+std::vector<Library>::iterator LibraryCollection::begin()
+{
+	return _libraries.begin();
+}
+
+std::vector<Library>::iterator LibraryCollection::end()
+{
+	return _libraries.end();
+}
+
+LibraryCollectionTableModel::LibraryCollectionTableModel(LibraryCollection& collection, QObject* parent) :
+    QAbstractTableModel {parent},
+    _collection {collection}
+{
+
+}
--- a/src/librarycollection.h	Mon Mar 27 14:56:05 2017 +0300
+++ b/src/librarycollection.h	Sun May 07 13:29:58 2017 +0300
@@ -20,8 +20,12 @@
 #include <QDir>
 #include "main.h"
 
-struct Library
+/*
+ * Models an LDraw library.
+ */
+class Library
 {
+public:
 	class SubdirectoryNotFoundError : public std::exception {};
 
 	enum FileType
@@ -33,18 +37,25 @@
 		LowResolutionPrimitive,
 	};
 
-	QDir path;
-	QString name;
+	Library(const QDir& path, const QString& name, bool frozen);
 
+	bool frozen() const;
+	QDir path() const;
+	QString name() const;
 	QDir subdirectory(FileType type) const;
 	static QString subdirectoryName(FileType type);
+
+private:
+	QDir _path;
+	QString _name;
+	bool _frozen;
 };
 
 /*
  * Models a collection of libraries. A library is a folder containing directories named "p" and
  * "parts" that contains part and subpart files.
  */
-class LibraryCollection : public QObject, private QVector<Library>
+class LibraryCollection : public QObject
 {
 	Q_OBJECT
 	using Super = QVector<Library>;
@@ -56,10 +67,25 @@
 		Library::FileType type;
 	};
 
-	void addLibrary(QDir dir, QString name);
+	void addLibrary(QDir dir, QString name, bool frozen);
 	QString find(QString relativePath) const;
 	QString find(const QString& name, Library::FileType type) const;
 	QString find(const FileSpec& spec) const;
+	std::vector<Library>::iterator begin();
+	std::vector<Library>::iterator end();
 
 	static const QStringList directoryNames;
+
+private:
+	std::vector<Library> _libraries;
 };
+
+#include <QAbstractTableModel>
+class LibraryCollectionTableModel : public QAbstractTableModel
+{
+public:
+	LibraryCollectionTableModel(LibraryCollection& collection, QObject* parent = nullptr);
+
+private:
+	LibraryCollection& _collection;
+};

mercurial