All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "../../../dataaccess/dataset/DataSet.h"
23 #include "../../../dataaccess/datasource/DataSourceManager.h"
24 #include "../../../dataaccess/datasource/DataSourceTransactor.h"
25 #include "../../../dataaccess/query/DataSetName.h"
26 #include "../../../dataaccess/query/Field.h"
27 #include "../../../dataaccess/query/From.h"
28 #include "../../../dataaccess/query/OrderBy.h"
29 #include "../../../dataaccess/query/OrderByItem.h"
30 #include "../../../dataaccess/query/Select.h"
31 #include "../../../dataaccess/utils/Utils.h"
32 #include "../../../maptools/DataSetLayer.h"
33 #include "../../../maptools/DataSetAdapterLayer.h"
34 #include "../../../maptools/QueryLayer.h"
35 
36 #include "DataSetTableView.h"
37 
38 // Qt
39 #include <QtGui/QMouseEvent>
40 #include <QMimeData>
41 #include <QDrag>
42 #include <QLabel>
43 #include <QPainter>
44 #include <QMessageBox>
45 
47 QHeaderView(Qt::Horizontal, view),
48  m_view(view),
49  m_doDragDrop(false),
50  m_acceptDrop(false),
51  m_layer(0),
52  m_dset(0)
53 {
54 }
55 
57 {
58  m_doDragDrop = b;
59 }
60 
62 {
63  return m_doDragDrop;
64 }
65 
67 {
68  m_acceptDrop = b;
69  if(m_acceptDrop)
70  {
71  setAcceptDrops(true);
72  setDropIndicatorShown(true);
73  }
74  else
75  {
76  setAcceptDrops(false);
77  setDropIndicatorShown(false);
78  }
79 }
80 
82 {
83  return m_acceptDrop;
84 }
85 
87 {
88  m_layer = layer;
89 }
90 
92 {
93  m_dset = dset;
94 }
95 
97 {
98  if(e->button() == Qt::LeftButton)
99  {
100  if(m_doDragDrop == false)
101  {
102  int c = m_view->columnAt(e->pos().x());
103 
104  QItemSelectionModel* sel = m_view->selectionModel();
105 
106  if(c > 0)
107  {
108  QItemSelectionModel::SelectionFlag selF = (sel->isColumnSelected(c, QModelIndex())) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
109 
110  if(m_view->selectionMode() == QAbstractItemView::SingleSelection)
111  sel->clear();
112 
113 
114  QModelIndex tl = m_view->model()->index(0, c);
115  QModelIndex br = m_view->model()->index(model()->rowCount()-1, c);
116 
117  QItemSelection newSel(tl, br);
118 
119  sel->select(newSel, selF);
120  }
121  else
122  sel->clear();
123  }
124  else
125  {
126  QPixmap p(200, 50);
127  QPainter painter(&p);
128  QRect r = painter.boundingRect(p.rect(), Qt::AlignCenter, "Link Table");
129  r = QRect(0, 0, r.width(), r.height());
130  QPoint hotSpot(r.center());
131  QPixmap pixmap(r.size());
132  painter.end();
133  painter.begin(&pixmap);
134  painter.setPen(QPen(Qt::blue));
135  painter.drawText(r, "Link Table");
136  painter.end();
137 
138  QMimeData *mimeData = new QMimeData;
139  mimeData->setText("TerraView: Link Table");
140 
141  if(m_layer)
142  {
143  te::da::DataSourcePtr ds = GetDataSource(m_layer);
144  std::string sid(ds->getId());
145  mimeData->setData("DataSourceId", sid.c_str());
146  std::string stype(ds->getType());
147  mimeData->setData("DataSourceType", stype.c_str());
148  m_connInfo = ds->getConnectionInfo();
149  QString connInfo;
150  connInfo.setNum((unsigned long long)&m_connInfo);
151  mimeData->setData("DataSourceConnInfo", connInfo.toStdString().c_str());
152  std::string lname(m_layer->getSchema()->getName());
153  mimeData->setData("LayerName", lname.c_str());
154  std::string ltytle(m_layer->getTitle());
155  mimeData->setData("LayerTitle", ltytle.c_str());
156  std::string lid(m_layer->getId());
157  mimeData->setData("LayerId", lid.c_str());
158  std::string ltype(m_layer->getType());
159  mimeData->setData("LayerType", ltype.c_str());
160  }
161 
162  size_t col = logicalIndexAt(e->pos());
163  QString s;
164  s.setNum(col);
165  mimeData->setData("FieldNumber", s.toStdString().c_str());
166 
167  std::string colName = m_dset->getPropertyName(col);
168  mimeData->setData("FieldName", colName.c_str());
169 
170  int propType = m_dset->getPropertyDataType(col);
171  s.setNum(propType);
172  mimeData->setData("FieldType", s.toStdString().c_str());
173 
174  QDrag drag(this);
175  drag.setMimeData(mimeData);
176  drag.setPixmap(pixmap);
177  drag.setHotSpot(hotSpot);
178 
179  Qt::DropAction dropAction = drag.exec(Qt::LinkAction);
180  }
181  }
182  //else if(e->button() == Qt::MiddleButton) // code for drag and drop test
183  //{
184  // m_doDragDrop = !m_doDragDrop;
185  // m_acceptDrop = m_doDragDrop;
186  // setDragDrop(m_doDragDrop);
187  // setAcceptDrop(m_acceptDrop);
188  // if(m_doDragDrop)
189  // QMessageBox::information(this, "", "DRAG DROP ENABLED");
190  // else
191  // QMessageBox::information(this, "", "DRAG DROP DISABLED");
192  //}
193 
194  QHeaderView::mousePressEvent(e);
195 }
196 
198 {
199  if(e->source() == this || m_acceptDrop == false)
200  return;
201 
202  const QMimeData* mdata = e->mimeData();
203  if(mdata->text() == "TerraView: Link Table")
204  e->setAccepted(true);
205  else
206  e->setAccepted(false);
207 }
208 
210 {
211  if(e->source() == this || m_acceptDrop == false)
212  return;
213 
214  const QMimeData* mdata = e->mimeData();
215  if(mdata->text() == "TerraView: Link Table")
216  e->setAccepted(true);
217  else
218  e->setAccepted(false);
219 }
220 
222 {
223  if(e->source() == this || m_acceptDrop == false)
224  return;
225 
226  const QMimeData* mdata = e->mimeData();
227  std::string text = mdata->text().toStdString();
228  if(text == "TerraView: Link Table")
229  {
230  e->setDropAction(Qt::LinkAction);
231 
232  if(m_layer)
233  {
234  te::da::DataSourcePtr sds = GetDataSource(m_layer);
235  m_secondLinkInfo.m_dataSourceId = sds->getId();
236  m_secondLinkInfo.m_dataSourceType = sds->getType();
237  m_secondLinkInfo.m_connInfo = sds->getConnectionInfo();
238  m_secondLinkInfo.m_layerId = m_layer->getId();
239  m_secondLinkInfo.m_layerName = m_layer->getSchema()->getName();
240  m_secondLinkInfo.m_layerTitle = m_layer->getTitle();
241  m_secondLinkInfo.m_layerType = m_layer->getType();
242 
243  if(mdata->data("DataSourceId").isNull() == false)
244  {
245  m_firstLinkInfo.m_dataSourceId = mdata->data("DataSourceId").data();
246  m_firstLinkInfo.m_dataSourceType = mdata->data("DataSourceType").data();
247  QString connInfo(mdata->data("DataSourceConnInfo").data());
248  m_firstLinkInfo.m_connInfo = *(std::map<std::string, std::string>*)connInfo.toULongLong();
249  m_firstLinkInfo.m_layerName = mdata->data("LayerName").data();
250  m_firstLinkInfo.m_layerTitle = mdata->data("LayerTitle").data();
251  m_firstLinkInfo.m_layerId = mdata->data("LayerId").data();
252  m_firstLinkInfo.m_layerType = mdata->data("LayerType").data();
253 
254  if(m_firstLinkInfo.m_dataSourceId != m_secondLinkInfo.m_dataSourceId)
255  {
256  QMessageBox::warning(this, "Link Error", "The data are from different sources");
257  return;
258  }
259 
260  if(m_firstLinkInfo.m_layerId == m_secondLinkInfo.m_layerId)
261  {
262  QMessageBox::warning(this, "Link Error", "The data are from the same layer");
263  return;
264  }
265  }
266  }
267 
268  QString fromFieldNumber(mdata->data("FieldNumber").data());
269  m_firstLinkInfo.m_fieldNumber = fromFieldNumber.toUInt();
270  m_firstLinkInfo.m_fieldName = mdata->data("FieldName").data();
271  QString fromFieldType(mdata->data("FieldType").data());
272  m_firstLinkInfo.m_fieldType = fromFieldType.toUInt();
273 
274  m_secondLinkInfo.m_fieldNumber = logicalIndexAt(e->pos());
275  m_secondLinkInfo.m_fieldType = m_dset->getPropertyDataType(m_secondLinkInfo.m_fieldNumber);
276  if(m_secondLinkInfo.m_fieldType != m_firstLinkInfo.m_fieldType)
277  {
278  QMessageBox::warning(this, "Link Error", "Field types do not match");
279  return;
280  }
281  m_secondLinkInfo.m_fieldName = m_dset->getPropertyName(m_secondLinkInfo.m_fieldNumber);
282 
283  if(mdata->data("DataSourceId").isNull() == false && m_layer)
284  emit linkTable(m_firstLinkInfo, m_secondLinkInfo);
285  else
286  emit linkTable(m_firstLinkInfo.m_fieldName, m_secondLinkInfo.m_fieldName);
287  }
288 }
289 
291 {
292  // Getting data source, if it is available
294 
295  if(layer->getType() == "DATASETLAYER")
296  {
297  const te::map::DataSetLayer* l = dynamic_cast<const te::map::DataSetLayer*>(layer);
298 
299  if(l != 0)
301  }
302  else if(layer->getType() == "DATASETADAPTERLAYER")
303  {
304  const te::map::DataSetAdapterLayer* l = dynamic_cast<const te::map::DataSetAdapterLayer*>(layer);
305 
306  if(l != 0)
308  }
309  else if(layer->getType() == "QUERYLAYER")
310  {
311  const te::map::QueryLayer* l = dynamic_cast<const te::map::QueryLayer*>(layer);
312 
313  if(l != 0)
315  }
316 
317  return ds;
318 }
virtual const std::string & getType() const =0
It returns the layer type.
This is the base class for layers.
Definition: AbstractLayer.h:76
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
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
te::da::DataSourcePtr GetDataSource(const te::map::AbstractLayer *layer)
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.
const std::string & getDataSourceId() const
void dragEnterEvent(QDragEnterEvent *e)
Handles the drag enter event.
const std::string & getDataSourceId() const
void dragMoveEvent(QDragMoveEvent *e)
Handles the drag move event.
const std::string & getDataSourceId() const
Definition: QueryLayer.cpp:352
void dropEvent(QDropEvent *e)
Handles the drop Event.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
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.
te::da::DataSourcePtr GetDataSource(const te::map::AbstractLayer *layer)
It gets the layer data source.