kis_internal_color_selector.cpp 4.64 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 23 24 25 26
/*
 * 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>

#include "KoColorSpaceRegistry.h"

#include "kis_signal_compressor.h"
27
#include "KisViewManager.h"
28 29 30 31 32
#include "KoColorDisplayRendererInterface.h"
#include "kis_display_color_converter.h"
#include "kis_spinbox_color_selector.h"

#include "kis_internal_color_selector.h"
33
#include "ui_wdgdlginternalcolorselector.h"
34 35 36 37 38 39

struct KisInternalColorSelector::Private
{
    bool allowUpdates = true;
    KoColor currentColor;
    const KoColorSpace *currentColorSpace;
40 41
    bool chooseAlpha = false;
    //KisSpinboxColorSelector *spinBoxSelector;
42 43 44
    KisSignalCompressor *compressColorChanges;
};

45
KisInternalColorSelector::KisInternalColorSelector(QWidget *parent, KoColor color, bool modal, const QString &caption)
46 47 48
    : QDialog(parent)
     ,m_d(new Private)
{
49 50 51 52 53 54 55
    setModal(modal);
    m_ui = new Ui_WdgDlgInternalColorSelector();
    m_ui->setupUi(this);
    if (!modal) {
        m_ui->buttonBox->hide();
    }

56 57
    setWindowTitle(caption);

58
    m_d->currentColor = color;
59
    m_d->currentColorSpace = m_d->currentColor.colorSpace();
60 61 62

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

    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
65
    m_d->compressColorChanges = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
66
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
67 68 69

    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
70 71 72 73
}

KisInternalColorSelector::~KisInternalColorSelector()
{
74
    delete m_ui;
75 76 77 78 79 80
    //TODO: Does the scoped pointer also need to be deleted???
}

void KisInternalColorSelector::slotColorUpdated(KoColor newColor)
{
    //if the update did not come from this selector...
81 82
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
        qDebug()<<"Color as received by the internal color selector" << KoColor::toQString(newColor);
83
        m_d->currentColor = newColor;
84
        updateAllElements(QObject::sender());
85 86 87
    }
}

88
void KisInternalColorSelector::colorSpaceChanged(const KoColorSpace *cs)
89 90 91 92 93 94 95
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
    //Empty the layout.
96
    m_ui->spinboxselector->slotSetColorSpace(m_d->currentColorSpace);
97 98 99

}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
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;
}

void KisInternalColorSelector::setDialogModal(bool modal)
{
    m_ui->buttonBox->show();
    setModal(true);
}

124 125 126 127 128 129 130 131 132 133 134
void KisInternalColorSelector::slotConfigurationChanged()
{
    //m_d->canvas->displayColorConverter()->
    //slotColorSpaceChanged(m_d->canvas->image()->colorSpace());
}

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

135
void KisInternalColorSelector::updateAllElements(QObject *source)
136 137
{
    //update everything!!!
138 139
    if (source != m_ui->spinboxselector) {
        m_ui->spinboxselector->slotSetColor(m_d->currentColor);
140 141 142 143 144 145
    }

    if (source != this->parent()) {
        emit(signalForegroundColorChosen(m_d->currentColor));
        m_d->compressColorChanges->start();
    }
146 147
}

148

149 150 151 152
void KisInternalColorSelector::endUpdateWithNewColor()
{
    m_d->allowUpdates = true;
}