-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathFileTypesModel.cpp
303 lines (266 loc) · 9.19 KB
/
FileTypesModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//-----------------------------------------------------------------------------
// File: FileTypesModel.cpp
//-----------------------------------------------------------------------------
// Project: Kactus 2
// Author: Joni-Matti Maatta
// Date: 20.02.2013
//
// Description:
// Model for file types data.
//-----------------------------------------------------------------------------
#include "FileTypesModel.h"
#include "FileTypeColumns.h"
#include <QStringList>
//-----------------------------------------------------------------------------
// Function: FileTypesModel::FileTypesModel()
//-----------------------------------------------------------------------------
FileTypesModel::FileTypesModel(QSettings& settings, QObject *parent): QAbstractTableModel(parent)
{
// Parse existing file types from settings.
beginResetModel();
settings.beginGroup("FileTypes");
for (QString const& typeName : settings.childGroups())
{
settings.beginGroup(typeName);
FileTypeEntry entry;
entry.name = typeName;
entry.editInKactus = settings.value("EditInKactus").toBool();
entry.extensions = settings.value("Extensions").toString();
entry.executable = settings.value("Executable").toString();
entries_.append(entry);
settings.endGroup();
}
settings.endGroup();
endResetModel();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::rowCount()
//-----------------------------------------------------------------------------
int FileTypesModel::rowCount(QModelIndex const& parent) const
{
if (parent.isValid())
{
return 0;
}
return entries_.size();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::columnCount()
//-----------------------------------------------------------------------------
int FileTypesModel::columnCount(QModelIndex const& parent) const
{
if (parent.isValid())
{
return 0;
}
return FileTypeColumns::COLUMN_COUNT;
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::data()
//-----------------------------------------------------------------------------
QVariant FileTypesModel::data(QModelIndex const& index, int role) const
{
// Check for invalid index.
if (!index.isValid() || index.row() < 0 || index.row() >= entries_.size())
{
return QVariant();
}
if (role == Qt::DisplayRole || role == Qt::EditRole)
{
FileTypeEntry const& entry = entries_.at(index.row());
if (index.column() == FileTypeColumns::NAME)
{
return entry.name;
}
else if (index.column() == FileTypeColumns::EDITABLE_IN_KACTUS)
{
return entry.editInKactus;
}
else if (index.column() == FileTypeColumns::EXTENSIONS)
{
return entry.extensions;
}
else if (index.column() == FileTypeColumns::EXECUTABLE)
{
return entry.executable;
}
else
{
return QVariant();
}
}
else if (role == Qt::UserRole)
{
QStringList specifiedFileTypes;
for (FileTypeEntry const& entry : entries_)
{
specifiedFileTypes.append(entry.name);
}
return specifiedFileTypes;
}
return QVariant();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::headerData()
//-----------------------------------------------------------------------------
QVariant FileTypesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
{
if (section == FileTypeColumns::NAME)
{
return tr("File type");
}
else if (section == FileTypeColumns::EDITABLE_IN_KACTUS)
{
return tr("Edit in Kactus2");
}
else if (section == FileTypeColumns::EXTENSIONS)
{
return tr("Extensions");
}
else if (section == FileTypeColumns::EXECUTABLE)
{
return tr("Execute with");
}
else
{
return QVariant();
}
}
return QVariant();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::setData()
//-----------------------------------------------------------------------------
bool FileTypesModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
// Check for invalid index.
if (!index.isValid() || index.row() < 0 || index.row() >= entries_.size())
{
return false;
}
if (role == Qt::EditRole)
{
if (index.column() == FileTypeColumns::NAME)
{
QString name = value.toString();
// if the file type has already been defined
for (const FileTypesModel::FileTypeEntry& entry : entries_)
{
if (entry.name.compare(name, Qt::CaseInsensitive) == 0)
{
return false;
}
}
entries_[index.row()].name = name;
}
else if (index.column() == FileTypeColumns::EDITABLE_IN_KACTUS)
{
entries_[index.row()].editInKactus = value.toBool();
}
else if (index.column() == FileTypeColumns::EXTENSIONS)
{
entries_[index.row()].extensions = value.toString();
}
else if (index.column() == FileTypeColumns::EXECUTABLE)
{
entries_[index.row()].executable = value.toString();
}
else
{
return false;
}
emit dataChanged(index, index);
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::flags()
//-----------------------------------------------------------------------------
Qt::ItemFlags FileTypesModel::flags(QModelIndex const& index) const
{
if (!index.isValid())
{
return Qt::NoItemFlags;
}
return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::onAddRow()
//-----------------------------------------------------------------------------
void FileTypesModel::onAddRow()
{
beginInsertRows(QModelIndex(), entries_.size(), entries_.size());
entries_.append(FileTypeEntry());
endInsertRows();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::onRemoveRow()
//-----------------------------------------------------------------------------
void FileTypesModel::onRemoveRow(int row)
{
// Check if the row is invalid.
if (row < 0 || row >= entries_.size())
{
return;
}
// Remove the entry at the given row.
beginRemoveRows(QModelIndex(), row, row);
entries_.removeAt(row);
endRemoveRows();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::onAddItem()
//-----------------------------------------------------------------------------
void FileTypesModel::onAddItem(const QModelIndex& index)
{
int row = entries_.size();
// If the index is valid then add the item to the correct position.
if (index.isValid())
{
row = index.row();
}
// Insert a new empty file type entry.
beginInsertRows(QModelIndex(), row, row);
entries_.insert(row, FileTypeEntry());
endInsertRows();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::onRemoveItem()
//-----------------------------------------------------------------------------
void FileTypesModel::onRemoveItem(const QModelIndex& index)
{
if (!index.isValid())
{
return;
}
// Make sure the row number if valid.
if (index.row() < 0 || index.row() >= entries_.size())
{
return;
}
// Remove the entry at the row specified by the index.
beginRemoveRows(QModelIndex(), index.row(), index.row());
entries_.removeAt(index.row());
endRemoveRows();
}
//-----------------------------------------------------------------------------
// Function: FileTypesModel::apply()
//-----------------------------------------------------------------------------
void FileTypesModel::apply(QSettings& settings)
{
// first clear all previous defined file types to remove duplicates
settings.remove("FileTypes");
settings.beginGroup("FileTypes");
for (FileTypeEntry const& entry : entries_)
{
settings.beginGroup(entry.name);
settings.setValue("EditInKactus", entry.editInKactus);
settings.setValue("Extensions", entry.extensions);
settings.setValue("Executable", entry.executable);
settings.endGroup();
}
settings.endGroup();
}