From ded7f3cd6696eafaf5c2d42d5937090c5a86f125 Mon Sep 17 00:00:00 2001 From: GPayne Date: Fri, 11 Sep 2020 12:09:44 -0600 Subject: [PATCH] Updated tree model for assets --- .../ext/launcher/include/assettreeitem.h | 8 ++- .../ext/launcher/include/assettreemodel.h | 11 +++- .../ext/launcher/src/assettreeitem.cpp | 16 +++++- .../ext/launcher/src/assettreemodel.cpp | 55 ++++++++++++++++--- 4 files changed, 77 insertions(+), 13 deletions(-) diff --git a/apps/OpenSpace/ext/launcher/include/assettreeitem.h b/apps/OpenSpace/ext/launcher/include/assettreeitem.h index f42d3a2ab6..78fb014465 100644 --- a/apps/OpenSpace/ext/launcher/include/assettreeitem.h +++ b/apps/OpenSpace/ext/launcher/include/assettreeitem.h @@ -47,8 +47,11 @@ public: bool setData(int column, const QVariant &value); bool isChecked() const { return _checked; } void setChecked( bool set ) { _checked = set; } - bool isAsset(); - bool isCategory(); + bool isAsset() const; + bool isCategory() const; + void setExistsInFilesystem(bool fileExists); + bool doesExistInFilesystem() const; + QString name() const; static const int checkboxColumn = 1; private: @@ -56,6 +59,7 @@ private: QVector _itemData; assetTreeItem* _parentItem; bool _checked = false; + bool _existsInFilesystem = true; }; #endif // __OPENSPACE_LAUNCHER___ASSETTREEITEM___H__ diff --git a/apps/OpenSpace/ext/launcher/include/assettreemodel.h b/apps/OpenSpace/ext/launcher/include/assettreemodel.h index 3006b5e942..6aad41824c 100644 --- a/apps/OpenSpace/ext/launcher/include/assettreemodel.h +++ b/apps/OpenSpace/ext/launcher/include/assettreemodel.h @@ -35,6 +35,7 @@ struct importElement std::string line; int level = -1; bool checked = false; + bool existsInFilesystem = true; }; class assetTreeModel : public QAbstractItemModel @@ -53,6 +54,7 @@ public: QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(int row, int column, const QModelIndex& parent = QModelIndex()) const; + assetTreeItem* assetItem(const QModelIndex &index); int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; Qt::ItemFlags flags(const QModelIndex &index) const override; @@ -60,10 +62,15 @@ public: int role = Qt::EditRole) override; std::vector selectedAssets(); void importModelData(const std::string contents); - bool isItemChecked(QModelIndex& index) const; - bool isItemAsset(QModelIndex& index) const; + bool isChecked(QModelIndex& index) const; + bool isAsset(QModelIndex& index) const; + bool inFilesystem(QModelIndex& index) const; int childCount(QModelIndex& index) const; assetTreeItem* child(int row) const; + QString name(QModelIndex& index) const; + void setName(QModelIndex& index, QString name); + void setChecked(QModelIndex& index, bool checked); + void setExistenceInFilesystem(QModelIndex& index, bool fileExists); private: std::string headerTitle; diff --git a/apps/OpenSpace/ext/launcher/src/assettreeitem.cpp b/apps/OpenSpace/ext/launcher/src/assettreeitem.cpp index fb40c0c182..13ac3862e2 100644 --- a/apps/OpenSpace/ext/launcher/src/assettreeitem.cpp +++ b/apps/OpenSpace/ext/launcher/src/assettreeitem.cpp @@ -85,14 +85,26 @@ bool assetTreeItem::setData(int column, const QVariant &value) return true; } -bool assetTreeItem::isAsset() { +bool assetTreeItem::isAsset() const { return (childCount() == 0); } -bool assetTreeItem::isCategory() { +bool assetTreeItem::isCategory() const { return (childCount() > 0); } +void assetTreeItem::setExistsInFilesystem(bool fileExists) { + _existsInFilesystem = fileExists; +} + +bool assetTreeItem::doesExistInFilesystem() const { + return _existsInFilesystem; +} + +QString assetTreeItem::name() const { + return QString(data(0).toString()); +} + bool assetTreeItem::insertChildren(int position, int count, int columns) { if (position < 0 || position > _childItems.size()) diff --git a/apps/OpenSpace/ext/launcher/src/assettreemodel.cpp b/apps/OpenSpace/ext/launcher/src/assettreemodel.cpp index 33eb8f2796..27ccebdb57 100644 --- a/apps/OpenSpace/ext/launcher/src/assettreemodel.cpp +++ b/apps/OpenSpace/ext/launcher/src/assettreemodel.cpp @@ -25,6 +25,7 @@ #include "assettreeitem.h" #include "assettreemodel.h" #include +#include assetTreeModel::assetTreeModel(QString header1, QString header2, QObject* parent) : QAbstractItemModel(parent), @@ -60,7 +61,10 @@ void assetTreeModel::importInsertItem(std::istringstream& iss, assetTreeItem* pa if (levelChange == 0) { parent->insertChildren(++nChildInsert, 1, 2); parent->child(nChildInsert)->setData(0, QString::fromUtf8(elem.line.c_str())); - parent->child(nChildInsert)->setData(1, (elem.checked ? Qt::Checked : Qt::Unchecked)); + bool shouldMakeElemChecked = (elem.checked || !elem.existsInFilesystem); + Qt::CheckState check = (shouldMakeElemChecked) ? Qt::Checked : Qt::Unchecked; + parent->child(nChildInsert)->setData(1, check); + parent->child(nChildInsert)->setExistsInFilesystem(elem.existsInFilesystem); continueToNextLine = importGetNextLine(elem, iss); } else if (levelChange == 1) { @@ -80,7 +84,9 @@ bool assetTreeModel::importGetNextLine(importElement& elem, std::istringstream& elem.level = -1; } else { - elem.checked = (elem.line.substr(0, 1).compare("1") == 0) ? true : false; + elem.checked = (elem.line.substr(0, 1).compare("0") == 0) ? false : true; + elem.existsInFilesystem = (elem.line.substr(0, 1).compare("x") == 0) ? + false : true; elem.line = elem.line.substr(1); elem.level = getLevelFromLine(elem.line); trimWhitespaceFromLine(elem.line); @@ -115,21 +121,42 @@ assetTreeItem* assetTreeModel::getItem(const QModelIndex &index) const { return rootItem; } -bool assetTreeModel::isItemChecked(QModelIndex& index) const { +bool assetTreeModel::isChecked(QModelIndex& index) const { assetTreeItem* item = getItem(index); int checked = item->data(1).toInt(); return (checked == Qt::Checked) ? true : false; } -bool assetTreeModel::isItemAsset(QModelIndex& index) const { +bool assetTreeModel::isAsset(QModelIndex& index) const { assetTreeItem* item = getItem(index); return item->isAsset(); } +bool assetTreeModel::inFilesystem(QModelIndex& index) const { + assetTreeItem* item = getItem(index); + return item->doesExistInFilesystem(); +} + int assetTreeModel::childCount(QModelIndex& index) const { return getItem(index)->childCount(); } +QString assetTreeModel::name(QModelIndex& index) const { + return getItem(index)->name(); +} + +void assetTreeModel::setName(QModelIndex& index, QString name) { + getItem(index)->setData(0, name); +} + +void assetTreeModel::setChecked(QModelIndex& index, bool checked) { + getItem(index)->setData(1, checked ? Qt::Checked : Qt::Unchecked); +} + +void assetTreeModel::setExistenceInFilesystem(QModelIndex& index, bool fileExists) { + getItem(index)->setExistsInFilesystem(fileExists); +} + assetTreeItem* assetTreeModel::child(int row) const { QModelIndex i = index(row, 0); int nKids = childCount(i); @@ -179,6 +206,10 @@ QModelIndex assetTreeModel::parent(const QModelIndex& index) const { return createIndex(parentItem->childNumber(), 0, parentItem); } +assetTreeItem* assetTreeModel::assetItem(const QModelIndex &index) { + return getItem(index); +} + int assetTreeModel::rowCount(const QModelIndex& parent) const { const assetTreeItem* parentItem = getItem(parent); return parentItem ? parentItem->childCount() : 0; @@ -204,10 +235,20 @@ QVariant assetTreeModel::data(const QModelIndex& index, int role) const { } } - if (role != Qt::DisplayRole) + if (role == Qt::ForegroundRole) { + if (item->doesExistInFilesystem()) { + return QVariant(QColor(Qt::black)); + } + else { + return QVariant(QColor(Qt::red)); + } + } + else if (role == Qt::DisplayRole) { + return item->data(index.column()); + } + else { return QVariant(); - - return item->data(index.column()); + } } bool assetTreeModel::setData(const QModelIndex& index, const QVariant& value, int role)