Skip to content

Commit b2fbea1

Browse files
Merge pull request #30852 from juli27/fixQColorDialogCustomColors
fw/ui: Save custom colors of QColorDialog in settings
2 parents e1e7f07 + 5de811b commit b2fbea1

File tree

6 files changed

+86
-6
lines changed

6 files changed

+86
-6
lines changed

src/framework/ui/internal/uiconfiguration.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
#include "settings.h"
3030
#include "themeconverter.h"
3131

32-
#include <QScreen>
3332
#include <QFontDatabase>
33+
#include <QJsonArray>
3434
#include <QJsonDocument>
3535
#include <QJsonObject>
36-
#include <QJsonArray>
36+
#include <QScreen>
37+
#include <QSettings>
3738

3839
#ifdef Q_OS_WIN
3940
#include <QOperatingSystemVersion>
@@ -43,12 +44,14 @@
4344

4445
#include "log.h"
4546

47+
using namespace Qt::Literals;
4648
using namespace muse;
4749
using namespace muse::ui;
4850
using namespace muse::async;
4951

5052
static const Settings::Key UI_THEMES_KEY("ui", "ui/application/themes");
5153
static const Settings::Key UI_CURRENT_THEME_CODE_KEY("ui", "ui/application/currentThemeCode");
54+
static const Settings::Key UI_CUSTOM_COLORS_KEY("ui", "ui/application/customColors");
5255
static const Settings::Key UI_FOLLOW_SYSTEM_THEME_KEY("ui", "ui/application/followSystemTheme");
5356
static const Settings::Key UI_FONT_FAMILY_KEY("ui", "ui/theme/fontFamily");
5457
static const Settings::Key UI_FONT_SIZE_KEY("ui", "ui/theme/fontSize");
@@ -64,11 +67,30 @@ static const int FLICKABLE_MAX_VELOCITY = 4000;
6467

6568
static const int TOOLTIP_DELAY = 500;
6669

70+
// read custom colors saved by Qt < 6.9
71+
// see: https://github.com/qt/qtbase/blob/v6.2.4/src/gui/kernel/qplatformdialoghelper.cpp#L292-L302
72+
static std::vector<Val> readLegacyCustomColors()
73+
{
74+
constexpr size_t customColorCount = 16;
75+
76+
QSettings settings(QSettings::UserScope, u"QtProject"_s);
77+
std::vector<Val> legacyValues(customColorCount, Val(QColorConstants::White));
78+
for (size_t i = 0; i < customColorCount; ++i) {
79+
const QVariant value = settings.value(u"Qt/customColors/"_s + QString::number(i));
80+
if (value.isValid()) {
81+
legacyValues[i] = Val(QColor::fromRgb(value.toUInt()));
82+
}
83+
}
84+
85+
return legacyValues;
86+
}
87+
6788
void UiConfiguration::init()
6889
{
6990
m_config = ConfigReader::read(":/configs/ui.cfg");
7091

7192
settings()->setDefaultValue(UI_CURRENT_THEME_CODE_KEY, Val(LIGHT_THEME_CODE));
93+
settings()->setDefaultValue(UI_CUSTOM_COLORS_KEY, Val(readLegacyCustomColors()));
7294
settings()->setDefaultValue(UI_FOLLOW_SYSTEM_THEME_KEY, Val(false));
7395
settings()->setDefaultValue(UI_FONT_FAMILY_KEY, Val(defaultFontFamily()));
7496
settings()->setDefaultValue(UI_FONT_SIZE_KEY, Val(defaultFontSize()));
@@ -906,3 +928,27 @@ int UiConfiguration::tooltipDelay() const
906928
{
907929
return TOOLTIP_DELAY;
908930
}
931+
932+
std::vector<QColor> UiConfiguration::colorDialogCustomColors() const
933+
{
934+
const ValList colorVals = settings()->value(UI_CUSTOM_COLORS_KEY).toList();
935+
936+
std::vector<QColor> customColors;
937+
customColors.reserve(colorVals.size());
938+
for (const auto& colorVal : colorVals) {
939+
customColors.push_back(colorVal.toQColor());
940+
}
941+
942+
return customColors;
943+
}
944+
945+
void UiConfiguration::setColorDialogCustomColors(const std::vector<QColor>& customColors)
946+
{
947+
ValList colorVals;
948+
colorVals.reserve(customColors.size());
949+
for (const auto& color: customColors) {
950+
colorVals.emplace_back(color);
951+
}
952+
953+
settings()->setLocalValue(UI_CUSTOM_COLORS_KEY, Val(colorVals));
954+
}

src/framework/ui/internal/uiconfiguration.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class UiConfiguration : public IUiConfiguration, public Injectable, public async
131131

132132
int tooltipDelay() const override;
133133

134+
std::vector<QColor> colorDialogCustomColors() const override;
135+
void setColorDialogCustomColors(const std::vector<QColor>&) override;
136+
134137
private:
135138
void initThemes();
136139
void correctUserFontIfNeeded();

src/framework/ui/iuiconfiguration.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include <optional>
2626

27+
#include <QColor>
28+
2729
#include "modularity/imoduleinterface.h"
2830

2931
#include "global/types/retval.h"
@@ -124,5 +126,8 @@ class IUiConfiguration : MODULE_EXPORT_INTERFACE
124126
virtual int flickableMaxVelocity() const = 0;
125127

126128
virtual int tooltipDelay() const = 0;
129+
130+
virtual std::vector<QColor> colorDialogCustomColors() const = 0;
131+
virtual void setColorDialogCustomColors(const std::vector<QColor>&) = 0;
127132
};
128133
}

src/framework/ui/tests/mocks/uiconfigurationmock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,8 @@ class UiConfigurationMock : public IUiConfiguration
108108
MOCK_METHOD(int, flickableMaxVelocity, (), (const, override));
109109

110110
MOCK_METHOD(int, tooltipDelay, (), (const, override));
111+
112+
MOCK_METHOD(std::vector<QColor>, colorDialogCustomColors, (), (const, override));
113+
MOCK_METHOD(void, setColorDialogCustomColors, (const std::vector<QColor>&), (override));
111114
};
112115
}

src/framework/ui/view/interactiveprovider.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ void InteractiveProvider::raiseWindowInStack(QObject* newActiveWindow)
7979
}
8080
}
8181

82+
static std::vector<QColor> getCustomColors()
83+
{
84+
const int customColorCount = QColorDialog::customCount();
85+
std::vector<QColor> customColors;
86+
customColors.reserve(customColorCount);
87+
for (int i = 0; i < customColorCount; ++i) {
88+
customColors.push_back(QColorDialog::customColor(i));
89+
}
90+
91+
return customColors;
92+
}
93+
94+
static void setCustomColors(const std::vector<QColor>& customColors)
95+
{
96+
const int customColorCount = std::min(QColorDialog::customCount(), static_cast<int>(customColors.size()));
97+
for (int i = 0; i < customColorCount; ++i) {
98+
QColorDialog::setCustomColor(i, customColors[i]);
99+
}
100+
}
101+
82102
async::Promise<Color> InteractiveProvider::selectColor(const Color& color, const std::string& title)
83103
{
84104
if (m_isSelectColorOpened) {
@@ -91,6 +111,8 @@ async::Promise<Color> InteractiveProvider::selectColor(const Color& color, const
91111

92112
m_isSelectColorOpened = true;
93113

114+
setCustomColors(config()->colorDialogCustomColors());
115+
94116
return async::make_promise<Color>([this, color, title](auto resolve, auto reject) {
95117
//! FIX https://github.com/musescore/MuseScore/issues/23208
96118
shortcutsRegister()->setActive(false);
@@ -105,6 +127,8 @@ async::Promise<Color> InteractiveProvider::selectColor(const Color& color, const
105127
QObject::connect(dlg, &QColorDialog::finished, [this, dlg, resolve, reject](int result) {
106128
dlg->deleteLater();
107129

130+
config()->setColorDialogCustomColors(getCustomColors());
131+
108132
m_isSelectColorOpened = false;
109133
shortcutsRegister()->setActive(true);
110134

src/framework/ui/view/interactiveprovider.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
* You should have received a copy of the GNU General Public License
2020
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2121
*/
22-
#ifndef MUSE_UI_INTERACTIVEPROVIDER_H
23-
#define MUSE_UI_INTERACTIVEPROVIDER_H
22+
#pragma once
2423

2524
#include <QObject>
2625
#include <QVariant>
@@ -30,6 +29,7 @@
3029
#include "global/async/asyncable.h"
3130

3231
#include "modularity/ioc.h"
32+
#include "ui/iuiconfiguration.h"
3333
#include "../iinteractiveprovider.h"
3434
#include "../iinteractiveuriregister.h"
3535
#include "../imainwindow.h"
@@ -56,6 +56,7 @@ class InteractiveProvider : public QObject, public IInteractiveProvider, public
5656
{
5757
Q_OBJECT
5858

59+
Inject<IUiConfiguration> config = { this };
5960
Inject<IInteractiveUriRegister> uriRegister = { this };
6061
Inject<IMainWindow> mainWindow = { this };
6162
Inject<muse::extensions::IExtensionsProvider> extensionsProvider = { this };
@@ -149,5 +150,3 @@ class InteractiveProvider : public QObject, public IInteractiveProvider, public
149150
bool m_isSelectColorOpened = false;
150151
};
151152
}
152-
153-
#endif // MUSE_UI_INTERACTIVEPROVIDER_H

0 commit comments

Comments
 (0)