1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
// SPDX-FileCopyrightText: Copyright (c) Kitware Inc.
// SPDX-FileCopyrightText: Copyright (c) Sandia Corporation
// SPDX-License-Identifier: BSD-3-Clause
#ifndef pqPythonSyntaxHighlighter_h
#define pqPythonSyntaxHighlighter_h
#undef slots
#include "vtkPython.h"
#define slots Q_SLOTS
#include "pqPythonModule.h"
#include <QObject>
#include "vtkPythonCompatibility.h"
#include "vtkPythonInterpreter.h"
#include "vtkSmartPyObject.h"
class QTextEdit;
class QUndoStack;
struct pqPythonTextHistoryEntry;
/**
* This class is a helper object to attach to a QTextEdit to add Python
* syntax highlighting to the text that is displayed. The pygments python
* module is used to generate syntax highlighting. Since mixing tabs and
* spaces is an error for python's indentation, tabs are highlighted in red.
*
* The QTextEdit is set up so that it uses a fixed-width font and tabs are
* the width of 4 spaces by the constructor.
*
* This will also optionally
* capture presses of the tab key that would go to the QTextEdit and
* insert 4 spaces instead of the tab. This option is enabled by default.
*/
class PQPYTHON_EXPORT pqPythonSyntaxHighlighter : public QObject
{
Q_OBJECT
public:
typedef QObject Superclass;
/**
* Creates a pqPythonSyntaxHighlighter on the given QTextEdit.
* The highlighter is not directly connected to the QTextEdit object (see \ref ConnectHighligter).
* NOTE: the optional tab key capture is enabled by the constructor
*/
explicit pqPythonSyntaxHighlighter(QObject* p, QTextEdit& textEdit);
~pqPythonSyntaxHighlighter() override = default;
/**
* Returns true if the tab key is being intercepted to insert spaces in
* the text edit
*/
bool isReplacingTabs() const;
/**
* Used to enable/disable tab key capture
* Passing true will cause the tab key to insert 4 spaces in the QTextEdit
*/
void setReplaceTabs(bool replaceTabs);
/**
* Returns the highlighted input text
*/
QString Highlight(const QString& src) const;
/**
* Connects the highlighter to text.
* This is not automatically called by the constructor
* as we might want to defer the connection to another widget.
*/
void ConnectHighligter() const;
protected:
/**
* This event filter is applied to the TextEdit to translate presses of the
* Tab key into 4 spaces being inserted
*/
bool eventFilter(QObject*, QEvent*) override;
private:
QTextEdit& TextEdit;
vtkSmartPyObject PygmentsModule;
vtkSmartPyObject HighlightFunction;
vtkSmartPyObject PythonLexer;
vtkSmartPyObject HtmlFormatter;
/**
* If true, replaces the tabs with 4 spaces
* whenever there is a new input inside the \ref TextEdit
*/
bool ReplaceTabs = true;
Q_DISABLE_COPY(pqPythonSyntaxHighlighter);
};
#endif
|