Debian Codegraph @

plugins/core/Standard/qCloudLayers/src/ccCloudLayersDlg.cpp

raw
1 #include "../include/ccCloudLayersDlg.h"
2
3 #include "../include/ccColorStyledDelegate.h"
4 #include "../include/ccMouseCircle.h"
5
6 //QT
7 #include <QColorDialog>
8 #include <QKeyEvent>
9 #include <QMessageBox>
10 #include <QSettings>
11 #include <QSortFilterProxyModel>
12 #include <QWidget>
13
14 //CC
15 #include <ccGLWindowInterface.h>
16 #include <ccMainAppInterface.h>
17 #include <ccPointCloud.h>
18
19 ccCloudLayersDlg::ccCloudLayersDlg(ccMainAppInterface* app, QWidget* parent)
20 : ccOverlayDialog(parent)
21 , Ui::ccCloudLayersDlg()
22 , m_app(app)
23 , m_helper(nullptr)
24 , m_mouseCircle(nullptr)
25 {
26 setupUi(this);
27
28 setWindowTitle(QString("Cloud layers plugin"));
29
30 // allow resize and move window
31 setWindowFlags(Qt::Tool | Qt::CustomizeWindowHint/* | Qt::WindowTitleHint*/);
32
33 // set model to tableView
34 initTableView();
35
36 // connect buttons
37 connect(pbAdd, &QPushButton::clicked, this, &ccCloudLayersDlg::addClicked);
38 connect(pbDelete, &QPushButton::clicked, this, &ccCloudLayersDlg::deleteClicked);
39 connect(pbStart, &QPushButton::clicked, this, &ccCloudLayersDlg::startClicked);
40 connect(pbPause, &QPushButton::clicked, this, &ccCloudLayersDlg::pauseClicked);
41 connect(pbApply, &QPushButton::clicked, this, &ccCloudLayersDlg::applyClicked);
42 connect(pbClose, &QPushButton::clicked, this, &ccCloudLayersDlg::closeClicked);
43
44 // connect comboboxes
45 connect(cbScalarField, qOverload<int>(&QComboBox::currentIndexChanged), this, &ccCloudLayersDlg::scalarFieldIndexChanged);
46 connect(cbInput, qOverload<int>(&QComboBox::currentIndexChanged), this, &ccCloudLayersDlg::inputClassIndexChanged);
47 connect(cbOutput, qOverload<int>(&QComboBox::currentIndexChanged), this, &ccCloudLayersDlg::outputClassIndexChanged);
48
49 // color picker
50 connect(tableView, &QTableView::doubleClicked, this, &ccCloudLayersDlg::tableViewDoubleClicked);
51
52 // asprs model changed signals
53 connect(&m_asprsModel, &ccAsprsModel::codeChanged, this, &ccCloudLayersDlg::codeChanged);
54 connect(&m_asprsModel, &ccAsprsModel::colorChanged, this, &ccCloudLayersDlg::colorChanged);
55
56 m_presets.append(QString("All Points"));
57 m_presets.append(QString("Visible Points"));
58
59 m_mouseCircle = new ccMouseCircle(m_app->getActiveGLWindow());
60 m_mouseCircle->setVisible(false);
61 }
62
63 ccCloudLayersDlg::~ccCloudLayersDlg()
64 {
65 setPointCloud(nullptr);
66
67 if (m_mouseCircle)
68 {
69 delete m_mouseCircle;
70 m_mouseCircle = nullptr;
71 }
72 }
73
74 void ccCloudLayersDlg::reject()
75 {
76 if (m_helper && m_helper->hasChanges())
77 {
78 if (QMessageBox::question(m_associatedWin->asWidget(), "Cloud layers plugin", "The cloud has been modified, are you sure you want exit?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
79 {
80 return;
81 }
82 }
83
84 stop(false);
85 }
86
87 bool ccCloudLayersDlg::start()
88 {
89 if (!m_helper)
90 {
91 return false;
92 }
93
94 m_asprsModel.load();
95 updateInputOutput();
96 loadSettings();
97
98 resetUI();
99 m_app->freezeUI(true);
100
101 connect(m_associatedWin->signalEmitter(), &ccGLWindowSignalEmitter::mouseMoved, this, &ccCloudLayersDlg::mouseMoved);
102
103 return ccOverlayDialog::start();
104 }
105
106 void ccCloudLayersDlg::stop(bool accepted)
107 {
108 if (m_mouseCircle && m_mouseCircle->isVisible())
109 pauseClicked();
110
111 if (accepted && m_helper)
112 {
113 m_helper->keepCurrentSFVisible();
114 }
115
116 setPointCloud(nullptr);
117
118 if (m_app)
119 {
120 m_app->freezeUI(false);
121 }
122
123 ccOverlayDialog::stop(accepted);
124 }
125
126 void ccCloudLayersDlg::setPointCloud(ccPointCloud* cloud)
127 {
128 if (m_helper)
129 {
130 delete m_helper;
131 m_helper = nullptr;
132 }
133
134 cbScalarField->clear();
135
136 if (cloud)
137 {
138 m_helper = new ccCloudLayersHelper(m_app, cloud);
139
140 cbScalarField->addItems(m_helper->getScalarFields());
141 }
142 }
143
144 void ccCloudLayersDlg::resetUI()
145 {
146 pbStart->setEnabled(true);
147 pbPause->setEnabled(false);
148 }
149
150 void ccCloudLayersDlg::initTableView()
151 {
152 QSortFilterProxyModel* proxyModel = new QSortFilterProxyModel(this);
153 proxyModel->setSourceModel(&m_asprsModel);
154 tableView->setModel(proxyModel);
155 tableView->setSortingEnabled(true);
156 tableView->sortByColumn(ccAsprsModel::CODE, Qt::AscendingOrder);
157
158 // set column delegates
159 tableView->setItemDelegateForColumn(ccAsprsModel::COLOR, new ccColorStyledDelegate(this));
160
161 tableView->horizontalHeader()->setSectionResizeMode(ccAsprsModel::NAME, QHeaderView::Stretch);
162 tableView->horizontalHeader()->setSectionResizeMode(ccAsprsModel::VISIBLE, QHeaderView::ResizeToContents);
163 tableView->horizontalHeader()->setSectionResizeMode(ccAsprsModel::CODE, QHeaderView::ResizeToContents);
164 tableView->horizontalHeader()->setSectionResizeMode(ccAsprsModel::COLOR, QHeaderView::ResizeToContents);
165 tableView->horizontalHeader()->setSectionResizeMode(ccAsprsModel::COUNT, QHeaderView::ResizeToContents);
166 }
167
168 void ccCloudLayersDlg::saveSettings()
169 {
170 QSettings settings;
171 settings.beginGroup("qCloudLayers");
172 {
173 if (cbScalarField->currentIndex() >= 0)
174 settings.setValue("ScalarField", cbScalarField->currentText());
175 if (cbInput->currentIndex() >= 0)
176 settings.setValue("InputClass", cbInput->currentText());
177 if (cbOutput->currentIndex() >= 0)
178 settings.setValue("OutputClass", cbOutput->currentText());
179
180 settings.beginGroup("Window");
181 {
182 settings.setValue("geometry", saveGeometry());
183 }
184 settings.sync();
185 }
186 settings.endGroup();
187 }
188
189 void ccCloudLayersDlg::loadSettings()
190 {
191 if (!m_helper)
192 {
193 return;
194 }
195
196 QSettings settings;
197 settings.beginGroup("qCloudLayers");
198 {
199 QString sfName = settings.value("ScalarField").toString();
200
201 int sfIndex = m_helper->getScalarFields().indexOf(sfName);
202 if (sfIndex < 0)
203 {
204 // previous scalar field not found
205 sfName = "Classification";
206 sfIndex = m_helper->getScalarFields().indexOf(sfName);
207 if (sfIndex < 0)
208 {
209 // we'll take the first one
210 sfIndex = 0;
211 }
212 }
213
214 cbScalarField->setCurrentIndex(sfIndex);
215
216 QString inputName = settings.value("InputClass").toString();
217 const ccAsprsModel::AsprsItem* item = m_asprsModel.find(inputName);
218
219 if (!item)
220 {
221 int index = m_presets.indexOf(inputName);
222 cbInput->setCurrentIndex(std::max(0, index));
223 }
224 else
225 {
226 cbInput->setCurrentIndex(m_asprsModel.indexOf(inputName) + m_presets.size());
227 }
228
229 QString outputName = settings.value("OutputClass").toString();
230 int outIndex = m_asprsModel.indexOf(outputName);
231 cbOutput->setCurrentIndex(outIndex == -1 ? 0 : outIndex);
232
233 settings.beginGroup("Window");
234 {
235 restoreGeometry(settings.value("geometry").toByteArray());
236 }
237 settings.endGroup();
238 }
239 settings.endGroup();
240 }
241
242 void ccCloudLayersDlg::addClicked()
243 {
244 QModelIndex index = m_asprsModel.createNewItem();
245 tableView->selectRow(index.row());
246 tableView->setCurrentIndex(index);
247
248 updateInputOutput();
249 }
250
251 void ccCloudLayersDlg::deleteClicked()
252 {
253 QItemSelectionModel* select = tableView->selectionModel();
254 if (!select->hasSelection())
255 return;
256
257 if (QMessageBox::question(m_associatedWin->asWidget(), "Cloud layers plugin", "Are you sure you want to delete this record(s)?",
258 QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
259 {
260 return;
261 }
262
263 QModelIndexList mapIndices = select->selectedIndexes();
264 qSort(mapIndices);
265
266 QModelIndexList sourceIndices;
267 for (QModelIndex index : mapIndices)
268 {
269 QModelIndex sourceIndex = static_cast<QSortFilterProxyModel*>(tableView->model())->mapToSource(index);
270 sourceIndices.append(sourceIndex);
271 }
272
273 ccAsprsModel::AsprsItem* to = m_asprsModel.getData().size() > 0 ? &(m_asprsModel.getData().front()) : nullptr;
274 for (int i = mapIndices.size(); i > 0; --i)
275 {
276 ccAsprsModel::AsprsItem& from = m_asprsModel.getData()[sourceIndices[i - 1].row()];
277 int affected = m_helper ? m_helper->moveItem(from, to) : 0;
278 if (to)
279 {
280 to->count += affected;
281 }
282
283 tableView->model()->removeRows(mapIndices[i - 1].row(), 1);
284 }
285
286 updateInputOutput();
287
288 m_associatedWin->redraw();
289 }
290
291 void ccCloudLayersDlg::startClicked()
292 {
293 if (nullptr == m_app->getActiveGLWindow())
294 {
295 return;
296 }
297
298 m_app->getActiveGLWindow()->setPickingMode(ccGLWindowInterface::PICKING_MODE::NO_PICKING);
299
300 //set orthographic view (as this tool doesn't work in perspective mode)
301 m_app->getActiveGLWindow()->setPerspectiveState(false, true);
302 m_app->getActiveGLWindow()->setInteractionMode(ccGLWindowInterface::INTERACT_SEND_ALL_SIGNALS);
303 m_mouseCircle->setVisible(true);
304
305 pbStart->setEnabled(false);
306 pbPause->setEnabled(true);
307 }
308
309 void ccCloudLayersDlg::pauseClicked()
310 {
311 if (nullptr == m_app->getActiveGLWindow())
312 {
313 return;
314 }
315
316 m_mouseCircle->setVisible(false);
317 m_app->getActiveGLWindow()->setPickingMode(ccGLWindowInterface::PICKING_MODE::DEFAULT_PICKING);
318 m_app->getActiveGLWindow()->setInteractionMode(ccGLWindowInterface::MODE_TRANSFORM_CAMERA);
319 m_app->getActiveGLWindow()->redraw(true, false);
320
321 pbStart->setEnabled(true);
322 pbPause->setEnabled(false);
323 }
324
325 void ccCloudLayersDlg::applyClicked()
326 {
327 m_asprsModel.save();
328
329 saveSettings();
330
331 if (m_helper)
332 {
333 m_helper->setVisible(true);
334 }
335
336 stop(true);
337 }
338
339 void ccCloudLayersDlg::closeClicked()
340 {
341 if (m_helper)
342 {
343 if (m_helper->hasChanges())
344 {
345 if (QMessageBox::question(m_associatedWin->asWidget(), "Cloud layers plugin", "The cloud has been modified, are you sure you want exit?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
346 {
347 return;
348 }
349 }
350
351 m_helper->restoreState();
352 }
353
354 stop(true);
355 }
356
357 void ccCloudLayersDlg::mouseMoved(int x, int y, Qt::MouseButtons buttons)
358 {
359 if (!m_helper)
360 {
361 return;
362 }
363 if (buttons != Qt::LeftButton)
364 {
365 return;
366 }
367
368 ccGLCameraParameters camera;
369 m_associatedWin->getGLCameraParameters(camera);
370
371 m_helper->projectCloud(camera);
372
373 QPointF pos2D = m_app->getActiveGLWindow()->toCenteredGLCoordinates(x, y);
374 CCVector2 center(static_cast<PointCoordinateType>(pos2D.x()), static_cast<PointCoordinateType>(pos2D.y()));
375
376 int radius = m_mouseCircle->getRadiusPx();
377 std::map<ScalarType, int> affected;
378 m_helper->mouseMove(center, radius * radius, affected);
379
380 // update point counts
381 for (const auto& kv : affected)
382 {
383 auto item = m_asprsModel.find(kv.first);
384 if (item)
385 item->count += kv.second;
386 }
387
388 m_asprsModel.refreshData();
389 }
390
391 bool ccCloudLayersDlg::eventFilter(QObject* obj, QEvent* event)
392 {
393 if (event->type() == QEvent::KeyPress)
394 {
395 QKeyEvent* ev = static_cast<QKeyEvent*>(event);
396 if (ev->key() == Qt::Key::Key_Alt)
397 {
398 swapInputOutput();
399 m_mouseCircle->setAllowScroll(false);
400 }
401 }
402 else if (event->type() == QEvent::KeyRelease)
403 {
404 QKeyEvent* ev = static_cast<QKeyEvent*>(event);
405 if (ev->key() == Qt::Key::Key_Alt)
406 {
407 swapInputOutput();
408 m_mouseCircle->setAllowScroll(true);
409 }
410 }
411
412 return false;
413 }
414
415 void ccCloudLayersDlg::scalarFieldIndexChanged(int index)
416 {
417 if (m_helper)
418 {
419 m_helper->setScalarFieldIndex(index);
420 m_helper->apply(m_asprsModel.getData());
421 m_helper->saveState();
422 }
423
424 // refresh point count
425 m_asprsModel.refreshData();
426 }
427
428 void ccCloudLayersDlg::inputClassIndexChanged(int index)
429 {
430 if (!m_helper)
431 {
432 return;
433 }
434 ccCloudLayersHelper::Parameters& params = m_helper->getParameters();
435 if (cbInput->currentIndex() < 0)
436 {
437 params.anyPoints = false;
438 params.visiblePoints = false;
439 params.input = nullptr;
440
441 return;
442 }
443
444 QString inputName = cbInput->itemText(cbInput->currentIndex());
445 params.anyPoints = (inputName == m_presets[0]);
446 params.visiblePoints = (inputName == m_presets[1]);
447 params.input = nullptr;
448
449 if (!(params.anyPoints || params.visiblePoints))
450 params.input = m_asprsModel.find(inputName);
451 }
452
453 void ccCloudLayersDlg::outputClassIndexChanged(int index)
454 {
455 if (!m_helper)
456 {
457 return;
458 }
459 ccCloudLayersHelper::Parameters& params = m_helper->getParameters();
460 if (cbInput->currentIndex() < 0)
461 {
462 params.output = nullptr;
463 return;
464 }
465
466 QString outputName = cbOutput->itemText(cbOutput->currentIndex());
467 params.output = m_asprsModel.find(outputName);
468 }
469
470 void ccCloudLayersDlg::codeChanged(ccAsprsModel::AsprsItem& item, int oldCode)
471 {
472 if (m_helper)
473 {
474 m_helper->changeCode(item, static_cast<ScalarType>(oldCode));
475 }
476 }
477
478 void ccCloudLayersDlg::colorChanged(ccAsprsModel::AsprsItem& item)
479 {
480 if (!m_helper)
481 {
482 return;
483 }
484
485 item.count = m_helper->apply(item, true);
486
487 // refresh point count
488 m_asprsModel.refreshData();
489 }
490
491 void ccCloudLayersDlg::tableViewDoubleClicked(const QModelIndex& index)
492 {
493 if (index.column() != ccAsprsModel::COLOR)
494 return;
495
496 QColor currColor = index.model()->data(index, Qt::DisplayRole).value<QColor>();
497 QColor color = QColorDialog::getColor(currColor, this, "Pick a color", QColorDialog::DontUseNativeDialog);
498
499 if (color.isValid() && color != currColor)
500 tableView->model()->setData(index, color, Qt::EditRole);
501 }
502
503 void ccCloudLayersDlg::updateInputOutput()
504 {
505 auto data = m_asprsModel.getData();
506
507 cbInput->clear();
508 cbInput->addItems(m_presets);
509 for (int i = 0; i < data.size(); ++i)
510 cbInput->addItem(data[i].name);
511
512 cbOutput->clear();
513 for (int i = 0; i < data.size(); ++i)
514 cbOutput->addItem(data[i].name);
515 }
516
517 void ccCloudLayersDlg::swapInputOutput()
518 {
519 int inputIndex = cbInput->currentIndex();
520 int outputIndex = cbOutput->currentIndex();
521
522 if (inputIndex < 0 || outputIndex < 0)
523 return;
524
525 QString text = cbInput->itemText(cbInput->currentIndex());
526 bool isPresets = m_presets.contains(text);
527
528 if (!isPresets)
529 {
530 cbInput->setCurrentIndex(outputIndex + m_presets.size());
531 cbOutput->setCurrentIndex(inputIndex - m_presets.size());
532 }
533
534 inputIndex = cbInput->currentIndex();
535 outputIndex = cbOutput->currentIndex();
536 }