GenericSelectItemDialog.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "GenericSelectItemDialog.h"
  10. #include <ui_GenericSelectItemDialog.h>
  11. // CGenericSelectItemDialog dialog
  12. CGenericSelectItemDialog::CGenericSelectItemDialog(QWidget* pParent /*=nullptr*/)
  13. : QDialog(pParent)
  14. , ui(new Ui::CGenericSelectItemDialog)
  15. , m_initialized(false)
  16. {
  17. m_mode = eMODE_LIST;
  18. m_bSet = false;
  19. m_bAllowNew = false;
  20. m_bShowDesc = true;
  21. setWindowTitle(tr("Please choose..."));
  22. ui->setupUi(this);
  23. connect(ui->m_listBox, &QListWidget::itemSelectionChanged, this, &CGenericSelectItemDialog::OnLbnSelchangeList);
  24. connect(ui->m_listBox, &QListWidget::itemDoubleClicked, this, &CGenericSelectItemDialog::OnLbnDoubleClick);
  25. connect(ui->m_tree, &QTreeWidget::itemSelectionChanged, this, &CGenericSelectItemDialog::OnTvnSelchangedTree);
  26. connect(ui->m_tree, &QTreeWidget::itemDoubleClicked, this, &CGenericSelectItemDialog::OnTvnDoubleClick);
  27. connect(ui->m_buttonBox, &QDialogButtonBox::accepted, this, &CGenericSelectItemDialog::accept);
  28. connect(ui->m_buttonBox, &QDialogButtonBox::rejected, this, &CGenericSelectItemDialog::reject);
  29. connect(ui->m_newButton, &QAbstractButton::clicked, this, &CGenericSelectItemDialog::OnBnClickedNew);
  30. }
  31. CGenericSelectItemDialog::~CGenericSelectItemDialog()
  32. {
  33. }
  34. // CGenericSelectItemDialog message handlers
  35. //////////////////////////////////////////////////////////////////////////
  36. void CGenericSelectItemDialog::OnInitDialog()
  37. {
  38. if (m_mode == eMODE_LIST)
  39. {
  40. ui->m_tree->hide();
  41. }
  42. else
  43. {
  44. ui->m_listBox->hide();
  45. }
  46. ui->m_newButton->setVisible(m_bAllowNew);
  47. ui->m_desc->setVisible(m_bShowDesc);
  48. if (m_bSet == false)
  49. {
  50. GetItems(m_items);
  51. }
  52. ReloadItems();
  53. if (m_mode == eMODE_TREE)
  54. {
  55. ui->m_tree->setFocus();
  56. }
  57. else
  58. {
  59. ui->m_listBox->setFocus();
  60. }
  61. }
  62. //////////////////////////////////////////////////////////////////////////
  63. void CGenericSelectItemDialog::GetItems([[maybe_unused]] std::vector<SItem>& outItems)
  64. {
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. void CGenericSelectItemDialog::ReloadTree()
  68. {
  69. ui->m_tree->clear();
  70. QTreeWidgetItem* hSelected = nullptr;
  71. std::map<QString, QTreeWidgetItem*, less_qstring_icmp> items;
  72. QRegularExpression sep(QStringLiteral("[\\/.") + m_treeSeparator + QStringLiteral("]+"));
  73. for (SItem& item : m_items)
  74. {
  75. QString name = item.name;
  76. QTreeWidgetItem* hRoot = nullptr;
  77. QString itemName;
  78. for (const QString& token : name.split(sep))
  79. {
  80. itemName += token + QStringLiteral("/");
  81. QTreeWidgetItem* hParentItem = stl::find_in_map(items, itemName, nullptr);
  82. if (!hParentItem)
  83. {
  84. hRoot = hRoot == nullptr ? new QTreeWidgetItem(ui->m_tree) : new QTreeWidgetItem(hRoot);
  85. hRoot->setText(0, token);
  86. items[itemName] = hRoot;
  87. }
  88. else
  89. {
  90. hRoot = hParentItem;
  91. }
  92. }
  93. Q_ASSERT(hRoot);
  94. hRoot->setData(0, Qt::UserRole, QVariant::fromValue<void*>(&item));
  95. if (!m_preselect.isEmpty() && m_preselect.compare(name, Qt::CaseInsensitive) == 0)
  96. {
  97. hSelected = hRoot;
  98. }
  99. }
  100. ui->m_tree->expandAll();
  101. if (hSelected != nullptr)
  102. {
  103. ui->m_tree->scrollToItem(hSelected);
  104. ui->m_tree->setCurrentItem(hSelected, 0);
  105. OnTvnSelchangedTree();
  106. }
  107. }
  108. //////////////////////////////////////////////////////////////////////////
  109. void CGenericSelectItemDialog::ReloadItems()
  110. {
  111. m_selectedItem.clear();
  112. m_selectedDesc.clear();
  113. if (m_mode == eMODE_TREE)
  114. {
  115. ReloadTree();
  116. }
  117. else // eMODE_LIST
  118. {
  119. std::vector<SItem>::const_iterator iter = m_items.begin();
  120. while (iter != m_items.end())
  121. {
  122. const SItem* pItem = &*iter;
  123. QListWidgetItem* item = new QListWidgetItem(pItem->name, ui->m_listBox);
  124. item->setData(Qt::UserRole, pItem->desc);
  125. ui->m_listBox->addItem(item);
  126. ++iter;
  127. }
  128. if (!m_preselect.isEmpty())
  129. {
  130. QList<QListWidgetItem*> foundItems = ui->m_listBox->findItems(m_preselect, Qt::MatchExactly);
  131. if (!foundItems.isEmpty())
  132. {
  133. ui->m_listBox->setCurrentItem(foundItems[0]);
  134. OnLbnSelchangeList();
  135. }
  136. }
  137. }
  138. ItemSelected();
  139. }
  140. //////////////////////////////////////////////////////////////////////////
  141. void CGenericSelectItemDialog::OnTvnSelchangedTree()
  142. {
  143. QTreeWidgetItem* item = ui->m_tree->currentItem();
  144. if (item)
  145. {
  146. SItem* pItem = reinterpret_cast<SItem*>(item->data(0, Qt::UserRole).value<void*>());
  147. if (pItem)
  148. {
  149. m_selectedItem = pItem->name;
  150. m_selectedDesc = pItem->desc;
  151. }
  152. else
  153. {
  154. m_selectedItem.clear();
  155. m_selectedDesc.clear();
  156. }
  157. ItemSelected();
  158. }
  159. }
  160. //////////////////////////////////////////////////////////////////////////
  161. void CGenericSelectItemDialog::OnTvnDoubleClick()
  162. {
  163. if (m_selectedItem.isEmpty() == false)
  164. {
  165. accept();
  166. }
  167. }
  168. //////////////////////////////////////////////////////////////////////////
  169. QString CGenericSelectItemDialog::GetSelectedItem()
  170. {
  171. return m_selectedItem;
  172. }
  173. //////////////////////////////////////////////////////////////////////////
  174. void CGenericSelectItemDialog::OnLbnDoubleClick()
  175. {
  176. if (m_selectedItem.isEmpty() == false)
  177. {
  178. accept();
  179. }
  180. }
  181. //////////////////////////////////////////////////////////////////////////
  182. void CGenericSelectItemDialog::OnLbnSelchangeList()
  183. {
  184. QListWidgetItem* item = ui->m_listBox->currentItem();
  185. if (item == nullptr)
  186. {
  187. m_selectedItem.clear();
  188. }
  189. else
  190. {
  191. m_selectedItem = item->text();
  192. m_selectedDesc = item->data(Qt::UserRole).toString();
  193. }
  194. ItemSelected();
  195. }
  196. //////////////////////////////////////////////////////////////////////////
  197. void CGenericSelectItemDialog::OnBnClickedNew()
  198. {
  199. done(New);
  200. }
  201. //////////////////////////////////////////////////////////////////////////
  202. void CGenericSelectItemDialog::ItemSelected()
  203. {
  204. if (m_selectedItem.isEmpty())
  205. {
  206. ui->m_desc->setText(tr("<Nothing selected>"));
  207. }
  208. else
  209. {
  210. if (m_selectedDesc.isEmpty())
  211. {
  212. ui->m_desc->setText(m_selectedItem);
  213. }
  214. else
  215. {
  216. ui->m_desc->setText(m_selectedDesc);
  217. }
  218. }
  219. }
  220. void CGenericSelectItemDialog::showEvent(QShowEvent* event)
  221. {
  222. if (!m_initialized)
  223. {
  224. OnInitDialog();
  225. m_initialized = true;
  226. }
  227. QDialog::showEvent(event);
  228. }
  229. #include <moc_GenericSelectItemDialog.cpp>