-
Notifications
You must be signed in to change notification settings - Fork 0
/
materialsmanager.cpp
367 lines (338 loc) · 13.8 KB
/
materialsmanager.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "materialsmanager.h"
#include "static_global.h"
#include <QDirIterator>
#include <QTextStream>
#include <QDebug>
#include <QNetworkInterface>
#include <QDateTime>
#include <QImage>
MaterialsManager::MaterialsManager(QObject *parent):
QAbstractListModel(parent)
{
init();
}
void MaterialsManager::init()
{
beginResetModel();
m_materialsFilePath.clear();
m_materials.clear();
m_materialsIndex.clear();
/*Creating default material*/
Material default_material;
default_material.set("Conifers(C14)",QUrl("qrc:/images/Images/woodbackground.png"),
20,0.350e-9,7000,440,8,16,14,0.4,2,3);
default_material.uniqueID="default";
m_materials[default_material.uniqueID]=default_material;
m_materialsFilePath.append("");
m_materialsIndex.append(default_material.uniqueID);
/*....*/
QString materialsDir=materialsPath;
QDirIterator dirIt(materialsDir);
while(dirIt.hasNext()){
dirIt.next();
if(dirIt.fileInfo().isDir()&& dirIt.fileName()!="." && dirIt.fileName()!=".."){
QDirIterator it(dirIt.fileInfo().canonicalFilePath());
while(it.hasNext()){
it.next();
if(it.fileInfo().suffix()=="material"){
QFile inputFile(it.filePath());
if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug()<<"Failed to open material file"<<it.filePath();
continue;
}
QTextStream inputStream(&inputFile);
QString line;
QStringList sub_parts,property;
Material material;
material.set(QString(),QUrl(),0,0,0,0,0,0,0,0,0,0);
do{
line=inputStream.readLine();
sub_parts=line.split(";",QString::SplitBehavior::SkipEmptyParts);
Q_FOREACH(QString part,sub_parts){
property=part.split(":",QString::SplitBehavior::SkipEmptyParts);
if(property.size()!=2){
qDebug()<<"Wrong property format: "<<part;
continue;
}
if(property[0]=="Name"){
material.name=property[1];
}
else if(property[0]=="UniqueID"){
material.uniqueID=property[1];
}
else if(property[0]=="Price") {
bool ok;
material.price=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="Density") {
bool ok;
material.density=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="Young") {
bool ok;
material.young=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="G") {
bool ok;
material.g=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="TextureImage") {
material.texture_img=QUrl::fromLocalFile(it.fileInfo().canonicalPath()+
"/"+property[1]);
}
else if(property[0]=="ft0") {
bool ok;
material.ft0=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="fc0") {
bool ok;
material.fc0=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="fmk") {
bool ok;
material.fmk=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="ft90") {
bool ok;
material.ft90=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="fc90") {
bool ok;
material.fc90=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else if(property[0]=="fvk") {
bool ok;
material.fvk=property[1].toFloat(&ok);
if(!ok) qDebug()<<"Convertion error:"<<part;
}
else{qDebug()<<"Unknown property:"<<part;}
}
}while(!line.isNull());
if(material.uniqueID.isEmpty() || material.name.isEmpty() || material.density<=0 ||
material.g<=0 || material.young<=0 || m_materials.contains(material.uniqueID)
|| material.fc0<=0 || material.fc90<=0 || material.fmk<=0 || material.ft0<=0
||material.ft90<=0||material.fvk<=0){
qDebug()<<"Invalid material or duplicated Id:"<< it.filePath();
}
else{
m_materials[material.uniqueID]=material;
m_materialsFilePath.append(it.fileInfo().canonicalFilePath());
m_materialsIndex.append(material.uniqueID);
}
}
}
}
}
endResetModel();
}
int MaterialsManager::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_materials.size();
}
QVariant MaterialsManager::data(const QModelIndex &index, int role) const
{
if((index.row()+index.column())>= m_materials.size()) return QVariant();
switch (role) {
case Qt::DecorationRole:
return m_materials[m_materialsIndex[(index.row()+index.column())]].texture_img;
break;
case Qt::DisplayRole:
return m_materials[m_materialsIndex[(index.row()+index.column())]].name;
break;
default:
return QVariant();
break;
}
}
QVariant MaterialsManager::get(int index, QString info) const
{
if(index<0 || index>=m_materials.size()) return QVariant();
if(info.compare("G",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].g;
}
else if(info.compare("Young",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].young;
}
else if(info.compare("Price",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].price;
}
else if(info.compare("Density",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].density;
}
else if(info.compare("UniqueID",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].uniqueID;
}
else if(info.compare("ft0",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].ft0;
}
else if(info.compare("fc0",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].fc0;
}
else if(info.compare("fmk",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].fmk;
}
else if(info.compare("ft90",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].ft90;
}
else if(info.compare("fc90",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].fc90;
}
else if(info.compare("fvk",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].fvk;
}
else if(info.compare("image",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].texture_img;
}
else if(info.compare("name",Qt::CaseInsensitive)==0){
return m_materials[m_materialsIndex[index]].name;
}
return QVariant();
}
QVariant MaterialsManager::get(QString uniqueID, QString info) const
{
if(!m_materials.contains(uniqueID)) return QVariant();
if(info.compare("G",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].g;
}
else if(info.compare("Young",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].young;
}
else if(info.compare("Price",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].price;
}
else if(info.compare("Density",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].density;
}
else if(info.compare("Index",Qt::CaseInsensitive)==0){
return m_materialsIndex.indexOf(uniqueID);
}
else if(info.compare("ft0",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].ft0;
}
else if(info.compare("fc0",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].fc0;
}
else if(info.compare("fmk",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].fmk;
}
else if(info.compare("ft90",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].ft90;
}
else if(info.compare("fc90",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].fc90;
}
else if(info.compare("fvk",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].fvk;
}
else if(info.compare("name",Qt::CaseInsensitive)==0){
return m_materials[uniqueID].name;
}
return QVariant();
}
bool MaterialsManager::createFile(QUrl imageUrl, QString name, QString density, QString price, QString Young, QString G,
QString fc0,QString fc90,
QString fmk,QString ft0,
QString ft90, QString fvk)
{
if(name.isEmpty() || density.isEmpty() || price.isEmpty() || Young.isEmpty() || G.isEmpty()){
//emit error("All fields should be filled");
return false;
}
auto netInterfaces=QNetworkInterface::allInterfaces();
if(netInterfaces.size()==0){
return false;
}
QString mac;
for(ushort interfaceID=0;interfaceID<netInterfaces.size();interfaceID++){
mac=netInterfaces[interfaceID].hardwareAddress();
if(!mac.isEmpty() && mac!="00:00:00:00:00:00" && mac!="02:00:00:00:00:00")
break;
else{
QFile macFile("/sys/class/net/"+netInterfaces[interfaceID].name()+"/address");
if (macFile.open(QFile::ReadOnly)){
QTextStream textStream(&macFile);
mac= QString(textStream.readLine());
macFile.close();
if(!mac.isEmpty() && mac!="00:00:00:00:00:00" && mac!="02:00:00:00:00:00")
break;
}
}
}
if(mac.isEmpty() || mac=="00:00:00:00:00:00" || mac=="02:00:00:00:00:00"){
qsrand(QDateTime::currentMSecsSinceEpoch());
mac="AAAAAA"+mac.setNum(qrand()%5000000,16);
}
mac.remove(':');
QString uniqueStr=mac+"-"+QDateTime::currentDateTime().toString(QString("dMMMyyhmsz"));
QString path=materialsPath+"/"+uniqueStr+"/";
QDir dir;
if(dir.exists(path)){
//emit error("Directoy exists");
return false;
}
if(!dir.mkdir(path)){
//emit error("Can't create dir");
return false;
}
QFile file(path+uniqueStr+".material");
file.open(QFile::WriteOnly);
QImage image(imageUrl.toLocalFile());
if(image.isNull())
image.load(":/ui/UI/MaterialDesigner/assets/No_Image_Available.jpg");
if(file.isOpen()){
QTextStream out(&file);
out<< "UniqueID:"<<uniqueStr<<";\n"
<<"Name:"<<name<<";\n"
<<"Density:"<<density<<";\n"
<<"Price:"<<price<<";\n"
<<"Young:"<<Young<<";\n"
<<"G:"<<G<<";\n"
<<"fc0:"<<fc0<<";\n"
<<"fc90:"<<fc90<<";\n"
<<"ft0:"<<ft0<<";\n"
<<"ft90:"<<ft90<<";\n"
<<"fmk:"<<fmk<<";\n"
<<"fvk:"<<fvk<<";\n"
<<"TextureImage:"<<uniqueStr<<".png;";
file.close();
image=image.scaledToWidth(200);
image.save(path+uniqueStr+".png");
init();
return true;
}
else{
file.remove();
//emit error("Can't create file or read image");
return false;
}
}
void MaterialsManager::deleteMaterial(int index){
if(index<0 || index>m_materialsFilePath.size()) return;
if(m_materialsFilePath[index].isEmpty()) return;
if(m_materialsFilePath[index].startsWith(QFileInfo(materialsPath).canonicalFilePath())){
QFileInfo info(m_materialsFilePath[index]);
QString path=info.canonicalPath();
QFile::remove(m_materialsFilePath[index]);
QUrl img_url=m_materials[m_materialsIndex[index]].texture_img;
if(img_url.isLocalFile()){
QString img_path=img_url.toLocalFile();
QFileInfo img_fileInfo(img_path);
if(img_fileInfo.canonicalFilePath().startsWith(QFileInfo(materialsPath).canonicalFilePath())){
QFile::remove(img_path);
}
}
QDir dir;
dir.rmdir(path);
init();
}
}