src/libraries.cpp

changeset 7
68443f5be176
child 8
44679e468ba9
equal deleted inserted replaced
6:73e448b2943d 7:68443f5be176
1 #include <QSettings>
2 #include "libraries.h"
3
4 LibraryManager::LibraryManager(QObject* parent):
5 QAbstractTableModel{parent}
6 {
7 }
8
9 LibraryManager::LibraryManager(QSettings* settings, QObject* parent) :
10 QAbstractTableModel{parent}
11 {
12 this->restoreFromSettings(settings);
13 }
14
15 QVector<Library>::const_iterator LibraryManager::begin() const
16 {
17 return this->libraries.begin();
18 }
19
20 QVector<Library>::const_iterator LibraryManager::end() const
21 {
22 return this->libraries.end();
23 }
24
25 /**
26 * @brief Searches the libraries for the specified file name.
27 * @param fileName File to search for
28 * @return Full path to the file, or empty string if not found.
29 */
30 QFileInfo LibraryManager::findFile(QString fileName) const
31 {
32 QFileInfo path;
33 fileName.replace("\\", "/");
34 bool found = false;
35 for (const Library& library : this->libraries)
36 {
37 path = library.path.absoluteFilePath(fileName);
38 if (path.exists() && path.isFile())
39 {
40 found = true;
41 break;
42 }
43 }
44 if (not found)
45 path = {};
46 return path;
47 }
48
49 void LibraryManager::addLibrary(const Library& library)
50 {
51 emit layoutAboutToBeChanged();
52 libraries.append(library);
53 emit layoutChanged();
54 }
55
56 const Library& LibraryManager::library(int libraryIndex) const
57 {
58 return this->libraries[libraryIndex];
59 }
60
61 void LibraryManager::setLibraryPath(int libraryIndex, const QDir& path)
62 {
63 if (this->isValidIndex(libraryIndex))
64 {
65 this->libraries[libraryIndex].path = path;
66 this->signalLibraryChange(libraryIndex);
67 }
68 }
69
70 void LibraryManager::setLibraryRole(int libraryIndex, const Library::Role role)
71 {
72 if (this->isValidIndex(libraryIndex))
73 {
74 this->libraries[libraryIndex].role = role;
75 this->signalLibraryChange(libraryIndex);
76 }
77 }
78
79 void LibraryManager::restoreFromSettings(QSettings* settings)
80 {
81 this->libraries = settings->value("libraries").value<Libraries>();
82 }
83
84 void LibraryManager::storeToSettings(QSettings* settings)
85 {
86 QVariant librariesValue = QVariant::fromValue(this->libraries);
87 settings->setValue("libraries", librariesValue);
88 }
89
90 int LibraryManager::count() const
91 {
92 return this->libraries.size();
93 }
94
95 bool LibraryManager::isValidIndex(int libraryIndex)
96 {
97 return libraryIndex >= 0 && libraryIndex < this->libraries.size();
98 }
99
100 QString Library::libraryRoleName(const Role role)
101 {
102 switch (role)
103 {
104 case Library::OfficialLibrary:
105 return LibraryManager::tr("Official library");
106 case Library::UnofficialLibrary:
107 return LibraryManager::tr("Unofficial library");
108 case Library::WorkingLibrary:
109 return LibraryManager::tr("Working library");
110 default:
111 return "???";
112 }
113 }
114
115 Qt::ItemFlags LibraryManager::flags(const QModelIndex& index) const
116 {
117 Q_UNUSED(index);
118 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
119 }
120
121 QVariant LibraryManager::data(const QModelIndex& index, int role) const
122 {
123 if (role != Qt::DisplayRole)
124 {
125 return {};
126 }
127 else
128 {
129 const int row = index.row();
130 const Column column = static_cast<Column>(index.column());
131 const Library& library = this->library(row);
132 switch (column)
133 {
134 case RoleColumn:
135 return Library::libraryRoleName(library.role);
136 case PathColumn:
137 return library.path.absolutePath();
138 }
139 return {};
140 }
141 }
142
143 QVariant LibraryManager::headerData(int section, Qt::Orientation orientation, int role) const
144 {
145 if (orientation == Qt::Horizontal and role == Qt::DisplayRole)
146 {
147 switch(static_cast<Column>(section))
148 {
149 case PathColumn:
150 return tr("Path");
151 case RoleColumn:
152 return tr("Role");
153 }
154 return {};
155 }
156 else
157 {
158 return {};
159 }
160 }
161
162 int LibraryManager::rowCount(const QModelIndex& parent) const
163 {
164 Q_UNUSED(parent)
165 return this->count();
166 }
167
168 int LibraryManager::columnCount(const QModelIndex& parent) const
169 {
170 Q_UNUSED(parent)
171 return 2;
172 }
173
174 void LibraryManager::signalLibraryChange(int libraryIndex)
175 {
176 const QModelIndex topLeft = this->index(libraryIndex, 0);
177 const QModelIndex bottomRight = this->index(libraryIndex, columnCount() - 1);
178 emit dataChanged(topLeft, bottomRight);
179 }
180
181 QDataStream& operator<<(QDataStream& stream, const Library& library)
182 {
183 const QString path = library.path.absolutePath();
184 const int role = static_cast<int>(library.role);
185 return stream << path << role;
186 }
187
188 QDataStream& operator>>(QDataStream& stream, Library& library)
189 {
190 QString path;
191 int role;
192 QDataStream& result = stream >> path >> role;
193 library.path = path;
194 library.role = static_cast<Library::Role>(role);
195 return result;
196 }

mercurial