kis_internal_color_selector.cpp 5.09 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 26 27

#include "KoColorSpaceRegistry.h"

#include "kis_signal_compressor.h"
28
#include "KisViewManager.h"
29
#include "KoColorDisplayRendererInterface.h"
30

31 32 33
#include "kis_spinbox_color_selector.h"

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

struct KisInternalColorSelector::Private
{
    bool allowUpdates = true;
    KoColor currentColor;
    const KoColorSpace *currentColorSpace;
41
    bool chooseAlpha = false;
42
    KisSignalCompressor *compressColorChanges;
43
    const KoColorDisplayRendererInterface *displayRenderer;
44 45
};

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

57 58
    setWindowTitle(caption);

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

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

66
    m_ui->visualSelector->slotSetColor(color);
67
    m_ui->visualSelector->setDisplayRenderer(displayRenderer);
68
    connect(m_ui->visualSelector, SIGNAL(sigNewColor(KoColor)), this, SLOT(slotColorUpdated(KoColor)));
69
    connect(m_ui->screenColorPicker, SIGNAL(sigNewColorPicked(KoColor)),this, SLOT(slotColorUpdated(KoColor)));
70
    //TODO: Add disable signal as well. Might be not necessary...?
71

72
    connect(this, SIGNAL(signalForegroundColorChosen(KoColor)), this, SLOT(slotLockSelector()));
73
    m_d->compressColorChanges = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
74
    connect(m_d->compressColorChanges, SIGNAL(timeout()), this, SLOT(endUpdateWithNewColor()));
75 76 77

    connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
78 79 80 81
}

KisInternalColorSelector::~KisInternalColorSelector()
{
82
    delete m_ui;
83 84 85 86 87 88
    //TODO: Does the scoped pointer also need to be deleted???
}

void KisInternalColorSelector::slotColorUpdated(KoColor newColor)
{
    //if the update did not come from this selector...
89 90
    if (m_d->allowUpdates || QObject::sender() == this->parent()) {
        qDebug()<<"Color as received by the internal color selector" << KoColor::toQString(newColor);
91
        m_d->currentColor = newColor;
92
        updateAllElements(QObject::sender());
93 94 95
    }
}

96
void KisInternalColorSelector::colorSpaceChanged(const KoColorSpace *cs)
97 98 99 100 101 102
{
    if (cs == m_d->currentColorSpace) {
        return;
    }

    m_d->currentColorSpace = KoColorSpaceRegistry::instance()->colorSpace(cs->colorModelId().id(), cs->colorDepthId().id(), cs->profile());
103
    m_ui->spinboxselector->slotSetColorSpace(m_d->currentColorSpace);
104 105 106

}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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;
}

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

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

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

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

152

153 154 155 156
void KisInternalColorSelector::endUpdateWithNewColor()
{
    m_d->allowUpdates = true;
}