running scripts list -> treeview initial

This commit is contained in:
Thijs Wenker 2015-01-10 01:29:06 +01:00
parent 225563ba00
commit d84a65a5f9
4 changed files with 164 additions and 41 deletions

View file

@ -11,6 +11,7 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#include <typeinfo>
#include <QUrl> #include <QUrl>
#include <QUrlQuery> #include <QUrlQuery>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -31,18 +32,30 @@ static const QString IS_TRUNCATED_NAME = "IsTruncated";
static const QString CONTAINER_NAME = "Contents"; static const QString CONTAINER_NAME = "Contents";
static const QString KEY_NAME = "Key"; static const QString KEY_NAME = "Key";
ScriptItem::ScriptItem(const QString& filename, const QString& fullPath) : TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type) :
_parent(parent),
_type(type) {
};
TreeNodeScript::TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin) :
TreeNodeBase(NULL, TREE_NODE_TYPE_SCRIPT),
_filename(filename), _filename(filename),
_fullPath(fullPath) { _fullPath(fullPath),
_origin(origin)
{
};
TreeNodeFolder::TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent) :
TreeNodeBase(parent, TREE_NODE_TYPE_FOLDER),
_foldername(foldername) {
}; };
ScriptsModel::ScriptsModel(QObject* parent) : ScriptsModel::ScriptsModel(QObject* parent) :
QAbstractListModel(parent), QAbstractItemModel(parent),
_loadingScripts(false), _loadingScripts(false),
_localDirectory(), _localDirectory(),
_fsWatcher(), _fsWatcher(),
_localFiles(), _treeNodes()
_remoteFiles()
{ {
_localDirectory.setFilter(QDir::Files | QDir::Readable); _localDirectory.setFilter(QDir::Files | QDir::Readable);
@ -57,23 +70,31 @@ ScriptsModel::ScriptsModel(QObject* parent) :
reloadRemoteFiles(); reloadRemoteFiles();
} }
QModelIndex ScriptsModel::index(int row, int column, const QModelIndex& parent) const {
return QModelIndex();
}
QModelIndex ScriptsModel::parent(const QModelIndex& child) const {
return QModelIndex();
}
QVariant ScriptsModel::data(const QModelIndex& index, int role) const { QVariant ScriptsModel::data(const QModelIndex& index, int role) const {
const QList<ScriptItem*>* files = NULL; /*const QList<TreeNodeScript*>* files = NULL;
int row = 0; int row = 0;
bool isLocal = index.row() < _localFiles.size(); bool isLocal = index.row() < _localFiles.size();
if (isLocal) { if (isLocal) {
files = &_localFiles; files = &_localFiles;
row = index.row(); row = index.row();
} else { } else {
files = &_remoteFiles; files = &_remoteFiles;
row = index.row() - _localFiles.size(); row = index.row() - _localFiles.size();
} }
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : "")); return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : ""));
} else if (role == ScriptPath) { } else if (role == ScriptPath) {
return QVariant((*files)[row]->getFullPath()); return QVariant((*files)[row]->getFullPath());
} }*/
return QVariant(); return QVariant();
} }
@ -81,9 +102,14 @@ int ScriptsModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) { if (parent.isValid()) {
return 0; return 0;
} }
return _localFiles.length() + _remoteFiles.length(); return 0;// _localFiles.length() + _remoteFiles.length();
} }
int ScriptsModel::columnCount(const QModelIndex& parent) const {
return 1;
}
void ScriptsModel::updateScriptsLocation(const QString& newPath) { void ScriptsModel::updateScriptsLocation(const QString& newPath) {
_fsWatcher.removePath(_localDirectory.absolutePath()); _fsWatcher.removePath(_localDirectory.absolutePath());
@ -93,7 +119,7 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) {
if (!_localDirectory.absolutePath().isEmpty()) { if (!_localDirectory.absolutePath().isEmpty()) {
_fsWatcher.addPath(_localDirectory.absolutePath()); _fsWatcher.addPath(_localDirectory.absolutePath());
} }
} }
reloadLocalFiles(); reloadLocalFiles();
} }
@ -101,8 +127,12 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) {
void ScriptsModel::reloadRemoteFiles() { void ScriptsModel::reloadRemoteFiles() {
if (!_loadingScripts) { if (!_loadingScripts) {
_loadingScripts = true; _loadingScripts = true;
while (!_remoteFiles.isEmpty()) { for (int i = _treeNodes.size() - 1; i >= 0; i--) {
delete _remoteFiles.takeFirst(); TreeNodeBase* node = _treeNodes.at(i);
if (typeid(*node) == typeid(TreeNodeScript) && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_REMOTE) {
delete node;
_treeNodes.removeAt(i);
}
} }
requestRemoteFiles(); requestRemoteFiles();
} }
@ -121,7 +151,6 @@ void ScriptsModel::requestRemoteFiles(QString marker) {
QNetworkRequest request(url); QNetworkRequest request(url);
QNetworkReply* reply = networkAccessManager.get(request); QNetworkReply* reply = networkAccessManager.get(request);
connect(reply, SIGNAL(finished()), SLOT(downloadFinished())); connect(reply, SIGNAL(finished()), SLOT(downloadFinished()));
} }
void ScriptsModel::downloadFinished() { void ScriptsModel::downloadFinished() {
@ -170,7 +199,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) {
xml.readNext(); xml.readNext();
lastKey = xml.text().toString(); lastKey = xml.text().toString();
if (jsRegex.exactMatch(xml.text().toString())) { if (jsRegex.exactMatch(xml.text().toString())) {
_remoteFiles.append(new ScriptItem(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey)); _treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey, SCRIPT_ORIGIN_REMOTE));
} }
} }
xml.readNext(); xml.readNext();
@ -178,7 +207,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) {
} }
xml.readNext(); xml.readNext();
} }
rebuildTree();
endResetModel(); endResetModel();
// Error handling // Error handling
@ -198,8 +227,15 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) {
void ScriptsModel::reloadLocalFiles() { void ScriptsModel::reloadLocalFiles() {
beginResetModel(); beginResetModel();
while (!_localFiles.isEmpty()) { for (int i = _treeNodes.size() - 1; i >= 0; i--) {
delete _localFiles.takeFirst(); TreeNodeBase* node = _treeNodes.at(i);
qDebug() << "deleting local " << i << " " << typeid(*node).name();
if (node->getType() == TREE_NODE_TYPE_SCRIPT &&
static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL)
{
delete node;
_treeNodes.removeAt(i);
}
} }
_localDirectory.refresh(); _localDirectory.refresh();
@ -207,8 +243,35 @@ void ScriptsModel::reloadLocalFiles() {
const QFileInfoList localFiles = _localDirectory.entryInfoList(); const QFileInfoList localFiles = _localDirectory.entryInfoList();
for (int i = 0; i < localFiles.size(); i++) { for (int i = 0; i < localFiles.size(); i++) {
QFileInfo file = localFiles[i]; QFileInfo file = localFiles[i];
_localFiles.append(new ScriptItem(file.fileName(), file.absoluteFilePath())); _treeNodes.append(new TreeNodeScript(file.fileName(), file.absoluteFilePath(), SCRIPT_ORIGIN_LOCAL));
} }
rebuildTree();
endResetModel(); 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);
qDebug() << "blup" << i << typeid(*node).name();
if (typeid(*node) == typeid(TreeNodeScript)) {
TreeNodeScript* script = static_cast<TreeNodeScript*>(node);
TreeNodeFolder* parent = NULL;
QString hash;
QStringList pathList = script->getFilename().split(tr("/"));
QStringList::const_iterator pathIterator;
for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) {
hash.append("/" + *pathIterator);
qDebug() << hash;
}
}
}
folders.clear();
}

View file

@ -12,30 +12,67 @@
#ifndef hifi_ScriptsModel_h #ifndef hifi_ScriptsModel_h
#define hifi_ScriptsModel_h #define hifi_ScriptsModel_h
#include <QAbstractListModel> #include <QAbstractItemModel>
#include <QDir> #include <QDir>
#include <QNetworkReply> #include <QNetworkReply>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
class ScriptItem { class TreeNodeFolder;
enum ScriptOrigin {
SCRIPT_ORIGIN_LOCAL,
SCRIPT_ORIGIN_REMOTE
};
enum TreeNodeType {
TREE_NODE_TYPE_SCRIPT,
TREE_NODE_TYPE_FOLDER
};
class TreeNodeBase {
public: public:
ScriptItem(const QString& filename, const QString& fullPath); const TreeNodeFolder* getParent() { return _parent; }
void setParent(TreeNodeFolder* parent) { _parent = parent; }
TreeNodeType getType() { return _type; }
private:
TreeNodeFolder* _parent;
TreeNodeType _type;
protected:
TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type);
};
class TreeNodeScript : public TreeNodeBase {
public:
TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin);
const QString& getFilename() { return _filename; }; const QString& getFilename() { return _filename; };
const QString& getFullPath() { return _fullPath; }; const QString& getFullPath() { return _fullPath; };
const ScriptOrigin getOrigin() { return _origin; };
private: private:
QString _filename; QString _filename;
QString _fullPath; QString _fullPath;
ScriptOrigin _origin;
}; };
class ScriptsModel : public QAbstractListModel { class TreeNodeFolder : public TreeNodeBase {
public:
TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent);
private:
QString _foldername;
};
class ScriptsModel : public QAbstractItemModel {
Q_OBJECT Q_OBJECT
public: public:
ScriptsModel(QObject* parent = NULL); ScriptsModel(QObject* parent = NULL);
QModelIndex index(int row, int column, const QModelIndex& parent) const;
QModelIndex parent(const QModelIndex& child) const;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const;
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const;
enum Role { enum Role {
ScriptPath = Qt::UserRole, ScriptPath = Qt::UserRole,
@ -50,13 +87,13 @@ protected slots:
protected: protected:
void requestRemoteFiles(QString marker = QString()); void requestRemoteFiles(QString marker = QString());
bool parseXML(QByteArray xmlFile); bool parseXML(QByteArray xmlFile);
void rebuildTree();
private: private:
bool _loadingScripts; bool _loadingScripts;
QDir _localDirectory; QDir _localDirectory;
QFileSystemWatcher _fsWatcher; QFileSystemWatcher _fsWatcher;
QList<ScriptItem*> _localFiles; QList<TreeNodeBase*> _treeNodes;
QList<ScriptItem*> _remoteFiles;
}; };
#endif // hifi_ScriptsModel_h #endif // hifi_ScriptsModel_h

View file

@ -50,10 +50,10 @@ RunningScriptsWidget::RunningScriptsWidget(QWidget* parent) :
_proxyModel.setSourceModel(&_scriptsModel); _proxyModel.setSourceModel(&_scriptsModel);
_proxyModel.sort(0, Qt::AscendingOrder); _proxyModel.sort(0, Qt::AscendingOrder);
_proxyModel.setDynamicSortFilter(true); _proxyModel.setDynamicSortFilter(true);
ui->scriptListView->setModel(&_proxyModel); ui->scriptTreeView->setModel(&_proxyModel);
connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &RunningScriptsWidget::updateFileFilter); connect(ui->filterLineEdit, &QLineEdit::textChanged, this, &RunningScriptsWidget::updateFileFilter);
connect(ui->scriptListView, &QListView::doubleClicked, this, &RunningScriptsWidget::loadScriptFromList); connect(ui->scriptTreeView, &QTreeView::doubleClicked, this, &RunningScriptsWidget::loadScriptFromList);
connect(ui->reloadAllButton, &QPushButton::clicked, connect(ui->reloadAllButton, &QPushButton::clicked,
Application::getInstance(), &Application::reloadAllScripts); Application::getInstance(), &Application::reloadAllScripts);
@ -80,7 +80,7 @@ void RunningScriptsWidget::loadScriptFromList(const QModelIndex& index) {
} }
void RunningScriptsWidget::loadSelectedScript() { void RunningScriptsWidget::loadSelectedScript() {
QModelIndex selectedIndex = ui->scriptListView->currentIndex(); QModelIndex selectedIndex = ui->scriptTreeView->currentIndex();
if (selectedIndex.isValid()) { if (selectedIndex.isValid()) {
loadScriptFromList(selectedIndex); loadScriptFromList(selectedIndex);
} }
@ -166,7 +166,7 @@ void RunningScriptsWidget::showEvent(QShowEvent* event) {
void RunningScriptsWidget::selectFirstInList() { void RunningScriptsWidget::selectFirstInList() {
if (_proxyModel.rowCount() > 0) { if (_proxyModel.rowCount() > 0) {
ui->scriptListView->setCurrentIndex(_proxyModel.index(0, 0)); ui->scriptTreeView->setCurrentIndex(_proxyModel.index(0, 0));
} }
} }
@ -177,7 +177,7 @@ bool RunningScriptsWidget::eventFilter(QObject* sender, QEvent* event) {
} }
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event); QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) { if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
QModelIndex selectedIndex = ui->scriptListView->currentIndex(); QModelIndex selectedIndex = ui->scriptTreeView->currentIndex();
if (selectedIndex.isValid()) { if (selectedIndex.isValid()) {
loadScriptFromList(selectedIndex); loadScriptFromList(selectedIndex);
} }

View file

@ -245,8 +245,8 @@ font: bold 16px;
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>328</width> <width>334</width>
<height>18</height> <height>20</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -390,10 +390,27 @@ font: bold 16px;</string>
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<widget class="QLabel" name="label_2">
<property name="styleSheet">
<string notr="true">color: #0e7077; font: bold 14px;</string>
</property>
<property name="text">
<string>Load script </string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>from URL</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QPushButton" name="loadScriptButton"> <widget class="QPushButton" name="loadScriptButton">
<property name="text"> <property name="text">
<string>Load script</string> <string>from Disk</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -429,7 +446,7 @@ font: bold 16px;</string>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QListView" name="scriptListView"> <widget class="QTreeView" name="scriptTreeView">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -442,6 +459,12 @@ font: bold 16px;</string>
<property name="horizontalScrollBarPolicy"> <property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum> <enum>Qt::ScrollBarAlwaysOff</enum>
</property> </property>
<property name="rootIsDecorated">
<bool>true</bool>
</property>
<attribute name="headerVisible">
<bool>true</bool>
</attribute>
</widget> </widget>
</item> </item>
</layout> </layout>