Overte C++ Documentation
JSConsole.h
1 //
2 // JSConsole.h
3 // interface/src/ui
4 //
5 // Created by Ryan Huffman on 05/12/14.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_JSConsole_h
15 #define hifi_JSConsole_h
16 
17 #include <memory>
18 
19 #include <QFutureWatcher>
20 #include <QObject>
21 #include <QCompleter>
22 #include <QtCore/QJsonArray>
23 #include <ScriptValue.h>
24 
25 #include "ui_console.h"
26 
27 class QStandardItemModel;
28 class ScriptManager;
29 using ScriptManagerPointer = std::shared_ptr<ScriptManager>;
30 
31 const QString CONSOLE_TITLE = "Scripting Console";
32 const float CONSOLE_WINDOW_OPACITY = 0.95f;
33 const int CONSOLE_WIDTH = 800;
34 const int CONSOLE_HEIGHT = 200;
35 
36 class JSConsole : public QWidget {
37  Q_OBJECT
38 public:
39  JSConsole(QWidget* parent, const ScriptManagerPointer& scriptManager = ScriptManagerPointer());
40  ~JSConsole();
41 
42  void setScriptManager(const ScriptManagerPointer& scriptManager = ScriptManagerPointer());
43  void clear();
44 
45 public slots:
46  void executeCommand(const QString& command);
47 
48 protected:
49  void setAndSelectCommand(const QString& command);
50  virtual bool eventFilter(QObject* sender, QEvent* event) override;
51  virtual void mouseReleaseEvent(QMouseEvent* event) override;
52  virtual void showEvent(QShowEvent* event) override;
53 
54 protected slots:
55  void scrollToBottom();
56  void resizeTextInput();
57  void handlePrint(const QString& message, const QString& scriptName);
58  void handleInfo(const QString& message, const QString& scriptName);
59  void handleWarning(const QString& message, const QString& scriptName);
60  void handleError(const QString& message, const QString& scriptName);
61  void commandFinished();
62 
63 private slots:
64  void insertCompletion(const QModelIndex& completion);
65  void highlightedCompletion(const QModelIndex& completion);
66 
67 private:
68  void appendMessage(const QString& gutter, const QString& message);
69  void setToNextCommandInHistory();
70  void setToPreviousCommandInHistory();
71  void resetCurrentCommandHistory();
72 
73  void readAPI();
74 
75  QStandardItemModel* getAutoCompleteModel(const QString& memberOf = nullptr);
76 
77  QFutureWatcher<QVariant> _executeWatcher;
78  Ui::Console* _ui;
79  int _currentCommandInHistory;
80  QString _savedHistoryFilename;
81  QList<QString> _commandHistory;
82  QString _rootCommand;
83  ScriptManagerPointer _scriptManager;
84  static const QString _consoleFileName;
85  QJsonArray _apiDocs;
86  QCompleter* _completer;
87  QString _completerModule {""};
88 };
89 
90 
91 #endif // hifi_JSConsole_h
Manages a single scripting engine.
Definition: ScriptManager.h:281