void ScriptsModel::reloadLocalFiles() { beginResetModel(); for (int i = _treeNodes.size() - 1; i >= 0; i--) { TreeNodeBase* node = _treeNodes.at(i); if (node->getType() == TREE_NODE_TYPE_SCRIPT && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL) { delete node; _treeNodes.removeAt(i); } } _localDirectory.refresh(); const QFileInfoList localFiles = _localDirectory.entryInfoList(); for (int i = 0; i < localFiles.size(); i++) { QFileInfo file = localFiles[i]; QString fileName = file.fileName(); QUrl absPath = normalizeScriptURL(QUrl::fromLocalFile(file.absoluteFilePath())); _treeNodes.append(new TreeNodeScript(fileName, absPath.toString(), SCRIPT_ORIGIN_LOCAL)); } rebuildTree(); endResetModel(); }
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; }
QList<TreeNodeBase*> ScriptsModel::getFolderNodes(TreeNodeFolder* parent) const { QList<TreeNodeBase*> result; for (int i = 0; i < _treeNodes.size(); i++) { TreeNodeBase* node = _treeNodes.at(i); if (node->getParent() == parent) { result.append(node); } } return result; }
void ScriptsModel::reloadDefaultFiles() { if (!_loadingScripts) { _loadingScripts = true; for (int i = _treeNodes.size() - 1; i >= 0; i--) { TreeNodeBase* node = _treeNodes.at(i); if (node->getType() == TREE_NODE_TYPE_SCRIPT && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_DEFAULT) { delete node; _treeNodes.removeAt(i); } } requestDefaultFiles(); } }
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; }