File indexing completed on 2024-05-12 15:50:06

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef KSYNTAXHIGHLIGHTING_THEME_H
0008 #define KSYNTAXHIGHLIGHTING_THEME_H
0009 
0010 #include "ksyntaxhighlighting_export.h"
0011 
0012 #include <QColor>
0013 #include <QExplicitlySharedDataPointer>
0014 #include <QTypeInfo>
0015 #include <qobjectdefs.h>
0016 
0017 namespace KSyntaxHighlighting
0018 {
0019 class ThemeData;
0020 class RepositoryPrivate;
0021 
0022 /**
0023  * Color theme definition used for highlighting.
0024  *
0025  * @section theme_intro Introduction
0026  *
0027  * The Theme provides a full color theme for painting the highlighted text.
0028  * One Theme is defined either as a *.theme file on disk, or as a file compiled
0029  * into the SyntaxHighlighting library by using Qt's resource system. Each
0030  * Theme has a unique name(), including a translatedName() if put into the UI.
0031  * Themes shipped by default are typically read-only, see isReadOnly().
0032  *
0033  * A Theme defines two sets of colors:
0034  * - Text colors, including foreground and background colors, colors for
0035  *   selected text, and properties such as bold and italic. These colors are
0036  *   used e.g. by the SyntaxHighlighter.
0037  * - Editor colors, including a background color for the entire editor widget,
0038  *   the line number color, code folding colors, etc.
0039  *
0040  * @section theme_text_colors Text Colors and the Class Format
0041  *
0042  * The text colors are used for syntax highlighting.
0043  * // TODO: elaborate more and explain relation to Format class
0044  *
0045  * @section theme_editor_colors Editor Colors
0046  *
0047  * If you want to use the SyntaxHighlighting framework to write your own text
0048  * editor, you also need to paint the background of the editing widget. In
0049  * addition, the editor may support showing line numbers, a folding bar, a
0050  * highlight for the current text line, and similar features. All these colors
0051  * are defined in terms of the "editor colors" and accessible by calling
0052  * editorColor() with the desired enum EditorColorRole.
0053  *
0054  * @section theme_access Accessing a Theme
0055  *
0056  * All available Theme%s are accessed through the Repository. These themes are
0057  * typically valid themes. If you create a Theme on your own, isValid() will
0058  * return @e false, and all colors provided by this Theme are in fact invalid
0059  * and therefore unusable.
0060  *
0061  * @see Format
0062  * @since 5.28
0063  */
0064 class KSYNTAXHIGHLIGHTING_EXPORT Theme
0065 {
0066     Q_GADGET
0067     Q_PROPERTY(QString name READ name)
0068     Q_PROPERTY(QString translatedName READ translatedName)
0069 public:
0070     // TODO KF6:
0071     // - make TextStyle an enum class
0072     // - move out of Theme into KSyntaxHighlighting
0073     // - do the same for EditorColorRole
0074 
0075     /**
0076      * Default styles that can be referenced from syntax definition XML files.
0077      * Make sure to choose readable colors with good contrast especially in
0078      * combination with the EditorColorRole%s.
0079      */
0080     enum TextStyle {
0081         //! Default text style for normal text and source code without
0082         //! special highlighting.
0083         Normal = 0,
0084         //! Text style for language keywords.
0085         Keyword,
0086         //! Text style for function definitions and function calls.
0087         Function,
0088         //! Text style for variables, if applicable. For instance, variables in
0089         //! PHP typically start with a '$', so all identifiers following the
0090         //! pattern $foo are highlighted as variable.
0091         Variable,
0092         //! Text style for control flow highlighting, such as @e if, @e then,
0093         //! @e else, @e return, or @e continue.
0094         ControlFlow,
0095         //! Text style for operators such as +, -, *, / and :: etc.
0096         Operator,
0097         //! Text style for built-in language classes and functions.
0098         BuiltIn,
0099         //! Text style for well-known extensions, such as Qt or boost.
0100         Extension,
0101         //! Text style for preprocessor statements.
0102         Preprocessor,
0103         //! Text style for attributes of functions or objects, e.g. \@override
0104         //! in Java, or __declspec(...) and __attribute__((...)) in C++.
0105         Attribute,
0106         //! Text style for single characters such as 'a'.
0107         Char,
0108         //! Text style for escaped characters in strings, such as "hello\n".
0109         SpecialChar,
0110         //! Text style for strings, for instance "hello world".
0111         String,
0112         //! Text style for verbatim strings such as HERE docs.
0113         VerbatimString,
0114         //! Text style for special strings such as regular expressions in
0115         //! ECMAScript or the LaTeX math mode.
0116         SpecialString,
0117         //! Text style for includes, imports, modules, or LaTeX packages.
0118         Import,
0119         //! Text style for data types such as int, char, float etc.
0120         DataType,
0121         //! Text style for decimal values.
0122         DecVal,
0123         //! Text style for numbers with base other than 10.
0124         BaseN,
0125         //! Text style for floating point numbers.
0126         Float,
0127         //! Text style for language constants, e.g. True, False, None in Python
0128         //! or nullptr in C/C++.
0129         Constant,
0130         //! Text style for normal comments.
0131         Comment,
0132         //! Text style for comments that reflect API documentation, such as
0133         //! doxygen /** */ comments.
0134         Documentation,
0135         //! Text style for annotations in comments, such as \@param in Doxygen
0136         //! or JavaDoc.
0137         Annotation,
0138         //! Text style that refers to variables in a comment, such as after
0139         //! \@param \<identifier\> in Doxygen or JavaDoc.
0140         CommentVar,
0141         //! Text style for region markers, typically defined by BEGIN/END.
0142         RegionMarker,
0143         //! Text style for information, such as the keyword \@note in Doxygen.
0144         Information,
0145         //! Text style for warnings, such as the keyword \@warning in Doxygen.
0146         Warning,
0147         //! Text style for comment specials such as TODO and WARNING in
0148         //! comments.
0149         Alert,
0150         //! Text style indicating wrong syntax.
0151         Error,
0152         //! Text style for attributes that do not match any of the other default
0153         //! styles.
0154         Others
0155     };
0156     Q_ENUM(TextStyle)
0157 
0158     /**
0159      * Editor color roles, used to paint line numbers, editor background etc.
0160      * The colors typically should have good contrast with the colors used
0161      * in the TextStyle%s.
0162      */
0163     enum EditorColorRole {
0164         //! Background color for the editing area.
0165         BackgroundColor = 0,
0166         //! Background color for selected text.
0167         TextSelection,
0168         //! Background color for the line of the current text cursor.
0169         CurrentLine,
0170         //! Background color for matching text while searching.
0171         SearchHighlight,
0172         //! Background color for replaced text for a search & replace action.
0173         ReplaceHighlight,
0174         //! Background color for matching bracket pairs (including quotes)
0175         BracketMatching,
0176         //! Foreground color for visualizing tabs and trailing spaces.
0177         TabMarker,
0178         //! Color used to underline spell check errors.
0179         SpellChecking,
0180         //! Color used to draw vertical indentation levels, typically a line.
0181         IndentationLine,
0182         //! Background color for the icon border.
0183         IconBorder,
0184         //! Background colors for code folding regions in the text area, as well
0185         //! as code folding indicators in the code folding border.
0186         CodeFolding,
0187         //! Foreground color for drawing the line numbers. This should have a
0188         //! good contrast with the IconBorder background color.
0189         LineNumbers,
0190         //! Foreground color for drawing the current line number. This should
0191         //! have a good contrast with the IconBorder background color.
0192         CurrentLineNumber,
0193         //! Color used in the icon border to indicate dynamically wrapped lines.
0194         //! This color should have a good contrast with the IconBorder
0195         //! background color.
0196         WordWrapMarker,
0197         //! Color used to draw a vertical line for marking changed lines.
0198         ModifiedLines,
0199         //! Color used to draw a vertical line for marking saved lines.
0200         SavedLines,
0201         //! Line color used to draw separator lines, e.g. at column 80 in the
0202         //! text editor area.
0203         Separator,
0204         //! Background color for bookmarks.
0205         MarkBookmark,
0206         //! Background color for active breakpoints.
0207         MarkBreakpointActive,
0208         //! Background color for a reached breakpoint.
0209         MarkBreakpointReached,
0210         //! Background color for inactive (disabled) breakpoints.
0211         MarkBreakpointDisabled,
0212         //! Background color for marking the current execution position.
0213         MarkExecution,
0214         //! Background color for general warning marks.
0215         MarkWarning,
0216         //! Background color for general error marks.
0217         MarkError,
0218         //! Background color for text templates (snippets).
0219         TemplateBackground,
0220         //! Background color for all editable placeholders in text templates.
0221         TemplatePlaceholder,
0222         //! Background color for the currently active placeholder in text
0223         //! templates.
0224         TemplateFocusedPlaceholder,
0225         //! Background color for read-only placeholders in text templates.
0226         TemplateReadOnlyPlaceholder
0227     };
0228     Q_ENUM(EditorColorRole)
0229 
0230     /**
0231      * Default constructor, creating an invalid Theme, see isValid().
0232      */
0233     Theme();
0234 
0235     /**
0236      * Copy constructor, sharing the Theme data with @p copy.
0237      */
0238     Theme(const Theme &copy);
0239 
0240     /**
0241      * Destructor.
0242      */
0243     ~Theme();
0244 
0245     /**
0246      * Assignment operator, sharing the Theme data with @p other.
0247      */
0248     Theme &operator=(const Theme &other);
0249 
0250     /**
0251      * Returns @c true if this is a valid Theme.
0252      * If the theme is invalid, none of the returned colors are well-defined.
0253      */
0254     bool isValid() const;
0255 
0256     /**
0257      * Returns the unique name of this Theme.
0258      * @see translatedName()
0259      */
0260     QString name() const;
0261 
0262     /**
0263      * Returns the translated name of this Theme. The translated name can be
0264      * used in the user interface.
0265      */
0266     QString translatedName() const;
0267 
0268     /**
0269      * Returns @c true if this Theme is read-only.
0270      *
0271      * A Theme is read-only, if the filePath() points to a non-writable file.
0272      * This is typically the case for Themes that are compiled into the executable
0273      * as resource file, as well as for theme files that are installed in read-only
0274      * system locations (e.g. /usr/share/).
0275      */
0276     bool isReadOnly() const;
0277 
0278     /**
0279      * Returns the full path and file name to this Theme.
0280      * Themes from the Qt resource return the Qt resource path.
0281      * Themes from disk return the local path.
0282      *
0283      * If the theme is invalid (isValid()), an empty string is returned.
0284      */
0285     QString filePath() const;
0286 
0287     /**
0288      * Returns the text color to be used for @p style.
0289      * @c 0 is returned for styles that do not specify a text color,
0290      * use the default text color in that case.
0291      */
0292     QRgb textColor(TextStyle style) const;
0293 
0294     /**
0295      * Returns the selected text color to be used for @p style.
0296      * @c 0 is returned for styles that do not specify a selected text color,
0297      * use the default textColor() in that case.
0298      */
0299     QRgb selectedTextColor(TextStyle style) const;
0300 
0301     /**
0302      * Returns the background color to be used for @p style.
0303      * @c 0 is returned for styles that do not specify a background color,
0304      * use the default background color in that case.
0305      */
0306     QRgb backgroundColor(TextStyle style) const;
0307 
0308     /**
0309      * Returns the background color to be used for selected text for @p style.
0310      * @c 0 is returned for styles that do not specify a background color,
0311      * use the default backgroundColor() in that case.
0312      */
0313     QRgb selectedBackgroundColor(TextStyle style) const;
0314 
0315     /**
0316      * Returns whether the given style should be shown in bold.
0317      */
0318     bool isBold(TextStyle style) const;
0319 
0320     /**
0321      * Returns whether the given style should be shown in italic.
0322      */
0323     bool isItalic(TextStyle style) const;
0324 
0325     /**
0326      * Returns whether the given style should be shown underlined.
0327      */
0328     bool isUnderline(TextStyle style) const;
0329 
0330     /**
0331      * Returns whether the given style should be shown struck through.
0332      */
0333     bool isStrikeThrough(TextStyle style) const;
0334 
0335 public:
0336     /**
0337      * Returns the editor color for the requested @p role.
0338      */
0339     QRgb editorColor(EditorColorRole role) const;
0340 
0341 private:
0342     /**
0343      * Constructor taking a shared ThemeData instance.
0344      */
0345     KSYNTAXHIGHLIGHTING_NO_EXPORT explicit Theme(ThemeData *data);
0346     friend class RepositoryPrivate;
0347     friend class ThemeData;
0348 
0349 private:
0350     /**
0351      * Shared data holder.
0352      */
0353     QExplicitlySharedDataPointer<ThemeData> m_data;
0354 };
0355 
0356 }
0357 
0358 QT_BEGIN_NAMESPACE
0359 Q_DECLARE_TYPEINFO(KSyntaxHighlighting::Theme, Q_MOVABLE_TYPE);
0360 QT_END_NAMESPACE
0361 
0362 #endif // KSYNTAXHIGHLIGHTING_THEME_H