ccEntitySelectionDlg.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: CloudCompare project #
  15. //# #
  16. //##########################################################################
  17. #include "ccEntitySelectionDlg.h"
  18. //ui
  19. #include <ui_entitySelectionDlg.h>
  20. //Qt
  21. #include <QDialog>
  22. #include <QListWidgetItem>
  23. ccEntitySelectionDialog::ccEntitySelectionDialog( const ccHObject::Container& entities,
  24. bool multiSelectionEnabled,
  25. int defaultSelectedIndex/*=0*/,
  26. QWidget* parent/*=nullptr*/,
  27. QString labelStr/*=QString()*/)
  28. : QDialog(parent, Qt::Tool)
  29. , m_ui(new Ui_EntitySelectionDialog)
  30. {
  31. m_ui->setupUi(this);
  32. //multi-selection mode
  33. if (multiSelectionEnabled)
  34. {
  35. m_ui->label->setText(tr("Select one or several entities:"));
  36. m_ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
  37. connect(m_ui->selectAllPushButton, &QPushButton::clicked, this, &ccEntitySelectionDialog::selectAll);
  38. connect(m_ui->selectNonePushButton, &QPushButton::clicked, this, &ccEntitySelectionDialog::selectNone);
  39. }
  40. else
  41. {
  42. m_ui->selectAllPushButton->setVisible(false);
  43. m_ui->selectNonePushButton->setVisible(false);
  44. }
  45. for (size_t i = 0; i < entities.size(); ++i)
  46. {
  47. //add one line per entity in the combo-box
  48. m_ui->listWidget->insertItem(static_cast<int>(i), new QListWidgetItem(QString("[ID %1] %2").arg(entities[i]->getUniqueID()).arg(entities[i]->getName())));
  49. }
  50. //default selection
  51. if (defaultSelectedIndex >= 0 && static_cast<size_t>(defaultSelectedIndex) < entities.size())
  52. {
  53. m_ui->listWidget->setItemSelected(m_ui->listWidget->item(defaultSelectedIndex), true);
  54. }
  55. //custom lalel
  56. if (!labelStr.isNull())
  57. {
  58. m_ui->label->setText(labelStr);
  59. }
  60. }
  61. ccEntitySelectionDialog::~ccEntitySelectionDialog()
  62. {
  63. if (m_ui)
  64. {
  65. delete m_ui;
  66. m_ui = nullptr;
  67. }
  68. }
  69. void ccEntitySelectionDialog::selectAll()
  70. {
  71. m_ui->listWidget->selectAll();
  72. }
  73. void ccEntitySelectionDialog::selectNone()
  74. {
  75. m_ui->listWidget->clearSelection();
  76. }
  77. int ccEntitySelectionDialog::getSelectedIndex() const
  78. {
  79. //get selected items
  80. QList<QListWidgetItem*> list = m_ui->listWidget->selectedItems();
  81. return list.empty() ? -1 : m_ui->listWidget->row(list.front());
  82. }
  83. void ccEntitySelectionDialog::getSelectedIndexes(std::vector<int>& indexes) const
  84. {
  85. //get selected items
  86. QList<QListWidgetItem*> list = m_ui->listWidget->selectedItems();
  87. try
  88. {
  89. indexes.resize(static_cast<size_t>(list.size()));
  90. }
  91. catch (const std::bad_alloc&)
  92. {
  93. //not enough memory?!
  94. return;
  95. }
  96. for (int i=0; i<list.size(); ++i)
  97. {
  98. indexes[i] = m_ui->listWidget->row(list[i]);
  99. }
  100. }
  101. int ccEntitySelectionDialog::SelectEntity( const ccHObject::Container& entities,
  102. int selectedIndex/*=0*/,
  103. QWidget* parent/*=nullptr*/,
  104. QString label/*=QString()*/)
  105. {
  106. ccEntitySelectionDialog epDlg(entities, false, selectedIndex, parent, label);
  107. if (!epDlg.exec())
  108. {
  109. return -1;
  110. }
  111. return epDlg.getSelectedIndex();
  112. }
  113. bool ccEntitySelectionDialog::SelectEntities( const ccHObject::Container& entities,
  114. std::vector<int>& selectedIndexes,
  115. QWidget* parent/*=nullptr*/,
  116. QString label/*=QString()*/)
  117. {
  118. selectedIndexes.clear();
  119. ccEntitySelectionDialog epDlg(entities, true, -1, parent, label);
  120. if (!epDlg.exec())
  121. {
  122. return false;
  123. }
  124. epDlg.getSelectedIndexes(selectedIndexes);
  125. return true;
  126. }