d2061e2
From f5e74db1b5245814d5993b6b16d58274ca8b89ab Mon Sep 17 00:00:00 2001
6ad7f13
From: Jan Grulich <jgrulich@redhat.com>
6ad7f13
Date: Thu, 27 Jul 2023 12:42:49 +0200
d2061e2
Subject: [PATCH 13/15] Document QGtk3Storage
6ad7f13
6ad7f13
Add internal documentation to header and implementation of
6ad7f13
QGtk3Storage
6ad7f13
---
6ad7f13
 .../platformthemes/gtk3/qgtk3storage.cpp      | 231 +++++++++++++++---
6ad7f13
 .../platformthemes/gtk3/qgtk3storage_p.h      |   1 +
6ad7f13
 2 files changed, 193 insertions(+), 39 deletions(-)
6ad7f13
6ad7f13
diff --git a/src/plugins/platformthemes/gtk3/qgtk3storage.cpp b/src/plugins/platformthemes/gtk3/qgtk3storage.cpp
12ab19e
index 0b6b8e8523..7775ac66e4 100644
6ad7f13
--- a/src/plugins/platformthemes/gtk3/qgtk3storage.cpp
6ad7f13
+++ b/src/plugins/platformthemes/gtk3/qgtk3storage.cpp
6ad7f13
@@ -24,7 +24,26 @@ QGtk3Storage::QGtk3Storage()
6ad7f13
     populateMap();
6ad7f13
 }
6ad7f13
 
6ad7f13
-// Set a brush from a source and resolve recursions
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \enum QGtk3Storage::SourceType
6ad7f13
+    \brief This enum represents the type of a color source.
6ad7f13
+
6ad7f13
+    \value Gtk Color is read from a GTK widget
6ad7f13
+    \value Fixed A fixed brush is specified
6ad7f13
+    \value Modified The color is a modification of another color (fixed or read from GTK)
6ad7f13
+    \omitvalue Invalid
6ad7f13
+ */
6ad7f13
+
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Find a brush from a source.
6ad7f13
+
6ad7f13
+    Returns a QBrush from a given \param source and a \param map of available brushes
6ad7f13
+    to search from.
6ad7f13
+
6ad7f13
+    A null QBrush is returned, if no brush corresponding to the source has been found.
6ad7f13
+ */
6ad7f13
 QBrush QGtk3Storage::brush(const Source &source, const BrushMap &map) const
6ad7f13
 {
6ad7f13
     switch (source.sourceType) {
6ad7f13
@@ -64,7 +83,14 @@ QBrush QGtk3Storage::brush(const Source &source, const BrushMap &map) const
6ad7f13
     Q_UNREACHABLE();
6ad7f13
 }
6ad7f13
 
6ad7f13
-// Find source for a recursion and take dark/light/unknown into consideration
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Recurse to find a source brush for modification.
6ad7f13
+
6ad7f13
+    Returns the source specified by the target brush \param b in the \param map of brushes.
6ad7f13
+    Takes dark/light/unknown into consideration.
6ad7f13
+    Returns an empty brush if no suitable one can be found.
6ad7f13
+ */
6ad7f13
 QGtk3Storage::Source QGtk3Storage::brush(const TargetBrush &b, const BrushMap &map) const
6ad7f13
 {
6ad7f13
 #define FIND(brush) if (map.contains(brush))\
6ad7f13
@@ -88,7 +114,16 @@ QGtk3Storage::Source QGtk3Storage::brush(const TargetBrush &b, const BrushMap &m
6ad7f13
 #undef FIND
6ad7f13
 }
6ad7f13
 
6ad7f13
-// Create a simple standard palette
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Returns a simple, hard coded base palette.
6ad7f13
+
6ad7f13
+    Create a hard coded palette with default colors as a fallback for any color that can't be
6ad7f13
+    obtained from GTK.
6ad7f13
+
6ad7f13
+    \note This palette will be used as a default baseline for the system palette, which then
6ad7f13
+    will be used as a default baseline for any other palette type.
6ad7f13
+ */
6ad7f13
 QPalette QGtk3Storage::standardPalette()
6ad7f13
 {
6ad7f13
     QColor backgroundColor(0xd4, 0xd0, 0xc8);
6ad7f13
@@ -105,7 +140,13 @@ QPalette QGtk3Storage::standardPalette()
6ad7f13
     return palette;
6ad7f13
 }
6ad7f13
 
6ad7f13
-// Deliver a palette styled according to the current GTK Theme
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Return a GTK styled QPalette.
6ad7f13
+
6ad7f13
+    Returns the pointer to a (cached) QPalette for \param type, with its brushes
6ad7f13
+    populated according to the current GTK theme.
6ad7f13
+ */
6ad7f13
 const QPalette *QGtk3Storage::palette(QPlatformTheme::Palette type) const
6ad7f13
 {
6ad7f13
     if (type >= QPlatformTheme::NPalettes)
6ad7f13
@@ -160,6 +201,12 @@ const QPalette *QGtk3Storage::palette(QPlatformTheme::Palette type) const
6ad7f13
     return &m_paletteCache[type].value();
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Return a GTK styled font.
6ad7f13
+
6ad7f13
+    Returns a QFont of \param type, styled according to the current GTK theme.
6ad7f13
+*/
6ad7f13
 const QFont *QGtk3Storage::font(QPlatformTheme::Font type) const
6ad7f13
 {
6ad7f13
     if (m_fontCache[type].has_value())
6ad7f13
@@ -169,6 +216,13 @@ const QFont *QGtk3Storage::font(QPlatformTheme::Font type) const
6ad7f13
     return &m_fontCache[type].value();
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Return a GTK styled standard pixmap if available.
6ad7f13
+
6ad7f13
+    Returns a pixmap specified by \param standardPixmap and \param size.
6ad7f13
+    Returns an empty pixmap if GTK doesn't support the requested one.
6ad7f13
+ */
6ad7f13
 QPixmap QGtk3Storage::standardPixmap(QPlatformTheme::StandardPixmap standardPixmap,
6ad7f13
                                      const QSizeF &size) const
6ad7f13
 {
6ad7f13
@@ -186,11 +240,19 @@ QPixmap QGtk3Storage::standardPixmap(QPlatformTheme::StandardPixmap standardPixm
6ad7f13
     return QPixmap::fromImage(image.scaled(size.toSize()));
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Returns a GTK styled file icon corresponding to \param fileInfo.
6ad7f13
+ */
6ad7f13
 QIcon QGtk3Storage::fileIcon(const QFileInfo &fileInfo) const
6ad7f13
 {
6ad7f13
     return m_interface ? m_interface->fileIcon(fileInfo) : QIcon();
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Clears all caches.
6ad7f13
+ */
6ad7f13
 void QGtk3Storage::clear()
6ad7f13
 {
6ad7f13
     m_appearance = Qt::Appearance::Unknown;
6ad7f13
@@ -202,6 +264,13 @@ void QGtk3Storage::clear()
6ad7f13
         cache.reset();
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Handles a theme change at runtime.
6ad7f13
+
6ad7f13
+    Clear all caches, re-populate with current GTK theme and notify the window system interface.
6ad7f13
+    This method is a callback for the theme change signal sent from GTK.
6ad7f13
+ */
6ad7f13
 void QGtk3Storage::handleThemeChange()
6ad7f13
 {
6ad7f13
     clear();
6ad7f13
@@ -209,6 +278,54 @@ void QGtk3Storage::handleThemeChange()
12ab19e
     QWindowSystemInterface::handleThemeChange(nullptr);
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Populates a map with information about how to locate colors in GTK.
6ad7f13
+
6ad7f13
+    This method creates a data structure to locate color information for each brush of a QPalette
6ad7f13
+    within GTK. The structure can hold mapping information for each QPlatformTheme::Palette
6ad7f13
+    enum value. If no specific mapping is stored for an enum value, the system palette is returned
6ad7f13
+    instead of a specific one. If no mapping is stored for the system palette, it will fall back to
6ad7f13
+    QGtk3Storage::standardPalette.
6ad7f13
+
6ad7f13
+    The method will populate the data structure with a standard mapping, covering the following
6ad7f13
+    palette types:
6ad7f13
+    \list
6ad7f13
+    \li QPlatformTheme::SystemPalette
6ad7f13
+    \li QPlatformTheme::CheckBoxPalette
6ad7f13
+    \li QPlatformTheme::RadioButtonPalette
6ad7f13
+    \li QPlatformTheme::ComboBoxPalette
6ad7f13
+    \li QPlatformTheme::GroupBoxPalette
6ad7f13
+    \li QPlatformTheme::MenuPalette
6ad7f13
+    \li QPlatformTheme::TextLineEditPalette
6ad7f13
+    \endlist
6ad7f13
+
6ad7f13
+    The method will check the environment variable {{QT_GUI_GTK_JSON_SAVE}}. If it points to a
6ad7f13
+    valid path with write access, it will write the standard mapping into a Json file.
6ad7f13
+    That Json file can be modified and/or extended.
6ad7f13
+    The Json syntax is
6ad7f13
+    - "QGtk3Palettes" (top level value)
6ad7f13
+        - QPlatformTheme::Palette
6ad7f13
+            - QPalette::ColorRole
6ad7f13
+                - Qt::Appearance
6ad7f13
+                - Qt::ColorGroup
6ad7f13
+                - Source data
6ad7f13
+                    - Source Type
6ad7f13
+                        - [source data]
6ad7f13
+
6ad7f13
+    If the environment variable {{QT_GUI_GTK_JSON_HARDCODED}} contains the keyword \c true,
6ad7f13
+    all sources are converted to fixed sources. In that case, they contain the hard coded HexRGBA
6ad7f13
+    values read from GTK.
6ad7f13
+
6ad7f13
+    The method will also check the environment variable {{QT_GUI_GTK_JSON}}. If it points to a valid
6ad7f13
+    Json file with read access, it will be parsed instead of creating a standard mapping.
6ad7f13
+    Parsing errors will be printed out with qCInfo if the logging category {{qt.qpa.gtk}} is activated.
6ad7f13
+    In case of a parsing error, the method will fall back to creating a standard mapping.
6ad7f13
+
6ad7f13
+    \note
6ad7f13
+    If a Json file contains only fixed brushes (e.g. exported with {{QT_GUI_GTK_JSON_HARDCODED=true}}),
6ad7f13
+    no colors will be imported from GTK.
6ad7f13
+ */
6ad7f13
 void QGtk3Storage::populateMap()
6ad7f13
 {
6ad7f13
     static QString m_themeName;
6ad7f13
@@ -248,6 +365,15 @@ void QGtk3Storage::populateMap()
6ad7f13
         qWarning() << "File" << jsonOutput << "could not be saved.\n";
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Return a palette map for saving.
6ad7f13
+
6ad7f13
+    This method returns the existing palette map, if the environment variable
6ad7f13
+    {{QT_GUI_GTK_JSON_HARDCODED}} is not set or does not contain the keyword \c true.
6ad7f13
+    If it contains the keyword \c true, it returns a palette map with all brush
6ad7f13
+    sources converted to fixed sources.
6ad7f13
+ */
6ad7f13
 const QGtk3Storage::PaletteMap QGtk3Storage::savePalettes() const
6ad7f13
 {
6ad7f13
     const QString hard = qEnvironmentVariable("QT_GUI_GTK_JSON_HARDCODED");
6ad7f13
@@ -282,21 +408,50 @@ const QGtk3Storage::PaletteMap QGtk3Storage::savePalettes() const
6ad7f13
     return map;
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Saves current palette mapping to a \param filename with Json format \param f.
6ad7f13
+
6ad7f13
+    Saves the current palette mapping into a QJson file,
6ad7f13
+    taking {{QT_GUI_GTK_JSON_HARDCODED}} into consideration.
6ad7f13
+    Returns \c true if saving was successful and \c false otherwise.
6ad7f13
+ */
6ad7f13
 bool QGtk3Storage::save(const QString &filename, QJsonDocument::JsonFormat f) const
6ad7f13
 {
6ad7f13
     return QGtk3Json::save(savePalettes(), filename, f);
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Returns a QJsonDocument with current palette mapping.
6ad7f13
+
6ad7f13
+    Saves the current palette mapping into a QJsonDocument,
6ad7f13
+    taking {{QT_GUI_GTK_JSON_HARDCODED}} into consideration.
6ad7f13
+    Returns \c true if saving was successful and \c false otherwise.
6ad7f13
+ */
6ad7f13
 QJsonDocument QGtk3Storage::save() const
6ad7f13
 {
6ad7f13
     return QGtk3Json::save(savePalettes());
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Loads palette mapping from Json file \param filename.
6ad7f13
+
6ad7f13
+    Returns \c true if the file was successfully parsed and \c false otherwise.
6ad7f13
+ */
6ad7f13
 bool QGtk3Storage::load(const QString &filename)
6ad7f13
 {
6ad7f13
     return QGtk3Json::load(m_palettes, filename);
6ad7f13
 }
6ad7f13
 
6ad7f13
+/*!
6ad7f13
+    \internal
6ad7f13
+    \brief Creates a standard palette mapping.
6ad7f13
+
6ad7f13
+    The method creates a hard coded standard mapping, used if no external Json file
6ad7f13
+    containing a valid mapping has been specified in the environment variable {{QT_GUI_GTK_JSON}}.
6ad7f13
+ */
6ad7f13
 void QGtk3Storage::createMapping()
6ad7f13
 {
6ad7f13
     // Hard code standard mapping
6ad7f13
@@ -332,41 +487,39 @@ void QGtk3Storage::createMapping()
6ad7f13
 #define CLEAR map.clear()
6ad7f13
 
6ad7f13
     /*
6ad7f13
-     *  Macro ussage:
6ad7f13
-     *
6ad7f13
-     *  1. Define a source
6ad7f13
-     *
6ad7f13
-     *  GTK(QGtkWidget, QGtkColorSource, GTK_STATE_FLAG)
6ad7f13
-     *  Fetch the color from a GtkWidget, related to a source and a state.
6ad7f13
-     *
6ad7f13
-     *  LIGHTER(ColorGroup, ColorROle, lighter)
6ad7f13
-     *  Use a color of the same QPalette related to ColorGroup and ColorRole.
6ad7f13
-     *  Make the color lighter (if lighter >100) or darker (if lighter < 100)
6ad7f13
-     *
6ad7f13
-     *  MODIFY(ColorGroup, ColorRole, red, green, blue)
6ad7f13
-     *  Use a color of the same QPalette related to ColorGroup and ColorRole.
6ad7f13
-     *  Modify it by adding red, green, blue.
6ad7f13
-     *
6ad7f13
-     *  FIX(const QBrush &)
6ad7f13
-     *  Use a fixed brush without querying GTK
6ad7f13
-     *
6ad7f13
-     *  2. Define the target
6ad7f13
-     *
6ad7f13
-     *  Use ADD(ColorGroup, ColorRole) to use the defined source for the
6ad7f13
-     *  color group / role in the current palette.
6ad7f13
-     *
6ad7f13
-     *  Use ADD(ColorGroup, ColorRole, Appearance) to use the defined source
6ad7f13
-     *  only for a specific appearance
6ad7f13
-     *
6ad7f13
-     *  3. Save mapping
6ad7f13
-     *  Save the defined mappings for a specific palette.
6ad7f13
-     *  If a mapping entry does not cover all color groups and roles of a palette,
6ad7f13
-     *  the system palette will be used for the remaining values.
6ad7f13
-     *  If the system palette does not have all combination of color groups and roles,
6ad7f13
-     *  the remaining ones will be populated by a hard coded fusion-style like palette.
6ad7f13
-     *
6ad7f13
-     *  4. Clear mapping
6ad7f13
-     *  Use CLEAR to clear the mapping and begin a new one.
6ad7f13
+       Macro usage:
6ad7f13
+
6ad7f13
+       1. Define a source
6ad7f13
+       GTK(QGtkWidget, QGtkColorSource, GTK_STATE_FLAG)
6ad7f13
+       Fetch the color from a GtkWidget, related to a source and a state.
6ad7f13
+
6ad7f13
+       LIGHTER(ColorGroup, ColorROle, lighter)
6ad7f13
+       Use a color of the same QPalette related to ColorGroup and ColorRole.
6ad7f13
+       Make the color lighter (if lighter >100) or darker (if lighter < 100)
6ad7f13
+
6ad7f13
+       MODIFY(ColorGroup, ColorRole, red, green, blue)
6ad7f13
+       Use a color of the same QPalette related to ColorGroup and ColorRole.
6ad7f13
+       Modify it by adding red, green, blue.
6ad7f13
+
6ad7f13
+       FIX(const QBrush &)
6ad7f13
+       Use a fixed brush without querying GTK
6ad7f13
+
6ad7f13
+       2. Define the target
6ad7f13
+       Use ADD(ColorGroup, ColorRole) to use the defined source for the
6ad7f13
+       color group / role in the current palette.
6ad7f13
+
6ad7f13
+       Use ADD(ColorGroup, ColorRole, Appearance) to use the defined source
6ad7f13
+       only for a specific appearance
6ad7f13
+
6ad7f13
+       3. Save mapping
6ad7f13
+       Save the defined mappings for a specific palette.
6ad7f13
+       If a mapping entry does not cover all color groups and roles of a palette,
6ad7f13
+       the system palette will be used for the remaining values.
6ad7f13
+       If the system palette does not have all combination of color groups and roles,
6ad7f13
+       the remaining ones will be populated by a hard coded fusion-style like palette.
6ad7f13
+
6ad7f13
+       4. Clear mapping
6ad7f13
+       Use CLEAR to clear the mapping and begin a new one.
6ad7f13
      */
6ad7f13
 
6ad7f13
 
6ad7f13
diff --git a/src/plugins/platformthemes/gtk3/qgtk3storage_p.h b/src/plugins/platformthemes/gtk3/qgtk3storage_p.h
6ad7f13
index 57f6aeea96..af628d49ff 100644
6ad7f13
--- a/src/plugins/platformthemes/gtk3/qgtk3storage_p.h
6ad7f13
+++ b/src/plugins/platformthemes/gtk3/qgtk3storage_p.h
6ad7f13
@@ -33,6 +33,7 @@ class QGtk3Storage
6ad7f13
 public:
6ad7f13
     QGtk3Storage();
6ad7f13
 
6ad7f13
+    // Enum documented in cpp file. Please keep it in line with updates made here.
6ad7f13
     enum class SourceType {
6ad7f13
         Gtk,
6ad7f13
         Fixed,
6ad7f13
-- 
6ad7f13
2.41.0
6ad7f13