From 7e72d9fa8f64c140a8807c184783a0f95617f697 Mon Sep 17 00:00:00 2001 From: GPayne Date: Fri, 11 Sep 2020 12:26:59 -0600 Subject: [PATCH] Assets support --- apps/OpenSpace/ext/launcher/CMakeLists.txt | 3 + apps/OpenSpace/ext/launcher/include/assets.h | 50 ++++++ .../ext/launcher/include/ui_assets.h | 70 ++++++++ apps/OpenSpace/ext/launcher/src/assets.cpp | 155 ++++++++++++++++++ apps/OpenSpace/ext/launcher/src/assets.ui | 95 +++++++++++ 5 files changed, 373 insertions(+) create mode 100644 apps/OpenSpace/ext/launcher/include/assets.h create mode 100644 apps/OpenSpace/ext/launcher/include/ui_assets.h create mode 100644 apps/OpenSpace/ext/launcher/src/assets.cpp create mode 100644 apps/OpenSpace/ext/launcher/src/assets.ui diff --git a/apps/OpenSpace/ext/launcher/CMakeLists.txt b/apps/OpenSpace/ext/launcher/CMakeLists.txt index d55676bf90..b033c02cc9 100644 --- a/apps/OpenSpace/ext/launcher/CMakeLists.txt +++ b/apps/OpenSpace/ext/launcher/CMakeLists.txt @@ -22,18 +22,21 @@ target_include_directories( set(HEADER_FILES include/addedscripts.h + include/assets.h include/assettreeitem.h include/assettreemodel.h include/filesystemaccess.h include/launcherwindow.h include/profileedit.h include/ui/addedscripts.h + include/ui/assets.h include/ui_editorwindow.h include/ui_launcherwindow.h ) set(SOURCE_FILES src/addedscripts.cpp + src/assets.cpp src/assettreeitem.cpp src/assettreemodel.cpp src/filesystemaccess.cpp diff --git a/apps/OpenSpace/ext/launcher/include/assets.h b/apps/OpenSpace/ext/launcher/include/assets.h new file mode 100644 index 0000000000..0b775cfb9f --- /dev/null +++ b/apps/OpenSpace/ext/launcher/include/assets.h @@ -0,0 +1,50 @@ +#ifndef ASSETS_H +#define ASSETS_H + +#include +#include +#include +#include "assettreemodel.h" +#include "filesystemaccess.h" + +QT_BEGIN_NAMESPACE +namespace Ui { +class assets; +} +QT_END_NAMESPACE + +struct Asset { + std::string path; + std::string name; +}; + +class assets : public QDialog +{ + Q_OBJECT + +public slots: + void cancel(); + void parseSelections(); + +public: + explicit assets(std::vector& imported, std::string& reportAssets, + QWidget *parent = nullptr); + ~assets(); + std::string createTextSummary(); + +protected: + //void resizeEvent(QResizeEvent* event); + +private: + void compareFilesystemWithProfileAssets(); + bool traverseToExpandSelectedItems(int nRows, QModelIndex parent); + void findPathMatch(std::string& path, std::string& filename); + void traverseToFindFilesystemMatch(QModelIndex parent, int nRows, + std::string dirname, std::string filename); + Ui::assets *ui; + QWidget* _parent; + std::vector& _data; + assetTreeModel _assetTreeModel; +}; + +#endif // ASSETS_H diff --git a/apps/OpenSpace/ext/launcher/include/ui_assets.h b/apps/OpenSpace/ext/launcher/include/ui_assets.h new file mode 100644 index 0000000000..4324310c3e --- /dev/null +++ b/apps/OpenSpace/ext/launcher/include/ui_assets.h @@ -0,0 +1,70 @@ +/******************************************************************************** +** Form generated from reading UI file 'assets.ui' +** +** Created by: Qt User Interface Compiler version 5.15.0 +** +** WARNING! All changes made in this file will be lost when recompiling UI file! +********************************************************************************/ + +#ifndef UI_ASSETS_H +#define UI_ASSETS_H + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class Ui_assets +{ +public: + QDialogButtonBox *buttonBox; + QTreeView *treeView; + + void setupUi(QDialog *assets) + { + if (assets->objectName().isEmpty()) + assets->setObjectName(QString::fromUtf8("assets")); + assets->resize(470, 442); + buttonBox = new QDialogButtonBox(assets); + buttonBox->setObjectName(QString::fromUtf8("buttonBox")); + buttonBox->setGeometry(QRect(110, 400, 341, 32)); + buttonBox->setOrientation(Qt::Horizontal); + buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok); + treeView = new QTreeView(assets); + treeView->setObjectName(QString::fromUtf8("treeView")); + treeView->setGeometry(QRect(20, 20, 431, 351)); + QFont font; + font.setFamily(QString::fromUtf8("Arial")); + treeView->setFont(font); + treeView->setStyleSheet(QString::fromUtf8("background-color: rgb(85, 87, 83);")); + treeView->setAlternatingRowColors(false); + treeView->setAnimated(false); + + retranslateUi(assets); + //QObject::connect(buttonBox, SIGNAL(accepted()), assets, SLOT(accept())); + QObject::connect(buttonBox, SIGNAL(rejected()), assets, SLOT(reject())); + + QMetaObject::connectSlotsByName(assets); + } // setupUi + + void retranslateUi(QDialog *assets) + { + assets->setWindowTitle(QCoreApplication::translate("assets", "Dialog", nullptr)); +#if QT_CONFIG(tooltip) + treeView->setToolTip(QCoreApplication::translate("assets", "

Expand arrow entries to browse assets in this OpenSpace installation. Enable checkbox to include an asset. Those assets highlighted in red are present in the profile but do not exist in this OpenSpace installation.

", nullptr)); +#endif // QT_CONFIG(tooltip) + } // retranslateUi + +}; + +namespace Ui { + class assets: public Ui_assets {}; +} // namespace Ui + +QT_END_NAMESPACE + +#endif // UI_ASSETS_H diff --git a/apps/OpenSpace/ext/launcher/src/assets.cpp b/apps/OpenSpace/ext/launcher/src/assets.cpp new file mode 100644 index 0000000000..6143db34af --- /dev/null +++ b/apps/OpenSpace/ext/launcher/src/assets.cpp @@ -0,0 +1,155 @@ +#include "assets.h" +#include "./ui_assets.h" +#include +#include +#include +#include +#include + +assets::assets(std::vector& imported, std::string& reportAssets, QWidget *parent) + : QDialog(parent) + , ui(new Ui::assets) + , _data(imported) + , _assetTreeModel(tr("Asset"), tr("Enabled")) +{ + ui->setupUi(this); + + _assetTreeModel.importModelData(reportAssets); + + ui->treeView->setModel(&_assetTreeModel); + ui->treeView->setRootIndex(_assetTreeModel.index(-1, 0)); + ui->treeView->setColumnWidth(0, ui->treeView->width() * 0.8); + ui->treeView->setAnimated(true); + ui->treeView->setSortingEnabled(false); + + compareFilesystemWithProfileAssets(); + + int nRows = _assetTreeModel.rowCount(_assetTreeModel.index(-1, 0)); + traverseToExpandSelectedItems(nRows, _assetTreeModel.index(-1, 0)); + + connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(parseSelections())); + connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(cancel())); +} + +void assets::compareFilesystemWithProfileAssets() { + for (Asset a : _data) { + findPathMatch(a.path, a.name); + } +} + +void assets::findPathMatch(std::string& path, std::string& filename) { + QModelIndex parent = _assetTreeModel.index(-1, 0); + int nRows = _assetTreeModel.rowCount(parent); + traverseToFindFilesystemMatch(parent, nRows, path, filename); +} + +void assets::traverseToFindFilesystemMatch(QModelIndex parent, int nRows, + std::string path, std::string filename) +{ + bool endOfPath = (path.length() == 0); + size_t slash = path.find_first_of('/', 0); + std::string firstDir = endOfPath ? path : path.substr(0, slash); + + if (!endOfPath) { + std::string nextPath = (slash == std::string::npos) ? "" : + path.substr(slash + 1); + bool foundDirMatch = false; + for (int r = 0; r < nRows; r++) { + QModelIndex idx = _assetTreeModel.index(r, 0, parent); + std::string assetName = _assetTreeModel.name(idx).toUtf8().constData(); + if (!_assetTreeModel.isAsset(idx)) { + if (firstDir.compare(assetName) == 0) { + int nChildRows = _assetTreeModel.childCount(idx); + foundDirMatch = true; + traverseToFindFilesystemMatch(idx, nChildRows, nextPath, filename); + break; + } + } + else { + continue; + } + } + if (!foundDirMatch) { + //Insert missing directory here with name and exists=false condition + _assetTreeModel.assetItem(parent)->insertChildren(nRows, 1, 2); + QModelIndex idx = _assetTreeModel.index(nRows, 0, parent); + _assetTreeModel.setName(idx, QString(firstDir.c_str())); + _assetTreeModel.setExistenceInFilesystem(idx, false); + traverseToFindFilesystemMatch(idx, 0, nextPath, filename); + } + } + else { + bool foundFileMatch = false; + for (int r = 0; r < nRows; r++) { + QModelIndex idx = _assetTreeModel.index(r, 0, parent); + std::string assetName = _assetTreeModel.name(idx).toUtf8().constData(); + + if (filename.compare(assetName) == 0) { + foundFileMatch = true; + _assetTreeModel.setChecked(idx, true); + break; + } + } + if (!foundFileMatch) { + //Insert missing file here with name and exists=false condition + _assetTreeModel.assetItem(parent)->insertChildren(nRows, 1, 2); + QModelIndex idx = _assetTreeModel.index(nRows, 0, parent); + _assetTreeModel.setName(idx, QString(filename.c_str())); + _assetTreeModel.setChecked(idx, true); + _assetTreeModel.setExistenceInFilesystem(idx, false); + } + } +} + +bool assets::traverseToExpandSelectedItems(int nRows, QModelIndex parent) { + bool isExpanded = false; + + for (int r = 0; r < nRows; r++) { + QModelIndex idx = _assetTreeModel.index(r, 0, parent); + + if (!_assetTreeModel.isAsset(idx)) { + int nChildRows = _assetTreeModel.childCount(idx); + if (traverseToExpandSelectedItems(nChildRows, idx)) { + ui->treeView->setExpanded(idx, true); + isExpanded = true; + } + } + else if (_assetTreeModel.isChecked(idx) || !_assetTreeModel.inFilesystem(idx)) { + isExpanded = true; + } + } + return isExpanded; +} + +std::string assets::createTextSummary() { + std::string summary; + for (std::string line : _assetTreeModel.selectedAssets()) { + summary += line + "\n"; + } + return summary; +} + +void assets::parseSelections() { + _data.clear(); + for (std::string selected : _assetTreeModel.selectedAssets()) { + Asset a; + size_t slash = selected.find_last_of('/'); + if (slash == std::string::npos) { + a.path = ""; + a.name = selected; + } + else { + a.path = selected.substr(0, slash); + a.name = selected.substr(slash + 1); + } + _data.push_back(a); + } + accept(); +} + +assets::~assets() { + delete ui; +} + +void assets::cancel() { +} diff --git a/apps/OpenSpace/ext/launcher/src/assets.ui b/apps/OpenSpace/ext/launcher/src/assets.ui new file mode 100644 index 0000000000..4fbab4c4e7 --- /dev/null +++ b/apps/OpenSpace/ext/launcher/src/assets.ui @@ -0,0 +1,95 @@ + + + assets + + + + 0 + 0 + 470 + 442 + + + + Dialog + + + + + 110 + 400 + 341 + 32 + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + 20 + 20 + 431 + 351 + + + + + Arial + + + + <html><head/><body><p>Expand arrow entries to browse assets in this OpenSpace installation. Enable checkbox to include an asset. Those assets highlighted in red are present in the profile but do not exist in this OpenSpace installation.</p></body></html> + + + background-color: rgb(85, 87, 83); + + + false + + + false + + + + + + + buttonBox + accepted() + assets + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + assets + reject() + + + 316 + 260 + + + 286 + 274 + + + + +