13 #include "../charts/Utils.h"
14 #include "../utils/ScopedCursor.h"
15 #include "../Config.h"
16 #include "../Exception.h"
17 #include "../../../common/Exception.h"
18 #include "../../../dataaccess/dataset/DataSet.h"
19 #include "../../../dataaccess/dataset/ObjectId.h"
20 #include "../../../dataaccess/dataset/ObjectIdSet.h"
21 #include "../../../dataaccess/dataset/DataSetTypeCapabilities.h"
22 #include "../../../dataaccess/datasource/DataSourceManager.h"
23 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
24 #include "../../../dataaccess/query/DataSetName.h"
25 #include "../../../dataaccess/query/Field.h"
26 #include "../../../dataaccess/query/From.h"
27 #include "../../../dataaccess/query/OrderBy.h"
28 #include "../../../dataaccess/query/OrderByItem.h"
29 #include "../../../dataaccess/query/Select.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../geometry/Geometry.h"
32 #include "../../../maptools/DataSetLayer.h"
33 #include "../../../maptools/DataSetAdapterLayer.h"
34 #include "../../../maptools/QueryLayer.h"
35 #include "../../../statistics/qt/StatisticsDialog.h"
39 #include <QContextMenuEvent>
41 #include <QDialogButtonBox>
42 #include <QHeaderView>
45 #include <QMessageBox>
65 for(
size_t i=0; i<nProps; i++)
67 cols.push_back((
int)i);
78 for (
int i=0; i<sz; i++)
89 std::vector<int>::iterator it;
91 QMenu* mnu =
new QMenu(hMnu);
92 mnu->setTitle(QObject::tr(
"Show hidden column"));
95 mnu->setEnabled(
false);
97 for(it=hSecs.begin(); it!=hSecs.end(); ++it)
99 QString cName = hView->model()->headerData(*it, Qt::Horizontal, Qt::DisplayRole).toString();
100 QAction* act =
new QAction(mnu);
103 act->setData(QVariant(*it));
105 QString tt = QObject::tr(
"Turns column \"%1\" visible.").arg(cName);
117 QAction* act =
new QAction(hMnu);
118 act->setText(QObject::tr(
"Show all columns"));
129 if(layer->
getType() ==
"DATASETLAYER")
136 else if(layer->
getType() ==
"DATASETADAPTERLAYER")
143 else if(layer->
getType() ==
"QUERYLAYER")
170 std::auto_ptr<te::da::Select>
GetSelectExpression(
const std::string& datasetName,
const std::vector<std::string>& colsNames,
const bool& asc)
174 fields.push_back(std::auto_ptr<te::da::Field>(
new te::da::Field(
"*")));
182 for(
size_t i=0; i<colsNames.size(); i++)
185 return std::auto_ptr<te::da::Select>(
new te::da::Select(fields, from, order_by));
190 std::vector<std::string> colsNames;
192 for(
size_t i = 0; i < cols.size(); ++i)
205 throw std::string(
"Fail to generate query.");
210 throw std::string(
"Fail to get data source.");
216 return std::auto_ptr<te::da::DataSet>();
222 std::auto_ptr<te::da::Select> query;
224 if(layer->
getType() ==
"DATASETLAYER")
231 else if(layer->
getType() ==
"DATASETADAPTERLAYER")
238 else if(layer->
getType() ==
"QUERYLAYER")
249 throw std::string(
"Fail to generate query.");
254 throw std::string(
"Fail to get data source.");
260 return std::auto_ptr<te::da::DataSet>();
266 std::vector<QString> res;
272 for(
size_t i=0; i<n; i++)
283 std::vector<int> geoCols;
293 size_t pos = (size_t)geoCols[0];
295 std::auto_ptr<te::gm::Geometry> g = dset->
getGeometry(pos);
297 return std::auto_ptr<te::gm::Envelope> (
new te::gm::Envelope(*g->getMBR()));
300 return std::auto_ptr<te::gm::Envelope>();
316 std::vector<size_t>::iterator it;
318 for(it = cols.begin(); it != cols.end(); ++it)
319 if((
size_t)col == *it)
329 std::vector<int> geoCols;
330 std::vector<int>::iterator it;
333 for(it = geoCols.begin(); it != geoCols.end(); ++it)
362 m_view->horizontalHeader()->installEventFilter(
this);
363 m_view->verticalHeader()->installEventFilter(
this);
364 m_view->viewport()->installEventFilter(
this);
371 m_view->connect(
this, SIGNAL(
sortData(
const bool&)), SLOT(sortByColumns(
const bool&)));
392 return QObject::eventFilter(watched, event);
394 QWidget* vport =
m_view->viewport();
395 QHeaderView* hHdr =
m_view->horizontalHeader();
396 QHeaderView* vHdr =
m_view->verticalHeader();
398 switch(event->type())
400 case QEvent::ContextMenu:
407 QContextMenuEvent* evt =
static_cast<QContextMenuEvent*
>(event);
408 QPoint pos = evt->globalPos();
410 m_columnPressed = hHdr->logicalIndex(hHdr->visualIndexAt(evt->pos().x()));
417 QAction* act =
new QAction(
m_hMenu);
418 act->setText(tr(
"Hide column"));
419 act->setToolTip(tr(
"Hides the selected column."));
426 act->setEnabled(
false);
428 m_hMenu->addAction(hMnu->menuAction());
433 QAction* act3 =
new QAction(
m_hMenu);
434 act3->setText(tr(
"Reset columns order"));
435 act3->setToolTip(tr(
"Put all columns in the original order."));
440 QAction* act5 =
new QAction(
m_hMenu);
441 act5->setText(tr(
"Sort data ASC"));
442 act5->setToolTip(tr(
"Sort data in ascendent order using selected columns."));
445 QAction* act9 =
new QAction(
m_hMenu);
446 act9->setText(tr(
"Sort data DESC"));
447 act9->setToolTip(tr(
"Sort data in descendent order using selected columns."));
452 QAction* act4 =
new QAction(
m_hMenu);
453 act4->setText(tr(
"Histogram"));
454 act4->setToolTip(tr(
"Creates a new histogram based on the data of the selected colunm."));
457 QAction* act6 =
new QAction(
m_hMenu);
458 act6->setText(tr(
"Statistics"));
459 act6->setToolTip(tr(
"Show the statistics summary of the selected colunm."));
465 connect(act, SIGNAL(triggered()), SLOT(
hideColumn()));
466 connect(hMnu, SIGNAL(triggered(QAction*)), SLOT(
showColumn(QAction*)));
470 m_view->connect(act2, SIGNAL(triggered()), SLOT(showAllColumns()));
471 m_view->connect(act3, SIGNAL(triggered()), SLOT(resetColumnsOrder()));
475 connect (act5, SIGNAL(triggered()), SLOT(
sortDataAsc()));
476 connect (act9, SIGNAL(triggered()), SLOT(
sortDataDesc()));
481 QAction* act7 =
new QAction(
m_hMenu);
482 act7->setText(tr(
"Add column"));
483 act7->setToolTip(tr(
"Adds a column to the table."));
485 act7->setEnabled(
m_caps->supportsAddColumn());
487 QAction* act8 =
new QAction(
m_hMenu);
488 act8->setText(tr(
"Remove column"));
489 act8->setToolTip(tr(
"Removes a column from the table."));
491 act8->setEnabled(
m_caps->supportsRemoveColumn());
493 QAction* act10 =
new QAction(
m_hMenu);
494 act10->setText(tr(
"Rename column"));
495 act10->setToolTip(tr(
"Renames a column of the table."));
498 QAction* act11 =
new QAction(
m_hMenu);
499 act11->setText(tr(
"Change column type"));
500 act11->setToolTip(tr(
"Changes the type of a column of the table."));
503 QAction* act12 =
new QAction(
m_hMenu);
504 act12->setText(tr(
"Change column data"));
505 act12->setToolTip(tr(
"Changes the data of a column of the table."));
508 QAction* act13 =
new QAction(
m_hMenu);
509 act13->setText(tr(
"Save editions"));
510 act13->setToolTip(tr(
"Save pendent editions to layer."));
514 m_view->connect(act7, SIGNAL(triggered()), SLOT(addColumn()));
516 connect(act8, SIGNAL(triggered()), SLOT(
removeColumn()));
517 connect (act10, SIGNAL(triggered()), SLOT(
renameColumn()));
518 connect (act11, SIGNAL(triggered()), SLOT(
retypeColumn()));
520 connect (act13, SIGNAL(triggered()), SIGNAL(
saveEditions()));
526 else if(watched == vport)
529 else if(watched == vHdr)
533 QContextMenuEvent* evt =
static_cast<QContextMenuEvent*
>(event);
534 QPoint pos = evt->globalPos();
537 QAction* act =
new QAction(
m_vMenu);
538 act->setText(tr(
"Enable auto scroll"));
539 act->setToolTip(tr(
"Goes to the selected row."));
541 act->setCheckable(
true);
549 act->setText(tr(
"Enable promotion"));
550 act->setToolTip(tr(
"Enables promotion of selected rows."));
552 act->setCheckable(
true);
570 return QObject::eventFilter(watched, event);
612 int column = act->data().toInt();
622 statisticDialog.exec();
704 std::auto_ptr<te::da::DataSetTypeCapabilities>
m_caps;
715 m_autoScrollEnabled(false),
717 m_promotionEnabled(false),
728 #if QT_VERSION >= 0x050000
729 horizontalHeader()->setSectionsMovable(
true);
731 horizontalHeader()->setMovable(
true);
734 setSelectionMode(QAbstractItemView::MultiSelection);
735 setSelectionBehavior(QAbstractItemView::SelectColumns);
747 connect(verticalHeader(), SIGNAL(selectedRow(
const int&,
const bool&)), SLOT(
highlightRow(
const int&,
const bool&)));
748 connect(verticalHeader(), SIGNAL(selectedRows(
const int&,
const int&)), SLOT(
highlightRows(
const int&,
const int&)));
753 if (m_model->hasEditions())
755 QMessageBox msgBox(
this);
756 msgBox.setText(
"There are unsaved changes on table.");
757 msgBox.setInformativeText(
"Do you want to save your changes?");
761 int ret = msgBox.exec();
773 std::auto_ptr<te::map::LayerSchema> sch = m_layer->
getSchema();
775 if (m_orderby.empty())
777 std::vector<te::dt::Property*> psps;
779 if(sch->getPrimaryKey())
780 psps = sch->getPrimaryKey()->getProperties();
782 psps = sch->getProperties();
784 std::vector<te::dt::Property*>::iterator it;
786 for(it = psps.begin(); it != psps.end(); ++it)
787 m_orderby.push_back((*it)->getName());
790 setDataSet(
GetDataSet(m_layer, m_orderby, m_orderAsc).release(), clearEditor);
791 setLayerSchema(sch.get());
795 m_popupFilter->setDataSetTypeCapabilities(caps);
798 m_model->setEditable(caps->supportsDataEdition());
804 setSelectionMode((dsc->getType().compare(
"OGR") == 0) ? SingleSelection : MultiSelection);
805 setSelectionBehavior((dsc->getType().compare(
"OGR") == 0) ? QAbstractItemView::SelectColumns : QAbstractItemView::SelectItems);
808 highlightOIds(m_layer->getSelected());
815 m_model->setDataSet(dset, clearEditor);
819 m_popupFilter->setDataSet(dset);
820 m_delegate->setDataSet(dset);
830 std::auto_ptr<te::da::ObjectIdSet> objs_ptr(objs);
832 m_delegate->setObjectIdSet(objs_ptr.get());
834 m_model->setPkeysColumns(objs_ptr->getPropertyPos());
836 m_model->getPromoter()->preProcessKeys(m_dset, objs_ptr->getPropertyPos());
844 m_delegate->setObjectIdSet(oids);
846 if(m_autoScrollEnabled && oids != 0 && oids->
size() > 0)
848 size_t row = m_model->getPromoter()->map2Row(*oids->
begin());
850 scrollTo(m_model->index((
int)row, 0));
853 if(m_promotionEnabled)
856 viewport()->repaint();
861 m_delegate->setColor(color);
868 return m_model->hasEditions();
873 int propType = m_dset->getPropertyDataType(column);
879 QDialog* dialog =
new QDialog(
this);
880 dialog->setFixedSize(160, 75);
882 QBoxLayout* vLayout =
new QBoxLayout(QBoxLayout::TopToBottom, dialog);
883 QBoxLayout* hLayout =
new QBoxLayout(QBoxLayout::LeftToRight, dialog);
885 QLabel* slicesProp =
new QLabel(QString::fromStdString(
"Number of Slices: "), dialog);
886 hLayout->addWidget(slicesProp);
888 QSpinBox* slicesSB =
new QSpinBox(dialog);
889 slicesSB->setValue(5);
890 slicesSB->setMinimum(2);
892 hLayout->addWidget(slicesSB);
893 vLayout->addLayout(hLayout);
895 QDialogButtonBox* bbox =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
896 connect(bbox, SIGNAL(accepted()), dialog, SLOT(accept()));
897 connect(bbox, SIGNAL(rejected()), dialog, SLOT(reject()));
898 vLayout->addWidget(bbox);
900 int res = dialog->exec();
901 if (res == QDialog::Accepted)
910 horizontalHeader()->hideSection(column);
915 horizontalHeader()->showSection(column);
922 dlg.
setOldColumnName(QString::fromStdString(m_dset->getPropertyName(column)));
924 if(dlg.exec() == QDialog::Accepted)
926 std::string oldName = dlg.
getOldName().toStdString();
927 std::string newName = dlg.
getNewName().toStdString();
931 throw Exception(tr(
"Fail to get data source.").toStdString());
933 if(!dsrc->isPropertyNameValid(newName))
934 throw Exception(tr(
"Invalid column name. Choose another.").toStdString());
936 dsrc->renameProperty(m_layer->getSchema()->getName(), oldName, newName);
946 std::auto_ptr<te::da::DataSetType> schema = m_layer->getSchema();
948 std::string dsetName = schema->
getName();
949 std::string columnName;
951 prp = schema->getProperty(column);
954 throw Exception(tr(
"Fail to get property of the dataset.").toStdString());
962 if(dlg.exec() == QDialog::Accepted)
967 throw Exception(tr(
"Fail to get data source.").toStdString());
971 dsrc->changePropertyDefinition(dsetName, columnName, dlg.
getProperty().release());
979 if(m_dset == 0 || m_layer == 0)
982 std::auto_ptr<te::da::DataSetType> schema = m_layer->getSchema();
983 std::string dsetName = schema->getName();
984 std::string columnName = schema->getProperty(column)->getName();
991 if(dlg.exec() == QDialog::Accepted)
996 throw Exception(tr(
"Fail to get data source.").toStdString());
1001 sql =
"UPDATE " + dsetName +
" SET " + columnName +
" = " + dlg.
getExpression().toStdString();
1010 QMessageBox::information(
this, tr(
"Updating data failure"), e.
what());
1014 QMessageBox::information(
this, tr(
"Updating data failure"), tr(
"Data source operation fail for unknown reason."));
1021 std::vector<int> hCols = m_popupFilter->getHiddenColumns();
1022 std::vector<int>::iterator it;
1024 for (it=hCols.begin(); it!=hCols.end(); ++it)
1025 horizontalHeader()->showSection(*it);
1030 QHeaderView* hdr = horizontalHeader();
1032 int nCols = hdr->count();
1034 for(
int i=0; i<nCols; i++)
1036 int visCol = hdr->visualIndex(i);
1039 hdr->moveSection(visCol, i);
1051 if(m_delegate->getSelected()->contains(oid))
1053 emit deselectOIds(oids);
1057 m_delegate->setObjectIdSet(m_layer->getSelected());
1059 if(m_promotionEnabled)
1062 viewport()->repaint();
1070 emit selectOIds(oids, add,
GetExtent(m_dset, m_model->getPromoter(), row).
get());
1072 if(m_promotionEnabled)
1075 viewport()->repaint();
1085 if(initRow < finalRow)
1100 emit selectOIds(oids,
true,
GetExtent(m_dset, m_model->getPromoter(),
final).
get());
1102 if(m_promotionEnabled)
1105 viewport()->repaint();
1115 m_model->setEnabled(
false);
1116 m_popupFilter->setEnabled(
false);
1119 m_model->promote(oids);
1121 m_popupFilter->setEnabled(
true);
1122 m_model->setEnabled(
true);
1125 viewport()->repaint();
1140 std::vector<int> selCols;
1144 int nCols = model()->columnCount();
1146 for(
int i=0; i<nCols; i++)
1148 int logRow = horizontalHeader()->logicalIndex(i);
1149 if(selectionModel()->isColumnSelected(logRow, QModelIndex()))
1151 m_orderby.push_back(m_dset->getPropertyName((
size_t)logRow));
1152 selCols.push_back(logRow);
1159 std::auto_ptr<te::da::DataSet> dset =
GetDataSet(m_layer, m_orderby, asc);
1162 throw te::common::Exception(tr(
"Sort operation not supported by the source of data.").toStdString());
1164 setDataSet(dset.release());
1166 viewport()->repaint();
1168 setUpdatesEnabled(
false);
1170 m_model->getPromoter()->preProcessKeys(m_dset, m_delegate->getSelected()->getPropertyPos());
1172 setUpdatesEnabled(
true);
1174 if(m_promotionEnabled)
1179 QMessageBox::information(
this, tr(
"Sorting columns failure"), tr(
"Could not sort data: ") + e.
what());
1185 m_model->showOIdsVisible(visible);
1187 horizontalHeader()->viewport()->repaint();
1199 std::auto_ptr<te::da::DataSetType> ds_t = m_layer->getSchema();
1201 std::string dsName = ds_t->getName();
1202 size_t n_prop = ds_t->getProperties().size();
1205 if(dlg.exec() == QDialog::Accepted)
1214 if(p->getName().empty())
1217 if(!ds->isPropertyNameValid(p->getName()))
1220 if(ds->propertyExists(dsName, p->getName()))
1223 ds->addProperty(dsName, p.get());
1225 if(ds->getType().compare(
"OGR") == 0)
1226 m_model->insertColumns(((
int)n_prop-1), 0);
1228 setLayer(m_layer,
false);
1233 QMessageBox::information(
this, tr(
"Creating column failure"), tr(
"The column could not be created: ") + e.
what());
1241 if(QMessageBox::question(
this, tr(
"Remove column"), tr(
"Are you sure you want to remove this column?"), QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
1248 std::auto_ptr<te::da::DataSetType> ds_t = m_layer->getSchema();
1249 std::string dsName = ds_t->getName();
1251 std::string pName = ds_t->getProperty(column)->getName();
1253 ds->dropProperty(dsName, pName);
1255 m_model->removeColumns(column, 0);
1257 setLayer(m_layer,
false);
1262 QMessageBox::information(
this, tr(
"Removing column failure"), tr(
"The column could not be removed: ") + e.
what());
1268 m_autoScrollEnabled = enable;
1273 m_promotionEnabled = enable;
1275 if(m_promotionEnabled)
1278 m_popupFilter->setPromotionEnabled(m_promotionEnabled);
1283 QItemSelection toRemove;
1285 for(
int i=initRow; i<=finalRow; i++)
1287 QModelIndexList idx = selectionModel()->selectedColumns(i);
1290 toRemove.select(idx.first(), idx.last());
1293 selectionModel()->select(toRemove, QItemSelectionModel::Deselect);
1302 std::vector< std::set<int> > ed;
1304 std::auto_ptr<te::map::LayerSchema> sch = m_layer->getSchema();
1306 std::auto_ptr<te::da::DataSet> md = m_model->getEditions(sch.get(), ed);
1317 std::auto_ptr<te::da::ObjectIdSet> objs1(objs);
1319 ds->update(sch->getName(), md.get(), ed, objs1->getPropertyPos());
1325 QMessageBox::warning(
this, tr(
"Save edition failure"), e.
what());
1329 #include "DataSetTableView.moc"
virtual const std::string & getType() const =0
It returns the layer type.
A Qt dialog for reset data of a column in the table.
bool IsGeometryColumn(te::da::DataSet *dset, const size_t &col)
std::vector< QString > GetColumnsNames(te::da::DataSet *dset)
const std::string & getDataSetName() const
A class that informs what kind of constraint and index is supported by a given data source...
te::da::DataSetTypeCapabilities * GetCapabilities(const te::map::AbstractLayer *layer)
The Field class can be used to model an expression that takes part of the output items of a SELECT...
TEDATAACCESSEXPORT void GetEmptyOIDSet(const DataSetType *type, ObjectIdSet *&set)
Returns an empty ObjectIdSet, with the definitions of fields that compose it.
std::vector< int > GetHiddenSections(QHeaderView *hView, te::da::DataSet *dset)
A Qt dialog for renaming columns into a table.
A model based on te::da::DataSet.
This is the base class for layers.
A class that models the name of a dataset used in a From clause.
boost::shared_ptr< DataSource > DataSourcePtr
A class that models the description of a dataset.
void setStatistics(te::da::DataSet *dataSet, const std::string prop)
virtual const char * what() const
It outputs the exception message.
boost::ptr_vector< OrderByItem > OrderBy
A class that can be used to model an ORDER BY clause.
A layer resulting from a query.
It models a property definition.
virtual bool move(std::size_t i)=0
It moves the dataset internal pointer to a given position.
void HideGeometryColumns(te::da::DataSet *dset, te::qt::widgets::DataSetTableView *view)
te::da::DataSourcePtr GetDataSource(const te::map::AbstractLayer *layer)
std::auto_ptr< te::da::DataSet > GetDataSet(const te::map::AbstractLayer *layer, const te::da::DataSet *set, const std::vector< int > &cols, const bool &asc)
A layer with reference to a DataSetTypeConverter.
A table view for a dataset.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
const std::string & getDataSourceId() const
This class represents a set of unique ids created in the same context. i.e. from the same data set...
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
A Qt dialog for inserting columns into a table.
This class represents an unique id for a data set element.
std::size_t size() const
It returns the object id set size.
void GetGeometryColumnsPositions(te::da::DataSet *dset, std::vector< int > &cols)
const std::string & getDataSourceId() const
TEDATAACCESSEXPORT void Save(const std::string &fileName)
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
bool IsPrimaryKey(const int &col, te::qt::widgets::DataSetTableView *view)
const std::string & getDataSourceId() const
te::da::Select * getQuery() const
QAction * GetShowAllMenu(QHeaderView *hView, te::da::DataSet *dset, QMenu *hMnu)
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
A Select models a query to be used when retrieving data from a DataSource.
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
int getType() const
It returns the property data type.
A dataset is the unit of information manipulated by the data access module of TerraLib.
std::auto_ptr< te::gm::Envelope > GetExtent(te::da::DataSet *dset, te::qt::widgets::Promoter *p, const int &rowPosition)
std::auto_ptr< te::da::Select > GetSelectExpression(const std::string &datasetName, const std::vector< std::string > &colsNames, const bool &asc)
A delegate for highlight the selected object ids.
const std::vector< std::size_t > & getPropertyPos() const
It returns the property positions used to generated the oids.
A layer with reference to a dataset.
QMenu * GetHiddenColumnsMenu(QHeaderView *hView, te::da::DataSet *dset, QMenu *hMnu)
A class that can be used in an ORDER BY clause to sort the items of a resulting query.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
std::set< ObjectId *, te::common::LessCmp< ObjectId * > >::const_iterator begin() const
Returns an iterator for the object ids in container.
virtual std::auto_ptr< LayerSchema > getSchema() const =0
It returns the layer schema.
const std::string & getName() const
It returns the property name.