27 #include "../../../../common/STLUtils.h" 28 #include "../../../../dataaccess/datasource/DataSourceFactory.h" 29 #include "../../../../dataaccess/datasource/DataSourceInfoManager.h" 30 #include "../../../../dataaccess/datasource/DataSourceManager.h" 31 #include "../../../../dataaccess/utils/Utils.h" 32 #include "../../../../datatype/Utils.h" 33 #include "../../../../qt/widgets/datasource/selector/DataSourceSelectorDialog.h" 34 #include "../../../../qt/widgets/property/NewPropertyWidget.h" 35 #include "../../../../se/Style.h" 36 #include "../../layer/utils/DataSet2Layer.h" 38 #include "ui_CreateLayerWidgetForm.h" 41 #include <QFileDialog> 42 #include <QMessageBox> 45 #include <boost/filesystem.hpp> 46 #include <boost/uuid/random_generator.hpp> 47 #include <boost/uuid/uuid_io.hpp> 51 te::qt::widgets::CreateLayerWidget::CreateLayerWidget(
QWidget* parent, Qt::WindowFlags f)
53 m_ui(new
Ui::CreateLayerWidgetForm)
57 m_ui->m_targetDatasourceToolButton->setIcon(QIcon::fromTheme(
"datasource"));
58 m_ui->m_addToolButton->setIcon(QIcon::fromTheme(
"list-add"));
59 m_ui->m_clearToolButton->setIcon(QIcon::fromTheme(
"edit-clear"));
62 QGridLayout* layout =
new QGridLayout(m_ui->m_widget);
63 layout->setContentsMargins(0, 0, 0, 0);
67 layout->addWidget(m_newPropWidget);
70 m_removeMapper =
new QSignalMapper(
this);
73 connect(m_ui->m_addToolButton, SIGNAL(pressed()),
this, SLOT(onAddPushButtonClicked()));
74 connect(m_ui->m_targetDatasourceToolButton, SIGNAL(pressed()),
this, SLOT(onTargetDatasourceToolButtonPressed()));
75 connect(m_ui->m_targetFileToolButton, SIGNAL(pressed()),
this, SLOT(onTargetFileToolButtonPressed()));
76 connect(m_ui->m_refLayerComboBox, SIGNAL(activated(
int)),
this, SLOT(onRefLayerComboBoxActivated(
int)));
78 connect(m_ui->m_clearToolButton, SIGNAL(clicked()),
this, SLOT(onClearToolButtonTriggered()));
80 connect(m_removeMapper, SIGNAL(mapped(
int)),
this, SLOT(onRemoveAttrExecuted(
int)));
98 m_ui->m_targetFileToolButton->setEnabled(
false);
99 m_ui->m_targetDatasourceToolButton->setEnabled(
false);
101 m_ui->m_newLayerNameLineEdit->clear();
102 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
107 m_ui->m_refLayerComboBox->clear();
108 m_ui->m_refLayerComboBox->addItem(
"");
110 for (
auto i : refLayers)
112 m_ui->m_refLayerComboBox->addItem(QString::fromUtf8(i->getTitle().c_str()), QVariant::fromValue(i));
121 QVariant v =
m_ui->m_refLayerComboBox->currentData(Qt::UserRole);
124 std::unique_ptr<te::da::DataSetType> schema = refLayer->getSchema();
126 std::vector<te::dt::Property*> props = schema->getProperties();
147 errorMessage =
"Define the data source first.";
153 errorMessage =
"Define the properties first.";
157 if (
m_ui->m_newLayerNameLineEdit->text().isEmpty())
159 errorMessage =
"Layer name not defined.";
163 std::string dsTypeName =
m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
167 for (std::size_t t = 0; t <
m_props.size(); ++t)
169 dsType->add(
m_props[t]->clone());
173 std::map<std::string, std::string> nopt;
177 boost::filesystem::path uri(
m_ui->m_repositoryLineEdit->text().toUtf8().data());
179 const std::string connInfo(
"file://" + uri.string());
185 dsOGR->createDataSet(dsType.get(), nopt);
187 catch (
const std::exception& ex)
189 errorMessage = ex.what();
194 errorMessage =
"Error creating layer.";
204 outputDataSource->createDataSet(dsType.get(), nopt);
206 catch (
const std::exception& ex)
208 errorMessage = ex.what();
213 errorMessage =
"Error creating layer.";
225 std::string dataSetName =
m_ui->m_newLayerNameLineEdit->text().toUtf8().data();
227 std::size_t idx = dataSetName.find(
".");
228 if (idx != std::string::npos)
229 dataSetName = dataSetName.substr(0, idx);
263 layer = ds2l(dsType);
265 QVariant v =
m_ui->m_refLayerComboBox->currentData(Qt::UserRole);
270 layer->setStyle(refLayer->getStyle()->clone());
271 layer->setSRID(refLayer->getSRID());
272 layer->setEncoding(refLayer->getEncoding());
304 m_ui->m_newLayerNameLineEdit->clear();
305 m_ui->m_newLayerNameLineEdit->setEnabled(
true);
310 std::list<te::da::DataSourceInfoPtr> dsPtrList = dlg.
getSelecteds();
312 if (dsPtrList.size() <= 0)
315 std::list<te::da::DataSourceInfoPtr>::iterator it = dsPtrList.begin();
317 m_ui->m_repositoryLineEdit->setText(QString(it->get()->getTitle().c_str()));
323 outputDataSource->open();
332 m_ui->m_newLayerNameLineEdit->clear();
333 m_ui->m_repositoryLineEdit->clear();
335 QString fileName = QFileDialog::getSaveFileName(
this, tr(
"Save as..."), QString(), tr(
"Shapefile (*.shp *.SHP);;"),
nullptr, QFileDialog::DontConfirmOverwrite);
337 if (fileName.isEmpty())
340 boost::filesystem::path outfile(fileName.toUtf8().data());
342 m_ui->m_repositoryLineEdit->setText(outfile.string().c_str());
344 m_ui->m_newLayerNameLineEdit->setText(outfile.leaf().string().c_str());
346 m_ui->m_newLayerNameLineEdit->setEnabled(
false);
351 boost::filesystem::path uri(
m_ui->m_repositoryLineEdit->text().toUtf8().data());
353 const std::string connInfo(
"file://" + uri.string());
357 boost::uuids::basic_random_generator<boost::mt19937> gen;
358 boost::uuids::uuid u = gen();
359 std::string id_ds = boost::uuids::to_string(u);
362 dsInfoPtr->setConnInfo(connInfo);
363 dsInfoPtr->setTitle(uri.stem().string());
364 dsInfoPtr->setAccessDriver(
"OGR");
365 dsInfoPtr->setType(
"OGR");
366 dsInfoPtr->setDescription(uri.string());
367 dsInfoPtr->setId(id_ds);
375 outputDataSource->open();
391 m_ui->m_tableWidget->setRowCount(0);
393 for (std::size_t t = 0; t <
m_props.size(); ++t)
398 int newrow =
m_ui->m_tableWidget->rowCount();
399 m_ui->m_tableWidget->insertRow(newrow);
402 QTableWidgetItem* itemName =
new QTableWidgetItem(p->
getName().c_str());
403 m_ui->m_tableWidget->setItem(newrow, 2, itemName);
407 QTableWidgetItem* itemType =
new QTableWidgetItem(propType.c_str());
408 itemType->setFlags(itemType->flags() ^ Qt::ItemIsEditable);
409 m_ui->m_tableWidget->setItem(newrow, 1, itemType);
413 removeToolButton->setIcon(QIcon::fromTheme(
"list-remove"));
414 m_ui->m_tableWidget->setCellWidget(newrow, 0, removeToolButton);
417 connect(removeToolButton, SIGNAL(pressed()),
m_removeMapper, SLOT(map()));
420 m_ui->m_tableWidget->resizeColumnsToContents();
421 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) 422 m_ui->m_tableWidget->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
433 QTableWidgetItem* item =
m_ui->m_tableWidget->item(row, column);
434 QString newName = item->text();
436 m_props[row]->setName(newName.toUtf8().data());
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
TEDATATYPEEXPORT std::string ConvertDataTypeToString(const int &dataType)
Function used to convert from a int (Data Type Enum) to a string.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
boost::shared_ptr< DataSetType > DataSetTypePtr
An atomic property like an integer or double.
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
static te::dt::Date ds(2010, 01, 01)
It models a property definition.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
int getType() const
It returns the property data type.
A class that represents a data source component.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
boost::shared_ptr< DataSourceInfo > DataSourceInfoPtr
const std::string & getName() const
It returns the property name.