feat: Added icons to instances in the explorer
This commit is contained in:
parent
fc063ad39f
commit
4ebf8002a5
11 changed files with 54 additions and 3 deletions
22
assets/icons/Silk-Icons.LICENSE
Normal file
22
assets/icons/Silk-Icons.LICENSE
Normal 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
BIN
assets/icons/instance.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 353 B |
BIN
assets/icons/part.png
Normal file
BIN
assets/icons/part.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 452 B |
BIN
assets/icons/script.png
Normal file
BIN
assets/icons/script.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 748 B |
BIN
assets/icons/workspace.png
Normal file
BIN
assets/icons/workspace.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 923 B |
|
@ -1,14 +1,20 @@
|
||||||
#include "explorermodel.h"
|
#include "explorermodel.h"
|
||||||
#include "objects/base/instance.h"
|
#include "objects/base/instance.h"
|
||||||
#include "qcontainerfwd.h"
|
#include "qcontainerfwd.h"
|
||||||
|
#include "qicon.h"
|
||||||
|
#include "qimage.h"
|
||||||
|
#include "qnamespace.h"
|
||||||
#include "qobject.h"
|
#include "qobject.h"
|
||||||
#include "qwidget.h"
|
#include "qwidget.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include "objects/base/instance.h"
|
||||||
|
|
||||||
// https://doc.qt.io/qt-6/qtwidgets-itemviews-simpletreemodel-example.html#testing-the-model
|
// 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)
|
ExplorerModel::ExplorerModel(InstanceRef dataRoot, QWidget *parent)
|
||||||
: QAbstractItemModel(parent)
|
: QAbstractItemModel(parent)
|
||||||
, rootItem(dataRoot) {
|
, rootItem(dataRoot) {
|
||||||
|
@ -96,11 +102,18 @@ int ExplorerModel::columnCount(const QModelIndex &parent) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant ExplorerModel::data(const QModelIndex &index, int role) const {
|
QVariant ExplorerModel::data(const QModelIndex &index, int role) const {
|
||||||
if (!index.isValid() || role != Qt::DisplayRole)
|
if (!index.isValid())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
const Instance *item = static_cast<const Instance*>(index.internalPointer());
|
Instance *item = static_cast<Instance*>(index.internalPointer());
|
||||||
|
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
return QString::fromStdString(item->name);
|
return QString::fromStdString(item->name);
|
||||||
|
case Qt::DecorationRole:
|
||||||
|
return iconOf(item->GetClass());
|
||||||
|
}
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariant ExplorerModel::headerData(int section, Qt::Orientation orientation,
|
QVariant ExplorerModel::headerData(int section, Qt::Orientation orientation,
|
||||||
|
@ -114,3 +127,14 @@ Qt::ItemFlags ExplorerModel::flags(const QModelIndex &index) const
|
||||||
return index.isValid()
|
return index.isValid()
|
||||||
? QAbstractItemModel::flags(index) : Qt::ItemFlags(Qt::NoItemFlags);
|
? 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;
|
||||||
|
}
|
|
@ -30,6 +30,7 @@ public:
|
||||||
private:
|
private:
|
||||||
InstanceRef rootItem;
|
InstanceRef rootItem;
|
||||||
QModelIndex toIndex(InstanceRef item);
|
QModelIndex toIndex(InstanceRef item);
|
||||||
|
QImage iconOf(InstanceType* type) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // EXPLORERMODEL_H
|
#endif // EXPLORERMODEL_H
|
||||||
|
|
|
@ -28,6 +28,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
setMouseTracking(true);
|
setMouseTracking(true);
|
||||||
|
|
||||||
ui->explorerView->setModel(new ExplorerModel(std::dynamic_pointer_cast<Instance>(workspace)));
|
ui->explorerView->setModel(new ExplorerModel(std::dynamic_pointer_cast<Instance>(workspace)));
|
||||||
|
ui->explorerView->setRootIsDecorated(false);
|
||||||
|
|
||||||
simulationInit();
|
simulationInit();
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ static InstanceType TYPE_ {
|
||||||
.super = NULL,
|
.super = NULL,
|
||||||
.className = "Instance",
|
.className = "Instance",
|
||||||
.constructor = NULL, // Instance is abstract and therefore not creatable
|
.constructor = NULL, // Instance is abstract and therefore not creatable
|
||||||
|
.explorerIcon = "instance",
|
||||||
};
|
};
|
||||||
|
|
||||||
InstanceType* Instance::TYPE = &TYPE_;
|
InstanceType* Instance::TYPE = &TYPE_;
|
||||||
|
|
|
@ -9,6 +9,7 @@ struct InstanceType {
|
||||||
InstanceType* super; // May be null
|
InstanceType* super; // May be null
|
||||||
std::string className;
|
std::string className;
|
||||||
InstanceConstructor constructor;
|
InstanceConstructor constructor;
|
||||||
|
std::string explorerIcon = "";
|
||||||
};
|
};
|
||||||
|
|
||||||
// Base class for all instances in the data model
|
// Base class for all instances in the data model
|
||||||
|
|
|
@ -5,6 +5,7 @@ static InstanceType TYPE_ {
|
||||||
.super = Instance::TYPE,
|
.super = Instance::TYPE,
|
||||||
.className = "Part",
|
.className = "Part",
|
||||||
.constructor = &Part::CreateGeneric,
|
.constructor = &Part::CreateGeneric,
|
||||||
|
.explorerIcon = "part",
|
||||||
};
|
};
|
||||||
|
|
||||||
InstanceType* Part::TYPE = &TYPE_;
|
InstanceType* Part::TYPE = &TYPE_;
|
||||||
|
|
Loading…
Add table
Reference in a new issue