Linux multi-monitor fullscreen support
[ryzomcore.git] / studio / src / plugins / object_viewer / vegetable / vegetable_dialog.cpp
blob2fc621411c246a882e181b8872f58a3421e8e6cb
1 /*
2 Object Viewer Qt
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/>.
20 #include "stdpch.h"
21 #include "vegetable_dialog.h"
23 // Qt includes
24 #include <QtGui/QFileDialog>
25 #include <QtGui/QMessageBox>
27 // NeL includes
28 #include <nel/3d/vegetable.h>
29 #include <nel/3d/tile_vegetable_desc.h>
30 #include "nel/misc/file.h"
32 // Project includes
33 #include "modules.h"
35 namespace NLQT
38 CVegetableDialog::CVegetableDialog(QWidget *parent)
39 : QDockWidget(parent)
41 _ui.setupUi(this);
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"),
65 ".",
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);
79 // update 3D view
80 Modules::veget().refreshVegetableDisplay();
82 updateVegetList();
87 void CVegetableDialog::appendVegetset()
89 // ask name of the load new vegetset file
90 QString fileName = QFileDialog::getOpenFileName(this, tr("Append vegetset file"),
91 ".",
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);
103 // update 3D view
104 Modules::veget().refreshVegetableDisplay();
106 updateVegetList();
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"),
120 ".",
121 tr("VegetSetFiles (*.vegetset);;"));
122 // after check
123 if (!fileName.isEmpty())
125 NLMISC::COFile f;
127 if( f.open(fileName.toUtf8().constData()) )
131 // save the vegetable set
132 f.serial(vegetSet);
134 catch(NLMISC::EStream &e)
136 QMessageBox::critical(this, "Failed to save file!", QString(e.what()), QMessageBox::Ok);
139 else
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);
152 // update view
153 QListWidgetItem *item = new QListWidgetItem(_ui.listWidget);
154 item->setText(QString(Modules::veget().getVegetable(id)->_vegetableName.c_str()));
156 // update 3D view
157 Modules::veget().refreshVegetableDisplay();
160 void CVegetableDialog::removeVegetList()
162 sint id = _ui.listWidget->currentRow();
163 if(id == -1) return;
165 Modules::veget().delVegetDesc(id);
167 QListWidgetItem *item = _ui.listWidget->takeItem(id);
168 delete item;
170 --id;
172 _ui.listWidget->setCurrentRow(id);
174 // update 3D view
175 Modules::veget().refreshVegetableDisplay();
178 void CVegetableDialog::insVegetList()
180 sint id = _ui.listWidget->currentRow();
181 if(id != -1)
183 // Add a new vegetable to the list.
184 Modules::veget().insEmptyVegetDesc(id);
186 // update view
187 QListWidgetItem *item = new QListWidgetItem();
188 item->setText(QString(Modules::veget().getVegetable(id)->_vegetableName.c_str()));
189 _ui.listWidget->insertItem(id, item);
191 // update 3D view
192 Modules::veget().refreshVegetableDisplay();
194 else
196 // perform like an add.
197 addVegetList();
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();
211 // update 3D view
212 Modules::veget().refreshVegetableDisplay();
216 void CVegetableDialog::copyVegetable()
220 void CVegetableDialog::loadVegetdesc()
222 QString fileName = QFileDialog::getOpenFileName(this, tr("Open Vegetable Descriptor"),
223 ".",
224 tr("vegetdesc files (*.vegetdesc);;"));
225 if (!fileName.isEmpty())
227 NLMISC::CIFile f;
229 if( f.open(fileName.toUtf8().constData()) )
231 NL3D::CVegetable veget;
234 // read the vegetable
235 f.serial(veget);
237 // Add a new vegetable to the list.
238 uint id = Modules::veget().addVegetDesc(veget);
240 // update view
241 QListWidgetItem *item = new QListWidgetItem(_ui.listWidget);
242 item->setText(QString(Modules::veget().getVegetable(id)->_vegetableName.c_str()));
244 // update 3D view
245 Modules::veget().refreshVegetableDisplay();
247 catch(NLMISC::EStream &e)
249 QMessageBox::critical(this, "Failed to load file!", QString(e.what()), QMessageBox::Ok);
252 else
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();
260 if(id == -1) return;
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"),
268 oldFileName,
269 tr("VegetDescFiles (*.vegetdesc);;"));
270 // after check
271 if (!fileName.isEmpty())
273 NLMISC::COFile f;
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);
287 else
288 QMessageBox::critical(this, "Failed to save file!", QString("Failed to open file for write!"), QMessageBox::Ok);
292 void CVegetableDialog::setVisibleVegetables(bool state)
294 // update view.
295 Modules::veget().enableLandscapeVegetable(state);
298 void CVegetableDialog::setCurrentItem(int row)
300 NL3D::CVegetable *veget = NULL;
301 if (row != -1)
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 */