Merge pull request #87 from huffman/atp-model

ATP Model
This commit is contained in:
Stephen Birarda 2016-03-09 16:57:26 -08:00
commit 0fae72fdab
8 changed files with 311 additions and 99 deletions

View file

@ -33,7 +33,7 @@ Window {
HifiConstants { id: hifi }
property var scripts: ScriptDiscoveryService;
property var scriptsModel: scripts.scriptsModelFilter
property var scriptsModel: Assets.mappingModel;
property var currentDirectory;
property alias currentFileUrl: fileUrlTextField.text;
@ -47,6 +47,11 @@ Window {
function doDeleteFile(path) {
console.log("Deleting " + path);
Assets.deleteMappings(path, function(err) {
print("Finished deleting path: ", path, err);
reload();
});
}
function doUploadFile(path, mapping, addToWorld) {
console.log("Uploading " + path + " to " + mapping + " (addToWorld: " + addToWorld + ")");
@ -56,6 +61,10 @@ Window {
function doRenameFile(oldPath, newPath) {
console.log("Renaming " + oldPath + " to " + newPath);
Assets.renameMapping(oldPath, newPath, function(err) {
print("Finished rename: ", err);
reload();
});
}
function fileExists(destinationPath) {
@ -89,6 +98,7 @@ Window {
function reload() {
print("reload");
scriptsModel.refresh();
}
function addToWorld() {
var path = scriptsModel.data(treeView.currentIndex, 0x100);
@ -111,7 +121,7 @@ Window {
});
object.selected.connect(function(destinationPath) {
if (fileExists(destinationPath)) {
askForOverride(path, function() {
askForOverride(destinationPath, function() {
doRenameFile(path, destinationPath);
});
} else {

View file

@ -66,6 +66,7 @@
#include <BuildInfo.h>
#include <AssetClient.h>
#include <AssetUpload.h>
#include <AssetMappingsScriptingInterface.h>
#include <AutoUpdater.h>
#include <AudioInjectorManager.h>
#include <CursorManager.h>
@ -412,6 +413,7 @@ bool setupEssentials(int& argc, char** argv) {
DependencyManager::set<MessagesClient>();
DependencyManager::set<UserInputMapper>();
DependencyManager::set<controller::ScriptingInterface, ControllerScriptingInterface>();
DependencyManager::set<AssetMappingsScriptingInterface>();
DependencyManager::set<InterfaceParentFinder>();
DependencyManager::set<EntityTreeRenderer>(true, qApp, qApp);
DependencyManager::set<CompositorHelper>();
@ -1266,6 +1268,7 @@ void Application::initializeUi() {
rootContext->setContextProperty("Quat", new Quat());
rootContext->setContextProperty("Vec3", new Vec3());
rootContext->setContextProperty("Uuid", new ScriptUUID());
rootContext->setContextProperty("Assets", DependencyManager::get<AssetMappingsScriptingInterface>().data());
rootContext->setContextProperty("AvatarList", DependencyManager::get<AvatarManager>().data());
@ -4282,8 +4285,8 @@ bool Application::askToSetAvatarUrl(const QString& url) {
case FSTReader::HEAD_AND_BODY_MODEL:
ok = QMessageBox::Ok == OffscreenUi::question("Set Avatar",
"Would you like to use '" + modelName + "' for your avatar?",
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
"Would you like to use '" + modelName + "' for your avatar?",
QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
break;
default:

View file

@ -12,6 +12,8 @@
#ifndef hifi_AssetClient_h
#define hifi_AssetClient_h
#include <QStandardItemModel>
#include <QtQml/QJSEngine>
#include <QString>
#include <map>

View file

@ -76,12 +76,12 @@ void GetAllMappingsRequest::doStart() {
}
if (!error) {
if (!_error) {
int numberOfMappings;
message->readPrimitive(&numberOfMappings);
for (auto i = 0; i < numberOfMappings; ++i) {
auto path = message->readString();
auto hash = message->readString();
auto hash = message->read(SHA256_HASH_LENGTH).toHex();
_mappings[path] = hash;
}
}

View file

@ -0,0 +1,221 @@
//
// AssetMappingsScriptingInterface.cpp
// libraries/script-engine/src
//
// Created by Ryan Huffman on 2016-03-09.
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AssetMappingsScriptingInterface.h"
#include <QtScript/QScriptEngine>
#include <AssetRequest.h>
#include <AssetUpload.h>
#include <MappingRequest.h>
#include <NetworkLogging.h>
AssetMappingsScriptingInterface::AssetMappingsScriptingInterface() {
}
AssetMappingsScriptingInterface::~AssetMappingsScriptingInterface() {
qDebug() << "Destroying mapping interface";
}
void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getMapping(QString path, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::deleteMappings(QStringList paths, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::getAllMappings(QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QJSValueList args { uint8_t(request->getError()), map };
callback.call(args);
request->deleteLater();
});
request->start();
}
void AssetMappingsScriptingInterface::renameMapping(QString oldPath, QString newPath, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
QJSValueList args { uint8_t(request->getError()) };
callback.call(args);
request->deleteLater();
});
request->start();
}
AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder)
: name(name),
fullPath(fullPath),
isFolder(isFolder) {
}
static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*");
AssetMappingModel::AssetMappingModel() {
}
AssetMappingModel::~AssetMappingModel() {
qDebug() << " DEST";
}
void AssetMappingModel::refresh() {
qDebug() << "Refreshing asset mapping model";
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto existingPaths = _pathToItemMap.keys();
for (auto& mapping : mappings) {
auto& path = mapping.first;
auto parts = path.split("/");
auto length = parts.length();
existingPaths.removeOne(mapping.first);
QString fullPath = "";
QStandardItem* lastItem = nullptr;
for (int i = 0; i < length; ++i) {
fullPath += (i == 0 ? "" : "/") + parts[i];
auto it = _pathToItemMap.find(fullPath);
if (it == _pathToItemMap.end()) {
qDebug() << "prefix not found: " << fullPath;
auto item = new QStandardItem(parts[i]);
bool isFolder = i < length - 1;
item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
item->setData(isFolder, Qt::UserRole + 1);
if (lastItem) {
lastItem->setChild(lastItem->rowCount(), 0, item);
} else {
appendRow(item);
}
lastItem = item;
_pathToItemMap[fullPath] = lastItem;
}
else {
lastItem = it.value();
}
}
Q_ASSERT(fullPath == path);
}
// Remove folders from list
auto it = existingPaths.begin();
while (it != existingPaths.end()) {
auto item = _pathToItemMap[*it];
if (item->data(Qt::UserRole + 1).toBool()) {
it = existingPaths.erase(it);
} else {
++it;
}
}
for (auto& path : existingPaths) {
Q_ASSERT(_pathToItemMap.contains(path));
qDebug() << "removing existing: " << path;
auto item = _pathToItemMap[path];
while (item) {
// During each iteration, delete item
QStandardItem* nextItem = nullptr;
auto parent = item->parent();
if (parent) {
parent->removeRow(item->row());
if (parent->rowCount() > 0) {
// The parent still contains children, set the nextItem to null so we stop processing
nextItem = nullptr;
} else {
nextItem = parent;
}
} else {
removeRow(item->row());
}
_pathToItemMap.remove(path);
//delete item;
item = nextItem;
}
//removeitem->index();
}
});
request->start();
}

View file

@ -0,0 +1,68 @@
//
// AssetScriptingInterface.h
// libraries/script-engine/src
//
// Created by Ryan Huffman on 2016-03-09.
// Copyright 2016 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#pragma once
#ifndef hifi_AssetMappingsScriptingInterface_h
#define hifi_AssetMappingsScriptingInterface_h
#include <QtCore/QObject>
#include <QtScript/QScriptValue>
#include <AssetClient.h>
class AssetMappingItem : public QStandardItem {
public:
AssetMappingItem(const QString& name, const QString& fullPath, bool isFolder);
QString name;
QString fullPath;
bool isFolder;
};
class AssetMappingModel : public QStandardItemModel {
Q_OBJECT
public:
AssetMappingModel();
~AssetMappingModel();
// QVariant AssetMappingModel::data(const QModelIndex& index, int role) const;
Q_INVOKABLE void refresh();
private:
QHash<QString, QStandardItem*> _pathToItemMap;
};
class AssetMappingsScriptingInterface : public QObject, public Dependency {
Q_OBJECT
Q_PROPERTY(AssetMappingModel* mappingModel READ getAssetMappingModel CONSTANT)
public:
AssetMappingsScriptingInterface();
~AssetMappingsScriptingInterface();
Q_INVOKABLE AssetMappingModel* getAssetMappingModel() { return &_assetMappingModel; }
Q_INVOKABLE void setMapping(QString path, QString hash, QJSValue callback);
Q_INVOKABLE void getMapping(QString path, QJSValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QJSValue callback);
Q_INVOKABLE void deleteMapping(QString path, QJSValue callback) { deleteMappings(QStringList(path), callback); }
Q_INVOKABLE void getAllMappings(QJSValue callback);
Q_INVOKABLE void renameMapping(QString oldPath, QString newPath, QJSValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
AssetMappingModel _assetMappingModel;
};
#endif // hifi_AssetMappingsScriptingInterface_h

View file

@ -84,90 +84,3 @@ void AssetScriptingInterface::downloadData(QString urlString, QScriptValue callb
assetRequest->start();
}
void AssetScriptingInterface::setMapping(QString path, QString hash, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createSetMappingRequest(path, hash);
connect(request, &SetMappingRequest::finished, this, [this, callback](SetMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::getMapping(QString path, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetMappingRequest(path);
connect(request, &GetMappingRequest::finished, this, [this, callback](GetMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()), request->getHash() };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::deleteMappings(QStringList paths, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createDeleteMappingsRequest(paths);
connect(request, &DeleteMappingsRequest::finished, this, [this, callback](DeleteMappingsRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::getAllMappings(QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createGetAllMappingsRequest();
connect(request, &GetAllMappingsRequest::finished, this, [this, callback](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings();
auto map = callback.engine()->newObject();
for (auto& kv : mappings ) {
map.setProperty(kv.first, kv.second);
}
QScriptValueList args { uint8_t(request->getError()), map };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}
void AssetScriptingInterface::renameMapping(QString oldPath, QString newPath, QScriptValue callback) {
auto assetClient = DependencyManager::get<AssetClient>();
auto request = assetClient->createRenameMappingRequest(oldPath, newPath);
connect(request, &RenameMappingRequest::finished, this, [this, callback](RenameMappingRequest* request) mutable {
QScriptValueList args { uint8_t(request->getError()) };
callback.call(_engine->currentContext()->thisObject(), args);
request->deleteLater();
});
request->start();
}

View file

@ -26,12 +26,7 @@ public:
Q_INVOKABLE void uploadData(QString data, QScriptValue callback);
Q_INVOKABLE void downloadData(QString url, QScriptValue downloadComplete);
Q_INVOKABLE void setMapping(QString path, QString hash, QScriptValue callback);
Q_INVOKABLE void getMapping(QString path, QScriptValue callback);
Q_INVOKABLE void deleteMappings(QStringList paths, QScriptValue callback);
Q_INVOKABLE void deleteMapping(QString path, QScriptValue callback) { deleteMappings(QStringList(path), callback); }
Q_INVOKABLE void getAllMappings(QScriptValue callback);
Q_INVOKABLE void renameMapping(QString oldPath, QString newPath, QScriptValue callback);
protected:
QSet<AssetRequest*> _pendingRequests;
QScriptEngine* _engine;