kis_visual_color_selector.cpp 25 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
/*
 * 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 "kis_visual_color_selector.h"

#include <QColor>
#include <QPixmap>
#include <QPainter>
#include <QPainterPath>
24
#include <QRect>
25 26 27 28 29
#include <QVector>
#include <QVBoxLayout>
#include <QList>

#include "KoColorConversions.h"
30
#include "KoColorDisplayRendererInterface.h"
31
#include "KoChannelInfo.h"
32
#include <QPointer>
33
#include "kis_signal_compressor.h"
34 35 36 37 38 39

struct KisVisualColorSelector::Private
{
    KoColor currentcolor;
    const KoColorSpace *currentCS;
    QList <KisVisualColorSelectorShape*> widgetlist;
40
    bool updateSelf = false;
41
    const KoColorDisplayRendererInterface *displayRenderer = 0;
42 43 44 45 46 47 48 49 50
    //Current coordinates.
    QVector <float> currentCoordinates;
};

KisVisualColorSelector::KisVisualColorSelector(QWidget *parent) : QWidget(parent), m_d(new Private)
{

    QVBoxLayout *layout = new QVBoxLayout;
    this->setLayout(layout);
51
    //m_d->updateSelf = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
52 53 54 55 56 57 58 59 60
}

KisVisualColorSelector::~KisVisualColorSelector()
{

}

void KisVisualColorSelector::slotSetColor(KoColor c)
{
61 62 63 64 65
    if (m_d->updateSelf==false) {
        m_d->currentcolor = c;
        if (m_d->currentCS != c.colorSpace()) {
            slotsetColorSpace(c.colorSpace());
        }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    }
    updateSelectorElements();
}

void KisVisualColorSelector::slotsetColorSpace(const KoColorSpace *cs)
{
    if (m_d->currentCS != cs)
    {
        m_d->currentCS = cs;
        if (this->layout()) {
            qDeleteAll(this->children());
        }
        m_d->widgetlist.clear();
        QHBoxLayout *layout = new QHBoxLayout;
        //redraw all the widgets.
        if (m_d->currentCS->colorChannelCount() == 1) {
            KisVisualRectangleSelectorShape *bar =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::onedimensional,KisVisualColorSelectorShape::Channel, cs, 0, 0);
            bar->setMaximumWidth(width()*0.1);
            bar->setMaximumHeight(height());
            connect (bar, SIGNAL(sigNewColor(KoColor)), this, SLOT(updateFromWidgets(KoColor)));
            layout->addWidget(bar);
            m_d->widgetlist.append(bar);
        } else if (m_d->currentCS->colorChannelCount() == 3) {
89 90 91 92 93 94 95 96 97
            KisVisualRectangleSelectorShape *bar =  new KisVisualRectangleSelectorShape(this,
                                                                                        KisVisualRectangleSelectorShape::onedimensional,
                                                                                        KisVisualColorSelectorShape::HSL,
                                                                                        cs, 0, 0,
                                                                                        m_d->displayRenderer);
            KisVisualRectangleSelectorShape *block =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::twodimensional,
                                                                                          KisVisualColorSelectorShape::HSL,
                                                                                          cs, 1, 2,
                                                                                          m_d->displayRenderer);
98 99 100 101
            bar->setMaximumWidth(width()*0.1);
            bar->setMaximumHeight(height());
            block->setMaximumWidth(width()*0.9);
            block->setMaximumHeight(height());
102 103 104
            bar->setColor(m_d->currentcolor);
            block->setColor(m_d->currentcolor);
            connect (bar, SIGNAL(sigNewColor(KoColor)), block, SLOT(setColorFromSibling(KoColor)));
105 106 107 108 109 110 111 112 113 114 115 116
            connect (block, SIGNAL(sigNewColor(KoColor)), SLOT(updateFromWidgets(KoColor)));
            layout->addWidget(bar);
            layout->addWidget(block);
            m_d->widgetlist.append(bar);
            m_d->widgetlist.append(block);
        } else if (m_d->currentCS->colorChannelCount() == 4) {
            KisVisualRectangleSelectorShape *block =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::twodimensional,KisVisualColorSelectorShape::Channel, cs, 0, 1);
            KisVisualRectangleSelectorShape *block2 =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::twodimensional,KisVisualColorSelectorShape::Channel, cs, 2, 3);
            block->setMaximumWidth(width()*0.5);
            block->setMaximumHeight(height());
            block2->setMaximumWidth(width()*0.5);
            block2->setMaximumHeight(height());
117 118 119
            block->setColor(m_d->currentcolor);
            block2->setColor(m_d->currentcolor);
            connect (block, SIGNAL(sigNewColor(KoColor)), block2, SLOT(setColorFromSibling(KoColor)));
120 121 122 123 124 125 126 127 128 129 130
            connect (block2, SIGNAL(sigNewColor(KoColor)), SLOT(updateFromWidgets(KoColor)));
            layout->addWidget(block);
            layout->addWidget(block2);
            m_d->widgetlist.append(block);
            m_d->widgetlist.append(block2);
        }
        this->setLayout(layout);
    }

}

131

132
void KisVisualColorSelector::setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer) {
133 134 135 136 137 138 139 140
    m_d->displayRenderer = displayRenderer;
    if (m_d->widgetlist.size()>0) {
        Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
            shape->setDisplayRenderer(displayRenderer);
        }
    }
}

141 142 143 144 145 146 147 148
void KisVisualColorSelector::updateSelectorElements()
{
    //first lock all elements from sending updates, then update all elements.
    Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
        shape->blockSignals(true);
    }

    Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
149 150 151 152 153
        if (m_d->updateSelf==false) {
            shape->setColor(m_d->currentcolor);
        } else {
            shape->setColorFromSibling(m_d->currentcolor);
        }
154 155 156 157 158 159 160 161 162 163 164
    }
    Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
        shape->blockSignals(false);
    }

}

void KisVisualColorSelector::updateFromWidgets(KoColor c)
{
    m_d->currentcolor = c;
    Q_EMIT sigNewColor(c);
165 166 167 168 169 170
    m_d->updateSelf = true;
}

void KisVisualColorSelector::leaveEvent(QEvent *)
{
    m_d->updateSelf = false;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

/*------------Selector shape------------*/
struct KisVisualColorSelectorShape::Private
{
    QPixmap gradient;
    QPixmap fullSelector;
    bool pixmapsNeedUpdate = true;
    QPointF currentCoordinates;
    Dimensions dimension;
    ColorModel model;
    const KoColorSpace *cs;
    KoColor currentColor;
    int channel1;
    int channel2;
186 187
    KisSignalCompressor *updateTimer;
    bool mousePressActive = false;
188
    const KoColorDisplayRendererInterface *displayRenderer = 0;
189 190 191 192 193 194 195
};

KisVisualColorSelectorShape::KisVisualColorSelectorShape(QWidget *parent,
                                                         KisVisualColorSelectorShape::Dimensions dimension,
                                                         KisVisualColorSelectorShape::ColorModel model,
                                                         const KoColorSpace *cs,
                                                         int channel1,
196
                                                         int channel2,
197
                                                         const KoColorDisplayRendererInterface *displayRenderer): QWidget(parent), m_d(new Private)
198 199 200 201 202 203 204 205 206 207 208
{
    m_d->dimension = dimension;
    m_d->model = model;
    m_d->cs = cs;
    m_d->currentColor = KoColor();
    m_d->currentColor.setOpacity(1.0);
    m_d->currentColor.convertTo(cs);
    int maxchannel = m_d->cs->colorChannelCount()-1;
    m_d->channel1 = qBound(0, channel1, maxchannel);
    m_d->channel2 = qBound(0, channel2, maxchannel);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
209
    m_d->updateTimer = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
210 211 212 213 214 215 216
    if (displayRenderer) {
        m_d->displayRenderer = displayRenderer;
        connect(m_d->displayRenderer, SIGNAL(displayConfigurationChanged()), this, SLOT(update()), Qt::UniqueConnection);
    } else {
        KoDumbColorDisplayRenderer::instance();
    }

217 218 219 220 221 222 223
}

KisVisualColorSelectorShape::~KisVisualColorSelectorShape()
{

}

224 225
void KisVisualColorSelectorShape::updateCursor()
{
226 227 228 229
    QPointF point1 = convertKoColorToShapeCoordinate(m_d->currentColor);
    if (point1 != m_d->currentCoordinates) {
        m_d->currentCoordinates = point1;
    }
230 231 232 233
}

QPointF KisVisualColorSelectorShape::getCursorPosition() {
    return m_d->currentCoordinates;
234 235 236 237 238 239 240 241
}

void KisVisualColorSelectorShape::setColor(KoColor c)
{
    if (c.colorSpace() != m_d->cs) {
        c.convertTo(m_d->cs);
    }
    m_d->currentColor = c;
242 243 244 245 246 247 248 249 250 251 252 253
    updateCursor();
    m_d->pixmapsNeedUpdate = true;
    update();
}

void KisVisualColorSelectorShape::setColorFromSibling(KoColor c)
{
    if (c.colorSpace() != m_d->cs) {
        c.convertTo(m_d->cs);
    }
    m_d->currentColor = c;
    Q_EMIT sigNewColor(c);
254 255 256 257
    m_d->pixmapsNeedUpdate = true;
    update();
}

258 259 260 261 262 263 264 265
void KisVisualColorSelectorShape::setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer)
{
    if (displayRenderer) {
        if (m_d->displayRenderer) {
            m_d->displayRenderer->disconnect(this);
        }
        m_d->displayRenderer = displayRenderer;
        connect(m_d->displayRenderer, SIGNAL(displayConfigurationChanged()),
266
                SLOT(updateFromChangedDisplayRenderer()), Qt::UniqueConnection);
267 268 269
    } else {
        m_d->displayRenderer = KoDumbColorDisplayRenderer::instance();
    }
270 271
}

272 273 274 275 276 277
void KisVisualColorSelectorShape::updateFromChangedDisplayRenderer()
{
    m_d->pixmapsNeedUpdate = true;
    repaint();
}

278 279 280 281 282 283 284 285 286 287
QColor KisVisualColorSelectorShape::getColorFromConverter(KoColor c){
    QColor col;
    if (m_d->displayRenderer) {
        col = m_d->displayRenderer->toQColor(c);
    } else {
        col = c.toQColor();
    }
    return col;
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
void KisVisualColorSelectorShape::slotSetActiveChannels(int channel1, int channel2)
{
    int maxchannel = m_d->cs->colorChannelCount()-1;
    m_d->channel1 = qBound(0, channel1, maxchannel);
    m_d->channel2 = qBound(0, channel2, maxchannel);
    m_d->pixmapsNeedUpdate = true;
    update();
}

QPixmap KisVisualColorSelectorShape::getPixmap()
{
    if (m_d->pixmapsNeedUpdate == true) {
        m_d->pixmapsNeedUpdate = false;
        m_d->gradient = QPixmap(width(), height());
        m_d->gradient.fill(Qt::black);
        QImage img(width(), height(), QImage::Format_RGB32);
        img.fill(Qt::black);

        for (int y = 0; y<img.height(); y++) {
            for (int x=0; x<img.width(); x++) {
                QPoint widgetPoint(x,y);
                QPointF newcoordinate = convertWidgetCoordinateToShapeCoordinate(widgetPoint);
                KoColor c = convertShapeCoordinateToKoColor(newcoordinate);
311
                QColor col = getColorFromConverter(c);
312 313 314 315 316 317 318 319 320 321 322 323 324 325
                img.setPixel(widgetPoint, col.rgb());
            }
        }

        m_d->gradient = QPixmap::fromImage(img, Qt::AvoidDither);
    }
    return m_d->gradient;
}

KoColor KisVisualColorSelectorShape::convertShapeCoordinateToKoColor(QPointF coordinates)
{
    KoColor c = m_d->currentColor;
    QVector <float> channelValues (c.colorSpace()->channelCount());
    channelValues.fill(1.0);
326
    c.colorSpace()->normalisedChannelsValue(c.data(), channelValues);
327 328 329 330
    QVector <qreal> maxvalue(c.colorSpace()->channelCount());
    maxvalue.fill(1.0);
    if (m_d->displayRenderer) {
        for (int ch = 0; ch<maxvalue.size(); ch++) {
331 332
            KoChannelInfo *channel = m_d->cs->channels()[ch];
            maxvalue[ch] = m_d->displayRenderer->maxVisibleFloatValue(channel);
333 334 335
            channelValues[ch] = channelValues[ch]/(maxvalue[ch]);
        }
    }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
336 337
    qreal huedivider = 1.0;
    qreal huedivider2 = 1.0;
338

Wolthera van Hövell's avatar
Wolthera van Hövell committed
339 340 341 342 343 344
    if (m_d->channel1==0) {
        huedivider = 360.0;
    }
    if (m_d->channel2==0) {
        huedivider2 = 360.0;
    }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
345
    if (m_d->model != ColorModel::Channel && c.colorSpace()->colorModelId().id() == "RGBA") {
346 347
        if (c.colorSpace()->colorModelId().id() == "RGBA") {
            if (m_d->model == ColorModel::HSV){
348
                QVector <float> inbetween(3);
349
                RGBToHSV(channelValues[2],channelValues[1], channelValues[0], &inbetween[0], &inbetween[1], &inbetween[2]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
350
                inbetween[m_d->channel1] = coordinates.x()*huedivider;
351
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
352
                    inbetween[m_d->channel2] = coordinates.y()*huedivider2;
353
                }
354
                HSVToRGB(inbetween[0], inbetween[1], inbetween[2], &channelValues[2], &channelValues[1], &channelValues[0]);
355 356
            } else if (m_d->model == ColorModel::HSL) {
                QVector <float> inbetween(3);
357
                RGBToHSL(channelValues[2],channelValues[1], channelValues[0], &inbetween[0], &inbetween[1], &inbetween[2]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
358
                inbetween[m_d->channel1] = coordinates.x()*huedivider;
359
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
360
                    inbetween[m_d->channel2] = coordinates.y()*huedivider2;
361
                }
362
                HSLToRGB(inbetween[0], inbetween[1], inbetween[2],&channelValues[2],&channelValues[1], &channelValues[0]);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
            } else if (m_d->model == ColorModel::HSI) {
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValues);
                QVector <qreal> inbetween(3);
                RGBToHSI(chan2[2],chan2[1], chan2[0], &inbetween[0], &inbetween[1], &inbetween[2]);
                inbetween[m_d->channel1] = coordinates.x();
                if (m_d->dimension == Dimensions::twodimensional) {
                    inbetween[m_d->channel2] = coordinates.y();
                }
                HSIToRGB(inbetween[0], inbetween[1], inbetween[2],&chan2[2],&chan2[1], &chan2[0]);
                channelValues = convertvectorqrealTofloat(chan2);
            } else /*if (m_d->model == ColorModel::HSY)*/ {
                QVector <qreal> luma= m_d->cs->lumaCoefficients();
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValues);
                QVector <qreal> inbetween(3);
                RGBToHSY(chan2[2],chan2[1], chan2[0], &inbetween[0], &inbetween[1], &inbetween[2],
                        luma[0], luma[1], luma[2]);
                inbetween[m_d->channel1] = coordinates.x();
                if (m_d->dimension == Dimensions::twodimensional) {
                    inbetween[m_d->channel2] = coordinates.y();
                }
                HSYToRGB(inbetween[0], inbetween[1], inbetween[2],&chan2[2],&chan2[1], &chan2[0],
                        luma[0], luma[1], luma[2]);
                channelValues = convertvectorqrealTofloat(chan2);
386 387
            }
        }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
388 389 390 391 392
    } else {
        channelValues[m_d->channel1] = coordinates.x();
        if (m_d->dimension == Dimensions::twodimensional) {
            channelValues[m_d->channel2] = coordinates.y();
        }
393
    }
394 395 396
    for (int i=0; i<channelValues.size();i++) {
        channelValues[i] = channelValues[i]*(maxvalue[i]);
    }
397 398 399 400 401 402 403 404 405 406 407 408
    c.colorSpace()->fromNormalisedChannelsValue(c.data(), channelValues);
    return c;
}

QPointF KisVisualColorSelectorShape::convertKoColorToShapeCoordinate(KoColor c)
{
    if (c.colorSpace() != m_d->cs) {
        c.convertTo(m_d->cs);
    }
    QVector <float> channelValues (m_d->currentColor.colorSpace()->channelCount());
    channelValues.fill(1.0);
    m_d->cs->normalisedChannelsValue(c.data(), channelValues);
409 410 411 412
    QVector <qreal> maxvalue(c.colorSpace()->channelCount());
    maxvalue.fill(1.0);
    if (m_d->displayRenderer) {
        for (int ch = 0; ch<maxvalue.size(); ch++) {
413 414
            KoChannelInfo *channel = m_d->cs->channels()[ch];
            maxvalue[ch] = m_d->displayRenderer->maxVisibleFloatValue(channel);
415 416 417
            channelValues[ch] = channelValues[ch]/(maxvalue[ch]);
        }
    }
418
    QPointF coordinates(0.0,0.0);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
419 420 421 422 423 424 425 426
    qreal huedivider = 1.0;
    qreal huedivider2 = 1.0;
    if (m_d->channel1==0) {
        huedivider = 360.0;
    }
    if (m_d->channel2==0) {
        huedivider2 = 360.0;
    }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
427
    if (m_d->model != ColorModel::Channel && c.colorSpace()->colorModelId().id() == "RGBA") {
428 429
        if (c.colorSpace()->colorModelId().id() == "RGBA") {
            if (m_d->model == ColorModel::HSV){
430
                QVector <float> inbetween(3);
431
                RGBToHSV(channelValues[2],channelValues[1], channelValues[0], &inbetween[0], &inbetween[1], &inbetween[2]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
432
                coordinates.setX(inbetween[m_d->channel1]/huedivider);
433
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
434
                    coordinates.setY(inbetween[m_d->channel2]/huedivider2);
435
                }
436 437
            } else if (m_d->model == ColorModel::HSL) {
                QVector <float> inbetween(3);
438
                RGBToHSL(channelValues[2],channelValues[1], channelValues[0], &inbetween[0], &inbetween[1], &inbetween[2]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
439
                coordinates.setX(inbetween[m_d->channel1]/huedivider);
440
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
441
                    coordinates.setY(inbetween[m_d->channel2]/huedivider2);
442
                }
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
            } else if (m_d->model == ColorModel::HSI) {
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValues);
                QVector <qreal> inbetween(3);
                RGBToHSI(chan2[2],chan2[1], chan2[0], &inbetween[0], &inbetween[1], &inbetween[2]);
                coordinates.setX(inbetween[m_d->channel1]);
                if (m_d->dimension == Dimensions::twodimensional) {
                    coordinates.setY(inbetween[m_d->channel2]);
                }
            } else /*if (m_d->model == ColorModel::HSY)*/ {
                QVector <qreal> luma = m_d->cs->lumaCoefficients();
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValues);
                QVector <qreal> inbetween(3);
                RGBToHSY(chan2[2],chan2[1], chan2[0], &inbetween[0], &inbetween[1], &inbetween[2], luma[0], luma[1], luma[2]);
                coordinates.setX(inbetween[m_d->channel1]);
                if (m_d->dimension == Dimensions::twodimensional) {
                    coordinates.setY(inbetween[m_d->channel2]);
                }
460 461
            }
        }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
462 463 464 465 466
    } else {
        coordinates.setX(channelValues[m_d->channel1]);
        if (m_d->dimension == Dimensions::twodimensional) {
            coordinates.setY(channelValues[m_d->channel2]);
        }
467 468 469 470
    }
    return coordinates;
}

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
QVector<float> KisVisualColorSelectorShape::convertvectorqrealTofloat(QVector<qreal> real)
{
    QVector <float> vloat(real.size());
    for (int i=0; i<real.size(); i++) {
        vloat[i] = real[i];
    }
    return vloat;
}

QVector<qreal> KisVisualColorSelectorShape::convertvectorfloatToqreal(QVector <float> vloat)
{
    QVector <qreal> real(vloat.size());
    for (int i=0; i<vloat.size(); i++) {
        real[i] = vloat[i];
    }
    return real;
}

489 490
void KisVisualColorSelectorShape::mousePressEvent(QMouseEvent *e)
{
491
    m_d->mousePressActive = true;
492
}
493 494

void KisVisualColorSelectorShape::mouseMoveEvent(QMouseEvent *e)
495
{
496 497 498 499 500 501 502 503 504 505 506 507
    if (m_d->mousePressActive==true && this->mask().contains(e->pos())) {
        QPointF coordinates = convertWidgetCoordinateToShapeCoordinate(e->pos());
        KoColor col = convertShapeCoordinateToKoColor(coordinates);
        setColor(col);
        if (!m_d->updateTimer->isActive()) {
            Q_EMIT sigNewColor(col);
            m_d->updateTimer->start();
        }
    } else {
        e->ignore();
    }
}
508

509 510 511
void KisVisualColorSelectorShape::mouseReleaseEvent(QMouseEvent *)
{
    m_d->mousePressActive = false;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
}
void KisVisualColorSelectorShape::paintEvent(QPaintEvent*)
{
    QPainter painter(this);

    if (m_d->pixmapsNeedUpdate) {
        getPixmap();
        setMask(getMaskMap());
    }
    drawCursor();
    painter.drawPixmap(0,0,m_d->fullSelector);
}

void KisVisualColorSelectorShape::resizeEvent(QResizeEvent *)
{
    m_d->pixmapsNeedUpdate = true;
}

KisVisualColorSelectorShape::Dimensions KisVisualColorSelectorShape::getDimensions()
{
    return m_d->dimension;
}

KisVisualColorSelectorShape::ColorModel KisVisualColorSelectorShape::getColorModel()
{
    return m_d->model;
}

void KisVisualColorSelectorShape::setFullImage(QPixmap full)
{
    m_d->fullSelector = full;
}
KoColor KisVisualColorSelectorShape::getCurrentColor()
{
    return m_d->currentColor;
}

/*-----------Rectangle Shape------------*/

KisVisualRectangleSelectorShape::KisVisualRectangleSelectorShape(QWidget *parent,
                                                                 Dimensions dimension,
                                                                 ColorModel model,
                                                                 const KoColorSpace *cs,
                                                                 int channel1, int channel2,
556 557
                                                                 const KoColorDisplayRendererInterface *displayRenderer,
                                                                 singelDTypes d)
558
    : KisVisualColorSelectorShape(parent, dimension, model, cs, channel1, channel2, displayRenderer)
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
    m_type = d;
}

KisVisualRectangleSelectorShape::~KisVisualRectangleSelectorShape()
{

}

QPointF KisVisualRectangleSelectorShape::convertShapeCoordinateToWidgetCoordinate(QPointF coordinate)
{
    qreal x = width()/2;
    qreal y = height()/2;
    KisVisualColorSelectorShape::Dimensions dimension = getDimensions();
    if (dimension == KisVisualColorSelectorShape::onedimensional && m_type == KisVisualRectangleSelectorShape::vertical) {
        y = coordinate.x()*height();
    } else if (dimension == KisVisualColorSelectorShape::onedimensional && m_type == KisVisualRectangleSelectorShape::horizontal) {
        x = coordinate.x()*width();
    } else {
        x = coordinate.x()*width();
        y = coordinate.y()*height();
    }
    return QPointF(x,y);
}

QPointF KisVisualRectangleSelectorShape::convertWidgetCoordinateToShapeCoordinate(QPoint coordinate)
{
    //default implementation:
    qreal x = 0.5;
    qreal y = 0.5;
    KisVisualColorSelectorShape::Dimensions dimension = getDimensions();
    if (dimension == KisVisualColorSelectorShape::onedimensional && m_type == KisVisualRectangleSelectorShape::vertical) {
        x = (qreal)coordinate.y()/(qreal)height();
    } else if (dimension == KisVisualColorSelectorShape::onedimensional && m_type == KisVisualRectangleSelectorShape::horizontal) {
        x = (qreal)coordinate.x()/(qreal)width();
    } else {
        x = (qreal)coordinate.x()/(qreal)width();
        y = (qreal)coordinate.y()/(qreal)height();
    }
    return QPointF(x, y);
}

QRegion KisVisualRectangleSelectorShape::getMaskMap()
{
    QRegion mask = QRegion(0,0,width(),height());
    return mask;
}

void KisVisualRectangleSelectorShape::drawCursor()
{
609
    QPointF cursorPoint = convertShapeCoordinateToWidgetCoordinate(getCursorPosition());
610
    QPixmap fullSelector = getPixmap();
611
    QColor col = getColorFromConverter(getCurrentColor());
612 613 614 615 616 617
    QPainter painter;
    painter.begin(&fullSelector);
    painter.setRenderHint(QPainter::Antialiasing);
    //QPainterPath path;
    QBrush fill;
    fill.setStyle(Qt::SolidPattern);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

    int cursorwidth = 5;
    QRect rect(cursorPoint.toPoint().x()-cursorwidth,cursorPoint.toPoint().y()-cursorwidth,
               cursorwidth*2,cursorwidth*2);
    if (m_type==KisVisualRectangleSelectorShape::vertical){
        int x = ( cursorPoint.x()-(width()/2)+1 );
        int y = ( cursorPoint.y()-cursorwidth );
        rect.setCoords(x, y, x+width()-2, y+(cursorwidth*2));
    } else {
        int x = cursorPoint.x()-cursorwidth;
        int y = cursorPoint.y()-(height()/2)+1;
        rect.setCoords(x, y, x+(cursorwidth*2), y+cursorwidth-2);
    }

    if (getDimensions() == KisVisualColorSelectorShape::onedimensional) {
        painter.setPen(Qt::white);
        fill.setColor(Qt::white);
        painter.setBrush(fill);
        painter.drawRect(rect);
        //set filter conversion!
638
        fill.setColor(col);
639 640 641 642 643 644 645 646 647 648 649
        painter.setPen(Qt::black);
        painter.setBrush(fill);
        rect.setCoords(rect.topLeft().x()+1, rect.topLeft().y()+1,
                       rect.topLeft().x()+rect.width()-2, rect.topLeft().y()+rect.height()-2);
        painter.drawRect(rect);

    } else {
        painter.setPen(Qt::white);
        fill.setColor(Qt::white);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
650
        fill.setColor(col);
651 652 653 654
        painter.setPen(Qt::black);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth-1.0, cursorwidth-1.0);
    }
655 656 657
    painter.end();
    setFullImage(fullSelector);
}