kis_internal_color_selector.cpp 6.68 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 56 57 58 59 60 61
    setModal(modal);
    m_ui = new Ui_WdgDlgInternalColorSelector();
    m_ui->setupUi(this);
    if (!modal) {
        m_ui->buttonBox->hide();
    }

62 63
    setWindowTitle(caption);

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

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

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

    m_ui->currentColor->setColor(m_d->currentColor);
    m_ui->previousColor->setColor(m_d->currentColor);
    connect(this, SIGNAL(accepted()), this, SLOT(setPreviousColor()));
93

94
    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
95
    m_d->compressColorChanges = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
96
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
97 98 99

    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
100 101 102 103
}

KisInternalColorSelector::~KisInternalColorSelector()
{
104
    delete m_ui;
105 106 107 108 109 110
    //TODO: Does the scoped pointer also need to be deleted???
}

void KisInternalColorSelector::slotColorUpdated(KoColor newColor)
{
    //if the update did not come from this selector...
111
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
112
        m_d->currentColor = newColor;
113
        updateAllElements(QObject::sender());
114 115 116
    }
}

117
void KisInternalColorSelector::colorSpaceChanged(const KoColorSpace *cs)
118 119 120 121 122 123
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
124
    m_ui->spinboxselector->slotSetColorSpace(m_d->currentColorSpace);
125 126 127

}

128 129 130 131 132 133 134 135 136 137
void KisInternalColorSelector::setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer)
{
    if (displayRenderer) {
        m_d->displayRenderer = displayRenderer;
        m_ui->visualSelector->setDisplayRenderer(displayRenderer);
    } else {
        m_d->displayRenderer = KoDumbColorDisplayRenderer::instance();
    }
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
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;
}

156 157 158 159 160 161 162 163 164 165 166
void KisInternalColorSelector::slotConfigurationChanged()
{
    //m_d->canvas->displayColorConverter()->
    //slotColorSpaceChanged(m_d->canvas->image()->colorSpace());
}

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

167 168 169 170 171 172 173 174 175
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());
    }
}

176
void KisInternalColorSelector::updateAllElements(QObject *source)
177 178
{
    //update everything!!!
179 180
    if (source != m_ui->spinboxselector) {
        m_ui->spinboxselector->slotSetColor(m_d->currentColor);
181
    }
182 183 184
    if (source != m_ui->visualSelector) {
        m_ui->visualSelector->slotSetColor(m_d->currentColor);
    }
185

186 187 188 189
    m_ui->previousColor->setColor(m_d->previousColor);

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

190 191 192 193
    if (source != this->parent()) {
        emit(signalForegroundColorChosen(m_d->currentColor));
        m_d->compressColorChanges->start();
    }
194 195
}

196

197 198 199 200
void KisInternalColorSelector::endUpdateWithNewColor()
{
    m_d->allowUpdates = true;
}
201 202 203 204 205 206

void KisInternalColorSelector::leaveEvent(QEvent *)
{
    setPreviousColor();

}