kis_internal_color_selector.cpp 7.44 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 40 41 42 43

struct KisInternalColorSelector::Private
{
    bool allowUpdates = true;
    KoColor currentColor;
44
    KoColor previousColor;
45
    const KoColorSpace *currentColorSpace;
46
    bool chooseAlpha = false;
47
    KisSignalCompressor *compressColorChanges;
48
    const KoColorDisplayRendererInterface *displayRenderer;
49 50
};

51
KisInternalColorSelector::KisInternalColorSelector(QWidget *parent, KoColor color, bool modal, const QString &caption, const KoColorDisplayRendererInterface *displayRenderer)
52 53 54
    : QDialog(parent)
     ,m_d(new Private)
{
55
    setModal(modal);
56
    this->setFocusPolicy(Qt::ClickFocus);
57 58 59 60 61 62
    m_ui = new Ui_WdgDlgInternalColorSelector();
    m_ui->setupUi(this);
    if (!modal) {
        m_ui->buttonBox->hide();
    }

63 64
    setWindowTitle(caption);

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

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

72
    m_ui->visualSelector->slotSetColor(color);
73
    m_ui->visualSelector->setDisplayRenderer(displayRenderer);
74
    connect(m_ui->visualSelector, SIGNAL(sigNewColor(KoColor)), this, SLOT(slotColorUpdated(KoColor)));
75
    connect(m_ui->screenColorPicker, SIGNAL(sigNewColorPicked(KoColor)),this, SLOT(slotColorUpdated(KoColor)));
76
    //TODO: Add disable signal as well. Might be not necessary...?
77 78 79 80 81 82 83 84 85 86 87 88 89
    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);
        }
    }
    connect(m_ui->paletteBox, SIGNAL(colorChanged(KoColor,bool)), this, SLOT(slotColorUpdated(KoColor)));
90
    m_ui->paletteBox->setDisplayRenderer(displayRenderer);
91 92

    m_ui->currentColor->setColor(m_d->currentColor);
93
    m_ui->currentColor->setDisplayRenderer(displayRenderer);
94
    m_ui->currentColor->setFrameStyle(QFrame::StyledPanel);
95
    m_ui->previousColor->setColor(m_d->currentColor);
96
    m_ui->previousColor->setDisplayRenderer(displayRenderer);
97
    m_ui->previousColor->setFrameStyle(QFrame::StyledPanel);
98
    connect(this, SIGNAL(accepted()), this, SLOT(setPreviousColor()));
99
    connect(m_ui->previousColor, SIGNAL(triggered(KoColorPatch*)), SLOT(slotSetColorFromPatch(KoColorPatch*)));
100

101
    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
102
    m_d->compressColorChanges = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
103
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
104 105 106

    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
107 108 109 110
}

KisInternalColorSelector::~KisInternalColorSelector()
{
111
    delete m_ui;
112 113 114 115 116 117
    //TODO: Does the scoped pointer also need to be deleted???
}

void KisInternalColorSelector::slotColorUpdated(KoColor newColor)
{
    //if the update did not come from this selector...
118
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
119
        m_d->currentColor = newColor;
120
        updateAllElements(QObject::sender());
121 122 123
    }
}

124
void KisInternalColorSelector::colorSpaceChanged(const KoColorSpace *cs)
125 126 127 128 129 130
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
131
    m_ui->spinboxselector->slotSetColorSpace(m_d->currentColorSpace);
132 133 134

}

135 136 137 138 139
void KisInternalColorSelector::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer)
{
    if (displayRenderer) {
        m_d->displayRenderer = displayRenderer;
        m_ui->visualSelector->setDisplayRenderer(displayRenderer);
140 141 142
        m_ui->currentColor->setDisplayRenderer(displayRenderer);
        m_ui->previousColor->setDisplayRenderer(displayRenderer);
        m_ui->paletteBox->setDisplayRenderer(displayRenderer);
143 144 145 146 147
    } else {
        m_d->displayRenderer = KoDumbColorDisplayRenderer::instance();
    }
}

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
KoColor KisInternalColorSelector::getModalColorDialog(const KoColor color, bool chooseAlpha, QWidget* parent, QString caption)
{
    KisInternalColorSelector dialog(parent, color, true, caption);
    dialog.chooseAlpha(chooseAlpha);
    dialog.exec();
    return dialog.getCurrentColor();
}

KoColor KisInternalColorSelector::getCurrentColor()
{
    return m_d->currentColor;
}

void KisInternalColorSelector::chooseAlpha(bool chooseAlpha)
{
    m_d->chooseAlpha = chooseAlpha;
}

166 167 168 169 170 171 172 173 174 175 176
void KisInternalColorSelector::slotConfigurationChanged()
{
    //m_d->canvas->displayColorConverter()->
    //slotColorSpaceChanged(m_d->canvas->image()->colorSpace());
}

void KisInternalColorSelector::slotLockSelector()
{
    m_d->allowUpdates = false;
}

177 178 179 180 181 182 183 184 185
void KisInternalColorSelector::setPreviousColor()
{
    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());
    }
}

186
void KisInternalColorSelector::updateAllElements(QObject *source)
187 188
{
    //update everything!!!
189 190
    if (source != m_ui->spinboxselector) {
        m_ui->spinboxselector->slotSetColor(m_d->currentColor);
191
    }
192 193 194
    if (source != m_ui->visualSelector) {
        m_ui->visualSelector->slotSetColor(m_d->currentColor);
    }
195

196 197 198 199
    m_ui->previousColor->setColor(m_d->previousColor);

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

200 201 202 203
    if (source != this->parent()) {
        emit(signalForegroundColorChosen(m_d->currentColor));
        m_d->compressColorChanges->start();
    }
204 205
}

206

207 208 209 210
void KisInternalColorSelector::endUpdateWithNewColor()
{
    m_d->allowUpdates = true;
}
211

212
void KisInternalColorSelector::focusInEvent(QFocusEvent *e)
213
{
214 215
        setPreviousColor();
}
216

217 218 219
void KisInternalColorSelector::slotSetColorFromPatch(KoColorPatch* patch)
{
    slotColorUpdated(patch->color());
220
}