feat: Added icons to instances in the explorer

This commit is contained in:
maelstrom 2025-01-18 18:50:56 +01:00
parent fc063ad39f
commit 4ebf8002a5
11 changed files with 54 additions and 3 deletions

View file

@ -0,0 +1,22 @@
Silk icon set 1.3
_________________________________________
Mark James
http://www.famfamfam.com/lab/icons/silk/
_________________________________________
This work is licensed under a
Creative Commons Attribution 2.5 License.
[ http://creativecommons.org/licenses/by/2.5/ ]
This means you may use it for any purpose,
and make any changes you like.
All I ask is that you include a link back
to this page in your credits.
Are you using this icon set? Send me an email
(including a link or picture if available) to
mjames@gmail.com
Any other questions about this icon set please
contact mjames@gmail.com

BIN
assets/icons/instance.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 353 B

BIN
assets/icons/part.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 452 B

BIN
assets/icons/script.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 748 B

BIN
assets/icons/workspace.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 923 B

View file

@ -1,14 +1,20 @@
#include "explorermodel.h"
#include "objects/base/instance.h"
#include "qcontainerfwd.h"
#include "qicon.h"
#include "qimage.h"
#include "qnamespace.h"
#include "qobject.h"
#include "qwidget.h"
#include "common.h"
#include <algorithm>
#include <optional>
#include "objects/base/instance.h"
// https://doc.qt.io/qt-6/qtwidgets-itemviews-simpletreemodel-example.html#testing-the-model
std::map<std::string, QImage> instanceIconCache;
ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent)
: QAbstractItemModel(parent)
, rootItem(dataRoot) {
@ -96,11 +102,18 @@ int ExplorerModel::columnCount(const QModelIndex &parent) const {
}
QVariant ExplorerModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || role != Qt::DisplayRole)
if (!index.isValid())
return {};
const Instance *item = static_cast<const Instance*>(index.internalPointer());
return QString::fromStdString(item->name);
Instance *item = static_cast<Instance*>(index.internalPointer());
switch (role) {
case Qt::DisplayRole:
return QString::fromStdString(item->name);
case Qt::DecorationRole:
return iconOf(item->GetClass());
}
return {};
}
QVariant ExplorerModel::headerData(int section, Qt::Orientation orientation,
@ -113,4 +126,15 @@ Qt::ItemFlags ExplorerModel::flags(const QModelIndex &index) const
{
return index.isValid()
? QAbstractItemModel::flags(index) : Qt::ItemFlags(Qt::NoItemFlags);
}
QImage ExplorerModel::iconOf(InstanceType* type) const {
if (instanceIconCache.count(type->className)) return instanceIconCache[type->className];
InstanceType* currentClass = type;
while (currentClass->explorerIcon.empty()) currentClass = currentClass->super;
QImage icon("assets/icons/" + QString::fromStdString(currentClass->explorerIcon));
instanceIconCache[type->className] = icon;
return icon;
}

View file

@ -30,6 +30,7 @@ public:
private:
InstanceRef rootItem;
QModelIndex toIndex(InstanceRef item);
QImage iconOf(InstanceType* type) const;
};
#endif // EXPLORERMODEL_H

View file

@ -28,6 +28,7 @@ MainWindow::MainWindow(QWidget *parent)
setMouseTracking(true);
ui->explorerView->setModel(new ExplorerModel(std::dynamic_pointer_cast<Instance>(workspace)));
ui->explorerView->setRootIsDecorated(false);
simulationInit();

View file

@ -9,6 +9,7 @@ static InstanceType TYPE_ {
.super = NULL,
.className = "Instance",
.constructor = NULL, // Instance is abstract and therefore not creatable
.explorerIcon = "instance",
};
InstanceType* Instance::TYPE = &TYPE_;

View file

@ -9,6 +9,7 @@ struct InstanceType {
InstanceType* super; // May be null
std::string className;
InstanceConstructor constructor;
std::string explorerIcon = "";
};
// Base class for all instances in the data model

View file

@ -5,6 +5,7 @@ static InstanceType TYPE_ {
.super = Instance::TYPE,
.className = "Part",
.constructor = &Part::CreateGeneric,
.explorerIcon = "part",
};
InstanceType* Part::TYPE = &TYPE_;