PeriDyno 1.2.1
Loading...
Searching...
No Matches
PConsoleWidget.cpp
Go to the documentation of this file.
1#include "PConsoleWidget.h"
2#include "Platform.h"
3#include <QListWidget>
4#include <QDir>
5#include <QStringList>
6#include <QHBoxLayout>
7#include <QListView>
8#include <QPixmap>
9#include "NodeFactory.h"
10
11namespace dyno
12{
13 class CustomFileSystemModel : public QFileSystemModel
14 {
15 public:
16 explicit CustomFileSystemModel(QObject* parent = nullptr) {};
18 private:
19 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const
20 {
21 if (index.isValid())
22 {
23 if (role == Qt::DecorationRole)
24 {
25 QFileInfo info = CustomFileSystemModel::fileInfo(index);
26 if (info.isFile())
27 {
28 if (info.suffix() == "png" || info.suffix() == "jpg" || info.suffix() == "bmp")
29 {
30 std::string iconPath = getAssetPath() + "/icon/ContentBrowser/image.png";
31 return QPixmap(iconPath.c_str());
32 }
33 else if (info.suffix() == "obj" || info.suffix() == "gltf" || info.suffix() == "glb" || info.suffix() == "fbx" || info.suffix() == "STL" || info.suffix() == "stl")
34 {
35 std::string iconPath = getAssetPath() + "/icon/ContentBrowser/3dModel.png";
36 return QPixmap(iconPath.c_str());
37 }
38 }
39 }
40 }
41
42 return QFileSystemModel::data(index, role);
43 }
44 };
45
46
48 QWidget(parent)
49 {
50// setMinimumHeight(200);
51 }
52
53 QContentBrowser::QContentBrowser(QWidget* parent /*= nullptr*/)
54 : QWidget()
55 {
56 QHBoxLayout* layout = new QHBoxLayout(this);
57 this->setLayout(layout);
58
59 std::string path = getAssetPath();
60 QDir root(path.c_str());
61
62 //Add file browser
63 model = new QFileSystemModel(this);
64 model->setRootPath(path.c_str());
65 model->setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
66 model->sort(0, Qt::AscendingOrder);
67
68// QObject::connect(model, SIGNAL(directoryLoaded(QString)), SLOT(findDirectory(QString)));
69
70 // File system
71 treeView = new QTreeView(this);
72 treeView->setModel(model);
73 treeView->setHeaderHidden(true); //Show tree header
74 treeView->setFixedWidth(520);
75 treeView->hideColumn(1);
76 treeView->hideColumn(2); //Hide second column
77 treeView->hideColumn(3);
78 treeView->setRootIndex(model->index(path.c_str()));
79 layout->addWidget(treeView);
80
81
82
83 QStringList filter;
84 filter <<"*.png" << "*.jpg" << "*.bmp" << "*.obj" << "*.gltf" << "*.glb" << "*.fbx" << "*.STL" << "*.stl" << "*.xml";
85 auto* listModel = new CustomFileSystemModel(this);
86 listModel->setRootPath(path.c_str());
87 listModel->setFilter(QDir::Files | QDir::NoDotAndDotDot);
88 listModel->setNameFilters(filter);
89 listModel->setNameFilterDisables(false);
90 listModel->sort(0, Qt::AscendingOrder);
91
92 listView = new QListView;
93 listView->setModel(listModel);
94 listView->setViewMode(QListView::IconMode);
95 listView->setIconSize(QSize(80, 80));
96 listView->setGridSize(QSize(120, 120));
97 listView->setUniformItemSizes(true);
98 listView->setResizeMode(QListWidget::Adjust);
99 listView->setTextElideMode(Qt::ElideRight);
100 listView->setRootIndex(listModel->index(path.c_str()));
101 layout->addWidget(listView);
102
103 connect(treeView, SIGNAL(clicked(const QModelIndex&)),
104 this, SLOT(treeItemSelected(const QModelIndex&)));
105
106 connect(listView, SIGNAL(clicked(const QModelIndex&)),
107 this, SLOT(assetItemSelected(const QModelIndex&)));
108
109 connect(listView, SIGNAL(doubleClicked(const QModelIndex&)),
110 this, SLOT(assetDoubleClicked(const QModelIndex&)));
111 }
112
113 void QContentBrowser::treeItemSelected(const QModelIndex& index)
114 {
115 QString name = model->fileName(index);
116 QString path = model->fileInfo(index).absolutePath() + "/" + name;
117
118 //A hack
119 QStringList filter;
120 filter << "*.png" << "*.jpg" << "*.bmp" << "*.obj" << "*.gltf" << "*.glb" << "*.fbx" << "*.STL" << "*.stl" << "*.xml";
121 auto* newListModel = new CustomFileSystemModel(this);
122 newListModel->setRootPath(path);
123 newListModel->setFilter(QDir::Files | QDir::NoDotAndDotDot);
124 newListModel->setNameFilters(filter);
125 newListModel->setNameFilterDisables(false);
126 newListModel->sort(0, Qt::AscendingOrder);
127
128 listView->setModel(newListModel);
129 listView->setRootIndex(newListModel->index(path));
130 }
131
132 void QContentBrowser::assetItemSelected(const QModelIndex& index)
133 {
134 QString name = model->fileName(index);
135 QString path = model->fileInfo(index).absolutePath() + "/" + name;
136
137
138 }
139
140 void QContentBrowser::assetDoubleClicked(const QModelIndex& index)
141 {
142 QString name = model->fileName(index);
143 QString path = model->fileInfo(index).absolutePath() + "/" + name;
144
145 std::cout << path.toStdString() << "\n";
146
147 auto ext = model->fileInfo(index).suffix().toStdString();
148 auto ext2Act = NodeFactory::instance()->nodeContentActions();
149 if (ext2Act.find(ext) != ext2Act.end())
150 {
151 auto func = ext2Act[ext];
152 if (func != nullptr) {
153 auto node = func(path.toStdString());
154
155 emit nodeCreated(node);
156 }
157 }
158 }
159}
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
CustomFileSystemModel(QObject *parent=nullptr)
static NodeFactory * instance()
std::map< std::string, std::function< std::shared_ptr< Node >(std::string)> > & nodeContentActions()
PConsoleWidget(QWidget *parent=nullptr)
void treeItemSelected(const QModelIndex &index)
QFileSystemModel * model
void assetDoubleClicked(const QModelIndex &index)
void nodeCreated(std::shared_ptr< Node > node)
void assetItemSelected(const QModelIndex &index)
QContentBrowser(QWidget *parent=nullptr)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25