This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextensionmodel.cpp
207 lines (176 loc) · 5.48 KB
/
extensionmodel.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
#include "stdafx.h"
#include "extensionmodel.h"
ExtensionModel *ExtensionModel::m_instance = nullptr;
ExtensionModel *ExtensionModel::instance(QObject *parent)
{
if (!m_instance)
{
m_instance = new ExtensionModel(parent);
}
return m_instance;
}
QModelIndex ExtensionModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column);
}
int ExtensionModel::rowCount(const QModelIndex & /*parent*/) const
{
return m_extensions.length();
}
int ExtensionModel::columnCount(const QModelIndex & /*parent*/) const
{
return 8;
}
QVariant ExtensionModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
return QVariant();
if (index.row() < 0 || index.row() >= m_extensions.length())
return QVariant();
ExtensionPtr r = m_extensions.at(index.row());
switch (index.column())
{
case 0:
return QVariant(r->title());
case 1:
return QVariant(r->author());
case 2:
return QVariant(r->field());
case 3:
return QVariant(r->method());
case 4:
return QVariant(r->from());
case 5:
return QVariant(r->shortcut());
case 6:
return QVariant(r->comment());
case 7:
return QVariant(r->content());
default:
break;
}
return QVariant();
}
Qt::ItemFlags ExtensionModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return QAbstractItemModel::flags(index);
}
QVariant ExtensionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section)
{
case 0:
return QVariant(tr("Title"));
case 1:
return QVariant(tr("Author"));
case 2:
return QVariant(tr("Field"));
case 3:
return QVariant(tr("Method"));
case 4:
return QVariant(tr("From"));
case 5:
return QVariant(tr("Shortcut"));
case 6:
return QVariant(tr("Comment"));
case 7:
return QVariant(tr("Content"));
default:
break;
}
}
return QVariant();
}
void ExtensionModel::scanExtensions()
{
QtConcurrent::run(this, &ExtensionModel::doScanExtensions);
}
void ExtensionModel::doScanExtensions()
{
#if defined(Q_OS_WIN)
QString path = QApplication::applicationDirPath() % "/extensions";
#elif defined(Q_OS_MAC)
QString path = QApplication::applicationDirPath() % "/../PlugIns/extensions";
#endif
path = ":/extensions";
scanExtensionsFromDirectory(path, "Built-in");
path = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) % "/extensions";
QDir dir(path);
if (!dir.exists())
dir.mkpath(path);
else
scanExtensionsFromDirectory(path, "Custom");
emit extensionScanned();
}
ExtensionPtr ExtensionModel::extensionByUuid(const QString &uuid)
{
auto it = std::find_if(m_extensions.begin(), m_extensions.end(), [=](ExtensionPtr e) { return e->uuid() == uuid; });
return m_extensions.end() != it ? *it : ExtensionPtr();
}
void ExtensionModel::removeExtension(ExtensionPtr e)
{
auto it = std::find_if(m_extensions.begin(), m_extensions.end(), [e](ExtensionPtr ie) { return ie->uuid() == e->uuid(); });
if (m_extensions.end() != it)
{
emit extensionRemoved(e);
auto i = std::distance(m_extensions.begin(), it);
beginRemoveRows(QModelIndex(), i, i);
m_extensions.removeAt(i);
endRemoveRows();
}
}
void ExtensionModel::updateExtension(ExtensionPtr e)
{
auto it = std::find_if(m_extensions.begin(), m_extensions.end(), [e](ExtensionPtr ie) { return ie->uuid() == e->uuid(); });
if (m_extensions.end() != it)
{
// modify
*it = e;
auto i = std::distance(m_extensions.begin(), it);
emit dataChanged(index(i, 0), index(i, 0));
emit extensionModified(e);
return;
}
// append
beginInsertRows(QModelIndex(), m_extensions.size(), m_extensions.size());
m_extensions.push_back(e);
endInsertRows();
emit extensionCreated(e);
}
ExtensionPtr ExtensionModel::extension(const QModelIndex &index)
{
if (index.row() < 0 || index.row() >= m_extensions.length())
return ExtensionPtr();
return m_extensions.at(index.row());
}
void ExtensionModel::onExtensionScanned()
{
emit dataChanged(index(0, 0), index(m_extensions.length() - 1, 0));
}
ExtensionModel::ExtensionModel(QObject *parent) : QAbstractTableModel(parent)
{
connect(this, &ExtensionModel::extensionScanned, this, &ExtensionModel::onExtensionScanned);
}
void ExtensionModel::scanExtensionsFromDirectory(const QString &path, const QString &from)
{
QDir dir(path);
QStringList nameFilters = QStringList() << "*.xml";
QFileInfoList fil = dir.entryInfoList(nameFilters, QDir::Files | QDir::NoDotAndDotDot);
for (const QFileInfo &fi : fil)
{
ExtensionPtr e(new Extension);
if (e->load(fi.absoluteFilePath()))
{
e->setFrom(from);
m_extensions.push_back(e);
}
}
}