File indexing completed on 2024-05-12 04:38:07

0001 /*
0002     SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_COLORCACHE_H
0008 #define KDEVPLATFORM_COLORCACHE_H
0009 
0010 #include <QObject>
0011 #include <QVector>
0012 #include <QColor>
0013 #include <QPointer>
0014 
0015 #include <KSyntaxHighlighting/Repository>
0016 
0017 #include <interfaces/icompletionsettings.h>
0018 #include <language/languageexport.h>
0019 
0020 namespace KSyntaxHighlighting {
0021 class Theme;
0022 }
0023 
0024 namespace KTextEditor {
0025 class Document;
0026 class View;
0027 }
0028 
0029 namespace KDevelop {
0030 class ConfigurableHighlightingColors;
0031 class IDocument;
0032 
0033 /**
0034  * A singleton which holds the global default colors, adapted to the current color scheme
0035  */
0036 class KDEVPLATFORMLANGUAGE_EXPORT ColorCache
0037     : public QObject
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     ~ColorCache() override;
0043 
0044     /// access the global color cache
0045     static ColorCache* self();
0046 
0047     /// adapt a given foreground color to the current color scheme
0048     /// @p ratio between 0 and 255 where 0 gives @see m_foregroundColor
0049     /// and 255 gives @p color
0050     ///
0051     /// @note if you are looking for a background color, simply setting an alpha
0052     ///       value should work.
0053     QColor blend(QColor color, uchar ratio) const;
0054 
0055     /// adapt a given background color to the current color scheme
0056     /// @p ratio between 0 and 255 where 0 gives @see m_foregroundColor
0057     /// and 255 gives @p color
0058     ///
0059     /// @note if you are looking for a background color, simply setting an alpha
0060     ///       value should work.
0061     QColor blendBackground(QColor color, uchar ratio) const;
0062 
0063     /// blend a color for local colorization according to the user settings
0064     /// @see blend()
0065     QColor blendLocalColor(QColor color) const;
0066 
0067     /// blend a color for global colorization according to the user settings
0068     /// @see blend()
0069     QColor blendGlobalColor(QColor color) const;
0070 
0071     /// access the default colors
0072     ConfigurableHighlightingColors* defaultColors() const;
0073 
0074     /**
0075      * @returns a primary color if @p num less primaryColorCount and a supplementary color if @p num >= primaryColorCount and < validColorCount
0076      * @see validColorCount()
0077      * @see primaryColorCount()
0078      */
0079     QColor generatedColor(uint num) const;
0080 
0081     /**
0082      * @returns the number of primary and supplementary colors
0083      *
0084      * @see generatedColor()
0085      * @see primaryColorCount()
0086      */
0087     uint validColorCount() const;
0088 
0089     /**
0090      * @returns number of primary colors
0091      *
0092      * When you run out of primary colors use supplementary colors
0093      */
0094     uint primaryColorCount() const;
0095 
0096     /// access the foreground color
0097     QColor foregroundColor() const;
0098 
0099 Q_SIGNALS:
0100     /// will be emitted whenever the colors got changed
0101     /// @see update()
0102     void colorsGotChanged();
0103 
0104 private Q_SLOTS:
0105     /// if necessary, adapt to the colors of this document
0106     void slotDocumentActivated();
0107     /// settings got changed, update to the settings of the sender
0108     void slotViewSettingsChanged();
0109 
0110     /// will regenerate colors from global KDE color scheme
0111     void updateColorsFromScheme();
0112     /// will regenerate colors with the proper intensity settings
0113     void updateColorsFromSettings();
0114 
0115     /// regenerate colors and emits @p colorsGotChanged()
0116     /// and finally triggers a rehighlight of the opened documents
0117     void updateInternal();
0118 
0119     bool tryActiveDocument();
0120 
0121 private:
0122     explicit ColorCache(QObject* parent = nullptr);
0123     static ColorCache* m_self;
0124 
0125     /// get @p totalGeneratedColors colors from the color wheel and adapt them to the current color scheme
0126     void generateColors();
0127 
0128     /// calls @c updateInternal() delayed to prevent double loading of language plugins.
0129     void update();
0130 
0131     /// try to access the KatePart settings for the given doc or fallback to the global KDE scheme
0132     /// and update the colors if necessary
0133     /// @see generateColors(), updateColorsFromScheme()
0134     void updateColorsFromView(KTextEditor::View* view);
0135 
0136     bool updateColorsFromTheme(const KSyntaxHighlighting::Theme& theme);
0137 
0138     void updateDefaultColorsFromSource();
0139 
0140     /// the default colors for the different types
0141     ConfigurableHighlightingColors* m_defaultColors;
0142 
0143     /// the generated colors
0144     QVector<QColor> m_colors;
0145 
0146     uint m_validColorCount;
0147 
0148     uint m_primaryColorCount;
0149 
0150     /// Maybe make this configurable: An offset where to start stepping through the color wheel
0151     uint m_colorOffset;
0152 
0153     /// the text color for the current color scheme
0154     QColor m_foregroundColor;
0155 
0156     /// the editor background color for the current color scheme
0157     QColor m_backgroundColor;
0158 
0159     /// How generated colors for local variables should be mixed with the foreground color.
0160     /// Between 0 and 255, where 255 means only foreground color, and 0 only the chosen color.
0161     uchar m_localColorRatio;
0162 
0163     /// How global colors (i.e. for types, uses, etc.) should be mixed with the foreground color.
0164     /// Between 0 and 255, where 255 means only foreground color, and 0 only the chosen color.
0165     uchar m_globalColorRatio;
0166 
0167     ICompletionSettings::GlobalColorSource m_globalColorSource;
0168 
0169     /// Whether declarations have to be rendered with a bold style or not.
0170     bool m_boldDeclarations;
0171 
0172     /// The view we are listening to for setting changes.
0173     QPointer<KTextEditor::View> m_view;
0174 
0175     KSyntaxHighlighting::Repository m_schemeRepo;
0176 };
0177 }
0178 
0179 #endif // KDEVPLATFORM_COLORCACHE_H