27 #include "../../../../common/Exception.h"
28 #include "../../../../common/Translator.h"
29 #include "../../../../dataaccess.h"
30 #include "../../../../qt/widgets/datasource/selector/DataSourceSelectorWidget.h"
31 #include "../../../../qt/widgets/datasource/selector/DataSourceSelectorWizardPage.h"
32 #include "../../../../qt/widgets/layer/utils/DataSet2Layer.h"
33 #include "../../../../qt/widgets/help/HelpPushButton.h"
34 #include "../../../../qt/widgets/rp/Utils.h"
35 #include "../../../../qt/widgets/utils/ScopedCursor.h"
36 #include "../../../../qt/af/ApplicationController.h"
37 #include "../../../../qt/af/Project.h"
38 #include "../../../../qt/af/events/LayerEvents.h"
39 #include "../../../../raster/Utils.h"
45 #include "ui_TL4ConverterWizardForm.h"
46 #include "ui_TL4ConnectorWizardPageForm.h"
47 #include "ui_TL4LayerSelectionWizardPageForm.h"
48 #include "ui_TL4RasterFolderSelectionWizardPageForm.h"
49 #include "ui_TL4FinalPageWizardPageForm.h"
55 #include <boost/filesystem.hpp>
56 #include <boost/lexical_cast.hpp>
59 #include <QtGui/QIcon>
60 #include <QtGui/QMessageBox>
61 #include <QtGui/QTableWidgetItem>
62 #include <QtGui/QGridLayout>
66 m_hasNonRaster(false),
69 m_ui(new Ui::TL4ConverterWizardForm)
77 m_connectorPage->setSubTitle(tr(
"Connect to a TerraLib 4.x database."));
95 m_resolveNamePage->setSubTitle(tr(
"Some layer names clashes with target data source names. Please, give a new name for the layers showed below"));
101 displayLayout->setContentsMargins(0,0,0,0);
104 labels <<
"" << tr(
"Source Names") << tr(
"Target Names");
109 m_finalPage->setSubTitle(tr(
"Select the layers that you want to create a Project Layer"));
119 connect(this->button(QWizard::NextButton), SIGNAL(pressed()),
this, SLOT(
next()));
120 connect(this->button(QWizard::BackButton), SIGNAL(pressed()),
this, SLOT(
back()));
121 connect(this->button(QWizard::FinishButton), SIGNAL(pressed()),
this, SLOT(
finish()));
122 connect(this->button(QWizard::CommitButton), SIGNAL(pressed()),
this, SLOT(
commit()));
126 this->setButton(QWizard::HelpButton, helpButton);
137 if(currentId() == PAGE_TERRALIB4_CONNECTOR)
138 return PAGE_LAYER_SELECTION;
140 if(currentId() == PAGE_LAYER_SELECTION)
143 return PAGE_DATASOURCE_SELECTOR;
146 return PAGE_RASTERFOLDER_SELECTOR;
148 return PAGE_LAYER_SELECTION;
151 if(currentId() == PAGE_DATASOURCE_SELECTOR)
154 return PAGE_RASTERFOLDER_SELECTOR;
156 return PAGE_NAME_RESOLVE_SELECTOR;
159 if(currentId() == PAGE_RASTERFOLDER_SELECTOR)
160 return PAGE_NAME_RESOLVE_SELECTOR;
162 if(currentId() == PAGE_NAME_RESOLVE_SELECTOR)
163 return PAGE_FINALPAGE;
170 int current_page_id = currentId();
172 if(current_page_id == PAGE_TERRALIB4_CONNECTOR)
174 if(!validTerraLib4Connection())
177 std::vector<std::string> datasets = m_tl4Database->getDataSetNames();
179 m_layerSelectionPage->setDatasets(datasets);
181 else if(current_page_id == PAGE_LAYER_SELECTION)
183 if(!validLayerSelection())
186 else if(current_page_id == PAGE_DATASOURCE_SELECTOR)
188 m_targetDataSource = *m_datasourceSelectorPage->getSelectorWidget()->getSelecteds().begin();
190 else if(current_page_id == PAGE_RASTERFOLDER_SELECTOR)
192 m_rasterFolderPath = m_rasterFolderSelectionPage->getPath();
194 else if(current_page_id == PAGE_NAME_RESOLVE_SELECTOR)
201 if(nextId() == PAGE_NAME_RESOLVE_SELECTOR)
205 std::vector<std::string> selectedLayer = m_layerSelectionPage->getChecked();
207 m_resolveNameTableWidget->clearContents();
208 m_resolveNameTableWidget->setRowCount(selectedLayer.size());
215 bool hasConflicts =
false;
217 for(std::size_t i = 0; i < selectedLayer.size(); ++i)
219 std::string targetDatasetName = selectedLayer[i];
222 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(selectedLayer[i]));
224 if(input_dataset_type->hasRaster())
226 QTableWidgetItem *conflictItem = 0;
228 if(boost::filesystem::exists(m_rasterFolderPath +
"/" + targetDatasetName +
".tif"))
232 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
236 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
239 conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
240 m_resolveNameTableWidget->setItem(i, 0, conflictItem);
245 QTableWidgetItem *conflictItem = 0;
247 if(tl5ds->dataSetExists(targetDatasetName))
251 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"delete"),
"");
255 conflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
258 conflictItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
259 m_resolveNameTableWidget->setItem(i, 0, conflictItem);
262 QTableWidgetItem *oldNameItem =
new QTableWidgetItem(selectedLayer[i].c_str());
263 oldNameItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
264 m_resolveNameTableWidget->setItem(i, 1, oldNameItem);
266 QTableWidgetItem *newNameItem =
new QTableWidgetItem(targetDatasetName.c_str());
267 m_resolveNameTableWidget->setItem(i, 2, newNameItem);
270 m_resolveNameTableWidget->resizeColumnsToContents();
271 m_resolveNameTableWidget->sortByColumn(1, Qt::AscendingOrder);
275 m_resolveNamePage->setTitle(tr(
"Resolve Name Conflicts"));
276 m_resolveNamePage->setSubTitle(tr(
"Some layer names clash with target data source dataset names. Please, give a new name for the layers showed below"));
280 m_resolveNamePage->setTitle(tr(
"No Name Conflicts"));
281 m_resolveNamePage->setSubTitle(tr(
"You can change the layer names in the target data source"));
285 catch(
const te::da::Exception& e)
287 QMessageBox::warning(
this, tr(
"Warning"), e.what());
292 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid this page!"));
301 std::map<std::string, std::string> connInfo = m_connectorPage->getConnInfo();
307 m_tl4Database->setConnectionInfo(connInfo);
308 m_tl4Database->open();
310 catch(
const te::da::Exception& e)
312 QMessageBox::warning(
this, tr(
"Warning"), e.what());
317 QMessageBox::warning(
this, tr(
"Warning"), tr(
"A connection to the informed Terralib 4.x database could not be established. Please, verify the informed parameters."));
326 if(!m_layerSelectionPage->hasChecked())
329 QMessageBox::warning(
this,
331 tr(
"Please, select at least one layer for conversion!"));
335 std::vector<std::string> layersNames = m_layerSelectionPage->getChecked();
338 m_hasNonRaster =
false;
345 for(std::size_t i = 0; i < layersNames.size(); ++i)
347 std::auto_ptr<te::da::DataSetType> dst(m_tl4Database->getDataSetType(layersNames[i]));
352 m_hasNonRaster =
true;
355 catch(
const te::da::Exception& e)
357 QMessageBox::warning(
this, tr(
"Warning"), e.what());
362 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid layer selection!"));
371 bool hasConflict =
false;
381 catch(
const te::da::Exception& e)
383 QMessageBox::warning(
this, tr(
"Warning"), e.what());
388 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
392 int nrows = m_resolveNameTableWidget->rowCount();
394 for(
int i = 0; i != nrows; ++i)
397 QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
402 std::string sourceName = item_source->text().toStdString();
405 QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
410 std::string targetName = item_target->text().toStdString();
417 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
419 if(input_dataset_type->hasRaster())
422 if(boost::filesystem::exists(m_rasterFolderPath +
"/" + targetName +
".tif"))
428 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
430 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
436 if(tl5ds->dataSetExists(targetName))
442 QTableWidgetItem *nonconflictItem =
new QTableWidgetItem(QIcon::fromTheme(
"check"),
"");
444 m_resolveNameTableWidget->setItem(i, 0, nonconflictItem);
448 catch(
const te::da::Exception& e)
450 QMessageBox::warning(
this, tr(
"Warning"), e.what());
455 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not valid layer names in target data source!"));
462 QString errMsg(tr(
"There still have name conflicts. Please, resolve the indicated conflicts before continue!"));
464 QMessageBox::warning(
this, tr(
"TerraLib 4.x Converter"), errMsg);
474 int rowCount = m_resolveNameTableWidget->rowCount();
476 for(
int i = 0; i < rowCount; ++i)
478 QString targetNameInTable = m_resolveNameTableWidget->item(i, 2)->text();
480 if(targetName.c_str() == targetNameInTable)
481 return m_resolveNameTableWidget->item(i, 1)->text().toStdString();
500 if(!validLayerNames())
507 std::vector<std::pair<std::string, std::string> > problematicDatasets;
508 std::vector<std::string> successfulDatasets;
517 catch(
const te::da::Exception& e)
520 QMessageBox::warning(
this, tr(
"Warning"), e.what());
526 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
530 int nrows = m_resolveNameTableWidget->rowCount();
532 for(
int i = 0; i != nrows; ++i)
535 QTableWidgetItem* item_source = m_resolveNameTableWidget->item(i, 1);
540 std::string sourceName = item_source->text().toStdString();
543 QTableWidgetItem* item_target = m_resolveNameTableWidget->item(i, 2);
548 std::string targetName = item_target->text().toStdString();
555 std::auto_ptr<te::da::DataSetType> input_dataset_type(m_tl4Database->getDataSetType(sourceName));
557 if(input_dataset_type->hasRaster())
560 std::auto_ptr<te::da::DataSet> ds = m_tl4Database->getDataSet(sourceName);
562 std::auto_ptr<te::rst::Raster> raster = ds->getRaster(
"Raster");
564 std::string newName = m_rasterFolderPath +
"/" + targetName +
".tif";
568 successfulDatasets.push_back(targetName);
573 input_dataset_type->setName(targetName);
575 std::auto_ptr<te::da::DataSetTypeConverter> dt_adapter(
new te::da::DataSetTypeConverter(input_dataset_type.get(), tl5ds->getCapabilities()));
577 std::auto_ptr<te::da::DataSet> ds(m_tl4Database->getDataSet(sourceName));
581 std::map<std::string, std::string> opt;
583 te::da::Create(tl5ds.get(), dt_adapter->getResult(), ds_adapter.get(), opt);
585 successfulDatasets.push_back(targetName);
590 std::pair<std::string, std::string> dproblem;
591 dproblem.first = sourceName;
592 dproblem.second = e.
what();
594 problematicDatasets.push_back(dproblem);
598 std::pair<std::string, std::string> dproblem;
599 dproblem.first = sourceName;
602 problematicDatasets.push_back(dproblem);
607 if(!problematicDatasets.empty())
609 QString error(tr(
"Some TerraLib 4.x Layers could not be converted: \n\n"));
612 for(std::size_t i = 0; i < problematicDatasets.size(); ++i)
614 error.append(QString(
" - ") + problematicDatasets[i].first.c_str() + QString(
""));
615 details.append(problematicDatasets[i].first.c_str() + QString(
":\n"));
616 details.append(problematicDatasets[i].second.c_str() + QString(
"\n\n"));
619 QMessageBox message(QMessageBox::Warning, tr(
"TerraLib 4.x Converter"), error, QMessageBox::Ok,
this);
620 message.setDetailedText(details);
626 m_finalPage->setDataSets(successfulDatasets);
633 std::vector<std::string> selected = m_finalPage->getSelected();
642 catch(
const te::da::Exception& e)
645 QMessageBox::warning(
this, tr(
"Warning"), e.what());
651 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Could not connect to TerraLib 5 data source!"));
659 for(std::size_t i = 0; i < selected.size(); ++i)
663 std::auto_ptr<te::da::DataSetType> sourceDt = m_tl4Database->getDataSetType(getOriginalName(selected[i]));
665 if(sourceDt->hasRaster())
667 std::map<std::string, std::string> connInfo;
668 connInfo[
"URI"] = m_rasterFolderPath +
"/" + selected[i] +
".tif";
676 std::auto_ptr<te::da::DataSetType> dsType = outDataSource->getDataSetType(selected[i]);
680 layer = converter(dt);
688 catch(
const te::da::Exception& e)
691 QMessageBox::warning(
this, tr(
"Warning"), e.what());
697 QMessageBox::warning(
this, tr(
"Warning"), tr(
"Automatic layer creation failed!"));
745 QMessageBox::warning(
this,
746 tr(
"TerraLib 4.x Converter"),
747 tr(
"This option is not implemented yet!\nWe will provide it soon!"));
std::auto_ptr< te::qt::widgets::DataSourceSelectorWizardPage > m_datasourceSelectorPage
bool validateCurrentPage()
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
std::auto_ptr< TL4LayerSelectionWizardPage > m_layerSelectionPage
An converter for DataSetType.
static std::auto_ptr< DataSource > make(const std::string &dsType)
std::auto_ptr< TL4RasterFolderSelectionWizardPage > m_rasterFolderSelectionPage
virtual const char * what() const
It outputs the exception message.
static ApplicationController & getInstance()
It gives access to the controller singleton.
Push button that uses te::qt::widgets::HelpManager on its mouse pressed implementation.
This event signals that a new layer was created.
TEQTWIDGETSEXPORT te::map::AbstractLayerPtr createLayer(const std::string &driverName, const std::map< std::string, std::string > &connInfo)
void setPageReference(const QString &ref)
Sets the documentation page reference.
std::auto_ptr< QTableWidget > m_resolveNameTableWidget
TERASTEREXPORT te::rst::RasterPtr CreateCopy(const te::rst::Raster &rin, const std::string &uri, const std::string &rType=std::string("GDAL"))
Create a new raster from existing one.
boost::shared_ptr< DataSource > DataSourcePtr
std::auto_ptr< Ui::TL4ConverterWizardForm > m_ui
std::auto_ptr< TL4FinalPageWizardPage > m_finalPage
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
boost::shared_ptr< DataSetType > DataSetTypePtr
#define TE_QT_PLUGIN_TERRALIB4(message)
It marks a string in order to get translated. This is a special mark used in the TERRALIB4 QtPlugin o...
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
bool validTerraLib4Connection()
std::string getOriginalName(const std::string &targetName)
TL4ConverterWizard(QWidget *parent=0, Qt::WindowFlags f=0)
std::auto_ptr< TL4ConnectorWizardPage > m_connectorPage
void broadcast(te::qt::af::evt::Event *evt)
Send events in broadcast for all registered components.
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
std::auto_ptr< QWizardPage > m_resolveNamePage
A wizard page that provides the capability of connecting to a TerraLib 4.x database.
An object that when created shows a cursor during its scope.
bool validLayerSelection()