DataSetTableHorizontalHeader.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 
22 #include "../../../core/uri/URI.h"
23 #include "../../../dataaccess/dataset/DataSet.h"
24 #include "../../../dataaccess/datasource/DataSourceManager.h"
25 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
26 #include "../../../dataaccess/query/DataSetName.h"
27 #include "../../../dataaccess/query/Field.h"
28 #include "../../../dataaccess/query/From.h"
29 #include "../../../dataaccess/query/OrderBy.h"
30 #include "../../../dataaccess/query/OrderByItem.h"
31 #include "../../../dataaccess/query/Select.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../maptools/DataSetLayer.h"
34 #include "../../../maptools/DataSetAdapterLayer.h"
35 #include "../../../maptools/QueryLayer.h"
36 
37 #include "DataSetTableView.h"
38 
39 // Qt
40 #include <QtGui/QMouseEvent>
41 #include <QMimeData>
42 #include <QDrag>
43 #include <QLabel>
44 #include <QPainter>
45 #include <QMessageBox>
46 
48 QHeaderView(Qt::Horizontal, view),
49  m_view(view),
50  m_doDragDrop(false),
51  m_acceptDrop(false),
52  m_layer(nullptr),
53  m_dset(nullptr)
54 {
55 }
56 
58 {
59  m_doDragDrop = b;
60 }
61 
63 {
64  return m_doDragDrop;
65 }
66 
68 {
69  m_acceptDrop = b;
70  if(m_acceptDrop)
71  {
72  setAcceptDrops(true);
73  setDropIndicatorShown(true);
74  }
75  else
76  {
77  setAcceptDrops(false);
78  setDropIndicatorShown(false);
79  }
80 }
81 
83 {
84  return m_acceptDrop;
85 }
86 
88 {
89  m_layer = layer;
90 }
91 
93 {
94  m_dset = dset;
95 }
96 
98 {
99  if(e->button() == Qt::LeftButton)
100  {
101  if(m_doDragDrop == false)
102  {
103  int c = m_view->columnAt(e->pos().x());
104 
105  QItemSelectionModel* sel = m_view->selectionModel();
106 
107  QItemSelectionModel::SelectionFlag selF = (sel->isColumnSelected(c, QModelIndex())) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
108 
109  if(m_view->selectionMode() == QAbstractItemView::SingleSelection)
110  sel->clear();
111 
112 
113  QModelIndex tl = m_view->model()->index(0, c);
114  QModelIndex br = m_view->model()->index(model()->rowCount()-1, c);
115 
116  QItemSelection newSel(tl, br);
117 
118  sel->select(newSel, selF);
119  }
120  else
121  {
122  QPixmap p(200, 50);
123  QPainter painter(&p);
124  QRect r = painter.boundingRect(p.rect(), Qt::AlignCenter, "Link Table");
125  r = QRect(0, 0, r.width(), r.height());
126  QPoint hotSpot(r.center());
127  QPixmap pixmap(r.size());
128  painter.end();
129  painter.begin(&pixmap);
130  painter.setPen(QPen(Qt::blue));
131  painter.drawText(r, "Link Table");
132  painter.end();
133 
134  QMimeData *mimeData = new QMimeData;
135  mimeData->setText("TerraView: Link Table");
136 
137  if(m_layer)
138  {
140  std::string sid(ds->getId());
141  mimeData->setData("DataSourceId", sid.c_str());
142  std::string stype(ds->getType());
143  mimeData->setData("DataSourceType", stype.c_str());
144  m_connInfo = ds->getConnectionInfo().uri();
145  QString connInfo;
146  connInfo.setNum((unsigned long long)&m_connInfo);
147  mimeData->setData("DataSourceConnInfo", connInfo.toUtf8().data());
148  std::string lname(m_layer->getSchema()->getName());
149  mimeData->setData("LayerName", lname.c_str());
150  std::string ltytle(m_layer->getTitle());
151  mimeData->setData("LayerTitle", ltytle.c_str());
152  std::string lid(m_layer->getId());
153  mimeData->setData("LayerId", lid.c_str());
154  std::string ltype(m_layer->getType());
155  mimeData->setData("LayerType", ltype.c_str());
156  }
157 
158  size_t col = logicalIndexAt(e->pos());
159  QString s;
160  s.setNum(col);
161  mimeData->setData("FieldNumber", s.toUtf8().data());
162 
163  std::string colName = m_dset->getPropertyName(col);
164  mimeData->setData("FieldName", colName.c_str());
165 
166  int propType = m_dset->getPropertyDataType(col);
167  s.setNum(propType);
168  mimeData->setData("FieldType", s.toUtf8().data());
169 
170  QDrag drag(this);
171  drag.setMimeData(mimeData);
172  drag.setPixmap(pixmap);
173  drag.setHotSpot(hotSpot);
174 
175  /*Qt::DropAction dropAction = */drag.exec(Qt::LinkAction);
176  }
177  }
178  //else if(e->button() == Qt::MiddleButton) // code for drag and drop test
179  //{
180  // m_doDragDrop = !m_doDragDrop;
181  // m_acceptDrop = m_doDragDrop;
182  // setDragDrop(m_doDragDrop);
183  // setAcceptDrop(m_acceptDrop);
184  // if(m_doDragDrop)
185  // QMessageBox::information(this, "", "DRAG DROP ENABLED");
186  // else
187  // QMessageBox::information(this, "", "DRAG DROP DISABLED");
188  //}
189 
190  QHeaderView::mousePressEvent(e);
191 }
192 
194 {
195  if(e->source() == this || m_acceptDrop == false)
196  return;
197 
198  const QMimeData* mdata = e->mimeData();
199  if(mdata->text() == "TerraView: Link Table")
200  e->setAccepted(true);
201  else
202  e->setAccepted(false);
203 }
204 
206 {
207  if(e->source() == this || m_acceptDrop == false)
208  return;
209 
210  const QMimeData* mdata = e->mimeData();
211  if(mdata->text() == "TerraView: Link Table")
212  e->setAccepted(true);
213  else
214  e->setAccepted(false);
215 }
216 
218 {
219  if(e->source() == this || m_acceptDrop == false)
220  return;
221 
222  const QMimeData* mdata = e->mimeData();
223  std::string text = mdata->text().toUtf8().data();
224  if(text == "TerraView: Link Table")
225  {
226  e->setDropAction(Qt::LinkAction);
227 
228  if(m_layer)
229  {
231  m_secondLinkInfo.m_dataSourceId = sds->getId();
232  m_secondLinkInfo.m_dataSourceType = sds->getType();
233  m_secondLinkInfo.m_connInfo = sds->getConnectionInfo().uri();
238 
239  if(mdata->data("DataSourceId").isNull() == false)
240  {
241  m_firstLinkInfo.m_dataSourceId = mdata->data("DataSourceId").data();
242  m_firstLinkInfo.m_dataSourceType = mdata->data("DataSourceType").data();
243  QString connInfo(mdata->data("DataSourceConnInfo").data());
244  m_firstLinkInfo.m_connInfo = connInfo.toStdString();
245  m_firstLinkInfo.m_layerName = mdata->data("LayerName").data();
246  m_firstLinkInfo.m_layerTitle = mdata->data("LayerTitle").data();
247  m_firstLinkInfo.m_layerId = mdata->data("LayerId").data();
248  m_firstLinkInfo.m_layerType = mdata->data("LayerType").data();
249 
251  {
252  QMessageBox::warning(this, "Link Error", "The data are from different sources");
253  return;
254  }
255 
257  {
258  QMessageBox::warning(this, "Link Error", "The data are from the same layer");
259  return;
260  }
261  }
262  }
263 
264  QString fromFieldNumber(mdata->data("FieldNumber").data());
265  m_firstLinkInfo.m_fieldNumber = fromFieldNumber.toUInt();
266  m_firstLinkInfo.m_fieldName = mdata->data("FieldName").data();
267  QString fromFieldType(mdata->data("FieldType").data());
268  m_firstLinkInfo.m_fieldType = fromFieldType.toUInt();
269 
270  m_secondLinkInfo.m_fieldNumber = logicalIndexAt(e->pos());
273  {
274  QMessageBox::warning(this, "Link Error", "Field types do not match");
275  return;
276  }
278 
279  if(mdata->data("DataSourceId").isNull() == false && m_layer)
281  else
283  }
284 }
285 
287 {
288  // Getting data source, if it is available
290 
291  if(layer->getType() == "DATASETLAYER")
292  {
293  const te::map::DataSetLayer* l = dynamic_cast<const te::map::DataSetLayer*>(layer);
294 
295  if(l != nullptr)
297  }
298  else if(layer->getType() == "DATASETADAPTERLAYER")
299  {
300  const te::map::DataSetAdapterLayer* l = dynamic_cast<const te::map::DataSetAdapterLayer*>(layer);
301 
302  if(l != nullptr)
304  }
305  else if(layer->getType() == "QUERYLAYER")
306  {
307  const te::map::QueryLayer* l = dynamic_cast<const te::map::QueryLayer*>(layer);
308 
309  if(l != nullptr)
311  }
312 
313  return ds;
314 }
virtual const std::string & getId() const
It returns the layer id.
DataSetTableView * m_view
data set table view pointer.
bool m_doDragDrop
True: do drag and drop. False: do not drag and drop.
void linkTable(std::string firstFieldName, std::string secondFieldName)
Signal emitted when the drop event runs.
This is the base class for layers.
Definition: AbstractLayer.h:77
boost::shared_ptr< DataSource > DataSourcePtr
virtual const std::string & getTitle() const
It returns the layer title.
virtual const std::string & getType() const =0
It returns the layer type.
const te::map::AbstractLayer * m_layer
To get information for drag and drop.
std::string m_dataSourceId
Data Source Id.
void mousePressEvent(QMouseEvent *e)
Handles the mouse pres event e initiates the drag and drop if the flag is active. ...
A layer resulting from a query.
Definition: QueryLayer.h:50
static te::dt::Date ds(2010, 01, 01)
int b
Definition: TsRtree.cpp:32
A layer with reference to a DataSetTypeConverter.
DataSetTableHorizontalHeader(DataSetTableView *view)
Constructor.
void setLayer(const te::map::AbstractLayer *layer)
Sets the layer to get drag and drop information.
A table view for a dataset.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
te::da::DataSet * m_dset
To get information for drag and drop.
virtual std::unique_ptr< LayerSchema > getSchema() const =0
It returns the layer schema.
void dragEnterEvent(QDragEnterEvent *e)
Handles the drag enter event.
void dragMoveEvent(QDragMoveEvent *e)
Handles the drag move event.
te::gm::Polygon * p
void dropEvent(QDropEvent *e)
Handles the drop Event.
A dataset is the unit of information manipulated by the data access module of TerraLib.
std::string m_dataSourceType
Data Source Type.
A customized table view for te::map::AbstractLayer objects. Uses a te::qt::widgets::DataSetModel as i...
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setDataSet(te::da::DataSet *dset)
Sets the data set to get drag and drop information.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
te::da::DataSourcePtr GetDataSource(const te::map::AbstractLayer *layer)
It gets the layer data source.
virtual const std::string & getDataSourceId() const
std::string m_connInfo
connection info used for QMimeData.
unsigned int col
bool m_acceptDrop
True: accept drop. False: not accept drop.
std::string m_connInfo
Data Source Connection Info.