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
1111 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1212 //
1313
14+#include <typeinfo>
1415 #include <QUrl>
1516 #include <QUrlQuery>
1617 #include <QXmlStreamReader>
@@ -31,18 +32,30 @@ static const QString IS_TRUNCATED_NAME = "IsTruncated"; · 14 unmodified lines
3132 static const QString CONTAINER_NAME = "Contents";
3233 static const QString KEY_NAME = "Key";
3334
34−ScriptItem::ScriptItem(const QString& filename, const QString& fullPath) :
35+TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type) :
36+ _parent(parent),
37+ _type(type) {
38+};
39+
40+TreeNodeScript::TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin) :
41+ TreeNodeBase(NULL, TREE_NODE_TYPE_SCRIPT),
3542 _filename(filename),
36− _fullPath(fullPath) {
43+ _fullPath(fullPath),
44+ _origin(origin)
45+{
46+};
47+
48+TreeNodeFolder::TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent) :
49+ TreeNodeBase(parent, TREE_NODE_TYPE_FOLDER),
50+ _foldername(foldername) {
3751 };
3852
3953 ScriptsModel::ScriptsModel(QObject* parent) :
40− QAbstractListModel(parent),
54+ QAbstractItemModel(parent),
4155 _loadingScripts(false),
4256 _localDirectory(),
4357 _fsWatcher(),
44− _localFiles(),
45− _remoteFiles()
58+ _treeNodes()
4659 {
4760
4861 _localDirectory.setFilter(QDir::Files | QDir::Readable);
@@ -57,23 +70,31 @@ ScriptsModel::ScriptsModel(QObject* parent) : · 8 unmodified lines
5770 reloadRemoteFiles();
5871 }
5972
73+QModelIndex ScriptsModel::index(int row, int column, const QModelIndex& parent) const {
74+ return QModelIndex();
75+}
76+
77+QModelIndex ScriptsModel::parent(const QModelIndex& child) const {
78+ return QModelIndex();
79+}
80+
6081 QVariant ScriptsModel::data(const QModelIndex& index, int role) const {
61− const QList<ScriptItem*>* files = NULL;
82+ /*const QList<TreeNodeScript*>* files = NULL;
6283 int row = 0;
6384 bool isLocal = index.row() < _localFiles.size();
64− if (isLocal) {
65− files = &_localFiles;
66− row = index.row();
67− } else {
68− files = &_remoteFiles;
69− row = index.row() - _localFiles.size();
70− }
85+ if (isLocal) {
86+ files = &_localFiles;
87+ row = index.row();
88+ } else {
89+ files = &_remoteFiles;
90+ row = index.row() - _localFiles.size();
91+ }
7192
7293 if (role == Qt::DisplayRole) {
7394 return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : ""));
7495 } else if (role == ScriptPath) {
7596 return QVariant((*files)[row]->getFullPath());
76− }
97+ }*/
7798 return QVariant();
7899 }
79100
@@ -81,9 +102,14 @@ int ScriptsModel::rowCount(const QModelIndex& parent) const { · 1 unmodified line
81102 if (parent.isValid()) {
82103 return 0;
83104 }
84− return _localFiles.length() + _remoteFiles.length();
105+ return 0;// _localFiles.length() + _remoteFiles.length();
85106 }
86107
108+int ScriptsModel::columnCount(const QModelIndex& parent) const {
109+ return 1;
110+}
111+
112+
87113 void ScriptsModel::updateScriptsLocation(const QString& newPath) {
88114 _fsWatcher.removePath(_localDirectory.absolutePath());
89115
@@ -93,7 +119,7 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 3 unmodified lines
93119 if (!_localDirectory.absolutePath().isEmpty()) {
94120 _fsWatcher.addPath(_localDirectory.absolutePath());
95121 }
96− }
122+ }
97123
98124 reloadLocalFiles();
99125 }
@@ -101,8 +127,12 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 1 unmodified line
101127 void ScriptsModel::reloadRemoteFiles() {
102128 if (!_loadingScripts) {
103129 _loadingScripts = true;
104− while (!_remoteFiles.isEmpty()) {
105− delete _remoteFiles.takeFirst();
130+ for (int i = _treeNodes.size() - 1; i >= 0; i--) {
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+ }
106136 }
107137 requestRemoteFiles();
108138 }
@@ -121,7 +151,6 @@ void ScriptsModel::requestRemoteFiles(QString marker) { · 12 unmodified lines
121151 QNetworkRequest request(url);
122152 QNetworkReply* reply = networkAccessManager.get(request);
123153 connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));
124−
125154 }
126155
127156 void ScriptsModel::downloadFinished() {
@@ -170,7 +199,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 42 unmodified lines
170199 xml.readNext();
171200 lastKey = xml.text().toString();
172201 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));
174203 }
175204 }
176205 xml.readNext();
@@ -178,7 +207,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 1 unmodified line
178207 }
179208 xml.readNext();
180209 }
181−
210+ rebuildTree();
182211 endResetModel();
183212
184213 // Error handling
@@ -198,8 +227,15 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 13 unmodified lines
198227 void ScriptsModel::reloadLocalFiles() {
199228 beginResetModel();
200229
201− while (!_localFiles.isEmpty()) {
202− delete _localFiles.takeFirst();
230+ for (int i = _treeNodes.size() - 1; i >= 0; i--) {
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+ }
203239 }
204240
205241 _localDirectory.refresh();
@@ -207,8 +243,35 @@ void ScriptsModel::reloadLocalFiles() { · 1 unmodified line
207243 const QFileInfoList localFiles = _localDirectory.entryInfoList();
208244 for (int i = 0; i < localFiles.size(); i++) {
209245 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));
211247 }
212−
248+ rebuildTree();
213249 endResetModel();
214250 }
251+
252+void 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+}