Assets support

This commit is contained in:
GPayne
2020-09-11 12:26:59 -06:00
parent 0ec1cf69f8
commit 7e72d9fa8f
5 changed files with 373 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,50 @@
#ifndef ASSETS_H
#define ASSETS_H
#include <QDialog>
#include <QWidget>
#include <QListWidgetItem>
#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<Asset>& 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<Asset>& _data;
assetTreeModel _assetTreeModel;
};
#endif // ASSETS_H

View File

@@ -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 <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QDialog>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QTreeView>
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", "<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>", nullptr));
#endif // QT_CONFIG(tooltip)
} // retranslateUi
};
namespace Ui {
class assets: public Ui_assets {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_ASSETS_H

View File

@@ -0,0 +1,155 @@
#include "assets.h"
#include "./ui_assets.h"
#include <qevent.h>
#include <QFileSystemModel>
#include <QScreen>
#include <sstream>
#include <string>
assets::assets(std::vector<Asset>& 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() {
}

View File

@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>assets</class>
<widget class="QDialog" name="assets">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>470</width>
<height>442</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>110</x>
<y>400</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QTreeView" name="treeView">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>431</width>
<height>351</height>
</rect>
</property>
<property name="font">
<font>
<family>Arial</family>
</font>
</property>
<property name="toolTip">
<string extracomment="Click checkbox of an asset in order to require it in the profile">&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(85, 87, 83);</string>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</property>
<property name="animated">
<bool>false</bool>
</property>
</widget>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>assets</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>assets</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>