Showing the changes to interface/src/ScriptsModel.cpp (1 of 6 files). Show all
Unified · Split
| @@ -11,7 +11,6 @@ · 10 unmodified lines | ||
| 11 | 11 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 12 | 12 | // |
| 13 | 13 | |
| 14 | −#include <typeinfo> | |
| 15 | 14 | #include <QUrl> |
| 16 | 15 | #include <QUrlQuery> |
| 17 | 16 | #include <QXmlStreamReader> |
| @@ -32,22 +31,21 @@ static const QString IS_TRUNCATED_NAME = "IsTruncated"; · 14 unmodified lines | ||
| 32 | 31 | static const QString CONTAINER_NAME = "Contents"; |
| 33 | 32 | static const QString KEY_NAME = "Key"; |
| 34 | 33 | |
| 35 | −TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type) : | |
| 34 | +TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, const QString& name, TreeNodeType type) : | |
| 36 | 35 | _parent(parent), |
| 36 | + _name(name), | |
| 37 | 37 | _type(type) { |
| 38 | 38 | }; |
| 39 | 39 | |
| 40 | −TreeNodeScript::TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin) : | |
| 41 | − TreeNodeBase(NULL, TREE_NODE_TYPE_SCRIPT), | |
| 42 | − _filename(filename), | |
| 40 | +TreeNodeScript::TreeNodeScript(const QString& localPath, const QString& fullPath, ScriptOrigin origin) : | |
| 41 | + TreeNodeBase(NULL, localPath.split("/").last(), TREE_NODE_TYPE_SCRIPT), | |
| 42 | + _localPath(localPath), | |
| 43 | 43 | _fullPath(fullPath), |
| 44 | − _origin(origin) | |
| 45 | −{ | |
| 44 | + _origin(origin) { | |
| 46 | 45 | }; |
| 47 | 46 | |
| 48 | 47 | TreeNodeFolder::TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent) : |
| 49 | − TreeNodeBase(parent, TREE_NODE_TYPE_FOLDER), | |
| 50 | − _foldername(foldername) { | |
| 48 | + TreeNodeBase(parent, foldername, TREE_NODE_TYPE_FOLDER) { | |
| 51 | 49 | }; |
| 52 | 50 | |
| 53 | 51 | ScriptsModel::ScriptsModel(QObject* parent) : |
| @@ -57,7 +55,6 @@ ScriptsModel::ScriptsModel(QObject* parent) : · 3 unmodified lines | ||
| 57 | 55 | _fsWatcher(), |
| 58 | 56 | _treeNodes() |
| 59 | 57 | { |
| 60 | − | |
| 61 | 58 | _localDirectory.setFilter(QDir::Files | QDir::Readable); |
| 62 | 59 | _localDirectory.setNameFilters(QStringList("*.js")); |
| 63 | 60 | |
| @@ -70,46 +67,56 @@ ScriptsModel::ScriptsModel(QObject* parent) : · 6 unmodified lines | ||
| 70 | 67 | reloadRemoteFiles(); |
| 71 | 68 | } |
| 72 | 69 | |
| 70 | +TreeNodeBase* ScriptsModel::getTreeNodeFromIndex(const QModelIndex& index) const { | |
| 71 | + if (index.isValid()) { | |
| 72 | + return static_cast<TreeNodeBase*>(index.internalPointer()); | |
| 73 | + } | |
| 74 | + return NULL; | |
| 75 | +} | |
| 76 | + | |
| 73 | 77 | QModelIndex ScriptsModel::index(int row, int column, const QModelIndex& parent) const { |
| 74 | − return QModelIndex(); | |
| 78 | + if (row < 0 || column < 0) { | |
| 79 | + return QModelIndex(); | |
| 80 | + } | |
| 81 | + return createIndex(row, column, getFolderNodes(static_cast<TreeNodeFolder*>(getTreeNodeFromIndex(parent))).at(row)); | |
| 75 | 82 | } |
| 76 | 83 | |
| 77 | 84 | QModelIndex ScriptsModel::parent(const QModelIndex& child) const { |
| 78 | − return QModelIndex(); | |
| 85 | + TreeNodeFolder* parent = (static_cast<TreeNodeBase*>(child.internalPointer()))->getParent(); | |
| 86 | + if (!parent) { | |
| 87 | + return QModelIndex(); | |
| 88 | + } | |
| 89 | + TreeNodeFolder* grandParent = parent->getParent(); | |
| 90 | + int row = getFolderNodes(grandParent).indexOf(parent); | |
| 91 | + return createIndex(row, 0, parent); | |
| 79 | 92 | } |
| 80 | 93 | |
| 81 | 94 | QVariant ScriptsModel::data(const QModelIndex& index, int role) const { |
| 82 | − /*const QList<TreeNodeScript*>* files = NULL; | |
| 83 | − int row = 0; | |
| 84 | − bool isLocal = index.row() < _localFiles.size(); | |
| 85 | − if (isLocal) { | |
| 86 | − files = &_localFiles; | |
| 87 | − row = index.row(); | |
| 88 | − } else { | |
| 89 | − files = &_remoteFiles; | |
| 90 | − row = index.row() - _localFiles.size(); | |
| 95 | + TreeNodeBase* node = getTreeNodeFromIndex(index); | |
| 96 | + if (node->getType() == TREE_NODE_TYPE_SCRIPT) { | |
| 97 | + TreeNodeScript* script = static_cast<TreeNodeScript*>(node); | |
| 98 | + if (role == Qt::DisplayRole) { | |
| 99 | + return QVariant(script->getName() + (script->getOrigin() == SCRIPT_ORIGIN_LOCAL ? " (local)" : "")); | |
| 100 | + } else if (role == ScriptPath) { | |
| 101 | + return QVariant(script->getFullPath()); | |
| 102 | + } | |
| 103 | + } else if (node->getType() == TREE_NODE_TYPE_FOLDER) { | |
| 104 | + TreeNodeFolder* folder = static_cast<TreeNodeFolder*>(node); | |
| 105 | + if (role == Qt::DisplayRole) { | |
| 106 | + return QVariant(folder->getName()); | |
| 107 | + } | |
| 91 | 108 | } |
| 92 | − | |
| 93 | − if (role == Qt::DisplayRole) { | |
| 94 | − return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : "")); | |
| 95 | − } else if (role == ScriptPath) { | |
| 96 | − return QVariant((*files)[row]->getFullPath()); | |
| 97 | − }*/ | |
| 98 | 109 | return QVariant(); |
| 99 | 110 | } |
| 100 | 111 | |
| 101 | 112 | int ScriptsModel::rowCount(const QModelIndex& parent) const { |
| 102 | − if (parent.isValid()) { | |
| 103 | − return 0; | |
| 104 | − } | |
| 105 | − return 0;// _localFiles.length() + _remoteFiles.length(); | |
| 113 | + return getFolderNodes(static_cast<TreeNodeFolder*>(getTreeNodeFromIndex(parent))).count(); | |
| 106 | 114 | } |
| 107 | 115 | |
| 108 | 116 | int ScriptsModel::columnCount(const QModelIndex& parent) const { |
| 109 | 117 | return 1; |
| 110 | 118 | } |
| 111 | 119 | |
| 112 | − | |
| 113 | 120 | void ScriptsModel::updateScriptsLocation(const QString& newPath) { |
| 114 | 121 | _fsWatcher.removePath(_localDirectory.absolutePath()); |
| 115 | 122 | |
| @@ -129,7 +136,9 @@ void ScriptsModel::reloadRemoteFiles() { · 13 unmodified lines | ||
| 129 | 136 | _loadingScripts = true; |
| 130 | 137 | for (int i = _treeNodes.size() - 1; i >= 0; i--) { |
| 131 | 138 | TreeNodeBase* node = _treeNodes.at(i); |
| 132 | − if (typeid(*node) == typeid(TreeNodeScript) && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_REMOTE) { | |
| 139 | + if (node->getType() == TREE_NODE_TYPE_SCRIPT && | |
| 140 | + static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_REMOTE) | |
| 141 | + { | |
| 133 | 142 | delete node; |
| 134 | 143 | _treeNodes.removeAt(i); |
| 135 | 144 | } |
| @@ -229,7 +238,6 @@ void ScriptsModel::reloadLocalFiles() { · 93 unmodified lines | ||
| 229 | 238 | |
| 230 | 239 | for (int i = _treeNodes.size() - 1; i >= 0; i--) { |
| 231 | 240 | TreeNodeBase* node = _treeNodes.at(i); |
| 232 | − qDebug() << "deleting local " << i << " " << typeid(*node).name(); | |
| 233 | 241 | if (node->getType() == TREE_NODE_TYPE_SCRIPT && |
| 234 | 242 | static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL) |
| 235 | 243 | { |
| @@ -251,7 +259,6 @@ void ScriptsModel::reloadLocalFiles() { · 15 unmodified lines | ||
| 251 | 259 | |
| 252 | 260 | void ScriptsModel::rebuildTree() { |
| 253 | 261 | for (int i = _treeNodes.size() - 1; i >= 0; i--) { |
| 254 | − | |
| 255 | 262 | if (_treeNodes.at(i)->getType() == TREE_NODE_TYPE_FOLDER) { |
| 256 | 263 | delete _treeNodes.at(i); |
| 257 | 264 | _treeNodes.removeAt(i); |
| @@ -260,18 +267,37 @@ void ScriptsModel::rebuildTree() { · 2 unmodified lines | ||
| 260 | 267 | QHash<QString, TreeNodeFolder*> folders; |
| 261 | 268 | for (int i = 0; i < _treeNodes.size(); i++) { |
| 262 | 269 | TreeNodeBase* node = _treeNodes.at(i); |
| 263 | − qDebug() << "blup" << i << typeid(*node).name(); | |
| 264 | − if (typeid(*node) == typeid(TreeNodeScript)) { | |
| 270 | + if (node->getType() == TREE_NODE_TYPE_SCRIPT) { | |
| 265 | 271 | TreeNodeScript* script = static_cast<TreeNodeScript*>(node); |
| 266 | 272 | TreeNodeFolder* parent = NULL; |
| 267 | 273 | QString hash; |
| 268 | − QStringList pathList = script->getFilename().split(tr("/")); | |
| 274 | + QStringList pathList = script->getLocalPath().split(tr("/")); | |
| 275 | + pathList.removeLast(); | |
| 269 | 276 | QStringList::const_iterator pathIterator; |
| 270 | 277 | for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) { |
| 271 | − hash.append("/" + *pathIterator); | |
| 272 | − qDebug() << hash; | |
| 278 | + hash.append(*pathIterator + "/"); | |
| 279 | + if (!folders.contains(hash)) { | |
| 280 | + folders[hash] = new TreeNodeFolder(*pathIterator, parent); | |
| 281 | + } | |
| 282 | + parent = folders[hash]; | |
| 273 | 283 | } |
| 284 | + script->setParent(parent); | |
| 274 | 285 | } |
| 275 | 286 | } |
| 287 | + QHash<QString, TreeNodeFolder*>::const_iterator folderIterator; | |
| 288 | + for (folderIterator = folders.constBegin(); folderIterator != folders.constEnd(); ++folderIterator) { | |
| 289 | + _treeNodes.append(*folderIterator); | |
| 290 | + } | |
| 276 | 291 | folders.clear(); |
| 277 | 292 | } |
| 293 | + | |
| 294 | +QList<TreeNodeBase*> ScriptsModel::getFolderNodes(TreeNodeFolder* parent) const { | |
| 295 | + QList<TreeNodeBase*> result; | |
| 296 | + for (int i = 0; i < _treeNodes.size(); i++) { | |
| 297 | + TreeNodeBase* node = _treeNodes.at(i); | |
| 298 | + if (node->getParent() == parent) { | |
| 299 | + result.append(node); | |
| 300 | + } | |
| 301 | + } | |
| 302 | + return result; | |
| 303 | +} | |