File: pqPythonSyntaxHighlighter.h

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (100 lines) | stat: -rw-r--r-- 2,786 bytes parent folder | download
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