3 Copyright (C) 2010 Dzmitry Kamiahin <dnk-88@tut.by>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "vegetable_dialog.h"
24 #include <QtGui/QFileDialog>
25 #include <QtGui/QMessageBox>
28 #include <nel/3d/vegetable.h>
29 #include <nel/3d/tile_vegetable_desc.h>
30 #include "nel/misc/file.h"
38 CVegetableDialog::CVegetableDialog(QWidget
*parent
)
43 connect(_ui
.addtPushButton
, SIGNAL(clicked()), this, SLOT(addVegetList()));
44 connect(_ui
.insPushButton
, SIGNAL(clicked()), this, SLOT(insVegetList()));
45 connect(_ui
.delPushButton
, SIGNAL(clicked()), this, SLOT(removeVegetList()));
46 connect(_ui
.clearPushButton
, SIGNAL(clicked()), this, SLOT(clearVegetList()));
47 connect(_ui
.copyVegetPushButton
, SIGNAL(clicked()), this, SLOT(copyVegetable()));
48 connect(_ui
.loadVegetdescPushButton
, SIGNAL(clicked()), this, SLOT(loadVegetdesc()));
49 connect(_ui
.saveVegetdescPushButton
, SIGNAL(clicked()), this, SLOT(saveVegetdesc()));
50 connect(_ui
.loadVegetsetPushButton
, SIGNAL(clicked()), this, SLOT(loadVegetset()));
51 connect(_ui
.appendVegetsetPushButton
, SIGNAL(clicked()), this, SLOT(appendVegetset()));
52 connect(_ui
.saveVegetsetPushButton
, SIGNAL(clicked()), this, SLOT(saveVegetset()));
53 connect(_ui
.showVegetCheckBox
, SIGNAL(toggled(bool)), this, SLOT(setVisibleVegetables(bool)));
54 connect(_ui
.listWidget
, SIGNAL(currentRowChanged(int)), this, SLOT(setCurrentItem(int)));
57 CVegetableDialog::~CVegetableDialog()
61 void CVegetableDialog::loadVegetset()
63 // ask name of the load new vegetset file
64 QString fileName
= QFileDialog::getOpenFileName(this, tr("Load a new vegetset file"),
66 tr("vegetset files (*.vegetset);;"));
67 if (!fileName
.isEmpty())
69 NL3D::CTileVegetableDesc vegetSet
;
70 // if succes to load the vegetSet
71 if(Modules::veget().loadVegetableSet(vegetSet
, fileName
.toUtf8().constData()))
73 // Delete all vegetables.
74 Modules::veget().clearVegetables();
76 // build them from list.
77 Modules::veget().appendVegetableSet(vegetSet
);
80 Modules::veget().refreshVegetableDisplay();
87 void CVegetableDialog::appendVegetset()
89 // ask name of the load new vegetset file
90 QString fileName
= QFileDialog::getOpenFileName(this, tr("Append vegetset file"),
92 tr("vegetset files (*.vegetset);;"));
93 if (!fileName
.isEmpty())
95 NL3D::CTileVegetableDesc vegetSet
;
96 // if succes to load the vegetSet
97 if(Modules::veget().loadVegetableSet(vegetSet
, fileName
.toUtf8().constData()))
99 // Do not Delete any vegetables.
100 // build them from list.
101 Modules::veget().appendVegetableSet(vegetSet
);
104 Modules::veget().refreshVegetableDisplay();
111 void CVegetableDialog::saveVegetset()
113 NL3D::CTileVegetableDesc vegetSet
;
115 // first build the vegetSet.
116 Modules::veget().buildVegetableSet(vegetSet
);
118 // Then try to save it.
119 QString fileName
= QFileDialog::getSaveFileName(this, tr("Save Vegetable Set"),
121 tr("VegetSetFiles (*.vegetset);;"));
123 if (!fileName
.isEmpty())
127 if( f
.open(fileName
.toUtf8().constData()) )
131 // save the vegetable set
134 catch(NLMISC::EStream
&e
)
136 QMessageBox::critical(this, "Failed to save file!", QString(e
.what()), QMessageBox::Ok
);
140 QMessageBox::critical(this, "Failed to save file!", QString("Failed to open file for write!"), QMessageBox::Ok
);
145 void CVegetableDialog::addVegetList()
147 // Add a new vegetable to the list.
148 uint id
= _ui
.listWidget
->count();
150 Modules::veget().insEmptyVegetDesc(id
);
153 QListWidgetItem
*item
= new QListWidgetItem(_ui
.listWidget
);
154 item
->setText(QString(Modules::veget().getVegetable(id
)->_vegetableName
.c_str()));
157 Modules::veget().refreshVegetableDisplay();
160 void CVegetableDialog::removeVegetList()
162 sint id
= _ui
.listWidget
->currentRow();
165 Modules::veget().delVegetDesc(id
);
167 QListWidgetItem
*item
= _ui
.listWidget
->takeItem(id
);
172 _ui
.listWidget
->setCurrentRow(id
);
175 Modules::veget().refreshVegetableDisplay();
178 void CVegetableDialog::insVegetList()
180 sint id
= _ui
.listWidget
->currentRow();
183 // Add a new vegetable to the list.
184 Modules::veget().insEmptyVegetDesc(id
);
187 QListWidgetItem
*item
= new QListWidgetItem();
188 item
->setText(QString(Modules::veget().getVegetable(id
)->_vegetableName
.c_str()));
189 _ui
.listWidget
->insertItem(id
, item
);
192 Modules::veget().refreshVegetableDisplay();
196 // perform like an add.
201 void CVegetableDialog::clearVegetList()
203 if (_ui
.listWidget
->count() == 0) return;
204 int ok
= QMessageBox::question(this, "Clear List", QString("Clear all the list?"), QMessageBox::Yes
| QMessageBox::No
);
205 if (ok
== QMessageBox::Yes
)
207 Modules::veget().clearVegetables();
209 _ui
.listWidget
->clear();
212 Modules::veget().refreshVegetableDisplay();
216 void CVegetableDialog::copyVegetable()
220 void CVegetableDialog::loadVegetdesc()
222 QString fileName
= QFileDialog::getOpenFileName(this, tr("Open Vegetable Descriptor"),
224 tr("vegetdesc files (*.vegetdesc);;"));
225 if (!fileName
.isEmpty())
229 if( f
.open(fileName
.toUtf8().constData()) )
231 NL3D::CVegetable veget
;
234 // read the vegetable
237 // Add a new vegetable to the list.
238 uint id
= Modules::veget().addVegetDesc(veget
);
241 QListWidgetItem
*item
= new QListWidgetItem(_ui
.listWidget
);
242 item
->setText(QString(Modules::veget().getVegetable(id
)->_vegetableName
.c_str()));
245 Modules::veget().refreshVegetableDisplay();
247 catch(NLMISC::EStream
&e
)
249 QMessageBox::critical(this, "Failed to load file!", QString(e
.what()), QMessageBox::Ok
);
253 QMessageBox::critical(this, "Failed to open file!", QString("Failed to open file for read!"), QMessageBox::Ok
);
257 void CVegetableDialog::saveVegetdesc()
259 sint id
= _ui
.listWidget
->currentRow();
262 CVegetableNode
*vegetNode
= Modules::veget().getVegetable(id
);
264 QString oldFileName
= QString(vegetNode
->_vegetableName
.c_str()) + ".vegetdesc";
266 // Then try to save it.
267 QString fileName
= QFileDialog::getSaveFileName(this, tr("Save Vegetable Descriptor"),
269 tr("VegetDescFiles (*.vegetdesc);;"));
271 if (!fileName
.isEmpty())
275 if( f
.open(fileName
.toUtf8().constData()) )
279 // save the vegetable
280 f
.serial(*vegetNode
->_vegetable
);
282 catch(NLMISC::EStream
&e
)
284 QMessageBox::critical(this, "Failed to save file!", QString(e
.what()), QMessageBox::Ok
);
288 QMessageBox::critical(this, "Failed to save file!", QString("Failed to open file for write!"), QMessageBox::Ok
);
292 void CVegetableDialog::setVisibleVegetables(bool state
)
295 Modules::veget().enableLandscapeVegetable(state
);
298 void CVegetableDialog::setCurrentItem(int row
)
300 NL3D::CVegetable
*veget
= NULL
;
302 veget
= Modules::veget().getVegetable(row
)->_vegetable
;
303 _ui
.densityPage
->setVegetableToEdit(veget
);
304 _ui
.appearancePage
->setVegetableToEdit(veget
);
305 _ui
.scalePage
->setVegetableToEdit(veget
);
306 _ui
.rotatePage
->setVegetableToEdit(veget
);
309 void CVegetableDialog::updateVegetList()
311 std::vector
<std::string
> listVegetables
;
312 Modules::veget().getListVegetables(listVegetables
);
314 _ui
.listWidget
->clear();
316 for (size_t i
= 0; i
< listVegetables
.size(); i
++)
318 QListWidgetItem
*item
= new QListWidgetItem(_ui
.listWidget
);
319 item
->setText(QString(listVegetables
[i
].c_str()));
323 } /* namespace NLQT */