LCOV - code coverage report
Current view: top level - muse/framework/ui/internal - themeconverter.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 78.0 % 41 32
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "themeconverter.h"
      24              : 
      25              : #include "translation.h"
      26              : 
      27              : #include "log.h"
      28              : 
      29              : using namespace muse;
      30              : using namespace muse::ui;
      31              : 
      32              : static const QString CODEKEY_KEY("codeKey");
      33              : static const QString TITLE_KEY("title");
      34              : 
      35              : static const std::vector<std::pair<ThemeStyleKey, QString > > s_keys = {
      36              :     { UNKNOWN, QString() },
      37              : 
      38              :     { BACKGROUND_PRIMARY_COLOR, "backgroundPrimaryColor" },
      39              :     { BACKGROUND_SECONDARY_COLOR, "backgroundSecondaryColor" },
      40              :     { BACKGROUND_TERTIARY_COLOR, "backgroundTertiaryColor" },
      41              :     { BACKGROUND_QUARTERNARY_COLOR, "backgroundQuarternaryColor" },
      42              :     { POPUP_BACKGROUND_COLOR, "popupBackgroundColor" },
      43              :     { PROJECT_TAB_COLOR, "projectTabColor" },
      44              :     { TEXT_FIELD_COLOR, "textFieldColor" },
      45              :     { ACCENT_COLOR, "accentColor" },
      46              :     { STROKE_COLOR, "strokeColor" },
      47              :     { STROKE_SECONDARY_COLOR, "strokeSecondaryColor" },
      48              :     { BUTTON_COLOR, "buttonColor" },
      49              :     { BORDER_WIDTH, "borderWidth" },
      50              :     { FONT_PRIMARY_COLOR, "fontPrimaryColor" },
      51              :     { FONT_SECONDARY_COLOR, "fontSecondaryColor" },
      52              :     { LINK_COLOR, "linkColor" },
      53              :     { FOCUS_COLOR, "focusColor" },
      54              :     { WHITE_COLOR, "whiteColor" },
      55              :     { BLACK_COLOR, "blackColor" },
      56              :     { PLAY_COLOR, "playColor" },
      57              :     { RECORD_COLOR, "recordColor" },
      58              : 
      59              :     { BORDER_WIDTH, "borderWidth" },
      60              :     { NAVIGATION_CONTROL_BORDER_WIDTH, "navigationControlBorderWidth" },
      61              : 
      62              :     { ACCENT_OPACITY_NORMAL, "accentOpacityNormal" },
      63              :     { ACCENT_OPACITY_HOVER, "accentOpacityHover" },
      64              :     { ACCENT_OPACITY_HIT, "accentOpacityHit" },
      65              :     { BUTTON_OPACITY_NORMAL, "buttonOpacityNormal" },
      66              :     { BUTTON_OPACITY_HOVER, "buttonOpacityHover" },
      67              :     { BUTTON_OPACITY_HIT, "buttonOpacityHit" },
      68              : 
      69              :     { ITEM_OPACITY_DISABLED, "itemOpacityDisabled" },
      70              : };
      71              : 
      72            3 : static QString titleForTheme(const ThemeInfo& theme)
      73              : {
      74            3 :     if (theme.codeKey == LIGHT_THEME_CODE) {
      75              :         //: The name of the light ui theme
      76            0 :         return muse::qtrc("ui", "Light");
      77            3 :     } else if (theme.codeKey == DARK_THEME_CODE) {
      78              :         //: The name of the dark ui theme
      79            3 :         return muse::qtrc("ui", "Dark");
      80            0 :     } else if (theme.codeKey == HIGH_CONTRAST_WHITE_THEME_CODE) {
      81              :         //: The name of the high contrast light ui theme
      82            0 :         return muse::qtrc("ui", "White");
      83            0 :     } else if (theme.codeKey == HIGH_CONTRAST_BLACK_THEME_CODE) {
      84              :         //: The name of the high contrast dark ui theme
      85            0 :         return muse::qtrc("ui", "Black");
      86              :     }
      87              : 
      88            0 :     return QString::fromStdString(theme.title);
      89              : }
      90              : 
      91           10 : static const QString& themeStyleKeyToString(ThemeStyleKey key)
      92              : {
      93          109 :     for (const auto& p : s_keys) {
      94          109 :         if (p.first == key) {
      95           10 :             return p.second;
      96              :         }
      97              :     }
      98              : 
      99            0 :     IF_ASSERT_FAILED_X(false, QString("not found string key for enum key: %1").arg(static_cast<int>(key))) {
     100              :     }
     101              : 
     102           25 :     static const QString null;
     103              :     return null;
     104              : }
     105              : 
     106           21 : static ThemeStyleKey themeStyleKeyFromString(const QString& str)
     107              : {
     108          404 :     for (const auto& p : s_keys) {
     109          395 :         if (p.second == str) {
     110           12 :             return p.first;
     111              :         }
     112              :     }
     113              : 
     114              :     /*IF_ASSERT_FAILED_X(false, QString("not found enum key for string key: %1").arg(str)) {
     115              :     }*/
     116              : 
     117            9 :     return ThemeStyleKey::UNKNOWN;
     118              : }
     119              : 
     120            3 : QVariantMap ThemeConverter::toMap(const ThemeInfo& theme)
     121              : {
     122            3 :     QVariantMap obj;
     123              : 
     124            3 :     obj[CODEKEY_KEY] = QString::fromStdString(theme.codeKey);
     125            3 :     obj[TITLE_KEY] = titleForTheme(theme);
     126              : 
     127           13 :     for (ThemeStyleKey key : theme.values.keys()) {
     128           10 :         obj[themeStyleKeyToString(key)] = theme.values[key];
     129            3 :     }
     130              : 
     131            3 :     return obj;
     132            0 : }
     133              : 
     134            4 : ThemeInfo ThemeConverter::fromMap(const QVariantMap& map)
     135              : {
     136            4 :     ThemeInfo theme;
     137            4 :     theme.codeKey = map[CODEKEY_KEY].toString().toStdString();
     138            4 :     theme.title = map[TITLE_KEY].toString().toStdString();
     139              : 
     140           25 :     for (const QString& key : map.keys()) {
     141           21 :         ThemeStyleKey styleKey = themeStyleKeyFromString(key);
     142           21 :         if (styleKey != UNKNOWN) {
     143           12 :             theme.values[styleKey] = map[key];
     144              :         }
     145            4 :     }
     146              : 
     147            4 :     return theme;
     148            0 : }
        

Generated by: LCOV version 2.0-1