-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarehouse3d.cpp
190 lines (177 loc) · 7.79 KB
/
warehouse3d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "warehouse3d.h"
#include <QDirIterator>
#include <QDebug>
#include <static_global.h>
#include <QImage>
#include <QVector2D>
QSet<QString> Warehouse3D::m_3Dasset_extensions=QSet<QString>()<< "obj"<<"dae"<<"3ds";
QSet<QString> Warehouse3D::m_image_extensions=QSet<QString>()<< "jpg"<<"jpeg"<<"png";
Warehouse3D::Warehouse3D(QObject *parent): QAbstractListModel(parent)
{
/*Default models*/
Object snow;
snow.decoration_img="qrc:/resources/Resources/Assets3D/snow/preview.png";
snow.properties["thumbnail"]=snow.decoration_img;
snow.properties["main_asset_url"]="qrc:/resources/Resources/Assets3D/snow/snow.obj";
snow.properties["main_asset_diffuse_map_url"]="qrc:/resources/Resources/Assets3D/snow/texture.png";
snow.properties["weight"]=0.03;
snow.properties["extent"]=QVector2D(-150,150);
snow.properties["type"]="uniform";
m_objectsIndexes.append(snow);
Object solar_pannel;
solar_pannel.decoration_img="qrc:/resources/Resources/Assets3D/solar_pannel/preview.png";
solar_pannel.properties["thumbnail"]=solar_pannel.decoration_img;
solar_pannel.properties["main_asset_url"]="qrc:/resources/Resources/Assets3D/solar_pannel/Solar.obj";
solar_pannel.properties["main_asset_diffuse_map_url"]="qrc:/resources/Resources/Assets3D/solar_pannel/Solar.png";
solar_pannel.properties["weight"]=0.05;
solar_pannel.properties["extent"]=QVector2D(-250,250);
solar_pannel.properties["type"]="uniform";
m_objectsIndexes.append(solar_pannel);
/*.........*/
QDirIterator it(assets3DPath);
while(it.hasNext()){
it.next();
Object obj;
if(it.fileInfo().isDir() && it.fileName()!="." && it.fileName()!=".." ){
obj.name=it.fileName();
bool isValid=true;
//Enter the dir
QDirIterator it2(it.fileInfo().canonicalFilePath());
while(it2.hasNext()){
it2.next();
if(it2.fileInfo().suffix()=="asset"){
QFile configFile(it2.filePath());
configFile.open(QFile::ReadOnly);
if(configFile.isOpen()){
QTextStream inputStream(&configFile);
QString line;
do{
line=inputStream.readLine();
if(!line.isEmpty()){
QStringList parts=line.split(":");
if(parts.size()>1){
obj.properties[parts[0]]=parts[1];
}
}
}while(!line.isEmpty());
configFile.close();
}
if(obj.properties.contains("thumbnail")){
QString thumbFile=it.fileInfo().canonicalFilePath()+"/"+obj.properties["thumbnail"].toString();
QImage img(thumbFile);
if(!img.isNull()){
obj.decoration_img=QUrl::fromLocalFile(thumbFile);
obj.properties["thumbnail"]=QUrl::fromLocalFile(thumbFile);
}
else
isValid=false;
}
else
isValid=false;
if(obj.properties.contains("main_asset_url")){
obj.properties["main_asset_url"]=QUrl::fromLocalFile(it.fileInfo().canonicalFilePath()+
"/"+obj.properties["main_asset_url"].toString());
}
else
isValid=false;
if(obj.properties.contains("main_asset_diffuse_map_url")){
obj.properties["main_asset_diffuse_map_url"]=QUrl::fromLocalFile(it.fileInfo().canonicalFilePath()+
"/"+obj.properties["main_asset_diffuse_map_url"].toString());
}
else
isValid=false;
if(obj.properties.contains("weight")){
auto weight=obj.properties["weight"].toString().toFloat();
if(weight>0)
obj.properties["weight"]=weight;
else isValid=false;
}
else
isValid=false;
if(obj.properties.contains("extent")){
QString stringExtent=obj.properties["extent"].toString();
QStringList minmax=stringExtent.split(",");
obj.properties["extent"]=QVector2D(qMin(minmax[0].toFloat(),minmax[1].toFloat()),
qMax(minmax[0].toFloat(),minmax[1].toFloat()));
}
else
isValid=false;
if(obj.properties.contains("type")){
QString load_type=obj.properties["type"].toString();
if(load_type.compare("uniform",Qt::CaseInsensitive))
obj.properties["type"]="uniform";
else
obj.properties["type"]="";
}
break;
}
}
if(!obj.name.isEmpty() && isValid )
m_objectsIndexes.append(obj);
}
}
}
int Warehouse3D::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_objectsIndexes.size();
}
QVariant Warehouse3D::data(const QModelIndex &index, int role) const
{
if((index.row()+index.column())>= m_objectsIndexes.size()) return QVariant();
switch (role) {
case Qt::DecorationRole:
return m_objectsIndexes[index.row()+index.column()].decoration_img;
break;
case Qt::DisplayRole:
return m_objectsIndexes[index.row()+index.column()].name;
break;
case Qt::WhatsThisRole:
if(m_objectsIndexes[index.row()+index.column()].properties.contains("type"))
return m_objectsIndexes[index.row()+index.column()].properties["type"];
else
return QVariant();
break;
default:
return QVariant();
break;
}
}
QVariant Warehouse3D::get(int index, QString info) const
{
if(index<0 || index>=m_objectsIndexes.size()) return QVariant();
if(info.compare("Properties",Qt::CaseInsensitive)==0){
return m_objectsIndexes[index].properties;
}
if(info.compare("main_asset_url",Qt::CaseInsensitive)==0){
if(m_objectsIndexes[index].properties.contains("main_asset_url"))
return m_objectsIndexes[index].properties["main_asset_url"];
else
return QVariant();
}
if(info.compare("main_asset_diffuse_map_url",Qt::CaseInsensitive)==0){
if(m_objectsIndexes[index].properties.contains("main_asset_diffuse_map_url"))
return m_objectsIndexes[index].properties["main_asset_diffuse_map_url"];
else
return QVariant();
}
if(info.compare("weight",Qt::CaseInsensitive)==0){
if(m_objectsIndexes[index].properties.contains("weight"))
return m_objectsIndexes[index].properties["weight"];
else
return QVariant();
}
if(info.compare("extent",Qt::CaseInsensitive)==0){
if(m_objectsIndexes[index].properties.contains("extent"))
return m_objectsIndexes[index].properties["extent"];
else
return QVariant();
}
if(info.compare("type",Qt::CaseInsensitive)==0){
if(m_objectsIndexes[index].properties.contains("type"))
return m_objectsIndexes[index].properties["type"];
else
return QVariant();
}
return QVariant();
}