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