Updated tree model for assets

This commit is contained in:
GPayne
2020-09-11 12:09:44 -06:00
parent e2c8541421
commit ded7f3cd66
4 changed files with 77 additions and 13 deletions

View File

@@ -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<QVariant> _itemData;
assetTreeItem* _parentItem;
bool _checked = false;
bool _existsInFilesystem = true;
};
#endif // __OPENSPACE_LAUNCHER___ASSETTREEITEM___H__

View File

@@ -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<std::string> 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;

View File

@@ -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())

View File

@@ -25,6 +25,7 @@
#include "assettreeitem.h"
#include "assettreemodel.h"
#include <sstream>
#include <QColor>
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)