src/libraries.cpp

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

mercurial