| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020 Mathias Wein <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "WGConfigSelectorTypes.h" |
| 8 |
|
| 9 |
#include <kconfig.h> |
| 10 |
#include <kis_debug.h> |
| 11 |
#include <KoColorProfile.h> |
| 12 |
#include <KoColorSpaceRegistry.h> |
| 13 |
|
| 14 |
#include <QApplication> |
| 15 |
#include <QGlobalStatic> |
| 16 |
#include <QStringList> |
| 17 |
#include <QTextStream> |
| 18 |
#include <QThread> |
| 19 |
|
| 20 |
namespace WGConfig { |
| 21 |
|
| 22 |
Q_GLOBAL_STATIC(WGConfigNotifier, s_notifier_instance) |
| 23 |
|
| 24 |
WGConfig::WGConfig(bool readOnly) |
| 25 |
: m_cfg( KSharedConfig::openConfig()->group(configGroupName())) |
| 26 |
, m_readOnly(readOnly) |
| 27 |
{ |
| 28 |
if (!readOnly) { |
| 29 |
KIS_SAFE_ASSERT_RECOVER_RETURN(qApp && qApp->thread() == QThread::currentThread()); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
WGConfig::~WGConfig() |
| 34 |
{ |
| 35 |
if (m_readOnly) return; |
| 36 |
|
| 37 |
if (qApp && qApp->thread() != QThread::currentThread()) { |
| 38 |
dbgKrita << "WARNING: WGConfig: requested config synchronization from nonGUI thread! Called from:" << kisBacktrace(); |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
m_cfg.sync(); |
| 43 |
} |
| 44 |
|
| 45 |
QString WGConfig::configGroupName() |
| 46 |
{ |
| 47 |
return QStringLiteral("WideGamutColorSelector"); |
| 48 |
} |
| 49 |
|
| 50 |
KisColorSelectorConfiguration WGConfig::colorSelectorConfiguration() const |
| 51 |
{ |
| 52 |
QString config = m_cfg.readEntry("colorSelectorConfiguration", QString()); |
| 53 |
return config.isEmpty() ? defaultColorSelectorConfiguration : KisColorSelectorConfiguration(config); |
| 54 |
} |
| 55 |
|
| 56 |
void WGConfig::setColorSelectorConfiguration(const KisColorSelectorConfiguration &config) |
| 57 |
{ |
| 58 |
m_cfg.writeEntry("colorSelectorConfiguration", config.toString()); |
| 59 |
} |
| 60 |
|
| 61 |
QVector<KisColorSelectorConfiguration> WGConfig::favoriteConfigurations(bool defaultValue) const |
| 62 |
{ |
| 63 |
if (defaultValue) { |
| 64 |
return defaultFavoriteConfigurations(); |
| 65 |
} |
| 66 |
|
| 67 |
QVector<KisColorSelectorConfiguration> favoriteConfigs; |
| 68 |
QString favorites = m_cfg.readEntry("favoriteSelectorConfigurations", QString()); |
| 69 |
if (favorites.isEmpty()) { |
| 70 |
return defaultFavoriteConfigurations(); |
| 71 |
} |
| 72 |
QStringList favoriteList(favorites.split(';')); |
| 73 |
for (QString &fav: favoriteList) { |
| 74 |
favoriteConfigs.append(KisColorSelectorConfiguration(fav)); |
| 75 |
} |
| 76 |
return favoriteConfigs; |
| 77 |
} |
| 78 |
|
| 79 |
QVector<KisColorSelectorConfiguration> WGConfig::defaultFavoriteConfigurations() |
| 80 |
{ |
| 81 |
using KCSC = KisColorSelectorConfiguration; |
| 82 |
QVector<KCSC> defaults; |
| 83 |
defaults.append(KCSC(KCSC::Triangle, KCSC::Ring, KCSC::SV, KCSC::H)); |
| 84 |
defaults.append(KCSC(KCSC::Square, KCSC::Ring, KCSC::SV, KCSC::H)); |
| 85 |
defaults.append(KCSC(KCSC::Wheel, KCSC::Slider, KCSC::VH, KCSC::hsvS)); |
| 86 |
return defaults; |
| 87 |
} |
| 88 |
|
| 89 |
void WGConfig::setFavoriteConfigurations(const QVector<KisColorSelectorConfiguration> &favoriteConfigs) |
| 90 |
{ |
| 91 |
QStringList favoriteList; |
| 92 |
for (const KisColorSelectorConfiguration &fav: favoriteConfigs) { |
| 93 |
favoriteList.append(fav.toString()); |
| 94 |
} |
| 95 |
m_cfg.writeEntry("favoriteSelectorConfigurations", favoriteList.join(';')); |
| 96 |
} |
| 97 |
|
| 98 |
QVector<ShadeLine> WGConfig::defaultShadeSelectorLines() |
| 99 |
{ |
| 100 |
QVector<ShadeLine> defaultLines; |
| 101 |
defaultLines.append(ShadeLine(QVector4D(0.3, 0, 0, 0))); |
| 102 |
defaultLines.append(ShadeLine(QVector4D(0, -0.5, 0, 0))); |
| 103 |
defaultLines.append(ShadeLine(QVector4D(0, 0, 0.5, 0))); |
| 104 |
defaultLines.append(ShadeLine(QVector4D(0, -0.2, 0.2, 0))); |
| 105 |
return defaultLines; |
| 106 |
} |
| 107 |
|
| 108 |
QVector<ShadeLine> WGConfig::shadeSelectorLines(bool defaultValue) const |
| 109 |
{ |
| 110 |
if (defaultValue) { |
| 111 |
return defaultShadeSelectorLines(); |
| 112 |
} |
| 113 |
|
| 114 |
QString configString = m_cfg.readEntry("minimalShadeSelectorLines", QString()); |
| 115 |
if (configString.isEmpty()) { |
| 116 |
return defaultShadeSelectorLines(); |
| 117 |
} |
| 118 |
QVector<ShadeLine> shadeLines; |
| 119 |
QStringList shadeLineList(configString.split('|')); |
| 120 |
for (const QString &line: std::as_const(shadeLineList)) { |
| 121 |
QVector4D gradient, offset; |
| 122 |
int patchCount = -1; |
| 123 |
QStringList values = line.split(';'); |
| 124 |
if (values.size() >= 4) { |
| 125 |
for (int i = 0; i < 4; i++) { |
| 126 |
gradient[i] = qBound(-1.0f, values.at(i).toFloat(), 1.0f); |
| 127 |
} |
| 128 |
} |
| 129 |
if (values.size() >= 8) { |
| 130 |
for (int i = 0; i < 4; i++) { |
| 131 |
offset[i] = qBound(-1.0f, values.at(i + 4).toFloat(), 1.0f); |
| 132 |
} |
| 133 |
} |
| 134 |
if (values.size() >= 9) { |
| 135 |
patchCount = qBound(-1, values.at(8).toInt(), 99); |
| 136 |
} |
| 137 |
shadeLines.append(ShadeLine(gradient, offset, patchCount)); |
| 138 |
} |
| 139 |
return shadeLines; |
| 140 |
} |
| 141 |
|
| 142 |
void WGConfig::setShadeSelectorLines(const QVector<ShadeLine> &shadeLines) |
| 143 |
{ |
| 144 |
QStringList shadeLineList; |
| 145 |
for (const ShadeLine &line: shadeLines) { |
| 146 |
QString lineString; |
| 147 |
QTextStream stream(&lineString); |
| 148 |
for (int i = 0; i < 4; i++) { |
| 149 |
stream << line.gradient[i] << ';'; |
| 150 |
} |
| 151 |
for (int i = 0; i < 4; i++) { |
| 152 |
stream << line.offset[i] << ';'; |
| 153 |
} |
| 154 |
stream << line.patchCount; |
| 155 |
shadeLineList.append(lineString); |
| 156 |
} |
| 157 |
m_cfg.writeEntry("minimalShadeSelectorLines", shadeLineList.join('|')); |
| 158 |
} |
| 159 |
|
| 160 |
const KoColorSpace* WGConfig::customSelectionColorSpace(bool defaultValue) const |
| 161 |
{ |
| 162 |
const KoColorSpace *cs = 0; |
| 163 |
|
| 164 |
if (!defaultValue) { |
| 165 |
QString modelID = m_cfg.readEntry("customColorSpaceModel", "RGBA"); |
| 166 |
QString depthID = m_cfg.readEntry("customColorSpaceDepthID", "U8"); |
| 167 |
QString profile = m_cfg.readEntry("customColorSpaceProfile", ""); |
| 168 |
|
| 169 |
cs = KoColorSpaceRegistry::instance()->colorSpace(modelID, depthID, profile); |
| 170 |
} |
| 171 |
|
| 172 |
if (!cs) { |
| 173 |
cs = KoColorSpaceRegistry::instance()->rgb8(); |
| 174 |
} |
| 175 |
|
| 176 |
return cs; |
| 177 |
} |
| 178 |
|
| 179 |
void WGConfig::setCustomSelectionColorSpace(const KoColorSpace *cs) |
| 180 |
{ |
| 181 |
if(cs) { |
| 182 |
m_cfg.writeEntry("customColorSpaceModel", cs->colorModelId().id()); |
| 183 |
m_cfg.writeEntry("customColorSpaceDepthID", cs->colorDepthId().id()); |
| 184 |
if (cs->profile()) { |
| 185 |
m_cfg.writeEntry("customColorSpaceProfile", cs->profile()->name()); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
WGConfigNotifier *notifier() |
| 191 |
{ |
| 192 |
return s_notifier_instance; |
| 193 |
} |
| 194 |
|
| 195 |
const KisColorSelectorConfiguration WGConfig::defaultColorSelectorConfiguration = |
| 196 |
KisColorSelectorConfiguration(KisColorSelectorConfiguration::Triangle, |
| 197 |
KisColorSelectorConfiguration::Ring, |
| 198 |
KisColorSelectorConfiguration::SV, |
| 199 |
KisColorSelectorConfiguration::H); |
| 200 |
|
| 201 |
void WGConfigNotifier::notifyConfigChanged() |
| 202 |
{ |
| 203 |
Q_EMIT configChanged(); |
| 204 |
} |
| 205 |
|
| 206 |
void WGConfigNotifier::notifySelectorConfigChanged() |
| 207 |
{ |
| 208 |
Q_EMIT selectorConfigChanged(); |
| 209 |
} |
| 210 |
|
| 211 |
// ======== Static Configuration Object Instantiation ======== |
| 212 |
|
| 213 |
const ColorPatches colorHistory { |
| 214 |
{ "colorHistory.orientation", Qt::Horizontal, Qt::Horizontal, Qt::Vertical, true }, |
| 215 |
{ "colorHistory.patchSize", QSize(16,16), QSize(10,10), QSize(99,99), true }, |
| 216 |
{ "colorHistory.maxCount", 30, 2, 200, true }, |
| 217 |
{ "colorHistory.rows", 1, 1, 20, true }, |
| 218 |
{ "colorHistory.scrolling", ScrollLongitudinal, ScrollNone, ScrollLaterally, true } |
| 219 |
}; |
| 220 |
|
| 221 |
const ColorPatches commonColors { |
| 222 |
{ "commonColors.orientation", Qt::Horizontal, Qt::Horizontal, Qt::Vertical, true }, |
| 223 |
{ "commonColors.patchSize", QSize(16,16), QSize(10,10), QSize(99,99), true }, |
| 224 |
{ "commonColors.maxCount", 20, 2, 200, true }, |
| 225 |
{ "commonColors.rows", 1, 1, 20, true }, |
| 226 |
{ "commonColors.scrolling", ScrollLongitudinal, ScrollNone, ScrollLaterally, true } |
| 227 |
}; |
| 228 |
|
| 229 |
const ColorPatches popupPatches { |
| 230 |
{ "popupColorPatchOrientation", Qt::Horizontal, Qt::Horizontal, Qt::Vertical, true }, |
| 231 |
{ "popupColorPatchSize", QSize(32,32), QSize(10,10), QSize(99,99), true }, |
| 232 |
{ "popupPatches.maxCount", 30, 2, 200, true }, |
| 233 |
{ "popupPatches.rows", 1, 1, 20, true }, |
| 234 |
{ "popupPatches.scrolling", ScrollLongitudinal, ScrollNone, ScrollLaterally, true } |
| 235 |
}; |
| 236 |
|
| 237 |
const GenericSetting<bool> proofToPaintingColors {"proofToPaintingColors", false}; |
| 238 |
const GenericSetting<bool> colorHistoryEnabled {"colorHistoryEnabled", true}; |
| 239 |
const GenericSetting<bool> commonColorsEnabled {"commonColorsEnabled", true}; |
| 240 |
const GenericSetting<bool> colorHistoryShowClearButton { "colorHistoryShowClearButton", false }; |
| 241 |
const GenericSetting<bool> commonColorsAutoUpdate { "commonColorsAutoUpdate", false }; |
| 242 |
|
| 243 |
const GenericSetting<bool> quickSettingsEnabled { "quickSettingsMenuEnabled", true }; |
| 244 |
const NumericSetting<int> popupSize { "popupSize", 300, 100, 500, true }; |
| 245 |
|
| 246 |
const GenericSetting<bool> minimalShadeSelectorEnabled {"minimalShadeSelectorEnabled", true}; |
| 247 |
const NumericSetting<int> shadeSelectorLineHeight { "shadeSelectorLineHeight", 10, 8, 99 }; |
| 248 |
const GenericSetting<bool> shadeSelectorUpdateOnExternalChanges { "shadeSelectorUpdateOnExternalChanges", true }; |
| 249 |
const GenericSetting<bool> shadeSelectorUpdateOnInteractionEnd { "shadeSelectorUpdateOnInteractionEnd", false }; |
| 250 |
const GenericSetting<bool> shadeSelectorUpdateOnRightClick { "shadeSelectorUpdateOnRightClick", true }; |
| 251 |
|
| 252 |
const NumericSetting<KisVisualColorModel::ColorModel> rgbColorModel { |
| 253 |
"rgbColorModel", KisVisualColorModel::HSV, KisVisualColorModel::HSV, KisVisualColorModel::HSY, true |
| 254 |
}; |
| 255 |
const NumericSetting<KisVisualColorSelector::RenderMode> selectorRenderMode { |
| 256 |
"renderMode", KisVisualColorSelector::DynamicBackground, KisVisualColorSelector::StaticBackground, KisVisualColorSelector::CompositeBackground, true |
| 257 |
}; |
| 258 |
|
| 259 |
} // namespace WGConfig |