src/libraries.cpp

Wed, 01 Jan 2020 17:45:56 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 01 Jan 2020 17:45:56 +0200
changeset 21
0133e565e072
parent 12
fe67489523b5
child 22
6da867fa5429
permissions
-rw-r--r--

things

7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QSettings>
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "libraries.h"
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
4 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
5 * @brief Constructs a new library manager
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
6 * @param parent Parent object
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
7 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 LibraryManager::LibraryManager(QObject* parent):
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QAbstractTableModel{parent}
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
13 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
14 * @brief Constructs a library manager from settings
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
15 * @param settings Settings to construct from
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
16 * @param parent Parent object
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
17 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 LibraryManager::LibraryManager(QSettings* settings, QObject* parent) :
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 QAbstractTableModel{parent}
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 this->restoreFromSettings(settings);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
24 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
25 * @brief Yields a begin-terator for the libraries
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
26 * @return iterator
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
27 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 QVector<Library>::const_iterator LibraryManager::begin() const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 return this->libraries.begin();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
33 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
34 * @brief Yields an end-iterator for the libraries
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
35 * @return iterator
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
36 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 QVector<Library>::const_iterator LibraryManager::end() const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 return this->libraries.end();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 /**
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 * @brief Searches the libraries for the specified file name.
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 * @param fileName File to search for
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 * @return Full path to the file, or empty string if not found.
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 */
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
47 QString LibraryManager::findFile(QString fileName) const
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
49 QString path;
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 fileName.replace("\\", "/");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 bool found = false;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 for (const Library& library : this->libraries)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 {
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
54 for (const QString& subdirectory : {"parts", "p"})
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 {
12
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
56 QDir directory = library.path;
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
57 directory.cd(subdirectory);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
58 QFileInfo fileInfo = directory.absoluteFilePath(fileName);
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
59 if (fileInfo.exists() && fileInfo.isFile())
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
60 {
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
61 path = fileInfo.absoluteFilePath();
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
62 found = true;
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
63 break;
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
64 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
65 }
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
66 if (found)
fe67489523b5 added dependency loading
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
67 {
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 break;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return path;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
74 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
75 * @brief Adds a new library to the end of the libraries list.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
76 * @param library Library to add
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
77 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 void LibraryManager::addLibrary(const Library& library)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 emit layoutAboutToBeChanged();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 libraries.append(library);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 emit layoutChanged();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
85 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
86 * @brief Removes a library by index. Does nothing if the index is not valid.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
87 * @param libraryIndex Index of the library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
88 */
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
89 void LibraryManager::removeLibrary(const int libraryIndex)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
90 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
91 Q_ASSERT(isValidIndex(libraryIndex));
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
92 if (isValidIndex(libraryIndex))
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
93 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
94 emit layoutAboutToBeChanged();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
95 libraries.remove(libraryIndex);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
96 emit layoutChanged();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
97 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
98 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
99
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
100 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
101 * @brief Gets a library by index.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
102 * @warning Index is assumed to be valid.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
103 * @param libraryIndex Index of the library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
104 * @return const reference
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
105 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 const Library& LibraryManager::library(int libraryIndex) const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
108 Q_ASSERT(isValidIndex(libraryIndex));
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 return this->libraries[libraryIndex];
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
112 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
113 * @brief Changes the path of the specified library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
114 * @param libraryIndex Index of the library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
115 * @param path New path
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
116 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119 if (this->isValidIndex(libraryIndex))
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 this->libraries[libraryIndex].path = path;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 this->signalLibraryChange(libraryIndex);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
126 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
127 * @brief Changes the role of the specified library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
128 * @param libraryIndex Index of the library
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
129 * @param role New role
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
130 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 if (this->isValidIndex(libraryIndex))
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 this->libraries[libraryIndex].role = role;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 this->signalLibraryChange(libraryIndex);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
140 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
141 * @brief Restores the libraries from the specified settings object. All unsaved
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
142 * changes are lost.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
143 * @param settings Settings object to restore from.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
144 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 void LibraryManager::restoreFromSettings(QSettings* settings)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 this->libraries = settings->value("libraries").value<Libraries>();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
150 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
151 * @brief Saves the libraries to the specified settings object.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
152 * @param settings Settings object to modify.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
153 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 void LibraryManager::storeToSettings(QSettings* settings)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 QVariant librariesValue = QVariant::fromValue(this->libraries);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 settings->setValue("libraries", librariesValue);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
160 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
161 * @returns the amount of libraries
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
162 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
163 int LibraryManager::count() const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
164 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
165 return this->libraries.size();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
166 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
167
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
168 void LibraryManager::moveLibrary(const int libraryFromIndex, const int libraryToIndex)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
169 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
170 if (isValidIndex(libraryFromIndex) and
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
171 (isValidIndex(libraryToIndex) or libraryToIndex == count()) and
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
172 libraryFromIndex != libraryToIndex)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
173 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
174 emit layoutAboutToBeChanged();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
175 const Library library = this->library(libraryFromIndex);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
176 if (libraryToIndex > libraryFromIndex)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
177 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
178 this->libraries.insert(libraryToIndex, library);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
179 this->libraries.removeAt(libraryFromIndex);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
180 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
181 else if (libraryToIndex < libraryFromIndex)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
182 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
183 this->libraries.removeAt(libraryFromIndex);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
184 this->libraries.insert(libraryToIndex, library);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
185 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
186 emit layoutChanged();
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
187 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
188 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
189
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
190 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
191 * @brief Checks whether the specified index points to a valid library.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
192 * @param libraryIndex Index to check
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
193 * @returns whether or not it is valid
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
194 */
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
195 bool LibraryManager::isValidIndex(const int libraryIndex) const
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
196 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
197 return libraryIndex >= 0 && libraryIndex < this->libraries.size();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
198 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
199
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
200 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
201 * @brief Gets a human-readable string for the specified role
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
202 * @param role Role to get a string for
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
203 * @returns string
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
204 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
205 QString Library::libraryRoleName(const Role role)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
206 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
207 switch (role)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
208 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
209 case Library::OfficialLibrary:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
210 return LibraryManager::tr("Official library");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
211 case Library::UnofficialLibrary:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
212 return LibraryManager::tr("Unofficial library");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
213 case Library::WorkingLibrary:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
214 return LibraryManager::tr("Working library");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
215 default:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
216 return "???";
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
217 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
218 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
219
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
220 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
221 * @brief Overload necessary to implement QAbstractTableModel
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
222 * @param index
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
223 * @return Item flags
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
224 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
225 Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
226 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
227 Q_UNUSED(index);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
228 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
229 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
230
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
231 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
232 * @brief Returns data needed to represent the libraries in a table. This function
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
233 * decides how data is represented in a table view.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
234 * @param index Index to get data for
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
235 * @param role Role of the data (role as in Qt model-view role, not LDraw library role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
236 * @returns variant
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
237 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
238 QVariant LibraryManager::data(const QModelIndex& index, int role) const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
239 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
240 if (role != Qt::DisplayRole)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
241 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
242 return {};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
243 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
244 else
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
245 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
246 const int row = index.row();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
247 const Column column = static_cast<Column>(index.column());
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
248 const Library& library = this->library(row);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
249 switch (column)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
250 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
251 case RoleColumn:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
252 return Library::libraryRoleName(library.role);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
253 case PathColumn:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
254 return library.path.absolutePath();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
255 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
256 return {};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
257 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
258 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
259
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
260 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
261 * @brief Returns header texts for a table view. We only implement column headers here.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
262 * @param section Index of the column (can also be a row but we only have column headers)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
263 * @param orientation Orientation (we only support horizontal orientation)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
264 * @param role Role of the data (role as in Qt model-view role, not LDraw library role)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
265 * @returns variant
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
266 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
267 QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
268 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
269 if (orientation == Qt::Horizontal and role == Qt::DisplayRole)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
270 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
271 switch(static_cast<Column>(section))
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
272 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
273 case PathColumn:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
274 return tr("Path");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
275 case RoleColumn:
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
276 return tr("Role");
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
277 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
278 return {};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
279 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
280 else
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
281 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
282 return {};
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
283 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
284 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
285
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
286 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
287 * @brief Overload necessary to implement QAbstractTableModel.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
288 * @returns how many rows there are in the table. Since there is one row per library, this returns
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
289 * the amount of libraries.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
290 */
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
291 int LibraryManager::rowCount(const QModelIndex&) const
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
292 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
293 return this->count();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
294 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
295
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
296 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
297 * @brief LibraryManager::columnCount
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
298 * @returns how many columns there are in the table.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
299 */
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
300 int LibraryManager::columnCount(const QModelIndex&) const
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
301 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
302 return 2;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
303 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
304
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
305 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
306 * @brief Signals view objects that the specified library has changed. This is necessary
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
307 * to update the table widget know when libraries are changed.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
308 * @param libraryIndex Index of the library.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
309 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
310 void LibraryManager::signalLibraryChange(int libraryIndex)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
311 {
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
312 Q_ASSERT(isValidIndex(libraryIndex));
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
313 const QModelIndex topLeft = this->index(libraryIndex, 0);
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
314 const QModelIndex bottomRight = this->index(libraryIndex, columnCount({}) - 1);
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
315 emit dataChanged(topLeft, bottomRight);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
316 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
317
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
318 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
319 * @brief Overload for operator<< to allow serializing a library into the configuration file.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
320 * @param stream Stream to write the library into
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
321 * @param library Library to write into the stream
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
322 * @returns the stream
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
323 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
324 QDataStream& operator<<(QDataStream& stream, const Library& library)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
325 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
326 const QString path = library.path.absolutePath();
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
327 const int role = static_cast<int>(library.role);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
328 return stream << path << role;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
329 }
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
330
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
331 /**
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
332 * @brief Overload for operator>> to allow serializing a library into the configuration file.
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
333 * @param stream Stream to read the library from
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
334 * @param library Library to obtain from the stream
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
335 * @returns the stream
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
336 */
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
337 QDataStream& operator>>(QDataStream& stream, Library& library)
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
338 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
339 QString path;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
340 int role;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
341 QDataStream& result = stream >> path >> role;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
342 library.path = path;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
343 library.role = static_cast<Library::Role>(role);
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
344 return result;
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
345 }

mercurial