GraphCanvasComboBox.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 <AzCore/PlatformDef.h>
  9. #include <QEvent>
  10. #include <QFocusEvent>
  11. #include <QHeaderView>
  12. #include <QScopedValueRollback>
  13. #include <QToolButton>
  14. #include <QVBoxLayout>
  15. #include <QWidgetAction>
  16. #include <Widgets/GraphCanvasComboBox.h>
  17. namespace GraphCanvas
  18. {
  19. ////////////////////////////////////////
  20. // GraphCanvasComboBoxFilterProxyModel
  21. ////////////////////////////////////////
  22. GraphCanvasComboBoxFilterProxyModel::GraphCanvasComboBoxFilterProxyModel(QObject*)
  23. {
  24. }
  25. bool GraphCanvasComboBoxFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const
  26. {
  27. if (m_filter.isEmpty())
  28. {
  29. return true;
  30. }
  31. QAbstractItemModel* model = sourceModel();
  32. if (!model)
  33. {
  34. return false;
  35. }
  36. QModelIndex index = model->index(sourceRow, filterKeyColumn(), sourceParent);
  37. QString test = model->data(index, filterRole()).toString();
  38. return (test.lastIndexOf(m_testRegex) >= 0);
  39. }
  40. void GraphCanvasComboBoxFilterProxyModel::SetFilter(const QString& filter)
  41. {
  42. m_filter = filter;
  43. m_testRegex = QRegExp(m_filter, Qt::CaseInsensitive);
  44. invalidateFilter();
  45. if (m_filter.isEmpty())
  46. {
  47. sort(-1);
  48. }
  49. else
  50. {
  51. sort(filterKeyColumn());
  52. }
  53. }
  54. ////////////////////////////
  55. // GraphCanvasComboBoxMenu
  56. ////////////////////////////
  57. GraphCanvasComboBoxMenu::GraphCanvasComboBoxMenu(ComboBoxItemModelInterface* model, QWidget* parent)
  58. : QDialog(parent, Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
  59. , m_disableHiding(false)
  60. , m_ignoreNextFocusIn(false)
  61. , m_modelInterface(model)
  62. {
  63. setProperty("HasNoWindowDecorations", true);
  64. setAttribute(Qt::WA_ShowWithoutActivating);
  65. m_filterProxyModel.setSourceModel(m_modelInterface->GetDropDownItemModel());
  66. m_filterProxyModel.sort(m_modelInterface->GetSortColumn());
  67. m_filterProxyModel.setFilterKeyColumn(m_modelInterface->GetCompleterColumn());
  68. m_tableView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
  69. m_tableView.setSelectionBehavior(QAbstractItemView::SelectRows);
  70. m_tableView.setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
  71. m_tableView.setModel(GetProxyModel());
  72. m_tableView.verticalHeader()->hide();
  73. m_tableView.verticalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents);
  74. m_tableView.horizontalHeader()->hide();
  75. m_tableView.horizontalHeader()->setStretchLastSection(true);
  76. m_tableView.installEventFilter(this);
  77. m_tableView.setFocusPolicy(Qt::FocusPolicy::ClickFocus);
  78. m_tableView.setMinimumWidth(0);
  79. QVBoxLayout* layout = new QVBoxLayout();
  80. layout->addWidget(&m_tableView);
  81. setLayout(layout);
  82. m_disableHidingStateSetter.AddStateController(GetDisableHidingStateController());
  83. QObject::connect(&m_tableView, &QTableView::clicked, this, &GraphCanvasComboBoxMenu::OnTableClicked);
  84. m_closeTimer.setInterval(0);
  85. QAction* escapeAction = new QAction(this);
  86. escapeAction->setShortcut(Qt::Key_Escape);
  87. addAction(escapeAction);
  88. connect(this, &QDialog::finished, this, [&]() {
  89. Q_EMIT OnFocusOut();
  90. });
  91. QObject::connect(escapeAction, &QAction::triggered, this, &GraphCanvasComboBoxMenu::CancelMenu);
  92. }
  93. GraphCanvasComboBoxMenu::~GraphCanvasComboBoxMenu()
  94. {
  95. }
  96. ComboBoxItemModelInterface* GraphCanvasComboBoxMenu::GetInterface()
  97. {
  98. return m_modelInterface;
  99. }
  100. const ComboBoxItemModelInterface* GraphCanvasComboBoxMenu::GetInterface() const
  101. {
  102. return m_modelInterface;
  103. }
  104. GraphCanvasComboBoxFilterProxyModel* GraphCanvasComboBoxMenu::GetProxyModel()
  105. {
  106. return &m_filterProxyModel;
  107. }
  108. const GraphCanvasComboBoxFilterProxyModel* GraphCanvasComboBoxMenu::GetProxyModel() const
  109. {
  110. return &m_filterProxyModel;
  111. }
  112. void GraphCanvasComboBoxMenu::ShowMenu()
  113. {
  114. clearFocus();
  115. m_tableView.clearFocus();
  116. m_tableView.selectionModel()->clearSelection();
  117. m_filterProxyModel.BeginModelReset();
  118. m_modelInterface->OnDropDownAboutToShow();
  119. m_filterProxyModel.EndModelReset();
  120. show();
  121. m_disableHidingStateSetter.ReleaseState();
  122. int rowHeight = m_tableView.rowHeight(0);
  123. if (rowHeight > 0)
  124. {
  125. // Generic padding of like 20 pixels.
  126. setMinimumHeight(aznumeric_cast<int>(rowHeight * 4.5 + 20));
  127. setMaximumHeight(aznumeric_cast<int>(rowHeight * 4.5 + 20));
  128. }
  129. }
  130. void GraphCanvasComboBoxMenu::HideMenu()
  131. {
  132. m_disableHidingStateSetter.ReleaseState();
  133. m_tableView.clearFocus();
  134. m_tableView.selectionModel()->clearSelection();
  135. clearFocus();
  136. reject();
  137. m_filterProxyModel.BeginModelReset();
  138. m_modelInterface->OnDropDownHidden();
  139. m_filterProxyModel.EndModelReset();
  140. }
  141. void GraphCanvasComboBoxMenu::reject()
  142. {
  143. if (!m_disableHiding.GetState())
  144. {
  145. QDialog::reject();
  146. }
  147. }
  148. bool GraphCanvasComboBoxMenu::eventFilter(QObject* object, QEvent* event)
  149. {
  150. if (object == &m_tableView)
  151. {
  152. switch (event->type())
  153. {
  154. case QEvent::FocusOut:
  155. HandleFocusOut();
  156. break;
  157. case QEvent::FocusIn:
  158. HandleFocusIn();
  159. break;
  160. }
  161. }
  162. return false;
  163. }
  164. void GraphCanvasComboBoxMenu::focusInEvent(QFocusEvent* focusEvent)
  165. {
  166. QDialog::focusInEvent(focusEvent);
  167. if (focusEvent->isAccepted())
  168. {
  169. if (!m_ignoreNextFocusIn)
  170. {
  171. HandleFocusIn();
  172. }
  173. else
  174. {
  175. m_ignoreNextFocusIn = false;
  176. }
  177. }
  178. }
  179. void GraphCanvasComboBoxMenu::focusOutEvent(QFocusEvent* focusEvent)
  180. {
  181. QDialog::focusOutEvent(focusEvent);
  182. HandleFocusOut();
  183. }
  184. void GraphCanvasComboBoxMenu::showEvent(QShowEvent* showEvent)
  185. {
  186. QDialog::showEvent(showEvent);
  187. // So, despite me telling it to not activate, the window still gets a focus in event.
  188. // But, it doesn't get a focus out event, since it doesn't actually accept the focus in event?
  189. m_ignoreNextFocusIn = true;
  190. m_tableView.selectionModel()->clearSelection();
  191. Q_EMIT VisibilityChanged(true);
  192. }
  193. void GraphCanvasComboBoxMenu::hideEvent(QHideEvent* hideEvent)
  194. {
  195. QDialog::hideEvent(hideEvent);
  196. clearFocus();
  197. Q_EMIT VisibilityChanged(false);
  198. m_tableView.selectionModel()->clearSelection();
  199. m_filterProxyModel.invalidate();
  200. }
  201. GraphCanvas::StateController<bool>* GraphCanvasComboBoxMenu::GetDisableHidingStateController()
  202. {
  203. return &m_disableHiding;
  204. }
  205. void GraphCanvasComboBoxMenu::SetSelectedIndex(QModelIndex index)
  206. {
  207. m_tableView.selectionModel()->clear();
  208. if (index.isValid()
  209. && index.row() >= 0
  210. && index.row() < m_filterProxyModel.rowCount())
  211. {
  212. QItemSelection rowSelection(m_filterProxyModel.index(index.row(), 0, index.parent()), m_filterProxyModel.index(index.row(), m_filterProxyModel.columnCount() - 1, index.parent()));
  213. m_tableView.selectionModel()->select(rowSelection, QItemSelectionModel::Select);
  214. m_tableView.scrollTo(m_filterProxyModel.index(index.row(), 0, index.parent()));
  215. }
  216. }
  217. QModelIndex GraphCanvasComboBoxMenu::GetSelectedIndex() const
  218. {
  219. if (m_tableView.selectionModel()->hasSelection())
  220. {
  221. QModelIndexList selectedIndexes = m_tableView.selectionModel()->selectedIndexes();
  222. if (!selectedIndexes.empty())
  223. {
  224. return selectedIndexes.front();
  225. }
  226. }
  227. return QModelIndex();
  228. }
  229. QModelIndex GraphCanvasComboBoxMenu::GetSelectedSourceIndex() const
  230. {
  231. if (m_tableView.selectionModel()->hasSelection())
  232. {
  233. QModelIndexList selectedIndexes = m_tableView.selectionModel()->selectedIndexes();
  234. if (!selectedIndexes.empty())
  235. {
  236. return m_filterProxyModel.mapToSource(selectedIndexes.front());
  237. }
  238. }
  239. return QModelIndex();
  240. }
  241. void GraphCanvasComboBoxMenu::OnTableClicked(const QModelIndex& modelIndex)
  242. {
  243. if (modelIndex.isValid())
  244. {
  245. QModelIndex sourceIndex = m_filterProxyModel.mapToSource(modelIndex);
  246. if (sourceIndex.isValid())
  247. {
  248. Q_EMIT OnIndexSelected(sourceIndex);
  249. QObject::disconnect(m_closeConnection);
  250. m_closeConnection = QObject::connect(&m_closeTimer, &QTimer::timeout, this, &QDialog::accept);
  251. m_closeTimer.start();
  252. }
  253. }
  254. }
  255. void GraphCanvasComboBoxMenu::HandleFocusIn()
  256. {
  257. m_disableHidingStateSetter.SetState(true);
  258. emit OnFocusIn();
  259. }
  260. void GraphCanvasComboBoxMenu::HandleFocusOut()
  261. {
  262. m_disableHidingStateSetter.ReleaseState();
  263. QObject::disconnect(m_closeConnection);
  264. m_closeConnection = QObject::connect(&m_closeTimer, &QTimer::timeout, this, &QDialog::reject);
  265. m_closeTimer.start();
  266. }
  267. ////////////////////////
  268. // GraphCanvasComboBox
  269. ////////////////////////
  270. GraphCanvasComboBox::GraphCanvasComboBox(ComboBoxItemModelInterface* modelInterface, QWidget* parent)
  271. : QLineEdit(parent)
  272. , m_lineEditInFocus(false)
  273. , m_popUpMenuInFocus(false)
  274. , m_hasFocus(false)
  275. , m_ignoreNextComplete(false)
  276. , m_recursionBlocker(false)
  277. , m_comboBoxMenu(modelInterface)
  278. , m_modelInterface(modelInterface)
  279. {
  280. setObjectName("ComboBoxLineEdit");
  281. setProperty("HasNoWindowDecorations", true);
  282. setProperty("DisableFocusWindowFix", true);
  283. QAction* action = addAction(QIcon(":/GraphCanvasEditorResources/settings_icon.png"), QLineEdit::ActionPosition::TrailingPosition);
  284. QObject::connect(action, &QAction::triggered, this, &GraphCanvasComboBox::OnOptionsClicked);
  285. QAction* escapeAction = new QAction(this);
  286. escapeAction->setShortcut(Qt::Key_Escape);
  287. addAction(escapeAction);
  288. QObject::connect(escapeAction, &QAction::triggered, this, &GraphCanvasComboBox::ResetComboBox);
  289. m_completer.setModel(m_modelInterface->GetCompleterItemModel());
  290. m_completer.setCompletionColumn(m_modelInterface->GetCompleterColumn());
  291. m_completer.setCompletionMode(QCompleter::CompletionMode::InlineCompletion);
  292. m_completer.setCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive);
  293. QObject::connect(this, &QLineEdit::textEdited, this, &GraphCanvasComboBox::OnTextChanged);
  294. QObject::connect(this, &QLineEdit::returnPressed, this, &GraphCanvasComboBox::OnReturnPressed);
  295. QObject::connect(this, &QLineEdit::editingFinished, this, &GraphCanvasComboBox::OnEditComplete);
  296. m_filterTimer.setInterval(500);
  297. QObject::connect(&m_filterTimer, &QTimer::timeout, this, &GraphCanvasComboBox::UpdateFilter);
  298. m_closeTimer.setInterval(0);
  299. QObject::connect(&m_closeTimer, &QTimer::timeout, this, &GraphCanvasComboBox::CloseMenu);
  300. m_focusTimer.setInterval(0);
  301. QObject::connect(&m_focusTimer, &QTimer::timeout, this, &GraphCanvasComboBox::HandleFocusState);
  302. QObject::connect(&m_comboBoxMenu, &GraphCanvasComboBoxMenu::OnIndexSelected, this, &GraphCanvasComboBox::UserSelectedIndex);
  303. QObject::connect(&m_comboBoxMenu, &GraphCanvasComboBoxMenu::OnFocusIn, this, &GraphCanvasComboBox::OnMenuFocusIn);
  304. QObject::connect(&m_comboBoxMenu, &GraphCanvasComboBoxMenu::OnFocusOut, this, &GraphCanvasComboBox::OnMenuFocusOut);
  305. QObject::connect(&m_comboBoxMenu, &GraphCanvasComboBoxMenu::CancelMenu, this, &GraphCanvasComboBox::ResetComboBox);
  306. m_comboBoxMenu.accept();
  307. m_disableHidingStateSetter.AddStateController(m_comboBoxMenu.GetDisableHidingStateController());
  308. }
  309. GraphCanvasComboBox::~GraphCanvasComboBox()
  310. {
  311. }
  312. void GraphCanvasComboBox::RegisterViewId(const ViewId& viewId)
  313. {
  314. m_viewId = viewId;
  315. }
  316. void GraphCanvasComboBox::SetAnchorPoint(QPoint globalPoint)
  317. {
  318. m_anchorPoint = globalPoint;
  319. UpdateMenuPosition();
  320. }
  321. void GraphCanvasComboBox::SetMenuWidth(qreal width)
  322. {
  323. m_displayWidth = width;
  324. UpdateMenuPosition();
  325. }
  326. void GraphCanvasComboBox::SetSelectedIndex(const QModelIndex& selectedIndex)
  327. {
  328. QModelIndex previousIndex = m_modelInterface->FindIndexForName(m_selectedName);
  329. if (previousIndex != selectedIndex)
  330. {
  331. m_selectedName = m_modelInterface->GetNameForIndex(selectedIndex);
  332. if (DisplayIndex(selectedIndex))
  333. {
  334. Q_EMIT SelectedIndexChanged(selectedIndex);
  335. }
  336. }
  337. }
  338. QModelIndex GraphCanvasComboBox::GetSelectedIndex() const
  339. {
  340. return m_modelInterface->FindIndexForName(m_selectedName);
  341. }
  342. void GraphCanvasComboBox::ClearOutlineColor()
  343. {
  344. setStyleSheet("");
  345. }
  346. void GraphCanvasComboBox::ResetComboBox()
  347. {
  348. HideMenu();
  349. QModelIndex selectedIndex = m_modelInterface->FindIndexForName(m_selectedName);
  350. DisplayIndex(selectedIndex);
  351. m_selectedName.clear();
  352. }
  353. void GraphCanvasComboBox::CancelInput()
  354. {
  355. QModelIndex selectedIndex = m_modelInterface->FindIndexForName(m_selectedName);
  356. DisplayIndex(selectedIndex);
  357. HideMenu();
  358. }
  359. void GraphCanvasComboBox::HideMenu()
  360. {
  361. m_disableHidingStateSetter.ReleaseState();
  362. m_comboBoxMenu.HideMenu();
  363. ViewNotificationBus::Handler::BusDisconnect(m_viewId);
  364. }
  365. bool GraphCanvasComboBox::IsMenuVisible() const
  366. {
  367. return !m_comboBoxMenu.isHidden();
  368. }
  369. void GraphCanvasComboBox::focusInEvent(QFocusEvent* focusEvent)
  370. {
  371. QLineEdit::focusInEvent(focusEvent);
  372. m_lineEditInFocus = true;
  373. m_focusTimer.start();
  374. grabKeyboard();
  375. }
  376. void GraphCanvasComboBox::focusOutEvent(QFocusEvent* focusEvent)
  377. {
  378. QLineEdit::focusOutEvent(focusEvent);
  379. m_lineEditInFocus = false;
  380. m_focusTimer.start();
  381. releaseKeyboard();
  382. }
  383. void GraphCanvasComboBox::keyPressEvent(QKeyEvent* keyEvent)
  384. {
  385. QLineEdit::keyPressEvent(keyEvent);
  386. switch(keyEvent->key())
  387. {
  388. case Qt::Key_Down:
  389. {
  390. if (m_comboBoxMenu.isHidden())
  391. {
  392. ClearFilter();
  393. DisplayMenu();
  394. }
  395. QModelIndex selectedIndex = m_comboBoxMenu.GetSelectedSourceIndex();
  396. if (!selectedIndex.isValid())
  397. {
  398. selectedIndex = m_modelInterface->FindIndexForName(m_selectedName);
  399. }
  400. QModelIndex mappedIndex;
  401. QModelIndex sourceIndex;
  402. if (selectedIndex.isValid())
  403. {
  404. sourceIndex = m_modelInterface->GetNextIndex(selectedIndex);
  405. }
  406. else
  407. {
  408. sourceIndex = m_modelInterface->GetDefaultIndex();
  409. }
  410. while (sourceIndex != selectedIndex)
  411. {
  412. mappedIndex = m_comboBoxMenu.GetProxyModel()->mapFromSource(sourceIndex);
  413. if (mappedIndex.isValid())
  414. {
  415. break;
  416. }
  417. sourceIndex = m_modelInterface->GetNextIndex(sourceIndex);
  418. }
  419. selectedIndex = sourceIndex;
  420. m_comboBoxMenu.SetSelectedIndex(mappedIndex);
  421. QString typeName = m_modelInterface->GetNameForIndex(selectedIndex);
  422. if (!typeName.isEmpty())
  423. {
  424. setText(typeName);
  425. setSelection(0, static_cast<int>(typeName.size()));
  426. m_completer.setCompletionPrefix(typeName);
  427. }
  428. keyEvent->accept();
  429. }
  430. break;
  431. case Qt::Key_Up:
  432. {
  433. if (m_comboBoxMenu.isHidden())
  434. {
  435. m_comboBoxMenu.GetProxyModel()->SetFilter("");
  436. DisplayMenu();
  437. }
  438. QModelIndex selectedIndex = m_comboBoxMenu.GetSelectedSourceIndex();
  439. if (!selectedIndex.isValid())
  440. {
  441. selectedIndex = m_modelInterface->FindIndexForName(m_selectedName);
  442. }
  443. QModelIndex mappedIndex;
  444. QModelIndex sourceIndex;
  445. if (selectedIndex.isValid())
  446. {
  447. sourceIndex = m_modelInterface->GetPreviousIndex(selectedIndex);
  448. }
  449. else
  450. {
  451. sourceIndex = m_modelInterface->GetPreviousIndex(m_modelInterface->GetDefaultIndex());
  452. }
  453. while (sourceIndex != selectedIndex)
  454. {
  455. mappedIndex = m_comboBoxMenu.GetProxyModel()->mapFromSource(sourceIndex);
  456. if (mappedIndex.isValid())
  457. {
  458. break;
  459. }
  460. sourceIndex = m_modelInterface->GetPreviousIndex(sourceIndex);
  461. }
  462. selectedIndex = sourceIndex;
  463. m_comboBoxMenu.SetSelectedIndex(mappedIndex);
  464. QString typeName = m_comboBoxMenu.GetInterface()->GetNameForIndex(sourceIndex);
  465. if (!typeName.isEmpty())
  466. {
  467. setText(typeName);
  468. setSelection(0, static_cast<int>(typeName.size()));
  469. m_completer.setCompletionPrefix(typeName);
  470. }
  471. keyEvent->accept();
  472. }
  473. break;
  474. case Qt::Key_Escape:
  475. ResetComboBox();
  476. keyEvent->accept();
  477. break;
  478. default:
  479. break;
  480. }
  481. }
  482. void GraphCanvasComboBox::OnViewScrolled()
  483. {
  484. ResetComboBox();
  485. }
  486. void GraphCanvasComboBox::OnViewCenteredOnArea()
  487. {
  488. ResetComboBox();
  489. }
  490. void GraphCanvasComboBox::OnZoomChanged(qreal /*zoomLevel*/)
  491. {
  492. }
  493. void GraphCanvasComboBox::UserSelectedIndex(const QModelIndex& selectedIndex)
  494. {
  495. QModelIndex previousIndex = m_modelInterface->FindIndexForName(m_selectedName);
  496. if (previousIndex != selectedIndex)
  497. {
  498. SetSelectedIndex(selectedIndex);
  499. Q_EMIT OnUserActionComplete();
  500. }
  501. }
  502. void GraphCanvasComboBox::OnTextChanged()
  503. {
  504. DisplayMenu();
  505. UpdateFilter();
  506. }
  507. void GraphCanvasComboBox::OnOptionsClicked()
  508. {
  509. if (m_comboBoxMenu.isHidden())
  510. {
  511. m_comboBoxMenu.GetProxyModel()->SetFilter("");
  512. DisplayMenu();
  513. }
  514. else
  515. {
  516. m_comboBoxMenu.accept();
  517. }
  518. }
  519. void GraphCanvasComboBox::OnReturnPressed()
  520. {
  521. const bool allowReset = false;
  522. if (SubmitData(allowReset))
  523. {
  524. m_comboBoxMenu.accept();
  525. }
  526. else
  527. {
  528. setText("");
  529. UpdateFilter();
  530. }
  531. Q_EMIT OnUserActionComplete();
  532. // When we press enter, we will also get an editing complete signal. We want to ignore that since we handled it here.
  533. m_ignoreNextComplete = true;
  534. }
  535. void GraphCanvasComboBox::OnEditComplete()
  536. {
  537. if (m_ignoreNextComplete)
  538. {
  539. m_ignoreNextComplete = false;
  540. return;
  541. }
  542. SubmitData(false);
  543. m_closeState = CloseMenuState::Reject;
  544. m_closeTimer.start();
  545. }
  546. void GraphCanvasComboBox::ClearFilter()
  547. {
  548. m_comboBoxMenu.GetProxyModel()->SetFilter("");
  549. }
  550. void GraphCanvasComboBox::UpdateFilter()
  551. {
  552. m_comboBoxMenu.GetProxyModel()->SetFilter(GetUserInputText());
  553. }
  554. void GraphCanvasComboBox::CloseMenu()
  555. {
  556. switch (m_closeState)
  557. {
  558. case CloseMenuState::Accept:
  559. m_comboBoxMenu.accept();
  560. break;
  561. default:
  562. m_comboBoxMenu.reject();
  563. break;
  564. }
  565. m_closeState = CloseMenuState::Reject;
  566. }
  567. void GraphCanvasComboBox::OnMenuFocusIn()
  568. {
  569. m_popUpMenuInFocus = true;
  570. m_focusTimer.start();
  571. }
  572. void GraphCanvasComboBox::OnMenuFocusOut()
  573. {
  574. m_popUpMenuInFocus = false;
  575. m_focusTimer.start();
  576. }
  577. void GraphCanvasComboBox::HandleFocusState()
  578. {
  579. bool focusState = m_lineEditInFocus || m_popUpMenuInFocus;
  580. if (focusState != m_hasFocus)
  581. {
  582. m_hasFocus = focusState;
  583. if (m_hasFocus)
  584. {
  585. Q_EMIT OnFocusIn();
  586. }
  587. else
  588. {
  589. Q_EMIT OnFocusOut();
  590. HideMenu();
  591. }
  592. }
  593. }
  594. bool GraphCanvasComboBox::DisplayIndex(const QModelIndex& index)
  595. {
  596. QSignalBlocker signalBlocker(this);
  597. QString name = m_modelInterface->GetNameForIndex(index);
  598. if (!name.isEmpty())
  599. {
  600. m_completer.setCompletionPrefix(name);
  601. setText(name);
  602. ClearFilter();
  603. }
  604. else
  605. {
  606. if (!m_selectedName.isEmpty())
  607. {
  608. QModelIndex currentIndex = m_modelInterface->FindIndexForName(m_selectedName);
  609. if (currentIndex != index)
  610. {
  611. DisplayIndex(currentIndex);
  612. }
  613. }
  614. else
  615. {
  616. m_completer.setCompletionPrefix("");
  617. setText("");
  618. UpdateFilter();
  619. }
  620. }
  621. return !m_selectedName.isEmpty();
  622. }
  623. bool GraphCanvasComboBox::SubmitData(bool allowReset)
  624. {
  625. QString inputName = text();
  626. QModelIndex inputIndex = m_modelInterface->FindIndexForName(inputName);
  627. // We didn't input a valid type. So default to our last previously known value.
  628. if (!inputIndex.isValid())
  629. {
  630. if (allowReset)
  631. {
  632. QModelIndex lastIndex = m_modelInterface->FindIndexForName(m_selectedName);
  633. DisplayIndex(lastIndex);
  634. inputIndex = lastIndex;
  635. }
  636. }
  637. else
  638. {
  639. SetSelectedIndex(inputIndex);
  640. }
  641. return inputIndex.isValid();
  642. }
  643. void GraphCanvasComboBox::DisplayMenu()
  644. {
  645. if (!m_recursionBlocker)
  646. {
  647. QScopedValueRollback<bool> valueRollback(m_recursionBlocker, true);
  648. if (m_comboBoxMenu.isHidden())
  649. {
  650. Q_EMIT OnMenuAboutToDisplay();
  651. ViewNotificationBus::Handler::BusConnect(m_viewId);
  652. qreal zoomLevel = 1.0;
  653. ViewRequestBus::EventResult(zoomLevel, m_viewId, &ViewRequests::GetZoomLevel);
  654. if (zoomLevel < 1.0)
  655. {
  656. zoomLevel = 1.0;
  657. }
  658. m_comboBoxMenu.GetInterface()->SetFontScale(zoomLevel);
  659. m_comboBoxMenu.ShowMenu();
  660. UpdateMenuPosition();
  661. }
  662. }
  663. if (!m_disableHidingStateSetter.HasState())
  664. {
  665. m_disableHidingStateSetter.SetState(true);
  666. }
  667. }
  668. QString GraphCanvasComboBox::GetUserInputText()
  669. {
  670. QString lineEditText = text();
  671. // The QCompleter doesn't seem to update the completion prefix when you delete anything, only when things are added.
  672. // To get it to update correctly when the user deletes something, I'm using the combination of things:
  673. //
  674. // 1) If we have a completion, that text will be auto filled into the quick filter because of the completion model.
  675. // So, we will compare those two values, and if they match, we know we want to search using the completion prefix.
  676. //
  677. // 2) If they don't match, it means that user deleted something, and the Completer didn't update it's internal state, so we'll just
  678. // use whatever is in the text box.
  679. //
  680. // 3) When the text field is set to empty, the current completion gets invalidated, but the prefix doesn't, so that gets special cased out.
  681. //
  682. // Extra fun: If you type in something, "Like" then delete a middle character, "Lie", and then put the k back in. It will auto complete the E
  683. // visually but the completion prefix will be the entire word.
  684. if (completer()
  685. && completer()->currentCompletion().compare(lineEditText, Qt::CaseInsensitive) == 0
  686. && !lineEditText.isEmpty())
  687. {
  688. lineEditText = completer()->completionPrefix();
  689. }
  690. return lineEditText;
  691. }
  692. void GraphCanvasComboBox::UpdateMenuPosition()
  693. {
  694. if (!m_comboBoxMenu.isHidden())
  695. {
  696. QRect dialogGeometry = m_comboBoxMenu.geometry();
  697. dialogGeometry.moveTopLeft(m_anchorPoint);
  698. dialogGeometry.setWidth(aznumeric_cast<int>(m_displayWidth));
  699. m_comboBoxMenu.setGeometry(dialogGeometry);
  700. }
  701. }
  702. }
  703. #include <Source/Widgets/moc_GraphCanvasComboBox.cpp>