ccItemSelectionDlg.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #include "ccItemSelectionDlg.h"
  18. //Qt
  19. #include <QDialog>
  20. #include <QListWidgetItem>
  21. ccItemSelectionDlg::ccItemSelectionDlg( bool multiSelectionEnabled,
  22. QWidget* parent/*=nullptr*/,
  23. QString itemName/*="entities"*/,
  24. QString labelStr/*=QString()*/)
  25. : QDialog(parent, Qt::Tool)
  26. , Ui::ItemSelectionDlg()
  27. {
  28. setupUi(this);
  29. //multi-selection mode
  30. if (multiSelectionEnabled)
  31. {
  32. label->setText(tr("Please select one or several %1:\n(press CTRL+A to select all)").arg(itemName));
  33. listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
  34. }
  35. else
  36. {
  37. label->setText(tr("Please select one %1").arg(itemName));
  38. }
  39. if (!labelStr.isNull())
  40. {
  41. label->setText(labelStr);
  42. }
  43. }
  44. void ccItemSelectionDlg::setItems(const QStringList& items, int defaultSelectedIndex)
  45. {
  46. for (int i = 0; i < items.size(); ++i)
  47. {
  48. //add one line per entity in the combo-box
  49. listWidget->insertItem(static_cast<int>(i), new QListWidgetItem(items[i]));
  50. }
  51. //default selection
  52. if (defaultSelectedIndex >= 0 && defaultSelectedIndex < items.size())
  53. {
  54. listWidget->setItemSelected(listWidget->item(defaultSelectedIndex), true);
  55. }
  56. }
  57. int ccItemSelectionDlg::getSelectedIndex() const
  58. {
  59. //get selected items
  60. QList<QListWidgetItem*> list = listWidget->selectedItems();
  61. return list.empty() ? -1 : listWidget->row(list.front());
  62. }
  63. void ccItemSelectionDlg::getSelectedIndexes(std::vector<int>& indexes) const
  64. {
  65. //get selected items
  66. QList<QListWidgetItem*> list = listWidget->selectedItems();
  67. try
  68. {
  69. indexes.resize(static_cast<size_t>(list.size()));
  70. }
  71. catch (const std::bad_alloc&)
  72. {
  73. //not enough memory?!
  74. return;
  75. }
  76. for (int i = 0; i < list.size(); ++i)
  77. {
  78. indexes[i] = listWidget->row(list[i]);
  79. }
  80. }
  81. int ccItemSelectionDlg::SelectEntity(const ccHObject::Container& entities,
  82. int selectedIndex/*=0*/,
  83. QWidget* parent/*=nullptr*/,
  84. QString label/*=QString()*/)
  85. {
  86. ccItemSelectionDlg epDlg(false, parent, tr("entity"), label);
  87. QStringList items;
  88. for (size_t i = 0; i < entities.size(); ++i)
  89. {
  90. //add one line per entity
  91. items << QString("%1 (ID=%2)").arg(entities[i]->getName()).arg(entities[i]->getUniqueID());
  92. }
  93. epDlg.setItems(items, selectedIndex);
  94. if (!epDlg.exec())
  95. {
  96. //cancelled by the user
  97. return -1;
  98. }
  99. return epDlg.getSelectedIndex();
  100. }
  101. bool ccItemSelectionDlg::SelectEntities(const ccHObject::Container& entities,
  102. std::vector<int>& selectedIndexes,
  103. QWidget* parent/*=nullptr*/,
  104. QString label/*=QString()*/)
  105. {
  106. selectedIndexes.clear();
  107. ccItemSelectionDlg epDlg(true, parent, tr("entities"), label);
  108. QStringList items;
  109. for (size_t i = 0; i < entities.size(); ++i)
  110. {
  111. //add one line per entity
  112. items << QString("%1 (ID=%2)").arg(entities[i]->getName()).arg(entities[i]->getUniqueID());
  113. }
  114. epDlg.setItems(items, -1);
  115. if (!epDlg.exec())
  116. return false;
  117. epDlg.getSelectedIndexes(selectedIndexes);
  118. return true;
  119. }