3
0

CommandLine.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <QObject>
  11. #include <QDialog>
  12. #include <QLineEdit>
  13. #include <QTableView>
  14. #include <QSortFilterProxyModel>
  15. #include <QAbstractTableModel>
  16. #include <AzCore/EBus/EBus.h>
  17. #include <AzCore/std/smart_ptr/unique_ptr.h>
  18. #include <AzCore/Memory/Memory.h>
  19. #include <AzCore/Memory/SystemAllocator.h>
  20. #include <AzCore/std/containers/vector.h>
  21. #include <AzCore/std/string/string.h>
  22. #include <AzCore/Console/Console.h>
  23. #endif
  24. namespace Ui
  25. {
  26. class CommandLine;
  27. }
  28. namespace ScriptCanvasEditor
  29. {
  30. namespace Widget
  31. {
  32. class Command
  33. {
  34. public:
  35. using Functor = AZStd::function<void(AZStd::vector<AZStd::string>)>;
  36. Command(const AZStd::string& name, const AZStd::string& description, Functor functor)
  37. : m_name(name)
  38. , m_description(description)
  39. , m_functor(functor)
  40. {}
  41. void operator()(const AZStd::vector<AZStd::string>& args)
  42. {
  43. m_functor(args);
  44. }
  45. const AZStd::string& GetName() const { return m_name; }
  46. const AZStd::string& GetDescription() const { return m_description; }
  47. private:
  48. AZStd::string m_name;
  49. AZStd::string m_description;
  50. Functor m_functor;
  51. };
  52. using CommandRegistry = AZStd::unordered_map<AZStd::string, AZStd::unique_ptr<Command>>;
  53. struct ScriptCanvasCommandLineRequests : public AZ::EBusTraits
  54. {
  55. virtual void AddCommand(const AZStd::string commandName, const AZStd::string description, Command::Functor) = 0;
  56. virtual void Invoke(const char* commandName) = 0;
  57. virtual void InvokeWithArguments(const char* commandName, const AZStd::vector<AZStd::string>&) = 0;
  58. using CommandNameList = AZStd::list<AZStd::pair<AZStd::string, AZStd::string>>;
  59. virtual CommandNameList GetCommands() = 0;
  60. };
  61. using ScriptCanvasCommandLineRequestBus = AZ::EBus<ScriptCanvasCommandLineRequests>;
  62. // TODO #lsempe: this deserves its own file
  63. // CommandListDataModel
  64. /////////////////////////////////////////////////////////////////////////////////////////////
  65. class CommandListDataModel : public QAbstractTableModel
  66. , ScriptCanvasCommandLineRequestBus::Handler
  67. {
  68. Q_OBJECT
  69. public:
  70. AZ_CLASS_ALLOCATOR(CommandListDataModel, AZ::SystemAllocator);
  71. enum ColumnIndex
  72. {
  73. CommandIndex,
  74. DescriptionIndex,
  75. TrailIndex,
  76. Count
  77. };
  78. enum CustomRole
  79. {
  80. Node = Qt::UserRole,
  81. Types,
  82. EBusSender,
  83. EBusHandler,
  84. Commands
  85. };
  86. CommandListDataModel(QWidget* parent = nullptr);
  87. ~CommandListDataModel() override;
  88. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
  89. QModelIndex parent(const QModelIndex &child) const override;
  90. int rowCount(const QModelIndex &parent = QModelIndex()) const override;
  91. int columnCount(const QModelIndex &parent = QModelIndex()) const override;
  92. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
  93. Qt::ItemFlags flags(const QModelIndex &index) const override;
  94. bool HasMatches(const AZStd::string& input);
  95. struct Entry
  96. {
  97. AZ::Uuid m_type;
  98. AZStd::string m_command;
  99. Entry()
  100. {
  101. m_type = AZ::Uuid::CreateNull();
  102. }
  103. };
  104. protected:
  105. AZStd::vector<Entry> m_entries;
  106. static CommandRegistry m_commands;
  107. void AddCommand(const AZStd::string commandName, const AZStd::string description, Command::Functor f) override
  108. {
  109. if (m_commands.find(commandName) == m_commands.end())
  110. {
  111. m_commands[commandName] = AZStd::make_unique<Command>(commandName, description, f);
  112. Entry entry;
  113. entry.m_command = commandName;
  114. entry.m_type = AZ::Uuid::CreateNull();
  115. m_entries.emplace_back(entry);
  116. }
  117. }
  118. void Invoke(const char* commandName) override
  119. {
  120. auto command = m_commands.find(commandName);
  121. if (command != m_commands.end())
  122. {
  123. command->second->operator()({});
  124. }
  125. }
  126. void InvokeWithArguments(const char* commandName, const AZStd::vector<AZStd::string>& args) override
  127. {
  128. auto command = m_commands.find(commandName);
  129. if (command != m_commands.end())
  130. {
  131. command->second->operator()(args);
  132. }
  133. }
  134. ScriptCanvasCommandLineRequests::CommandNameList GetCommands() override
  135. {
  136. ScriptCanvasCommandLineRequests::CommandNameList commands;
  137. for (auto& command : m_commands)
  138. {
  139. commands.push_back(AZStd::make_pair(command.second->GetName(), command.second->GetDescription()));
  140. }
  141. return commands;
  142. }
  143. };
  144. class CommandListDataProxyModel : public QSortFilterProxyModel
  145. {
  146. Q_OBJECT
  147. public:
  148. AZ_CLASS_ALLOCATOR(CommandListDataProxyModel, AZ::SystemAllocator);
  149. CommandListDataProxyModel(CommandListDataModel* commandListData, QObject* parent = nullptr);
  150. bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
  151. void SetInput(const AZStd::string& input)
  152. {
  153. m_input = input;
  154. invalidate();
  155. }
  156. protected:
  157. QCompleter* m_completer;
  158. AZStd::string m_input;
  159. };
  160. // CommandLineEdit
  161. /////////////////////////////////////////////////////////////////////////////////////////////
  162. class CommandLineEdit : public QLineEdit
  163. {
  164. Q_OBJECT
  165. public:
  166. CommandLineEdit(QWidget* parent = nullptr);
  167. void ResetState();
  168. Q_SIGNALS:
  169. void onFocusChange(bool focused);
  170. void onKeyReleased(QKeyEvent*);
  171. protected:
  172. void focusInEvent(QFocusEvent*) override;
  173. void focusOutEvent(QFocusEvent*) override;
  174. void keyPressEvent(QKeyEvent *) override;
  175. void keyReleaseEvent(QKeyEvent*) override;
  176. void onTextChanged(const QString&);
  177. void onTextEdited(const QString&);
  178. void onReturnPressed();
  179. bool m_empty;
  180. const QString m_defaultText;
  181. };
  182. // CommandLineList
  183. /////////////////////////////////////////////////////////////////////////////////////////////
  184. class CommandLineList : public QTableView
  185. {
  186. Q_OBJECT
  187. public:
  188. CommandLineList(QWidget* parent = nullptr);
  189. Q_SIGNALS:
  190. void onKeyReleased(QKeyEvent*);
  191. protected:
  192. void keyReleaseEvent(QKeyEvent* event) override
  193. {
  194. Q_EMIT(onKeyReleased(event));
  195. }
  196. };
  197. // CommandLine
  198. /////////////////////////////////////////////////////////////////////////////////////////////
  199. class CommandLine
  200. : public QWidget
  201. {
  202. Q_OBJECT
  203. public:
  204. CommandLine(QWidget* object = nullptr);
  205. void showEvent(QShowEvent *event) override;
  206. void onTextChanged(const QString&);
  207. void onEditKeyReleaseEvent(QKeyEvent*);
  208. void onListKeyReleaseEvent(QKeyEvent*);
  209. AZStd::unique_ptr<Ui::CommandLine> ui;
  210. };
  211. }
  212. }