Add proper model updating to asset mapping model

This commit is contained in:
Ryan Huffman 2016-03-09 16:29:51 -08:00
parent f23057b1c0
commit ebc6c8ce44
2 changed files with 67 additions and 29 deletions

View file

@ -20,6 +20,9 @@
AssetMappingsScriptingInterface::AssetMappingsScriptingInterface() { AssetMappingsScriptingInterface::AssetMappingsScriptingInterface() {
} }
AssetMappingsScriptingInterface::~AssetMappingsScriptingInterface() {
qDebug() << "Destroying mapping interface";
}
void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) { void AssetMappingsScriptingInterface::setMapping(QString path, QString hash, QJSValue callback) {
auto assetClient = DependencyManager::get<AssetClient>(); auto assetClient = DependencyManager::get<AssetClient>();
@ -118,7 +121,11 @@ AssetMappingItem::AssetMappingItem(const QString& name, const QString& fullPath,
static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*"); static int assetMappingModelMetatypeId = qRegisterMetaType<AssetMappingModel*>("AssetMappingModel*");
AssetMappingModel::AssetMappingModel(QObject* parent) { AssetMappingModel::AssetMappingModel() {
}
AssetMappingModel::~AssetMappingModel() {
qDebug() << " DEST";
} }
void AssetMappingModel::refresh() { void AssetMappingModel::refresh() {
@ -128,49 +135,77 @@ void AssetMappingModel::refresh() {
connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable { connect(request, &GetAllMappingsRequest::finished, this, [this](GetAllMappingsRequest* request) mutable {
auto mappings = request->getMappings(); auto mappings = request->getMappings();
auto existingPaths = _pathToItemMap.keys();
for (auto& mapping : mappings) { for (auto& mapping : mappings) {
auto& path = mapping.first; auto& path = mapping.first;
auto parts = path.split("/"); auto parts = path.split("/");
auto length = parts.length(); auto length = parts.length();
QString fullPath = parts[0]; existingPaths.removeOne(mapping.first);
QString fullPath = "";// parts[0];
QStandardItem* lastItem = nullptr; QStandardItem* lastItem = nullptr;
auto it = _pathToItemMap.find(fullPath); for (int i = 0; i < length; ++i) {
if (it == _pathToItemMap.end()) { fullPath += (i == 0 ? "" : "/") + parts[i];
lastItem = new QStandardItem(parts[0]);
lastItem->setData(parts[0], Qt::UserRole + 1);
lastItem->setData(fullPath, Qt::UserRole + 2);
_pathToItemMap[fullPath] = lastItem;
appendRow(lastItem);
}
else {
lastItem = it.value();
}
if (length > 1) { auto it = _pathToItemMap.find(fullPath);
for (int i = 1; i < length; ++i) { if (it == _pathToItemMap.end()) {
fullPath += "/" + parts[i]; qDebug() << "prefix not found: " << fullPath;
auto item = new QStandardItem(parts[i]);
auto it = _pathToItemMap.find(fullPath); bool isFolder = i < length - 1;
if (it == _pathToItemMap.end()) { item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
qDebug() << "prefix not found: " << fullPath; item->setData(isFolder, Qt::UserRole + 1);
auto item = new QStandardItem(parts[i]); if (lastItem) {
bool isFolder = i < length - 1;
item->setData(isFolder ? fullPath + "/" : fullPath, Qt::UserRole);
lastItem->setChild(lastItem->rowCount(), 0, item); lastItem->setChild(lastItem->rowCount(), 0, item);
lastItem = item; } else {
_pathToItemMap[fullPath] = lastItem; appendRow(item);
}
else {
lastItem = it.value();
} }
lastItem = item;
_pathToItemMap[fullPath] = lastItem;
}
else {
lastItem = it.value();
} }
} }
Q_ASSERT(fullPath == path); Q_ASSERT(fullPath == path);
} }
for (auto& path : existingPaths) {
Q_ASSERT(_pathToItemMap.contains(path));
auto item = _pathToItemMap[path];
if (item->data(Qt::UserRole + 1).toBool()) {
continue;
}
qDebug() << "removing existing: " << 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(); request->start();

View file

@ -32,7 +32,8 @@
class AssetMappingModel : public QStandardItemModel { class AssetMappingModel : public QStandardItemModel {
Q_OBJECT Q_OBJECT
public: public:
AssetMappingModel(QObject* parent = nullptr); AssetMappingModel();
~AssetMappingModel();
// QVariant AssetMappingModel::data(const QModelIndex& index, int role) const; // QVariant AssetMappingModel::data(const QModelIndex& index, int role) const;
@ -45,8 +46,10 @@
class AssetMappingsScriptingInterface : public QObject, public Dependency { class AssetMappingsScriptingInterface : public QObject, public Dependency {
Q_OBJECT Q_OBJECT
Q_PROPERTY(AssetMappingModel* mappingModel READ getAssetMappingModel CONSTANT)
public: public:
AssetMappingsScriptingInterface(); AssetMappingsScriptingInterface();
~AssetMappingsScriptingInterface();
Q_INVOKABLE AssetMappingModel* getAssetMappingModel() { return &_assetMappingModel; } Q_INVOKABLE AssetMappingModel* getAssetMappingModel() { return &_assetMappingModel; }