File indexing completed on 2024-05-12 04:02:21

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003     SPDX-FileCopyrightText: 2016 Dominik Haumann <dhaumann@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 #ifndef KSYNTAXHIGHLIGHTING_THEMEDATA_P_H
0009 #define KSYNTAXHIGHLIGHTING_THEMEDATA_P_H
0010 
0011 #include "textstyledata_p.h"
0012 #include "theme.h"
0013 
0014 #include <QHash>
0015 #include <QSharedData>
0016 
0017 #include <vector>
0018 
0019 namespace KSyntaxHighlighting
0020 {
0021 /**
0022  * Data container for a Theme.
0023  */
0024 class ThemeData : public QSharedData
0025 {
0026 public:
0027     static ThemeData *get(const Theme &theme)
0028     {
0029         return theme.m_data.data();
0030     }
0031 
0032     /**
0033      * Default constructor, creating an uninitialized ThemeData instance.
0034      */
0035     ThemeData();
0036 
0037     /**
0038      * Load the Theme data from the file @p filePath.
0039      * Note, that @p filePath either is a local file, or a qt resource location.
0040      */
0041     bool load(const QString &filePath);
0042 
0043     void loadComplete();
0044 
0045     /**
0046      * Returns the unique name of this Theme.
0047      */
0048     QString name() const;
0049 
0050     /**
0051      * Returns the revision of this Theme.
0052      * The revision in a .theme file should be increased with every change.
0053      */
0054     int revision() const;
0055 
0056     /**
0057      * Returns @c true if this Theme is read-only.
0058      * Typically, themes that are shipped by default are read-only.
0059      */
0060     bool isReadOnly() const;
0061 
0062     /**
0063      * Returns the full path and filename to this Theme.
0064      * Themes from the Qt resource return the Qt resource path.
0065      * Themes from disk return the local path.
0066      *
0067      * If the theme is invalid (isValid()), an empty string is returned.
0068      */
0069     QString filePath() const;
0070 
0071     /**
0072      * Returns the text color to be used for @p style.
0073      * @c 0 is returned for styles that do not specify a text color,
0074      * use the default text color in that case.
0075      */
0076     QRgb textColor(Theme::TextStyle style) const;
0077 
0078     /**
0079      * Returns the text color for selected to be used for @p style.
0080      * @c 0 is returned for styles that do not specify a selected text color,
0081      * use the textColor() in that case.
0082      */
0083     QRgb selectedTextColor(Theme::TextStyle style) const;
0084 
0085     /**
0086      * Returns the background color to be used for @p style.
0087      * @c 0 is returned for styles that do not specify a background color,
0088      * use the default background color in that case.
0089      */
0090     QRgb backgroundColor(Theme::TextStyle style) const;
0091 
0092     /**
0093      * Returns the background color for selected text to be used for @p style.
0094      * @c 0 is returned for styles that do not specify a selected background
0095      * color, use the default backgroundColor() in that case.
0096      */
0097     QRgb selectedBackgroundColor(Theme::TextStyle style) const;
0098 
0099     /**
0100      * Returns whether the given style should be shown in bold.
0101      */
0102     bool isBold(Theme::TextStyle style) const;
0103 
0104     /**
0105      * Returns whether the given style should be shown in italic.
0106      */
0107     bool isItalic(Theme::TextStyle style) const;
0108 
0109     /**
0110      * Returns whether the given style should be shown underlined.
0111      */
0112     bool isUnderline(Theme::TextStyle style) const;
0113 
0114     /**
0115      * Returns whether the given style should be shown struck through.
0116      */
0117     bool isStrikeThrough(Theme::TextStyle style) const;
0118 
0119 public:
0120     /**
0121      * Returns the editor color for the requested @p role.
0122      */
0123     QRgb editorColor(Theme::EditorColorRole role) const;
0124 
0125     /**
0126      * Returns the TextStyle override of a specific "itemData" with attributeName
0127      * in the syntax definition called definitionName.
0128      *
0129      * If no override exists, a valid TextStyleData with the respective default
0130      * TextStyle will be used, so the returned value is always valid.
0131      */
0132     TextStyleData textStyleOverride(const QString &definitionName, const QString &attributeName) const;
0133 
0134     /**
0135      * Returns the TextStyle data for the given @p style.
0136      */
0137     TextStyleData textStyle(Theme::TextStyle style) const;
0138 
0139 private:
0140     int m_revision = 0;
0141     QString m_name;
0142 
0143     //! Path to the file where the theme came from.
0144     //! This is either a resource location (":/themes/Default.theme"), or a file
0145     //! on disk (in a read-only or a writeable location).
0146     QString m_filePath;
0147 
0148     bool m_completelyLoaded = false;
0149 
0150     //! TextStyles
0151     std::vector<TextStyleData> m_textStyles;
0152 
0153     //! style overrides for individual itemData entries
0154     //! definition name -> attribute name -> style
0155     QHash<QString, QHash<QString, TextStyleData>> m_textStyleOverrides;
0156 
0157     //! Editor area colors
0158     QRgb m_editorColors[Theme::TemplateReadOnlyPlaceholder + 1];
0159 };
0160 
0161 }
0162 
0163 QT_BEGIN_NAMESPACE
0164 Q_DECLARE_TYPEINFO(KSyntaxHighlighting::TextStyleData, Q_RELOCATABLE_TYPE);
0165 QT_END_NAMESPACE
0166 
0167 #endif // KSYNTAXHIGHLIGHTING_THEMEDATA_P_H