History

running scripts list -> treeview initial

Thijs Wenker committed 11 years ago

Commit d84a65a1 parent 225563b4 files · +164 −41Browse files

Showing the changes to interface/src/ScriptsModel.cpp (1 of 4 files). Show all

Unified · Split

interface/src/ScriptsModel.cpp+88−25
@@ -11,6 +11,7 @@ · 10 unmodified lines
11// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html11// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12//12//
13 13
14#include <typeinfo>
14#include <QUrl>15#include <QUrl>
15#include <QUrlQuery>16#include <QUrlQuery>
16#include <QXmlStreamReader>17#include <QXmlStreamReader>
@@ -31,18 +32,30 @@ static const QString IS_TRUNCATED_NAME = "IsTruncated"; · 14 unmodified lines
31static const QString CONTAINER_NAME = "Contents";32static const QString CONTAINER_NAME = "Contents";
32static const QString KEY_NAME = "Key";33static const QString KEY_NAME = "Key";
33 34
34ScriptItem::ScriptItem(const QString& filename, const QString& fullPath) :35TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type) :
36 _parent(parent),
37 _type(type) {
38};
39
40TreeNodeScript::TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin) :
41 TreeNodeBase(NULL, TREE_NODE_TYPE_SCRIPT),
35 _filename(filename),42 _filename(filename),
36 _fullPath(fullPath) {43 _fullPath(fullPath),
44 _origin(origin)
45{
46};
47
48TreeNodeFolder::TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent) :
49 TreeNodeBase(parent, TREE_NODE_TYPE_FOLDER),
50 _foldername(foldername) {
37};51};
38 52
39ScriptsModel::ScriptsModel(QObject* parent) :53ScriptsModel::ScriptsModel(QObject* parent) :
40 QAbstractListModel(parent),54 QAbstractItemModel(parent),
41 _loadingScripts(false),55 _loadingScripts(false),
42 _localDirectory(),56 _localDirectory(),
43 _fsWatcher(),57 _fsWatcher(),
44 _localFiles(),58 _treeNodes()
45 _remoteFiles()
46{59{
47 60
48 _localDirectory.setFilter(QDir::Files | QDir::Readable);61 _localDirectory.setFilter(QDir::Files | QDir::Readable);
@@ -57,23 +70,31 @@ ScriptsModel::ScriptsModel(QObject* parent) : · 8 unmodified lines
57 reloadRemoteFiles();70 reloadRemoteFiles();
58}71}
59 72
73QModelIndex ScriptsModel::index(int row, int column, const QModelIndex& parent) const {
74 return QModelIndex();
75}
76
77QModelIndex ScriptsModel::parent(const QModelIndex& child) const {
78 return QModelIndex();
79}
80
60QVariant ScriptsModel::data(const QModelIndex& index, int role) const {81QVariant ScriptsModel::data(const QModelIndex& index, int role) const {
61 const QList<ScriptItem*>* files = NULL;82 /*const QList<TreeNodeScript*>* files = NULL;
62 int row = 0;83 int row = 0;
63 bool isLocal = index.row() < _localFiles.size();84 bool isLocal = index.row() < _localFiles.size();
64 if (isLocal) {85 if (isLocal) {
65 files = &_localFiles;86 files = &_localFiles;
66 row = index.row();87 row = index.row();
67 } else {88 } else {
68 files = &_remoteFiles;89 files = &_remoteFiles;
69 row = index.row() - _localFiles.size();90 row = index.row() - _localFiles.size();
70 }91 }
71 92
72 if (role == Qt::DisplayRole) {93 if (role == Qt::DisplayRole) {
73 return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : ""));94 return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : ""));
74 } else if (role == ScriptPath) {95 } else if (role == ScriptPath) {
75 return QVariant((*files)[row]->getFullPath());96 return QVariant((*files)[row]->getFullPath());
76 }97 }*/
77 return QVariant();98 return QVariant();
78}99}
79 100
@@ -81,9 +102,14 @@ int ScriptsModel::rowCount(const QModelIndex& parent) const { · 1 unmodified line
81 if (parent.isValid()) {102 if (parent.isValid()) {
82 return 0;103 return 0;
83 }104 }
84 return _localFiles.length() + _remoteFiles.length();105 return 0;// _localFiles.length() + _remoteFiles.length();
85}106}
86 107
108int ScriptsModel::columnCount(const QModelIndex& parent) const {
109 return 1;
110}
111
112
87void ScriptsModel::updateScriptsLocation(const QString& newPath) {113void ScriptsModel::updateScriptsLocation(const QString& newPath) {
88 _fsWatcher.removePath(_localDirectory.absolutePath());114 _fsWatcher.removePath(_localDirectory.absolutePath());
89 115
@@ -93,7 +119,7 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 3 unmodified lines
93 if (!_localDirectory.absolutePath().isEmpty()) {119 if (!_localDirectory.absolutePath().isEmpty()) {
94 _fsWatcher.addPath(_localDirectory.absolutePath());120 _fsWatcher.addPath(_localDirectory.absolutePath());
95 }121 }
96 } 122 }
97 123
98 reloadLocalFiles();124 reloadLocalFiles();
99}125}
@@ -101,8 +127,12 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 1 unmodified line
101void ScriptsModel::reloadRemoteFiles() {127void ScriptsModel::reloadRemoteFiles() {
102 if (!_loadingScripts) {128 if (!_loadingScripts) {
103 _loadingScripts = true;129 _loadingScripts = true;
104 while (!_remoteFiles.isEmpty()) {130 for (int i = _treeNodes.size() - 1; i >= 0; i--) {
105 delete _remoteFiles.takeFirst();131 TreeNodeBase* node = _treeNodes.at(i);
132 if (typeid(*node) == typeid(TreeNodeScript) && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_REMOTE) {
133 delete node;
134 _treeNodes.removeAt(i);
135 }
106 }136 }
107 requestRemoteFiles();137 requestRemoteFiles();
108 }138 }
@@ -121,7 +151,6 @@ void ScriptsModel::requestRemoteFiles(QString marker) { · 12 unmodified lines
121 QNetworkRequest request(url);151 QNetworkRequest request(url);
122 QNetworkReply* reply = networkAccessManager.get(request);152 QNetworkReply* reply = networkAccessManager.get(request);
123 connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));153 connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));
124
125}154}
126 155
127void ScriptsModel::downloadFinished() {156void ScriptsModel::downloadFinished() {
@@ -170,7 +199,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 42 unmodified lines
170 xml.readNext();199 xml.readNext();
171 lastKey = xml.text().toString();200 lastKey = xml.text().toString();
172 if (jsRegex.exactMatch(xml.text().toString())) {201 if (jsRegex.exactMatch(xml.text().toString())) {
173 _remoteFiles.append(new ScriptItem(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey));202 _treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey, SCRIPT_ORIGIN_REMOTE));
174 }203 }
175 }204 }
176 xml.readNext();205 xml.readNext();
@@ -178,7 +207,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 1 unmodified line
178 }207 }
179 xml.readNext();208 xml.readNext();
180 }209 }
181 210 rebuildTree();
182 endResetModel();211 endResetModel();
183 212
184 // Error handling213 // Error handling
@@ -198,8 +227,15 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 13 unmodified lines
198void ScriptsModel::reloadLocalFiles() {227void ScriptsModel::reloadLocalFiles() {
199 beginResetModel();228 beginResetModel();
200 229
201 while (!_localFiles.isEmpty()) {230 for (int i = _treeNodes.size() - 1; i >= 0; i--) {
202 delete _localFiles.takeFirst();231 TreeNodeBase* node = _treeNodes.at(i);
232 qDebug() << "deleting local " << i << " " << typeid(*node).name();
233 if (node->getType() == TREE_NODE_TYPE_SCRIPT &&
234 static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL)
235 {
236 delete node;
237 _treeNodes.removeAt(i);
238 }
203 }239 }
204 240
205 _localDirectory.refresh();241 _localDirectory.refresh();
@@ -207,8 +243,35 @@ void ScriptsModel::reloadLocalFiles() { · 1 unmodified line
207 const QFileInfoList localFiles = _localDirectory.entryInfoList();243 const QFileInfoList localFiles = _localDirectory.entryInfoList();
208 for (int i = 0; i < localFiles.size(); i++) {244 for (int i = 0; i < localFiles.size(); i++) {
209 QFileInfo file = localFiles[i];245 QFileInfo file = localFiles[i];
210 _localFiles.append(new ScriptItem(file.fileName(), file.absoluteFilePath()));246 _treeNodes.append(new TreeNodeScript(file.fileName(), file.absoluteFilePath(), SCRIPT_ORIGIN_LOCAL));
211 }247 }
212 248 rebuildTree();
213 endResetModel();249 endResetModel();
214}250}
251
252void ScriptsModel::rebuildTree() {
253 for (int i = _treeNodes.size() - 1; i >= 0; i--) {
254
255 if (_treeNodes.at(i)->getType() == TREE_NODE_TYPE_FOLDER) {
256 delete _treeNodes.at(i);
257 _treeNodes.removeAt(i);
258 }
259 }
260 QHash<QString, TreeNodeFolder*> folders;
261 for (int i = 0; i < _treeNodes.size(); i++) {
262 TreeNodeBase* node = _treeNodes.at(i);
263 qDebug() << "blup" << i << typeid(*node).name();
264 if (typeid(*node) == typeid(TreeNodeScript)) {
265 TreeNodeScript* script = static_cast<TreeNodeScript*>(node);
266 TreeNodeFolder* parent = NULL;
267 QString hash;
268 QStringList pathList = script->getFilename().split(tr("/"));
269 QStringList::const_iterator pathIterator;
270 for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) {
271 hash.append("/" + *pathIterator);
272 qDebug() << hash;
273 }
274 }
275 }
276 folders.clear();
277}