ConsoleSCB.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef CRYINCLUDE_EDITOR_CONTROLS_CONSOLESCB_H
  9. #define CRYINCLUDE_EDITOR_CONTROLS_CONSOLESCB_H
  10. #pragma once
  11. #if !defined(Q_MOC_RUN)
  12. #include "Settings.h"
  13. #include <AzToolsFramework/Editor/EditorSettingsAPIBus.h>
  14. #include <QLineEdit>
  15. #include <QPlainTextEdit>
  16. #include <QAbstractTableModel>
  17. #include <QDialog>
  18. #include <QLineEdit>
  19. #include <QPlainTextEdit>
  20. #include <QPushButton>
  21. #include <QScopedPointer>
  22. #include <QStyledItemDelegate>
  23. #endif
  24. class QMenu;
  25. class ConsoleWidget;
  26. class QFocusEvent;
  27. class QTableView;
  28. class CVarBlock;
  29. namespace Ui {
  30. class Console;
  31. }
  32. struct ConsoleLine
  33. {
  34. QString text;
  35. bool newLine;
  36. };
  37. typedef std::deque<ConsoleLine> Lines;
  38. class ConsoleLineEdit
  39. : public QLineEdit
  40. {
  41. Q_OBJECT
  42. public:
  43. explicit ConsoleLineEdit(QWidget* parent = nullptr);
  44. protected:
  45. void mouseDoubleClickEvent(QMouseEvent* ev) override;
  46. void keyPressEvent(QKeyEvent* ev) override;
  47. bool event(QEvent* ev) override;
  48. signals:
  49. void variableEditorRequested();
  50. private:
  51. void DisplayHistory(bool bForward);
  52. void ResetHistoryIndex();
  53. QStringList m_history;
  54. unsigned int m_historyIndex;
  55. bool m_bReusedHistory;
  56. };
  57. class ConsoleVariableItemDelegate
  58. : public QStyledItemDelegate
  59. {
  60. Q_OBJECT
  61. public:
  62. explicit ConsoleVariableItemDelegate(QObject* parent = nullptr);
  63. // Item delegate overrides for creating the custom editor widget and
  64. // setting/retrieving data to/from it
  65. void setEditorData(QWidget* editor, const QModelIndex& index) const override;
  66. void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
  67. QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
  68. void SetVarBlock(CVarBlock* varBlock);
  69. private:
  70. CVarBlock* m_varBlock;
  71. };
  72. class ConsoleVariableModel
  73. : public QAbstractTableModel
  74. {
  75. Q_OBJECT
  76. public:
  77. enum CustomRoles
  78. {
  79. VariableCustomRole = Qt::UserRole
  80. };
  81. explicit ConsoleVariableModel(QObject* parent = nullptr);
  82. // Table model overrides
  83. QVariant data(const QModelIndex& index, int role) const override;
  84. bool setData(const QModelIndex& index, const QVariant& value, int role) override;
  85. int rowCount(const QModelIndex& = {}) const override;
  86. int columnCount(const QModelIndex& = {}) const override;
  87. Qt::ItemFlags flags(const QModelIndex& index) const override;
  88. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
  89. void SetVarBlock(CVarBlock* varBlock);
  90. void ClearModifiedRows();
  91. private:
  92. CVarBlock* m_varBlock;
  93. QList<int> m_modifiedRows;
  94. };
  95. class ConsoleVariableEditor
  96. : public QWidget
  97. {
  98. Q_OBJECT
  99. public:
  100. explicit ConsoleVariableEditor(QWidget* parent = nullptr);
  101. static void RegisterViewClass();
  102. void HandleVariableRowUpdated(ICVar* pCVar);
  103. protected:
  104. void showEvent(QShowEvent* event) override;
  105. private:
  106. void SetVarBlock(CVarBlock* varBlock);
  107. QTableView* m_tableView;
  108. ConsoleVariableModel* m_model;
  109. ConsoleVariableItemDelegate* m_itemDelegate;
  110. CVarBlock* m_varBlock;
  111. static AZ::ConsoleCommandInvokedEvent::Handler m_commandInvokedHandler;
  112. };
  113. class CConsoleSCB
  114. : public QWidget
  115. , private AzToolsFramework::EditorPreferencesNotificationBus::Handler
  116. , public IEditorNotifyListener
  117. {
  118. Q_OBJECT
  119. public:
  120. explicit CConsoleSCB(QWidget* parent = nullptr);
  121. ~CConsoleSCB();
  122. static void RegisterViewClass();
  123. void SetInputFocus();
  124. void AddToConsole(const QString& text, bool bNewLine);
  125. void FlushText();
  126. QSize sizeHint() const override;
  127. QSize minimumSizeHint() const override;
  128. static CConsoleSCB* GetCreatedInstance();
  129. static void AddToPendingLines(const QString& text, bool bNewLine); // call this function instead of AddToConsole() until an instance of CConsoleSCB exists to prevent messages from getting lost
  130. // EditorPreferencesNotificationBus...
  131. void OnEditorPreferencesChanged() override;
  132. void RefreshStyle();
  133. private Q_SLOTS:
  134. void showVariableEditor();
  135. void toggleConsoleSearch();
  136. void findPrevious();
  137. void findNext();
  138. void toggleClearOnPlay();
  139. private:
  140. void OnEditorNotifyEvent(EEditorNotifyEvent event) override;
  141. void SetupOptionsMenu();
  142. void UpdateOptionsMenu();
  143. QScopedPointer<Ui::Console> ui;
  144. Lines m_lines;
  145. static Lines s_pendingLines;
  146. QList<QColor> m_colorTable;
  147. AzToolsFramework::ConsoleColorTheme m_backgroundTheme;
  148. class SearchHighlighter;
  149. SearchHighlighter* m_highlighter;
  150. QMenu* m_optionsMenu;
  151. QAction* m_clearOnPlayAction;
  152. };
  153. #endif // CRYINCLUDE_EDITOR_CONTROLS_CONSOLESCB_H