diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-01 16:45:55 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-04-07 11:00:57 +0200 |
commit | 5e5937b83c04439fcfb8af856c60080d4d729ae2 (patch) | |
tree | 5605a58e6dfb2f5dd9aeafe314e0de332213c3a8 /examples/widgets | |
parent | fbbae556c9bf060be366fd578986cae85ff8caec (diff) |
Brush up the syntax highlighter example
Change it to a simple highlighter for Python instead of the outdated
qmake syntax.
Remove the resource file.
Task-number: PYSIDE-1112
Change-Id: I49157eccd8dbd26fca85910afb404648d7cc97a3
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'examples/widgets')
4 files changed, 19 insertions, 175 deletions
diff --git a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.py b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.py index f57ba4851..97373773e 100644 --- a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.py +++ b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.py @@ -2,7 +2,7 @@ ############################################################################ ## ## Copyright (C) 2013 Riverbank Computing Limited. -## Copyright (C) 2020 The Qt Company Ltd. +## Copyright (C) 2021 The Qt Company Ltd. ## Contact: http://www.qt.io/licensing/ ## ## This file is part of the Qt for Python examples of the Qt Toolkit. @@ -42,16 +42,16 @@ """PySide6 port of the widgets/richtext/syntaxhighlighter example from Qt v5.x""" +import os +from pathlib import Path import sys import re from PySide6.QtCore import (QFile, Qt, QTextStream) -from PySide6.QtGui import (QColor, QFont, QKeySequence, QSyntaxHighlighter, - QTextCharFormat) +from PySide6.QtGui import (QColor, QFont, QFontDatabase, QKeySequence, + QSyntaxHighlighter, QTextCharFormat) from PySide6.QtWidgets import (QApplication, QFileDialog, QMainWindow, QPlainTextEdit) -import syntaxhighlighter_rc - class MainWindow(QMainWindow): def __init__(self, parent=None): @@ -73,7 +73,7 @@ class MainWindow(QMainWindow): if not file_name: file_name, _ = QFileDialog.getOpenFileName(self, self.tr("Open File"), "", - "qmake Files (*.pro *.prf *.pri)") + "Python Files (*.py)") if file_name: in_file = QFile(file_name) @@ -82,30 +82,23 @@ class MainWindow(QMainWindow): self._editor.setPlainText(stream.readAll()) def setup_editor(self): - variable_format = QTextCharFormat() - variable_format.setFontWeight(QFont.Bold) - variable_format.setForeground(Qt.blue) - self._highlighter.add_mapping("\\b[A-Z_]+\\b", variable_format) - - single_line_comment_format = QTextCharFormat() - single_line_comment_format.setBackground(QColor("#77ff77")) - self._highlighter.add_mapping("#[^\n]*", single_line_comment_format) - - quotation_format = QTextCharFormat() - quotation_format.setBackground(Qt.cyan) - quotation_format.setForeground(Qt.blue) - self._highlighter.add_mapping("\".*\"", quotation_format) + class_format = QTextCharFormat() + class_format.setFontWeight(QFont.Bold) + class_format.setForeground(Qt.blue) + pattern = r'^\s*class\s+\w+\(.*$' + self._highlighter.add_mapping(pattern, class_format) function_format = QTextCharFormat() function_format.setFontItalic(True) function_format.setForeground(Qt.blue) - self._highlighter.add_mapping("\\b[a-z0-9_]+\\(.*\\)", function_format) + pattern = r'^\s*def\s+\w+\s*\(.*\)\s*:\s*$' + self._highlighter.add_mapping(pattern, function_format) - font = QFont() - font.setFamily("Courier") - font.setFixedPitch(True) - font.setPointSize(10) + comment_format = QTextCharFormat() + comment_format.setBackground(QColor("#77ff77")) + self._highlighter.add_mapping(r'^\s*#.*$', comment_format) + font = QFontDatabase.systemFont(QFontDatabase.FixedFont) self._editor = QPlainTextEdit() self._editor.setFont(font) self._highlighter.setDocument(self._editor.document()) @@ -150,5 +143,5 @@ if __name__ == '__main__': window = MainWindow() window.resize(640, 512) window.show() - window.open_file(":/examples/example") + window.open_file(os.fspath(Path(__file__).resolve())) sys.exit(app.exec_()) diff --git a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.pyproject b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.pyproject index e42b221a8..de344e5dd 100644 --- a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.pyproject +++ b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.pyproject @@ -1,4 +1,3 @@ { - "files": ["syntaxhighlighter_rc.py", "syntaxhighlighter.py", - "syntaxhighlighter.qrc"] + "files": ["syntaxhighlighter.py"] } diff --git a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.qrc b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.qrc deleted file mode 100644 index e5f9abf1e..000000000 --- a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter.qrc +++ /dev/null @@ -1,5 +0,0 @@ -<!DOCTYPE RCC><RCC version="1.0"> -<qresource prefix="/" > - <file>examples/example</file> -</qresource> -</RCC> diff --git a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter_rc.py b/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter_rc.py deleted file mode 100644 index 1e53463a4..000000000 --- a/examples/widgets/richtext/syntaxhighlighter/syntaxhighlighter_rc.py +++ /dev/null @@ -1,143 +0,0 @@ -# Resource object code (Python 3) -# Created by: object code -# Created by: The Resource Compiler for Qt version 5.14.0 -# WARNING! All changes made in this file will be lost! - -from PySide6 import QtCore - -qt_resource_data = b"\ -\x00\x00\x06{\ -T\ -EMPLATE = app\x0aLA\ -NGUAGE = C++\x0aTAR\ -GET = as\ -sistant\x0a\x0aCONFIG \ - += qt war\ -n_on\x0aQT \ - += xml networ\ -k\x0a\x0aPROJECTNAME \ - = Assistan\ -t\x0aDESTDIR \ - = ../../bin\ -\x0a\x0aFORMS += findd\ -ialog.ui \x5c\x0a \ - helpdialog.ui\ - \x5c\x0a mainw\ -indow.ui \x5c\x0a \ - settingsdialo\ -g.ui \x5c\x0a t\ -abbedbrowser.ui \ -\x5c\x0a topicc\ -hooser.ui\x0a\x0aSOURC\ -ES += main.cpp \x5c\ -\x0a helpwin\ -dow.cpp \x5c\x0a \ - topicchooser.c\ -pp \x5c\x0a doc\ -uparser.cpp \x5c\x0a \ - settingsdi\ -alog.cpp \x5c\x0a \ - index.cpp \x5c\x0a \ - profile.c\ -pp \x5c\x0a con\ -fig.cpp \x5c\x0a \ - finddialog.cpp\ - \x5c\x0a helpd\ -ialog.cpp \x5c\x0a \ - mainwindow.c\ -pp \x5c\x0a tab\ -bedbrowser.cpp\x0a\x0a\ -HEADERS +\ -= helpwindow.h \x5c\ -\x0a topicch\ -ooser.h \x5c\x0a \ - docuparser.h \x5c\ -\x0a setting\ -sdialog.h \x5c\x0a \ - index.h \x5c\x0a \ - profile.h \ -\x5c\x0a finddi\ -alog.h \x5c\x0a \ - helpdialog.h \x5c\x0a\ - mainwind\ -ow.h \x5c\x0a t\ -abbedbrowser.h \x5c\ -\x0a config.\ -h\x0a\x0aRESOURCES += \ -assistant.qrc\x0a\x0aD\ -EFINES += QT_KEY\ -WORDS\x0a#DEFINES +\ -= QT_PALMTOPCEN\ -TER_DOCS\x0a!networ\ -k:DEFINES \ - += QT_INTERNAL_\ -NETWORK\x0aelse:QT \ -+= network\x0a!xml:\ - DEFINES \ - += QT_IN\ -TERNAL_XML\x0aelse:\ -QT += xml\x0ainclud\ -e( ../../src/qt_\ -professional.pri\ - )\x0a\x0awin32 {\x0a \ -LIBS += -lshell3\ -2\x0a RC_FILE = \ -assistant.rc\x0a}\x0a\x0a\ -macos {\x0a ICON\ - = assistant.icn\ -s\x0a TARGET = a\ -ssistant\x0a# QM\ -AKE_INFO_PLIST =\ - Info_mac.plist\x0a\ -}\x0a\x0a#target.path \ -= $$[QT_INSTALL_\ -BINS]\x0a#INSTALLS \ -+= target\x0a\x0a#assi\ -stanttranslation\ -s.files = *.qm\x0a#\ -assistanttransla\ -tions.path = $$[\ -QT_INSTALL_TRANS\ -LATIONS]\x0a#INSTAL\ -LS += assistantt\ -ranslations\x0a\x0aTRA\ -NSLATIONS \ - = assistant_de.\ -ts \x5c\x0a \ - assistant\ -_fr.ts\x0a\x0a\x0aunix:!c\ -ontains(QT_CONFI\ -G, zlib):LIBS +=\ - -lz\x0a\x0a\x0atarget.pa\ -th=$$[QT_INSTALL\ -_BINS]\x0aINSTALLS \ -+= target\x0a\ -" - -qt_resource_name = b"\ -\x00\x08\ -\x0e\x84\x7fC\ -\x00e\ -\x00x\x00a\x00m\x00p\x00l\x00e\x00s\ -\x00\x07\ -\x0c\xe8G\xe5\ -\x00e\ -\x00x\x00a\x00m\x00p\x00l\x00e\ -" - -qt_resource_struct = b"\ -\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ -\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x02\ -\x00\x00\x00\x00\x00\x00\x00\x00\ -\x00\x00\x00\x16\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\ -\x00\x00\x01e\xaf\x16\xd2\xa1\ -" - -def qInitResources(): - QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) - -def qCleanupResources(): - QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) - -qInitResources() |