| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2021 Mathias Wein <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "WGActionManager.h" |
| 8 |
|
| 9 |
#include "WGColorSelectorDock.h" |
| 10 |
#include "WGColorPatches.h" |
| 11 |
#include "WGColorPreviewToolTip.h" |
| 12 |
#include "WGConfigSelectorTypes.h" |
| 13 |
#include "WGMyPaintShadeSelector.h" |
| 14 |
#include "WGSelectorPopup.h" |
| 15 |
#include "WGSelectorWidgetBase.h" |
| 16 |
#include "WGShadeSelector.h" |
| 17 |
|
| 18 |
#include <kis_action.h> |
| 19 |
#include <kis_action_manager.h> |
| 20 |
#include <kis_canvas2.h> |
| 21 |
#include <kis_canvas_resource_provider.h> |
| 22 |
#include <kis_display_color_converter.h> |
| 23 |
#include <kis_signal_compressor.h> |
| 24 |
#include <KisViewManager.h> |
| 25 |
#include <KisVisualColorSelector.h> |
| 26 |
|
| 27 |
#include <QVector4D> |
| 28 |
|
| 29 |
WGActionManager::WGActionManager(WGColorSelectorDock *parentDock) |
| 30 |
: QObject(parentDock) |
| 31 |
, m_docker(parentDock) |
| 32 |
, m_displayConfig(new WGSelectorDisplayConfig) |
| 33 |
, m_colorTooltip(new WGColorPreviewToolTip) |
| 34 |
, m_colorChangeCompressor(new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this)) |
| 35 |
, m_colorModel(new KisVisualColorModel) |
| 36 |
{ |
| 37 |
m_lastUsedColor.setOpacity(quint8(0)); |
| 38 |
connect(m_colorChangeCompressor, SIGNAL(timeout()), SLOT(slotUpdateDocker())); |
| 39 |
connect(m_colorModel.data(), SIGNAL(sigChannelValuesChanged(QVector4D,quint32)), SLOT(slotChannelValuesChanged())); |
| 40 |
connect(WGConfig::notifier(), SIGNAL(configChanged()), SLOT(slotConfigChanged())); |
| 41 |
connect(WGConfig::notifier(), SIGNAL(selectorConfigChanged()), SLOT(slotSelectorConfigChanged())); |
| 42 |
slotConfigChanged(); |
| 43 |
} |
| 44 |
|
| 45 |
WGActionManager::~WGActionManager() |
| 46 |
{ |
| 47 |
delete m_colorSelectorPopup; |
| 48 |
delete m_shadeSelectorPopup; |
| 49 |
delete m_myPaintSelectorPopup; |
| 50 |
delete m_colorHistoryPopup; |
| 51 |
} |
| 52 |
|
| 53 |
void WGActionManager::setCanvas(KisCanvas2 *canvas, KisCanvas2 *oldCanvas) |
| 54 |
{ |
| 55 |
Q_UNUSED(oldCanvas); |
| 56 |
KisDisplayColorConverter *converter = canvas ? canvas->displayColorConverter() : 0; |
| 57 |
m_displayConfig->setDisplayConverter(converter); |
| 58 |
if (m_colorSelector) { |
| 59 |
m_colorSelector->setDisplayRenderer(m_displayConfig->displayConverter()->displayRendererInterface()); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void WGActionManager::registerActions(KisViewManager *viewManager) |
| 64 |
{ |
| 65 |
KisActionManager *actionManager = viewManager->actionManager(); |
| 66 |
KisAction *action; |
| 67 |
action = actionManager->createAction("show_wg_color_selector"); |
| 68 |
connect(action, SIGNAL(triggered()), SLOT(slotShowColorSelectorPopup())); |
| 69 |
action = actionManager->createAction("show_wg_shade_selector"); |
| 70 |
connect(action, SIGNAL(triggered()), SLOT(slotShowShadeSelectorPopup())); |
| 71 |
action = actionManager->createAction("show_wg_mypaint_selector"); |
| 72 |
connect(action, SIGNAL(triggered()), SLOT(slotShowMyPaintSelectorPopup())); |
| 73 |
action = actionManager->createAction("show_wg_color_history"); |
| 74 |
connect(action, SIGNAL(triggered()), SLOT(slotShowColorHistoryPopup())); |
| 75 |
action = actionManager->createAction("wgcs_lighten_color"); |
| 76 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotIncreaseLightness())); |
| 77 |
action = actionManager->createAction("wgcs_darken_color"); |
| 78 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotDecreaseLightness())); |
| 79 |
action = actionManager->createAction("wgcs_increase_saturation"); |
| 80 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotIncreaseSaturation())); |
| 81 |
action = actionManager->createAction("wgcs_decrease_saturation"); |
| 82 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotDecreaseSaturation())); |
| 83 |
action = actionManager->createAction("wgcs_shift_hue_clockwise"); |
| 84 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotShiftHueCW())); |
| 85 |
action = actionManager->createAction("wgcs_shift_hue_counterclockwise"); |
| 86 |
connect(action, SIGNAL(triggered(bool)), SLOT(slotShiftHueCCW())); |
| 87 |
} |
| 88 |
|
| 89 |
void WGActionManager::setLastUsedColor(const KoColor &col) |
| 90 |
{ |
| 91 |
m_lastUsedColor = col; |
| 92 |
} |
| 93 |
|
| 94 |
void WGActionManager::updateWidgetSize(QWidget *widget, int size) |
| 95 |
{ |
| 96 |
QSizePolicy hsp = widget->sizePolicy(); |
| 97 |
if (hsp.horizontalPolicy() != QSizePolicy::Fixed) { |
| 98 |
widget->setFixedWidth(size); |
| 99 |
} else { |
| 100 |
widget->setFixedWidth(QWIDGETSIZE_MAX); // looks weird, but that really resets min size to 0... |
| 101 |
} |
| 102 |
if (hsp.verticalPolicy() != QSizePolicy::Fixed) { |
| 103 |
widget->setFixedHeight(size); |
| 104 |
} else { |
| 105 |
widget->setFixedHeight(QWIDGETSIZE_MAX); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void WGActionManager::showPopup(WGSelectorPopup *popup) |
| 110 |
{ |
| 111 |
// preparations |
| 112 |
m_isSynchronizing = true; |
| 113 |
if (m_currentPopup) { |
| 114 |
m_currentPopup->hide(); |
| 115 |
m_currentPopup = 0; |
| 116 |
} |
| 117 |
const KisVisualColorModel &dockerModel = m_docker->colorModel(); |
| 118 |
m_colorModel->copyState(dockerModel); |
| 119 |
m_colorTooltip->setLastUsedColor(m_displayConfig->displayConverter()->toQColor(m_lastUsedColor)); |
| 120 |
QColor baseCol = m_displayConfig->displayConverter()->toQColor(m_colorModel->currentColor()); |
| 121 |
m_colorTooltip->setCurrentColor(baseCol); |
| 122 |
m_colorTooltip->setPreviousColor(baseCol); |
| 123 |
m_isSynchronizing = false; |
| 124 |
|
| 125 |
m_currentPopup = popup; |
| 126 |
popup->slotShowPopup(); |
| 127 |
m_colorTooltip->show(popup); |
| 128 |
} |
| 129 |
|
| 130 |
void WGActionManager::loadColorSelectorSettings(WGConfig::Accessor &cfg) |
| 131 |
{ |
| 132 |
m_colorSelector->setRenderMode(cfg.get(WGConfig::selectorRenderMode)); |
| 133 |
slotSelectorConfigChanged(); |
| 134 |
} |
| 135 |
|
| 136 |
void WGActionManager::modifyHSX(int channel, float amount) |
| 137 |
{ |
| 138 |
if (channel < 0 || channel > 2) { |
| 139 |
return; |
| 140 |
} |
| 141 |
if (m_docker->colorModel().isHSXModel()) { |
| 142 |
QVector4D channelValues = m_docker->colorModel().channelValues(); |
| 143 |
channelValues[channel] = qBound(0.0f, channelValues[channel] + amount, 1.0f); |
| 144 |
m_docker->setChannelValues(channelValues); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
void WGActionManager::slotConfigChanged() |
| 149 |
{ |
| 150 |
WGConfig::Accessor cfg; |
| 151 |
int popupSize = cfg.get(WGConfig::popupSize); |
| 152 |
bool proofColors = cfg.get(WGConfig::proofToPaintingColors); |
| 153 |
m_displayConfig->setPreviewInPaintingCS(proofColors); |
| 154 |
|
| 155 |
if (m_colorSelector) { |
| 156 |
loadColorSelectorSettings(cfg); |
| 157 |
m_colorSelector->setProofColors(proofColors); |
| 158 |
updateWidgetSize(m_colorSelector, popupSize); |
| 159 |
} |
| 160 |
if (m_shadeSelector) { |
| 161 |
m_shadeSelector->updateSettings(); |
| 162 |
updateWidgetSize(m_shadeSelector, popupSize); |
| 163 |
} |
| 164 |
if (m_myPaintSelector) { |
| 165 |
m_myPaintSelector->updateSettings(); |
| 166 |
updateWidgetSize(m_myPaintSelector, popupSize); |
| 167 |
} |
| 168 |
if (m_colorHistoryPopup) { |
| 169 |
WGSelectorWidgetBase *selector = m_colorHistoryPopup->selectorWidget(); |
| 170 |
KIS_ASSERT(selector); |
| 171 |
selector->updateSettings(); |
| 172 |
updateWidgetSize(selector, popupSize); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void WGActionManager::slotSelectorConfigChanged() |
| 177 |
{ |
| 178 |
if (m_colorSelector) { |
| 179 |
WGConfig::Accessor cfg; |
| 180 |
KisColorSelectorConfiguration selectorConf = cfg.colorSelectorConfiguration(); |
| 181 |
m_colorSelector->setConfiguration(&selectorConf); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void WGActionManager::slotPopupClosed(WGSelectorPopup *popup) |
| 186 |
{ |
| 187 |
if (popup == m_currentPopup) { |
| 188 |
m_currentPopup = 0; |
| 189 |
m_colorTooltip->hide(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void WGActionManager::slotShowColorSelectorPopup() |
| 194 |
{ |
| 195 |
if (!m_colorSelectorPopup) { |
| 196 |
WGConfig::Accessor cfg; |
| 197 |
m_colorSelectorPopup = new WGSelectorPopup(); |
| 198 |
m_colorSelector = new KisVisualColorSelector(m_colorSelectorPopup, m_colorModel); |
| 199 |
m_colorSelector->setDisplayRenderer(m_displayConfig->displayConverter()->displayRendererInterface()); |
| 200 |
updateWidgetSize(m_colorSelector, cfg.get(WGConfig::popupSize)); |
| 201 |
m_colorSelectorPopup->setSelectorWidget(m_colorSelector); |
| 202 |
connect(m_colorSelectorPopup, SIGNAL(sigPopupClosed(WGSelectorPopup*)), |
| 203 |
SLOT(slotPopupClosed(WGSelectorPopup*))); |
| 204 |
connect(m_colorSelector, SIGNAL(sigInteraction(bool)), SLOT(slotColorInteraction(bool))); |
| 205 |
|
| 206 |
loadColorSelectorSettings(cfg); |
| 207 |
} |
| 208 |
|
| 209 |
// update gamut mask |
| 210 |
KisCanvas2 *canvas = qobject_cast<KisCanvas2*>(m_docker->observedCanvas()); |
| 211 |
if (canvas) { |
| 212 |
KisCanvasResourceProvider *resourceProvider = canvas->imageView()->resourceProvider(); |
| 213 |
if (resourceProvider->gamutMaskActive()) { |
| 214 |
m_colorSelector->slotGamutMaskChanged(resourceProvider->currentGamutMask()); |
| 215 |
} |
| 216 |
else { |
| 217 |
m_colorSelector->slotGamutMaskUnset(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
showPopup(m_colorSelectorPopup); |
| 222 |
} |
| 223 |
|
| 224 |
void WGActionManager::slotShowShadeSelectorPopup() |
| 225 |
{ |
| 226 |
if (!m_shadeSelectorPopup) { |
| 227 |
m_shadeSelectorPopup = new WGSelectorPopup(); |
| 228 |
m_shadeSelector = new WGShadeSelector(m_displayConfig, m_colorModel, m_shadeSelectorPopup); |
| 229 |
m_shadeSelector->updateSettings(); |
| 230 |
updateWidgetSize(m_shadeSelector, WGConfig::Accessor().get(WGConfig::popupSize)); |
| 231 |
m_shadeSelectorPopup->setSelectorWidget(m_shadeSelector); |
| 232 |
connect(m_shadeSelectorPopup, SIGNAL(sigPopupClosed(WGSelectorPopup*)), |
| 233 |
SLOT(slotPopupClosed(WGSelectorPopup*))); |
| 234 |
connect(m_shadeSelector, SIGNAL(sigColorInteraction(bool)), SLOT(slotColorInteraction(bool))); |
| 235 |
} |
| 236 |
|
| 237 |
showPopup(m_shadeSelectorPopup); |
| 238 |
} |
| 239 |
|
| 240 |
void WGActionManager::slotShowMyPaintSelectorPopup() |
| 241 |
{ |
| 242 |
if (!m_myPaintSelectorPopup) { |
| 243 |
m_myPaintSelectorPopup = new WGSelectorPopup(); |
| 244 |
m_myPaintSelector = new WGMyPaintShadeSelector(m_displayConfig, m_myPaintSelectorPopup, |
| 245 |
WGSelectorWidgetBase::PopupMode); |
| 246 |
updateWidgetSize(m_myPaintSelector, WGConfig::Accessor().get(WGConfig::popupSize)); |
| 247 |
m_myPaintSelector->setModel(m_colorModel); |
| 248 |
m_myPaintSelectorPopup->setSelectorWidget(m_myPaintSelector); |
| 249 |
connect(m_myPaintSelectorPopup, SIGNAL(sigPopupClosed(WGSelectorPopup*)), |
| 250 |
SLOT(slotPopupClosed(WGSelectorPopup*))); |
| 251 |
connect(m_myPaintSelector, SIGNAL(sigColorInteraction(bool)), SLOT(slotColorInteraction(bool))); |
| 252 |
} |
| 253 |
|
| 254 |
showPopup(m_myPaintSelectorPopup); |
| 255 |
} |
| 256 |
|
| 257 |
void WGActionManager::slotShowColorHistoryPopup() |
| 258 |
{ |
| 259 |
if (!m_colorHistoryPopup) { |
| 260 |
m_colorHistoryPopup = new WGSelectorPopup; |
| 261 |
WGColorPatches *history = new WGColorPatches(m_displayConfig, m_docker->colorHistory()); |
| 262 |
history->setUiMode(WGSelectorWidgetBase::PopupMode); |
| 263 |
history->setPreset(WGColorPatches::History); |
| 264 |
history->updateSettings(); |
| 265 |
updateWidgetSize(history, WGConfig::Accessor().get(WGConfig::popupSize)); |
| 266 |
m_colorHistoryPopup->setSelectorWidget(history); |
| 267 |
connect(m_colorHistoryPopup, SIGNAL(sigPopupClosed(WGSelectorPopup*)), |
| 268 |
SLOT(slotPopupClosed(WGSelectorPopup*))); |
| 269 |
connect(history, SIGNAL(sigColorInteraction(bool)), SLOT(slotColorPatchInteraction(bool))); |
| 270 |
connect(history, SIGNAL(sigColorChanged(KoColor)), SLOT(slotColorSelected(KoColor))); |
| 271 |
} |
| 272 |
showPopup(m_colorHistoryPopup); |
| 273 |
} |
| 274 |
|
| 275 |
void WGActionManager::slotIncreaseLightness() |
| 276 |
{ |
| 277 |
modifyHSX(2, 0.1f); |
| 278 |
} |
| 279 |
|
| 280 |
void WGActionManager::slotDecreaseLightness() |
| 281 |
{ |
| 282 |
modifyHSX(2, -0.1f); |
| 283 |
} |
| 284 |
|
| 285 |
void WGActionManager::slotIncreaseSaturation() |
| 286 |
{ |
| 287 |
modifyHSX(1, 0.1f); |
| 288 |
} |
| 289 |
|
| 290 |
void WGActionManager::slotDecreaseSaturation() |
| 291 |
{ |
| 292 |
modifyHSX(1, -0.1f); |
| 293 |
} |
| 294 |
|
| 295 |
void WGActionManager::slotShiftHueCW() |
| 296 |
{ |
| 297 |
modifyHSX(0, 0.1f); |
| 298 |
} |
| 299 |
|
| 300 |
void WGActionManager::slotShiftHueCCW() |
| 301 |
{ |
| 302 |
modifyHSX(0, -0.1f); |
| 303 |
} |
| 304 |
|
| 305 |
void WGActionManager::slotChannelValuesChanged() |
| 306 |
{ |
| 307 |
// FIXME: KoColorDisplayRendererInterface's displayConfigurationChanged() |
| 308 |
// signal (e.g. layer switches) makes the color model Q_EMIT new channel values |
| 309 |
// and this would overwrite the color resources with outdated data! |
| 310 |
// so make sure a popup is actually active |
| 311 |
if (!m_isSynchronizing && m_currentPopup) { |
| 312 |
m_colorChangeCompressor->start(); |
| 313 |
QColor color = m_displayConfig->displayConverter()->toQColor(m_colorModel->currentColor()); |
| 314 |
m_colorTooltip->setCurrentColor(color); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
void WGActionManager::slotColorInteraction(bool active) |
| 319 |
{ |
| 320 |
if (active) { |
| 321 |
QColor baseCol = m_displayConfig->displayConverter()->toQColor(m_colorModel->currentColor()); |
| 322 |
m_colorTooltip->setCurrentColor(baseCol); |
| 323 |
m_colorTooltip->setPreviousColor(baseCol); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
void WGActionManager::slotColorPatchInteraction(bool active) |
| 328 |
{ |
| 329 |
KoCanvasBase *canvas = m_docker->observedCanvas(); |
| 330 |
if (active && canvas) { |
| 331 |
KoColor fgColor = canvas->resourceManager()->foregroundColor(); |
| 332 |
QColor baseCol = m_docker->displayColorConverter()->toQColor(fgColor); |
| 333 |
m_colorTooltip->setCurrentColor(baseCol); |
| 334 |
m_colorTooltip->setPreviousColor(baseCol); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/* Directly set canvas resource for color history, |
| 339 |
*/ |
| 340 |
void WGActionManager::slotColorSelected(const KoColor &color) |
| 341 |
{ |
| 342 |
// CAVEAT: currently, the color history does not allow background color setting |
| 343 |
if (!m_docker->observedCanvas()) { |
| 344 |
return; |
| 345 |
} |
| 346 |
m_docker->observedCanvas()->resourceManager()->setForegroundColor(color); |
| 347 |
QColor previewCol = m_docker->displayColorConverter()->toQColor(color); |
| 348 |
m_colorTooltip->setCurrentColor(previewCol); |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
void WGActionManager::slotUpdateDocker() |
| 353 |
{ |
| 354 |
m_docker->setChannelValues(m_colorModel->channelValues()); |
| 355 |
} |