kis_dlg_internal_color_selector.cpp 9.14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) Wolthera van Hovell tot Westerflier <griffinvalley@gmail.com>, (C) 2016
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <QList>
#include <QAbstractSpinBox>
#include <QSpinBox>
#include <QDoubleSpinBox>
23
#include <QPointer>
24 25

#include "KoColorSpaceRegistry.h"
26 27 28
#include <KoColorSet.h>
#include <KoResourceServerProvider.h>
#include <KoResourceServer.h>
29 30

#include "kis_signal_compressor.h"
31
#include "KisViewManager.h"
32
#include "KoColorDisplayRendererInterface.h"
33

34 35 36
#include "kis_spinbox_color_selector.h"

#include "kis_internal_color_selector.h"
37
#include "ui_wdgdlginternalcolorselector.h"
38
#include "kis_config.h"
39
#include "kis_config_notifier.h"
40
#include "kis_color_input.h"
41

42
struct KisDlgInternalColorSelector::Private
43 44 45
{
    bool allowUpdates = true;
    KoColor currentColor;
46
    KoColor previousColor;
47
    KoColor sRGB = KoColor(KoColorSpaceRegistry::instance()->rgb8());
48
    const KoColorSpace *currentColorSpace;
49
    bool lockUsedCS = false;
50
    bool chooseAlpha = false;
51
    KisSignalCompressor *compressColorChanges;
52
    const KoColorDisplayRendererInterface *displayRenderer;
53
    KisHexColorInput *hexColorInput;
54 55
};

56
KisDlgInternalColorSelector::KisDlgInternalColorSelector(QWidget *parent, KoColor color, Config config, const QString &caption, const KoColorDisplayRendererInterface *displayRenderer)
57 58 59
    : QDialog(parent)
     ,m_d(new Private)
{
60
    setModal(config.modal);
61
    this->setFocusPolicy(Qt::ClickFocus);
62 63 64
    m_ui = new Ui_WdgDlgInternalColorSelector();
    m_ui->setupUi(this);

65 66
    setWindowTitle(caption);

67
    m_d->currentColor = color;
68
    m_d->currentColorSpace = m_d->currentColor.colorSpace();
69
    m_d->displayRenderer = displayRenderer;
70 71 72

    m_ui->spinboxselector->slotSetColor(color);
    connect(m_ui->spinboxselector, SIGNAL(sigNewColor(KoColor)), this, SLOT(slotColorUpdated(KoColor)));
73

74
    m_ui->visualSelector->slotSetColor(color);
75
    m_ui->visualSelector->setDisplayRenderer(displayRenderer);
76
    m_ui->visualSelector->setConfig(false, config.modal);
77
    if (config.visualColorSelector) {
78 79
        connect(m_ui->visualSelector, SIGNAL(sigNewColor(KoColor)), this, SLOT(slotColorUpdated(KoColor)));
        connect(KisConfigNotifier::instance(), SIGNAL(configChanged()), m_ui->visualSelector, SLOT(ConfigurationChanged()));
80 81 82 83 84 85 86 87
    } else {
        m_ui->visualSelector->hide();
    }
    if (config.screenColorPicker) {
        connect(m_ui->screenColorPicker, SIGNAL(sigNewColorPicked(KoColor)),this, SLOT(slotColorUpdated(KoColor)));
    } else {
        m_ui->screenColorPicker->hide();
    }
88
    //TODO: Add disable signal as well. Might be not necessary...?
89 90 91 92 93 94 95 96 97 98 99 100
    KisConfig cfg;
    QString paletteName = cfg.readEntry("internal_selector_active_color_set", QString());
    KoResourceServer<KoColorSet>* rServer = KoResourceServerProvider::instance()->paletteServer(false);
    KoColorSet *savedPal = rServer->resourceByName(paletteName);
    if (savedPal) {
        m_ui->paletteBox->setColorSet(savedPal);
    } else {
        savedPal = rServer->resources().first();
        if (savedPal) {
            m_ui->paletteBox->setColorSet(savedPal);
        }
    }
101
    if (config.paletteBox) {
102 103
        connect(m_ui->paletteBox, SIGNAL(colorChanged(KoColor,bool)), this, SLOT(slotColorUpdated(KoColor)));
        m_ui->paletteBox->setDisplayRenderer(displayRenderer);
104 105 106
    } else {
        m_ui->paletteBox->hide();
    }
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    if (config.prevNextButtons) {
        m_ui->currentColor->setColor(m_d->currentColor);
        m_ui->currentColor->setDisplayRenderer(displayRenderer);
        m_ui->previousColor->setColor(m_d->currentColor);
        m_ui->previousColor->setDisplayRenderer(displayRenderer);
        connect(this, SIGNAL(accepted()), this, SLOT(setPreviousColor()));
        connect(m_ui->previousColor, SIGNAL(triggered(KoColorPatch*)), SLOT(slotSetColorFromPatch(KoColorPatch*)));
    } else {
        m_ui->currentColor->hide();
        m_ui->previousColor->hide();
    }

    if (config.hexInput) {
        m_d->sRGB.fromKoColor(m_d->currentColor);
        m_d->hexColorInput = new KisHexColorInput(this, &m_d->sRGB);
        m_d->hexColorInput->update();
        connect(m_d->hexColorInput, SIGNAL(updated()), SLOT(slotSetColorFromHex()));
125
        m_ui->rightPane->addWidget(m_d->hexColorInput);
126 127
        m_d->hexColorInput->setToolTip(i18n("This is a hexcode input, for webcolors. It can only get colors in the sRGB space."));
    }
128

129
    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
130
    m_d->compressColorChanges = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
131
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
132 133 134

    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
135 136
}

137
KisDlgInternalColorSelector::~KisDlgInternalColorSelector()
138
{
139
    delete m_ui;
140 141 142
    //TODO: Does the scoped pointer also need to be deleted???
}

143
void KisDlgInternalColorSelector::slotColorUpdated(KoColor newColor)
144 145
{
    //if the update did not come from this selector...
146
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
147 148 149 150 151 152
        if (m_d->lockUsedCS){
            newColor.convertTo(m_d->currentColorSpace);
            m_d->currentColor = newColor;
        } else {
            m_d->currentColor = newColor;
        }
153
        updateAllElements(QObject::sender());
154 155 156
    }
}

157
void KisDlgInternalColorSelector::colorSpaceChanged(const KoColorSpace *cs)
158 159 160 161 162 163
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
164
    m_ui->spinboxselector->slotSetColorSpace(m_d->currentColorSpace);
165 166 167
    m_ui->visualSelector->slotsetColorSpace(m_d->currentColorSpace);

}
168

169
void KisDlgInternalColorSelector::lockUsedColorSpace(const KoColorSpace *cs)
170 171 172
{
    colorSpaceChanged(cs);
    m_d->lockUsedCS = true;
173 174
}

175
void KisDlgInternalColorSelector::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer)
176 177 178 179
{
    if (displayRenderer) {
        m_d->displayRenderer = displayRenderer;
        m_ui->visualSelector->setDisplayRenderer(displayRenderer);
180 181 182
        m_ui->currentColor->setDisplayRenderer(displayRenderer);
        m_ui->previousColor->setDisplayRenderer(displayRenderer);
        m_ui->paletteBox->setDisplayRenderer(displayRenderer);
183 184 185 186 187
    } else {
        m_d->displayRenderer = KoDumbColorDisplayRenderer::instance();
    }
}

188
KoColor KisDlgInternalColorSelector::getModalColorDialog(const KoColor color, QWidget* parent, QString caption)
189
{
190
    Config config = Config();
191
    KisDlgInternalColorSelector dialog(parent, color, config, caption);
192 193 194 195
    dialog.exec();
    return dialog.getCurrentColor();
}

196
KoColor KisDlgInternalColorSelector::getCurrentColor()
197 198 199 200
{
    return m_d->currentColor;
}

201
void KisDlgInternalColorSelector::chooseAlpha(bool chooseAlpha)
202 203 204 205
{
    m_d->chooseAlpha = chooseAlpha;
}

206
void KisDlgInternalColorSelector::slotConfigurationChanged()
207 208 209 210 211
{
    //m_d->canvas->displayColorConverter()->
    //slotColorSpaceChanged(m_d->canvas->image()->colorSpace());
}

212
void KisDlgInternalColorSelector::slotLockSelector()
213 214 215 216
{
    m_d->allowUpdates = false;
}

217
void KisDlgInternalColorSelector::setPreviousColor()
218 219 220 221 222 223 224 225
{
    m_d->previousColor = m_d->currentColor;
    KisConfig cfg;
    if (m_ui->paletteBox->colorSet()) {
        cfg.writeEntry("internal_selector_active_color_set", m_ui->paletteBox->colorSet()->name());
    }
}

226
void KisDlgInternalColorSelector::updateAllElements(QObject *source)
227 228
{
    //update everything!!!
229 230
    if (source != m_ui->spinboxselector) {
        m_ui->spinboxselector->slotSetColor(m_d->currentColor);
231
    }
232 233 234
    if (source != m_ui->visualSelector) {
        m_ui->visualSelector->slotSetColor(m_d->currentColor);
    }
235

236 237 238 239 240
    if (source != m_d->hexColorInput) {
        m_d->sRGB.fromKoColor(m_d->currentColor);
        m_d->hexColorInput->update();
    }

241 242 243 244
    m_ui->previousColor->setColor(m_d->previousColor);

    m_ui->currentColor->setColor(m_d->currentColor);

245 246 247 248
    if (source != this->parent()) {
        emit(signalForegroundColorChosen(m_d->currentColor));
        m_d->compressColorChanges->start();
    }
249 250
}

251

252
void KisDlgInternalColorSelector::endUpdateWithNewColor()
253 254 255
{
    m_d->allowUpdates = true;
}
256

257
void KisDlgInternalColorSelector::focusInEvent(QFocusEvent *)
258
{
259
    //setPreviousColor();
260
}
261

262
void KisDlgInternalColorSelector::slotSetColorFromPatch(KoColorPatch* patch)
263 264
{
    slotColorUpdated(patch->color());
265
}
266

267
void KisDlgInternalColorSelector::slotSetColorFromHex()
268 269 270
{
    slotColorUpdated(m_d->sRGB);
}
271