GraphCanvasComboBox.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QCompleter>
  11. #include <QDialog>
  12. #include <QLineEdit>
  13. #include <QMenu>
  14. #include <QSortFilterProxyModel>
  15. #include <QTableView>
  16. #include <QTimer>
  17. #include <QWidget>
  18. #include <AzCore/Memory/SystemAllocator.h>
  19. #include <AzCore/PlatformDef.h>
  20. #include <GraphCanvas/Components/ViewBus.h>
  21. #include <GraphCanvas/Editor/EditorTypes.h>
  22. #include <GraphCanvas/Utils/StateControllers/StackStateController.h>
  23. #include <GraphCanvas/Widgets/ComboBox/ComboBoxItemModelInterface.h>
  24. #endif
  25. namespace GraphCanvas
  26. {
  27. class GraphCanvasComboBoxFilterProxyModel
  28. : public QSortFilterProxyModel
  29. {
  30. Q_OBJECT
  31. public:
  32. AZ_CLASS_ALLOCATOR(GraphCanvasComboBoxFilterProxyModel, AZ::SystemAllocator);
  33. GraphCanvasComboBoxFilterProxyModel(QObject* parent = nullptr);
  34. bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
  35. void SetFilter(const QString& filter);
  36. void BeginModelReset()
  37. {
  38. beginResetModel();
  39. }
  40. void EndModelReset()
  41. {
  42. endResetModel();
  43. invalidate();
  44. }
  45. private:
  46. QString m_filter;
  47. QRegExp m_testRegex;
  48. };
  49. class GraphCanvasComboBoxMenu
  50. : public QDialog
  51. {
  52. Q_OBJECT
  53. public:
  54. GraphCanvasComboBoxMenu(ComboBoxItemModelInterface* model, QWidget* parent = nullptr);
  55. ~GraphCanvasComboBoxMenu() override;
  56. ComboBoxItemModelInterface* GetInterface();
  57. const ComboBoxItemModelInterface* GetInterface() const;
  58. GraphCanvasComboBoxFilterProxyModel* GetProxyModel();
  59. const GraphCanvasComboBoxFilterProxyModel* GetProxyModel() const;
  60. void ShowMenu();
  61. void HideMenu();
  62. void reject() override;
  63. bool eventFilter(QObject* object, QEvent* event) override;
  64. void focusInEvent(QFocusEvent* focusEvent) override;
  65. void focusOutEvent(QFocusEvent* focusEvent) override;
  66. void showEvent(QShowEvent* showEvent) override;
  67. void hideEvent(QHideEvent* hideEvent) override;
  68. GraphCanvas::StateController<bool>* GetDisableHidingStateController();
  69. void SetSelectedIndex(QModelIndex index);
  70. QModelIndex GetSelectedIndex() const;
  71. QModelIndex GetSelectedSourceIndex() const;
  72. void OnTableClicked(const QModelIndex& modelIndex);
  73. Q_SIGNALS:
  74. void OnIndexSelected(QModelIndex index);
  75. void VisibilityChanged(bool visibility);
  76. void CancelMenu();
  77. void OnFocusIn();
  78. void OnFocusOut();
  79. private:
  80. void HandleFocusIn();
  81. void HandleFocusOut();
  82. QTimer m_closeTimer;
  83. QMetaObject::Connection m_closeConnection;
  84. QTableView m_tableView;
  85. ComboBoxItemModelInterface* m_modelInterface;
  86. GraphCanvasComboBoxFilterProxyModel m_filterProxyModel;
  87. GraphCanvas::StateSetter<bool> m_disableHidingStateSetter;
  88. GraphCanvas::StackStateController<bool> m_disableHiding;
  89. bool m_ignoreNextFocusIn;
  90. };
  91. class GraphCanvasComboBox
  92. : public QLineEdit
  93. , public ViewNotificationBus::Handler
  94. {
  95. Q_OBJECT
  96. public:
  97. AZ_CLASS_ALLOCATOR(GraphCanvasComboBox, AZ::SystemAllocator);
  98. GraphCanvasComboBox(ComboBoxItemModelInterface* comboBoxModel, QWidget* parent = nullptr);
  99. ~GraphCanvasComboBox();
  100. void RegisterViewId(const ViewId& viewId);
  101. void SetAnchorPoint(QPoint globalPoint);
  102. void SetMenuWidth(qreal width);
  103. void SetSelectedIndex(const QModelIndex& sourceIndex);
  104. QModelIndex GetSelectedIndex() const;
  105. // Weird issue where the background color gets dropped when provide the override. Going to pass it in for now.
  106. void ClearOutlineColor();
  107. void ResetComboBox();
  108. void CancelInput();
  109. void HideMenu();
  110. bool IsMenuVisible() const;
  111. // QLineEdit
  112. void focusInEvent(QFocusEvent* focusEvent) override;
  113. void focusOutEvent(QFocusEvent* focusEvent) override;
  114. void keyPressEvent(QKeyEvent* keyEvent) override;
  115. ////
  116. // ViewNotificationBus
  117. void OnViewScrolled() override;
  118. void OnViewCenteredOnArea() override;
  119. void OnZoomChanged(qreal zoomLevel) override;
  120. ////
  121. Q_SIGNALS:
  122. void SelectedIndexChanged(const QModelIndex& index);
  123. void OnUserActionComplete();
  124. void OnMenuAboutToDisplay();
  125. void OnFocusIn();
  126. void OnFocusOut();
  127. protected:
  128. void UserSelectedIndex(const QModelIndex& proxyIndex);
  129. void OnTextChanged();
  130. void OnOptionsClicked();
  131. void OnReturnPressed();
  132. void OnEditComplete();
  133. void ClearFilter();
  134. void UpdateFilter();
  135. void CloseMenu();
  136. void OnMenuFocusIn();
  137. void OnMenuFocusOut();
  138. void HandleFocusState();
  139. private:
  140. enum class CloseMenuState
  141. {
  142. Reject,
  143. Accept
  144. };
  145. bool DisplayIndex(const QModelIndex& index);
  146. bool SubmitData(bool allowRest = true);
  147. void DisplayMenu();
  148. QString GetUserInputText();
  149. void UpdateMenuPosition();
  150. QPoint m_anchorPoint;
  151. qreal m_displayWidth;
  152. QTimer m_focusTimer;
  153. QTimer m_filterTimer;
  154. QTimer m_closeTimer;
  155. CloseMenuState m_closeState;
  156. ViewId m_viewId;
  157. QString m_selectedName;
  158. QCompleter m_completer;
  159. GraphCanvasComboBoxMenu m_comboBoxMenu;
  160. ComboBoxItemModelInterface* m_modelInterface;
  161. GraphCanvas::StateSetter<bool> m_disableHidingStateSetter;
  162. bool m_lineEditInFocus;
  163. bool m_popUpMenuInFocus;
  164. bool m_hasFocus;
  165. bool m_ignoreNextComplete;
  166. bool m_recursionBlocker;
  167. };
  168. }