GenericSelectItemDialog.cpp 7.0 KB

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