SnapOptionsDialog.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 /*!
21  \file terralib/edit/qt/SnapOptionsWidget.cpp
22 
23  \brief A dialog used to configure geometry snap options.
24 */
25 
26 // TerraLib
27 #include "../../dataaccess/dataset/DataSet.h"
28 #include "../Snap.h"
29 #include "../SnapManager.h"
30 #include "SnapOptionsDialog.h"
31 #include "ui_SnapOptionsDialogForm.h"
32 
33 // Qt
34 #include <QCheckBox>
35 #include <QComboBox>
36 #include <QLineEdit>
37 #include <QTableWidgetItem>
38 
39 // STL
40 #include <cassert>
41 
43 
44 te::edit::SnapOptionsDialog::SnapOptionsDialog(QWidget* parent, Qt::WindowFlags f)
45  : QDialog(parent, f),
46  m_ui(new Ui::SnapOptionsDialogForm),
47  m_display(nullptr)
48 {
49  m_ui->setupUi(this);
50 
51  // Signals & slots
52  connect(m_ui->m_okPushButton, SIGNAL(pressed()), this, SLOT(onOkPushButtonPressed()));
53  connect(m_ui->m_cancelPushButton, SIGNAL(clicked()), this, SLOT(onCancelPushButtonClicked()));
54 
55  m_ui->m_helpPushButton->setNameSpace("dpi.inpe.br.plugins");
56  m_ui->m_helpPushButton->setPageReference("plugins/edit/snappingtolerance.html");
57 }
58 
60 
61 void te::edit::SnapOptionsDialog::setLayers(const std::list<te::map::AbstractLayerPtr>& layers)
62 {
63  m_layers = layers;
64 
65  buildOptions();
66 }
67 
69 {
70  m_display = display;
71 }
72 
74 {
75  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = m_layers.begin(); it != m_layers.end(); ++it)
76  buildOptions(*it);
77 }
78 
80 {
81  for(std::size_t i = 0; i < layer->getChildrenCount(); ++i)
82  {
83  te::map::AbstractLayerPtr child(boost::dynamic_pointer_cast<te::map::AbstractLayer>(layer->getChild(i)));
84  buildOptions(child);
85  }
86 
87  if(layer->getType() == "FOLDER_LAYER")
88  return;
89 
90  std::unique_ptr<te::da::DataSetType> dsType = layer->getSchema();
91  if (dsType->hasRaster())
92  return;
93 
94  int row = m_ui->m_tableOptionsWidget->rowCount() + 1;
95  m_ui->m_tableOptionsWidget->setRowCount(row);
96 
97  int currentRow = row - 1;
98  int currentCollumn = 0;
99 
100  // Enable
101  QCheckBox* enableCheckBox = new QCheckBox(m_ui->m_tableOptionsWidget);
102 
103  if(SnapManager::getInstance().hasSnap(layer->getId()))
104  enableCheckBox->setChecked(true);
105 
106  m_ui->m_tableOptionsWidget->setCellWidget(currentRow, currentCollumn++, enableCheckBox);
107 
108  // Layer
109  QTableWidgetItem* layerItem = new QTableWidgetItem(layer->getTitle().c_str());
110  layerItem->setTextAlignment(Qt::AlignCenter);
111  layerItem->setFlags(Qt::ItemIsEnabled);
112  layerItem->setData(Qt::UserRole, QVariant::fromValue(layer));
113  m_ui->m_tableOptionsWidget->setItem(currentRow, currentCollumn++, layerItem);
114 
115  // Snap Mode
116  QComboBox* modeComboBox = new QComboBox(m_ui->m_tableOptionsWidget);
117  modeComboBox->addItem(tr("vertex"));
118  modeComboBox->setEnabled(false);
119  m_ui->m_tableOptionsWidget->setCellWidget(currentRow, currentCollumn++, modeComboBox);
120 
121  // Tolerance
122  QValidator *validator = new QIntValidator(4, 100, this);
123  QLineEdit *edit = new QLineEdit(m_ui->m_tableOptionsWidget);
124  edit->setValidator(validator);
125  edit->setText("16");
126 
127  if(SnapManager::getInstance().hasSnap(layer->getId()))
128  {
129  Snap* snap = SnapManager::getInstance().getSnap(layer->getId());
130  edit->setText(QString::number(snap->getTolerance()));
131  }
132 
133  m_ui->m_tableOptionsWidget->setCellWidget(currentRow, currentCollumn++, edit);
134 
135  // Units
136  QComboBox* unitsComboBox = new QComboBox(m_ui->m_tableOptionsWidget);
137  unitsComboBox->addItem(tr("pixels"));
138  unitsComboBox->setEnabled(false);
139  m_ui->m_tableOptionsWidget->setCellWidget(currentRow, currentCollumn++, unitsComboBox);
140 }
141 
143 {
144  for(int i = 0; i < m_ui->m_tableOptionsWidget->rowCount(); ++i) // for each option
145  {
146  QCheckBox* enableCheckBox = dynamic_cast<QCheckBox*>(m_ui->m_tableOptionsWidget->cellWidget(i, 0));
147 
148  // Get the layer
149  te::map::AbstractLayerPtr layer = m_ui->m_tableOptionsWidget->item(i, 1)->data(Qt::UserRole).value<te::map::AbstractLayerPtr>();
150 
151  // TODO: mode, tolerance and units
152 
153  if(enableCheckBox->isChecked())
154  {
155  if(SnapManager::getInstance().hasSnap(layer->getId()) == false)
156  {
157  // Build the snap
158  std::unique_ptr<te::da::DataSet> dataset(layer->getData());
159  SnapManager::getInstance().buildSnap(layer->getId(), layer->getSRID(), dataset.get());
160 
161  if (m_display)
162  {
163  const te::gm::Envelope& env = m_display->getExtent();
164  SnapManager::getInstance().setWorld(env.m_llx, env.m_lly, env.m_urx, env.m_ury, m_display->getWidth(), m_display->getHeight());
165  }
166  }
167  }
168  else
169  {
170  // Remove the snap
171  SnapManager::getInstance().removeSnap(layer->getId());
172  }
173  }
174 
175  close();
176 }
177 
179 {
180  reject();
181 }
This class implements geometry snap concept.
Definition: Snap.h:63
double getTolerance() const
Definition: Snap.cpp:75
unsigned int getWidth() const
It returns the MapDisplay current width in pixels.
std::unique_ptr< Ui::SnapOptionsDialogForm > m_ui
Dialog form.
double m_urx
Upper right corner x-coordinate.
A widget to control the display of a set of layers.
void setLayers(const std::list< te::map::AbstractLayerPtr > &layers)
double m_llx
Lower left corner x-coordinate.
Q_DECLARE_METATYPE(te::map::AbstractLayerPtr) te
static SnapManager & getInstance()
It returns a reference to the singleton instance.
An Envelope defines a 2D rectangular region.
URI C++ Library.
Definition: Attributes.h:37
te::qt::widgets::MapDisplay * m_display
The map display associated with the tool.
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
void setMapDisplay(te::qt::widgets::MapDisplay *display)
double m_lly
Lower left corner y-coordinate.
unsigned int getHeight() const
It returns the MapDisplay current height in pixels.
~SnapOptionsDialog()
Destructor.
double m_ury
Upper right corner y-coordinate.
A dialog used to configure geometry snap options.
std::list< te::map::AbstractLayerPtr > m_layers
The layer list.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr