/
projecthandle.cpp
340 lines (281 loc) · 8.58 KB
/
projecthandle.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
#include "projecthandle.h"
#include <iostream>
#include <stdio.h>
#include <QFileDialog>
#include <QByteArray>
ProjectHandle::ProjectHandle(QObject *parent) : QObject(parent)
{
map=NULL;
colorMap=NULL;
projName="Default project";
projPath="";
libPath="lib/lib.lst";
projObjects.clear();
version="0.01a";
}
void ProjectHandle::loadData(){
QString Name = QFileDialog::getOpenFileName(NULL,tr("Open picture"), ".",tr("Project files (*.bmp *.jpg *.jpeg *.png)"));
if (Name.isEmpty()){
return;
}
QImage* load_image = new QImage(Name);
New_dlg dlg(libPath);
QPixmap pic =QPixmap::fromImage(load_image->scaled(dlg.pic_label->size())) ;
dlg.pic_label->setPixmap(pic);
QFile libFile(libPath);
if(!libFile.open(QFile::ReadOnly | QFile::WriteOnly)){
emit addToLog("Не найден файл библиотек");
QDir dir;
QFileInfo inf(libPath);
dir=inf.dir();
dir.mkdir(dir.absolutePath());
QTextStream s(&libFile);
s << "";
}else{
QTextStream readS(&libFile);
while(!readS.atEnd()){
QString inp;
readS >> inp;
dlg.material_Box->addItem(inp);
}
}
if(!dlg.exec()){
delete load_image;
return;
}
colorMap = load_image;
PObject object;
if(dlg.width_sizeBox->currentText()=="um"){
object.width = dlg.width_SpinBox->value();
}else{
object.width = dlg.width_SpinBox->value()*1000;
}
if(dlg.height_sizeBox->currentText()=="um"){
object.height = dlg.height_SpinBox->value();
}else{
object.height = dlg.height_SpinBox->value()*1000;
}
object.map = new Image;
object.map->data = new uchar[colorMap->width()*colorMap->height()];
object.material = dlg.material_Box->currentText();
object.map->sizeX = colorMap->width() ;
object.map->sizeY = colorMap->height() ;
calculate(&object);
map=object.map;
object.path=Name;
QFileInfo inf(Name);
projObjects.insert(inf.fileName(),object);
curName=inf.fileName();
emit addToList(inf.fileName());
emit cleanGl();
emit set_glwMap(map);
emit imageLoad(map->sizeX,map->sizeY);
}
void ProjectHandle::changeCurrent(QModelIndex index){
emit cleanGl();
map=NULL;
delete colorMap;
map=projObjects[index.data().toString()].map;
colorMap = new QImage(projObjects[index.data().toString()].path);
if(map==NULL){
PObject object = projObjects[index.data().toString()];
object.map = new Image;
object.map->data = new uchar[colorMap->width()*colorMap->height()];
object.map->sizeX =colorMap->width() ;
object.map->sizeY =colorMap->height() ;
int k=0;
for(int i=0;i<(colorMap->height());i++){
for(int j=0;j<colorMap->width();j++){
QRgb tmp=colorMap->pixel(j,i);
uchar t=static_cast<uchar>(qBlue(tmp));
object.map->data[k]=t;
k++;
}
}
calculate(&object);
QFileInfo inf(object.path);
projObjects.insert(inf.fileName(),object);
map=object.map;
}
emit set_glwMap(map);
emit imageLoad(map->sizeX,map->sizeY);
return;
}
void ProjectHandle::newProject(){
if(projPath!=""){
if(closeProject()==1) return;
}
QFileDialog dlg;
dlg.setDefaultSuffix("fproj");
QString path = dlg.getSaveFileName(NULL,tr("Project location"), ".",tr("Project files (*.fproj)"));
if(path.isEmpty()){
return;
}
QFileInfo inf(path);
if(inf.suffix()!="fproj"){
QString tmp=inf.baseName();
tmp+=".fproj";
path=inf.absolutePath()+"/"+tmp;
}
this->projPath=path;
QDomDocument doc("xml version=\"1.0\" encoding=\"UTF-8\"");
QDomElement root = doc.createElement(inf.baseName());
doc.appendChild(root);
root.setAttribute("appVersion",version);
QFile pfile(path);
if(pfile.open(QIODevice::WriteOnly)){
QTextStream stream(&pfile);
stream << doc.toString();
pfile.close();
projName=inf.baseName();
}
return;
}
void ProjectHandle::saveProject(){
if(this->projPath==""){
QFileDialog dlg;
dlg.setDefaultSuffix("fproj");
QString path = dlg.getSaveFileName(NULL,tr("Project location"), ".",tr("Project files (*.fproj)"));
if(path.isEmpty()){
return;
}
QFileInfo inf(path);
if(inf.suffix()!="fproj"){
QString tmp=inf.baseName();
tmp+=".fproj";
path=inf.absolutePath()+"/"+tmp;
}
this->projPath=path;
}
QFileInfo inf(projPath);
QDomDocument doc(inf.baseName());
QDomElement root = doc.createElement("Project");
doc.appendChild(root);
root.setAttribute("appVersion",version);
root.setAttribute("Name",projName);
QHash<QString,PObject>::iterator iter;
iter=projObjects.begin();
for(int i=0;i<projObjects.size();i++){
PObject tmp=iter.value();
QDomElement obj = doc.createElement("Object");
root.appendChild(obj);
obj.setAttribute("Path",tmp.path);
obj.setAttribute("Material",tmp.material);
obj.setAttribute("Height",tmp.height);
obj.setAttribute("Width",tmp.width);
iter++;
}
QFile pfile(projPath);
if(pfile.open(QIODevice::WriteOnly)){
QTextStream stream(&pfile);
stream << doc.toString();
pfile.close();
projName=inf.baseName();
}
}
void ProjectHandle::clearProject(){
emit cleanGl();
emit cleanWindow();
if(map!=NULL){
if(map->data!=NULL){
delete map->data;
}
delete map;
map=NULL;
}
if(colorMap!=NULL){
delete colorMap;
colorMap = NULL;
}
projName="Default project";
projPath="";
projObjects.clear();
}
int ProjectHandle::closeProject(){
QMessageBox msgBox;
msgBox.setText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
// Save was clicked
saveProject();
clearProject();
return 0;
case QMessageBox::Discard:
// Don't Save was clicked
clearProject();
return 0;
case QMessageBox::Cancel:
// Cancel was clicked
return 1;
default:
// should never be reached
return 1;
}
}
void ProjectHandle::openProject()
{
QMessageBox msgBox;
if(projObjects.size()!=0){
if(closeProject()==1) return;
}
QFileDialog dlg;
QString path = dlg.getOpenFileName(NULL,tr("Project file"), ".",tr("Project files (*.fproj)"));
if(path.isEmpty()){
return;
}
this->projPath=path;
QFile pfile(path);
QDomDocument doc;
doc.setContent(&pfile);
QDomNode node = doc.documentElement();
if(node.toElement().tagName()=="Project"){
projName=node.toElement().attribute("Name");
node=node.firstChild();
}
while(!node.isNull()){
if(node.toElement().tagName()=="Object"){
PObject obj;
obj.path = node.toElement().attribute("Path");
obj.material = node.toElement().attribute("Material");
obj.height = node.toElement().attribute("Height").toFloat();
obj.width = node.toElement().attribute("Width").toFloat();
obj.map = NULL;
QFileInfo inf(obj.path);
projObjects.insert(inf.fileName(),obj);
emit addToList(inf.fileName());
}
node=node.nextSibling();
}
}
void ProjectHandle::openLib()
{
Lib_dialog *lib=new Lib_dialog(libPath);
lib->exec();
delete lib;
}
void ProjectHandle::calculate(PObject *obj)
{
/*
Sjuda pihaj raschet karty vysot. Poluchennuju kartu polozhish' v obj->map; U nej est' svojstvo data - sama karta.
Dannye o vseh cvetah budut hranitsja v colorMap. obrabotannuju kartu polozhi tuda zhe.
*/
//debug and testing block
//пока не написана обработка достаем синий канал
int k=0;
for(int i=0;i<(colorMap->height());i++){
for(int j=0;j<colorMap->width();j++){
QRgb tmp=colorMap->pixel(j,i);
uchar t=static_cast<uchar>(qBlue(tmp));
obj->map->data[k]=t;
k++;
}
}
//debug and testing block
}
PObject ProjectHandle::getPObj()
{
return projObjects[this->curName];
}