void ScriptsModel::rebuildTree() { for (int i = _treeNodes.size() - 1; i >= 0; i--) { if (_treeNodes.at(i)->getType() == TREE_NODE_TYPE_FOLDER) { delete _treeNodes.at(i); _treeNodes.removeAt(i); } } QHash<QString, TreeNodeFolder*> folders; for (int i = 0; i < _treeNodes.size(); i++) { TreeNodeBase* node = _treeNodes.at(i); if (node->getType() == TREE_NODE_TYPE_SCRIPT) { TreeNodeScript* script = static_cast<TreeNodeScript*>(node); TreeNodeFolder* parent = NULL; QString hash; QStringList pathList = script->getLocalPath().split(tr("/")); pathList.removeLast(); QStringList::const_iterator pathIterator; for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) { hash.append(*pathIterator + "/"); if (!folders.contains(hash)) { folders[hash] = new TreeNodeFolder(*pathIterator, parent); } parent = folders[hash]; } script->setParent(parent); } } QHash<QString, TreeNodeFolder*>::const_iterator folderIterator; for (folderIterator = folders.constBegin(); folderIterator != folders.constEnd(); ++folderIterator) { _treeNodes.append(*folderIterator); } folders.clear(); }
QVariantList RunningScriptsWidget::getPublicChildNodes(TreeNodeFolder* parent) { QVariantList result; QList<TreeNodeBase*> treeNodes = qApp->getRunningScriptsWidget()->getScriptsModel() ->getFolderNodes(parent); for (int i = 0; i < treeNodes.size(); i++) { TreeNodeBase* node = treeNodes.at(i); if (node->getType() == TREE_NODE_TYPE_FOLDER) { TreeNodeFolder* folder = static_cast<TreeNodeFolder*>(node); QVariantMap resultNode; resultNode.insert("name", node->getName()); resultNode.insert("type", "folder"); resultNode.insert("children", getPublicChildNodes(folder)); result.append(resultNode); continue; } TreeNodeScript* script = static_cast<TreeNodeScript*>(node); if (script->getOrigin() == ScriptOrigin::SCRIPT_ORIGIN_LOCAL) { continue; } QVariantMap resultNode; resultNode.insert("name", node->getName()); resultNode.insert("type", "script"); resultNode.insert("url", script->getFullPath()); result.append(resultNode); } return result; }
QVariant ScriptsModel::data(const QModelIndex& index, int role) const { TreeNodeBase* node = getTreeNodeFromIndex(index); if (node->getType() == TREE_NODE_TYPE_SCRIPT) { TreeNodeScript* script = static_cast<TreeNodeScript*>(node); if (role == Qt::DisplayRole) { return QVariant(script->getName() + (script->getOrigin() == SCRIPT_ORIGIN_LOCAL ? " (local)" : "")); } else if (role == ScriptPath) { return QVariant(script->getFullPath()); } } else if (node->getType() == TREE_NODE_TYPE_FOLDER) { TreeNodeFolder* folder = static_cast<TreeNodeFolder*>(node); if (role == Qt::DisplayRole) { return QVariant(folder->getName()); } } return QVariant(); }
QVariantList ScriptEngines::getLocal() { QVariantList result; QList<TreeNodeBase*> treeNodes = getScriptsModel().getFolderNodes(NULL); for (int i = 0; i < treeNodes.size(); i++) { TreeNodeBase* node = treeNodes.at(i); if (node->getType() != TREE_NODE_TYPE_SCRIPT) { continue; } TreeNodeScript* script = static_cast<TreeNodeScript*>(node); if (script->getOrigin() != ScriptOrigin::SCRIPT_ORIGIN_LOCAL) { continue; } QVariantMap resultNode; resultNode.insert("name", node->getName()); resultNode.insert("path", script->getFullPath()); result.append(resultNode); } return result; }