kis_internal_color_selector.cpp 4 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 27
/*
 * 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"
#include "kis_canvas_resource_provider.h"
28
#include "KisViewManager.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "KoColorDisplayRendererInterface.h"
#include "kis_display_color_converter.h"
#include "kis_spinbox_color_selector.h"

#include "kis_internal_color_selector.h"

struct KisInternalColorSelector::Private
{
    bool allowUpdates = true;
    KoColor currentColor;
    const KoColorSpace *currentColorSpace;


    KisSpinboxColorSelector *spinBoxSelector;
    KisSignalCompressor *compressColorChanges;
};

46
KisInternalColorSelector::KisInternalColorSelector(QWidget *parent, KoColor color, const QString &caption)
47 48 49 50 51
    : QDialog(parent)
     ,m_d(new Private)
{
    setModal(false);
    setWindowTitle(caption);
52
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
53

54
    m_d->currentColor = color;
55
    m_d->currentColorSpace = m_d->currentColor.colorSpace();
56 57 58 59
    m_d->spinBoxSelector = new KisSpinboxColorSelector(this, m_d->currentColor);
    if (m_d->spinBoxSelector) {qDebug()<<"valid";}
    //ui->base->addWidget(m_d->spinBoxSelector);
    //ui->verticalLayout->addWidget(m_d->spinBoxSelector);
60 61 62 63
    connect(m_d->spinBoxSelector, SIGNAL(sigNewColor(KoColor)), this, SLOT(slotColorUpdated(KoColor)));

    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
    m_d->compressColorChanges = new KisSignalCompressor(500 /* ms */, KisSignalCompressor::POSTPONE, this);
64
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
65 66 67 68 69 70 71 72 73 74 75
}

KisInternalColorSelector::~KisInternalColorSelector()
{
    delete ui;
    //TODO: Does the scoped pointer also need to be deleted???
}

void KisInternalColorSelector::slotColorUpdated(KoColor newColor)
{
    //if the update did not come from this selector...
76 77
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
        qDebug()<<"Color as received by the internal color selector" << KoColor::toQString(newColor);
78
        m_d->currentColor = newColor;
79
        updateAllElements(QObject::sender());
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    }
}

void KisInternalColorSelector::slotColorSpaceChanged(const KoColorSpace *cs)
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
    //Empty the layout.
    m_d->spinBoxSelector->slotSetColorSpace(m_d->currentColorSpace);

}

void KisInternalColorSelector::slotConfigurationChanged()
{
    //m_d->canvas->displayColorConverter()->
    //slotColorSpaceChanged(m_d->canvas->image()->colorSpace());
}

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

106
void KisInternalColorSelector::updateAllElements(QObject *source)
107 108
{
    //update everything!!!
109 110 111 112 113 114 115 116
    if (source != m_d->spinBoxSelector) {
        m_d->spinBoxSelector->slotSetColor(m_d->currentColor);
    }

    if (source != this->parent()) {
        emit(signalForegroundColorChosen(m_d->currentColor));
        m_d->compressColorChanges->start();
    }
117 118 119 120 121 122
}

void KisInternalColorSelector::endUpdateWithNewColor()
{
    m_d->allowUpdates = true;
}