kis_visual_color_selector.cpp 57.9 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
#include <QVector>
#include <QVBoxLayout>
#include <QList>
28 29
#include <QPolygon>
#include <QRect>
30
#include <QtMath>
31

32 33 34
#include <KSharedConfig>
#include <KConfigGroup>

35
#include "KoColorConversions.h"
36
#include "KoColorDisplayRendererInterface.h"
37
#include "KoChannelInfo.h"
38
#include <KoColorModelStandardIds.h>
39
#include <QPointer>
40
#include "kis_signal_compressor.h"
41 42 43 44 45 46

struct KisVisualColorSelector::Private
{
    KoColor currentcolor;
    const KoColorSpace *currentCS;
    QList <KisVisualColorSelectorShape*> widgetlist;
47
    bool updateSelf = false;
48
    const KoColorDisplayRendererInterface *displayRenderer = 0;
49
    KisVisualColorSelector::Configuration acs_config;
50
    //Current coordinates.
51
    QVector <qreal> currentCoordinates;
52 53 54 55
};

KisVisualColorSelector::KisVisualColorSelector(QWidget *parent) : QWidget(parent), m_d(new Private)
{
56
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
57 58
    QVBoxLayout *layout = new QVBoxLayout;
    this->setLayout(layout);
59 60 61

    KConfigGroup cfg =  KSharedConfig::openConfig()->group("advancedColorSelector");
    m_d->acs_config = Configuration::fromString(cfg.readEntry("colorSelectorConfiguration", KisVisualColorSelector::Configuration().toString()));
62
    //m_d->updateSelf = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
63 64 65 66 67 68 69 70 71
}

KisVisualColorSelector::~KisVisualColorSelector()
{

}

void KisVisualColorSelector::slotSetColor(KoColor c)
{
72 73 74 75 76
    if (m_d->updateSelf==false) {
        m_d->currentcolor = c;
        if (m_d->currentCS != c.colorSpace()) {
            slotsetColorSpace(c.colorSpace());
        }
77 78 79 80 81 82 83 84 85
    }
    updateSelectorElements();
}

void KisVisualColorSelector::slotsetColorSpace(const KoColorSpace *cs)
{
    if (m_d->currentCS != cs)
    {
        m_d->currentCS = cs;
86 87
        slotRebuildSelectors();
    }
88

89
}
90

91 92 93 94 95 96 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 124 125 126 127 128 129
void KisVisualColorSelector::slotRebuildSelectors()
{
    if (this->children().at(0)) {
        qDeleteAll(this->children());
    }
    m_d->widgetlist.clear();
    QLayout *layout = new QHBoxLayout;
    //redraw all the widgets.
    if (m_d->currentCS->colorChannelCount() == 1) {
        KisVisualRectangleSelectorShape *bar =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::onedimensional,KisVisualColorSelectorShape::Channel, m_d->currentCS, 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) {
        int sizeValue = qMin(width(), height());
        int borderWidth = qMax(sizeValue*0.1, 20.0);

        KisVisualColorSelectorShape::ColorModel modelS = KisVisualColorSelectorShape::HSV;
        int channel1 = 0;
        int channel2 = 1;
        int channel3 = 2;

        switch(m_d->acs_config.subTypeParameter)
        {
        case H:
            channel1 = 0; break;
        case hsyS:
        case hsiS:
        case hslS:
        case hsvS:
            channel1 = 1; break;
        case V:
        case L:
        case I:
        case Y:
            channel1 = 2; break;
        }
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        switch(m_d->acs_config.mainTypeParameter)
        {
        case hsySH:
            modelS = KisVisualColorSelectorShape::HSY;
            channel2 = 0;
            channel3 = 1;
            break;
        case hsiSH:
            modelS = KisVisualColorSelectorShape::HSL;
            channel2 = 0;
            channel3 = 1;
            break;
        case hslSH:
            modelS = KisVisualColorSelectorShape::HSI;
            channel2 = 0;
            channel3 = 1;
            break;
        case hsvSH:
            modelS = KisVisualColorSelectorShape::HSV;
            channel2 = 0;
            channel3 = 1;
            break;
        case YH:
            modelS = KisVisualColorSelectorShape::HSY;
            channel2 = 0;
            channel3 = 2;
            break;
        case LH:
            modelS = KisVisualColorSelectorShape::HSL;
            channel2 = 0;
            channel3 = 2;
            break;
        case IH:
            modelS = KisVisualColorSelectorShape::HSL;
            channel2 = 0;
            channel3 = 2;
            break;
        case VH:
            modelS = KisVisualColorSelectorShape::HSV;
            channel2 = 0;
            channel3 = 2;
            break;
        case SY:
            modelS = KisVisualColorSelectorShape::HSY;
            channel2 = 1;
            channel3 = 2;
            break;
        case SI:
            modelS = KisVisualColorSelectorShape::HSI;
            channel2 = 1;
            channel3 = 2;
            break;
        case SL:
            modelS = KisVisualColorSelectorShape::HSL;
            channel2 = 1;
            channel3 = 2;
            break;
        case SV:
189
        case SV2:
190 191 192 193 194
            modelS = KisVisualColorSelectorShape::HSV;
            channel2 = 1;
            channel3 = 2;
            break;
        }
195 196 197 198
        if (m_d->acs_config.mainType==Triangle) {
            modelS = KisVisualColorSelectorShape::HSV;
            //Triangle only really works in HSV mode.
        }
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        KisVisualColorSelectorShape *bar;
        if (m_d->acs_config.subType==Ring) {
             bar =  new KisVisualEllipticalSelectorShape(this,
                                                                                     KisVisualColorSelectorShape::onedimensional,
                                                                                     modelS,
                                                                                     m_d->currentCS, channel1, channel1,
                                                                                     m_d->displayRenderer, borderWidth,KisVisualEllipticalSelectorShape::border);
            bar->resize(sizeValue, sizeValue);
        } else if (m_d->acs_config.subType==Slider) {
            bar =  new KisVisualRectangleSelectorShape(this,
                                                                                        KisVisualRectangleSelectorShape::onedimensional,
                                                                                        modelS,
                                                                                        m_d->currentCS, channel1, channel2,
                                                                                        m_d->displayRenderer, borderWidth);
            bar->setMaximumWidth(borderWidth);
            bar->setMinimumWidth(borderWidth);
            bar->setMinimumHeight(sizeValue);
        }
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
        bar->setColor(m_d->currentcolor);
        m_d->widgetlist.append(bar);

        KisVisualColorSelectorShape *block;
        if (m_d->acs_config.mainType==Triangle) {
            block =  new KisVisualTriangleSelectorShape(this, KisVisualColorSelectorShape::twodimensional,
                                                                                        modelS,
                                                                                        m_d->currentCS, channel2, channel3,
                                                                                        m_d->displayRenderer);
            block->setGeometry(bar->getSpaceForTriangle(this->geometry()));
        } else if (m_d->acs_config.mainType==Square) {
            block =  new KisVisualRectangleSelectorShape(this, KisVisualColorSelectorShape::twodimensional,
                                                                                          modelS,
                                                                                          m_d->currentCS, channel2, channel3,
                                                                                          m_d->displayRenderer);
            block->setGeometry(bar->getSpaceForSquare(this->geometry()));
234
         } else {
235 236 237 238 239 240
            block =  new KisVisualEllipticalSelectorShape(this, KisVisualColorSelectorShape::twodimensional,
                                                                                            modelS,
                                                                                            m_d->currentCS, channel2, channel3,
                                                                                            m_d->displayRenderer);
            block->setGeometry(bar->getSpaceForCircle(this->geometry()));

241 242
        }

243 244 245
        block->setColor(m_d->currentcolor);
        connect (bar, SIGNAL(sigNewColor(KoColor)), block, SLOT(setColorFromSibling(KoColor)));
        connect (block, SIGNAL(sigNewColor(KoColor)), SLOT(updateFromWidgets(KoColor)));
246 247
        connect (bar, SIGNAL(sigHSXchange()), SLOT(HSXwrangler()));
        connect (block, SIGNAL(sigHSXchange()), SLOT(HSXwrangler()));
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        m_d->widgetlist.append(block);


    } else if (m_d->currentCS->colorChannelCount() == 4) {
        KisVisualRectangleSelectorShape *block =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::twodimensional,KisVisualColorSelectorShape::Channel, m_d->currentCS, 0, 1);
        KisVisualRectangleSelectorShape *block2 =  new KisVisualRectangleSelectorShape(this, KisVisualRectangleSelectorShape::twodimensional,KisVisualColorSelectorShape::Channel, m_d->currentCS, 2, 3);
        block->setMaximumWidth(width()*0.5);
        block->setMaximumHeight(height());
        block2->setMaximumWidth(width()*0.5);
        block2->setMaximumHeight(height());
        block->setColor(m_d->currentcolor);
        block2->setColor(m_d->currentcolor);
        connect (block, SIGNAL(sigNewColor(KoColor)), block2, SLOT(setColorFromSibling(KoColor)));
        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);
268 269
}

270
void KisVisualColorSelector::setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer) {
271 272 273 274 275 276 277 278
    m_d->displayRenderer = displayRenderer;
    if (m_d->widgetlist.size()>0) {
        Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
            shape->setDisplayRenderer(displayRenderer);
        }
    }
}

279 280 281 282 283 284 285 286
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) {
287 288 289 290 291
        if (m_d->updateSelf==false) {
            shape->setColor(m_d->currentcolor);
        } else {
            shape->setColorFromSibling(m_d->currentcolor);
        }
292 293 294 295 296 297 298 299 300 301 302
    }
    Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
        shape->blockSignals(false);
    }

}

void KisVisualColorSelector::updateFromWidgets(KoColor c)
{
    m_d->currentcolor = c;
    Q_EMIT sigNewColor(c);
303 304 305 306 307 308
    m_d->updateSelf = true;
}

void KisVisualColorSelector::leaveEvent(QEvent *)
{
    m_d->updateSelf = false;
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
void KisVisualColorSelector::resizeEvent(QResizeEvent *) {
    int sizeValue = qMin(width(), height());
    int borderWidth = qMax(sizeValue*0.1, 20.0);
    QRect newrect(0,0, this->geometry().width(), this->geometry().height());
    if (m_d->currentCS->colorChannelCount()==3) {
        if (m_d->acs_config.subType==Ring) {
            m_d->widgetlist.at(0)->resize(sizeValue,sizeValue);
        } else if (m_d->acs_config.subType==Slider) {
            m_d->widgetlist.at(0)->setMaximumWidth(borderWidth);
            m_d->widgetlist.at(0)->setMinimumWidth(borderWidth);
            m_d->widgetlist.at(0)->setMinimumHeight(sizeValue);
            m_d->widgetlist.at(0)->setMaximumHeight(sizeValue);
        }
        m_d->widgetlist.at(0)->setBorderWidth(borderWidth);

        if (m_d->acs_config.mainType==Triangle) {
            m_d->widgetlist.at(1)->setGeometry(m_d->widgetlist.at(0)->getSpaceForTriangle(newrect));
        } else if (m_d->acs_config.mainType==Square) {
            m_d->widgetlist.at(1)->setGeometry(m_d->widgetlist.at(0)->getSpaceForSquare(newrect));
        } else if (m_d->acs_config.mainType==Wheel) {
            m_d->widgetlist.at(1)->setGeometry(m_d->widgetlist.at(0)->getSpaceForCircle(newrect));
        }
    }
    Q_FOREACH (KisVisualColorSelectorShape *shape, m_d->widgetlist) {
        shape->update();
    }
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
void KisVisualColorSelector::HSXwrangler()
{
    m_d->currentCoordinates = QVector <qreal>(3);

    QVector <qreal> w1 = m_d->widgetlist.at(0)->getHSX(m_d->currentCoordinates, true);
    QVector <qreal> w2 = m_d->widgetlist.at(1)->getHSX(m_d->currentCoordinates, true);
    QVector <int> ch(3);q

    ch[0] = m_d->widgetlist.at(0)->getChannels().at(0);
    ch[1] = m_d->widgetlist.at(1)->getChannels().at(0);
    ch[2] = m_d->widgetlist.at(1)->getChannels().at(1);

    m_d->currentCoordinates[ch[0]] = w1[ch[0]];
    m_d->currentCoordinates[ch[1]] = w2[ch[1]];
    m_d->currentCoordinates[ch[2]] = w2[ch[2]];

    m_d->widgetlist.at(0)->setHSX(m_d->currentCoordinates);
    m_d->widgetlist.at(1)->setHSX(m_d->currentCoordinates);
}

359 360 361
/*------------Selector shape------------*/
struct KisVisualColorSelectorShape::Private
{
362 363 364
    QImage gradient;
    QImage fullSelector;
    bool imagesNeedUpdate= true;
365 366 367 368 369 370 371
    QPointF currentCoordinates;
    Dimensions dimension;
    ColorModel model;
    const KoColorSpace *cs;
    KoColor currentColor;
    int channel1;
    int channel2;
372
    KisSignalCompressor *updateTimer;
373
    KisSignalCompressor *siblingTimer;
374
    bool mousePressActive = false;
375
    const KoColorDisplayRendererInterface *displayRenderer = 0;
376 377 378
    qreal hue = 0.0;
    qreal sat = 0.0;

379 380 381 382 383 384 385
};

KisVisualColorSelectorShape::KisVisualColorSelectorShape(QWidget *parent,
                                                         KisVisualColorSelectorShape::Dimensions dimension,
                                                         KisVisualColorSelectorShape::ColorModel model,
                                                         const KoColorSpace *cs,
                                                         int channel1,
386
                                                         int channel2,
387
                                                         const KoColorDisplayRendererInterface *displayRenderer): QWidget(parent), m_d(new Private)
388 389 390 391 392 393 394 395 396 397 398
{
    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);
399
    m_d->updateTimer = new KisSignalCompressor(100 /* ms */, KisSignalCompressor::POSTPONE, this);
400
    m_d->siblingTimer = new KisSignalCompressor(30 /* ms */, KisSignalCompressor::POSTPONE, this);
401
    setDisplayRenderer(displayRenderer);
402
    show();
403

404 405 406 407 408 409 410
}

KisVisualColorSelectorShape::~KisVisualColorSelectorShape()
{

}

411 412
void KisVisualColorSelectorShape::updateCursor()
{
413 414 415 416
    QPointF point1 = convertKoColorToShapeCoordinate(m_d->currentColor);
    if (point1 != m_d->currentCoordinates) {
        m_d->currentCoordinates = point1;
    }
417 418 419 420
}

QPointF KisVisualColorSelectorShape::getCursorPosition() {
    return m_d->currentCoordinates;
421 422 423 424 425 426 427 428
}

void KisVisualColorSelectorShape::setColor(KoColor c)
{
    if (c.colorSpace() != m_d->cs) {
        c.convertTo(m_d->cs);
    }
    m_d->currentColor = c;
429
    updateCursor();
430
    convertShapeCoordinateToKoColor(getCursorPosition());
431
    m_d->imagesNeedUpdate = true;
432 433 434 435 436 437 438 439 440 441
    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);
442
    m_d->imagesNeedUpdate = true;
443 444 445
    update();
}

446 447 448 449 450 451 452 453 454 455
void KisVisualColorSelectorShape::setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer)
{
    if (displayRenderer) {
        if (m_d->displayRenderer) {
            m_d->displayRenderer->disconnect(this);
        }
        m_d->displayRenderer = displayRenderer;
    } else {
        m_d->displayRenderer = KoDumbColorDisplayRenderer::instance();
    }
456 457
    connect(m_d->displayRenderer, SIGNAL(displayConfigurationChanged()),
            SLOT(updateFromChangedDisplayRenderer()), Qt::UniqueConnection);
458

459 460
}

461 462
void KisVisualColorSelectorShape::updateFromChangedDisplayRenderer()
{
463
    m_d->imagesNeedUpdate = true;
464
    updateCursor();
465
    //m_d->currentColor = convertShapeCoordinateToKoColor(getCursorPosition());
466
    update();
467 468
}

469 470 471 472 473
void KisVisualColorSelectorShape::forceImageUpdate()
{
    m_d->imagesNeedUpdate = true;
}

474 475
QColor KisVisualColorSelectorShape::getColorFromConverter(KoColor c){
    QColor col;
476
    if (m_d->displayRenderer && m_d->displayRenderer->getPaintingColorSpace()==m_d->cs) {
477 478 479 480 481 482 483
        col = m_d->displayRenderer->toQColor(c);
    } else {
        col = c.toQColor();
    }
    return col;
}

484 485 486 487 488
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);
489
    m_d->imagesNeedUpdate = true;
490 491 492
    update();
}

493
QImage KisVisualColorSelectorShape::getImageMap()
494
{
495 496 497
    if (m_d->imagesNeedUpdate == true) {
        m_d->imagesNeedUpdate = false;
        m_d->gradient = QImage(width(), height(), QImage::Format_ARGB32);
498
        m_d->gradient.fill(Qt::transparent);
499 500
        QImage img(width(), height(), QImage::Format_ARGB32);
        img.fill(Qt::transparent);
501
        for (int y = 0; y<img.height(); y++) {
502 503
            uint* data = reinterpret_cast<uint*>(img.scanLine(y));
            for (int x=0; x<img.width(); x++,  ++data) {
504 505 506
                QPoint widgetPoint(x,y);
                QPointF newcoordinate = convertWidgetCoordinateToShapeCoordinate(widgetPoint);
                KoColor c = convertShapeCoordinateToKoColor(newcoordinate);
507
                QColor col = getColorFromConverter(c);
508
                *data = col.rgba();
509 510 511
            }
        }

512
        m_d->gradient = img;
513 514 515 516
    }
    return m_d->gradient;
}

517
KoColor KisVisualColorSelectorShape::convertShapeCoordinateToKoColor(QPointF coordinates, bool cursor)
518 519 520 521
{
    KoColor c = m_d->currentColor;
    QVector <float> channelValues (c.colorSpace()->channelCount());
    channelValues.fill(1.0);
522
    c.colorSpace()->normalisedChannelsValue(c.data(), channelValues);
523
    QVector <float> channelValuesDisplay = channelValues;
524 525
    QVector <qreal> maxvalue(c.colorSpace()->channelCount());
    maxvalue.fill(1.0);
526 527
    if (m_d->displayRenderer
            && m_d->displayRenderer->getPaintingColorSpace()==m_d->cs
528 529 530 531 532
            && m_d->cs->colorDepthId() == Float16BitsColorDepthID
            && m_d->cs->colorDepthId() == Float32BitsColorDepthID
            && m_d->cs->colorDepthId() == Float64BitsColorDepthID
            && m_d->cs->colorModelId() != LABAColorModelID
            && m_d->cs->colorModelId() != CMYKAColorModelID) {
533
        for (int ch = 0; ch<maxvalue.size(); ch++) {
534 535
            KoChannelInfo *channel = m_d->cs->channels()[ch];
            maxvalue[ch] = m_d->displayRenderer->maxVisibleFloatValue(channel);
536
            channelValues[ch] = channelValues[ch]/(maxvalue[ch]);
537
            channelValuesDisplay[KoChannelInfo::displayPositionToChannelIndex(ch, m_d->cs->channels())] = qBound((float)0.0,channelValues[ch], (float)1.0);
538 539 540
        }
    } else {
        for (int i =0; i<channelValues.size();i++) {
541
            channelValuesDisplay[KoChannelInfo::displayPositionToChannelIndex(i, m_d->cs->channels())] = qBound((float)0.0,channelValues[i], (float)1.0);
542 543
        }
    }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
544 545
    qreal huedivider = 1.0;
    qreal huedivider2 = 1.0;
546

Wolthera van Hövell's avatar
Wolthera van Hövell committed
547 548 549 550 551 552
    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
553
    if (m_d->model != ColorModel::Channel && c.colorSpace()->colorModelId().id() == "RGBA") {
554 555
        if (c.colorSpace()->colorModelId().id() == "RGBA") {
            if (m_d->model == ColorModel::HSV){
Wolthera van Hövell's avatar
Wolthera van Hövell committed
556 557 558 559 560
                /*
                 * RGBToHSV has a undefined hue possibility. This means that hue will be -1.
                 * This can be annoying for dealing with a selector, but I understand it is being
                 * used for the KoColorSelector... For now implement a qMax here.
                 */
561
                QVector <float> inbetween(3);
562
                RGBToHSV(channelValuesDisplay[0],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2]);
563
                inbetween = convertvectorqrealTofloat(getHSX(convertvectorfloatToqreal(inbetween)));
Wolthera van Hövell's avatar
Wolthera van Hövell committed
564
                inbetween[m_d->channel1] = coordinates.x()*huedivider;
565
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
566
                    inbetween[m_d->channel2] = coordinates.y()*huedivider2;
567
                }
568
                if (cursor==true){setHSX(convertvectorfloatToqreal(inbetween));Q_EMIT sigHSXchange();}
569
                HSVToRGB(qMax(inbetween[0],(float)0.0), inbetween[1], inbetween[2], &channelValuesDisplay[0], &channelValuesDisplay[1], &channelValuesDisplay[2]);
570
            } else if (m_d->model == ColorModel::HSL) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
571 572 573 574
                /*
                 * HSLToRGB can give negative values on the grey. I fixed the fromNormalisedChannel function to clamp,
                 * but you might want to manually clamp for floating point values.
                 */
575
                QVector <float> inbetween(3);
576
                RGBToHSL(channelValuesDisplay[1],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2]);
577
                inbetween = convertvectorqrealTofloat(getHSX(convertvectorfloatToqreal(inbetween)));
Wolthera van Hövell's avatar
Wolthera van Hövell committed
578
                inbetween[m_d->channel1] = coordinates.x()*huedivider;
579
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
580
                    inbetween[m_d->channel2] = coordinates.y()*huedivider2;
581
                }
582
                if (cursor==true){setHSX(convertvectorfloatToqreal(inbetween));Q_EMIT sigHSXchange();}
583
                HSLToRGB(inbetween[0], inbetween[1], inbetween[2],&channelValuesDisplay[0],&channelValuesDisplay[1], &channelValuesDisplay[2]);
584
            } else if (m_d->model == ColorModel::HSI) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
585 586 587
                /*
                 * HSI is a modified HSY function.
                 */
588
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValuesDisplay);
589
                QVector <qreal> inbetween(3);
590
                RGBToHSI(chan2[0],chan2[1], chan2[2], &inbetween[0], &inbetween[1], &inbetween[2]);
591
                inbetween = getHSX(inbetween);
592 593 594 595
                inbetween[m_d->channel1] = coordinates.x();
                if (m_d->dimension == Dimensions::twodimensional) {
                    inbetween[m_d->channel2] = coordinates.y();
                }
596
                if (cursor==true){setHSX(inbetween);Q_EMIT sigHSXchange();}
597 598
                HSIToRGB(inbetween[0], inbetween[1], inbetween[2],&chan2[0],&chan2[1], &chan2[2]);
                channelValuesDisplay = convertvectorqrealTofloat(chan2);
599
            } else /*if (m_d->model == ColorModel::HSY)*/ {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
600 601 602 603 604
                /*
                 * HSY is pretty slow to render due being a pretty over-the-top function.
                 * Might be worth investigating whether HCY can be used instead, but I have had
                 * some weird results with that.
                 */
605
                QVector <qreal> luma= m_d->cs->lumaCoefficients();
606
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValuesDisplay);
607
                QVector <qreal> inbetween(3);
608
                RGBToHSY(chan2[0],chan2[1], chan2[2], &inbetween[0], &inbetween[1], &inbetween[2],
609
                        luma[0], luma[1], luma[2]);
610
                inbetween = getHSX(inbetween);
611 612 613 614
                inbetween[m_d->channel1] = coordinates.x();
                if (m_d->dimension == Dimensions::twodimensional) {
                    inbetween[m_d->channel2] = coordinates.y();
                }
615
                if (cursor==true){setHSX(inbetween);Q_EMIT sigHSXchange();}
616
                HSYToRGB(inbetween[0], inbetween[1], inbetween[2],&chan2[0],&chan2[1], &chan2[2],
617
                        luma[0], luma[1], luma[2]);
618
                channelValuesDisplay = convertvectorqrealTofloat(chan2);
619 620
            }
        }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
621
    } else {
622
        channelValuesDisplay[m_d->channel1] = coordinates.x();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
623
        if (m_d->dimension == Dimensions::twodimensional) {
624
            channelValuesDisplay[m_d->channel2] = coordinates.y();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
625
        }
626
    }
627
    for (int i=0; i<channelValues.size();i++) {
628
        channelValues[i] = qBound(0.0,channelValuesDisplay[KoChannelInfo::displayPositionToChannelIndex(i, m_d->cs->channels())]*(maxvalue[i]),1.0);
629
    }
630 631 632 633 634 635 636 637 638 639 640 641
    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);
642
    QVector <float> channelValuesDisplay = channelValues;
643 644
    QVector <qreal> maxvalue(c.colorSpace()->channelCount());
    maxvalue.fill(1.0);
645 646
    if (m_d->displayRenderer
            && m_d->displayRenderer->getPaintingColorSpace()==m_d->cs
647 648 649 650 651
            && m_d->cs->colorDepthId() == Float16BitsColorDepthID
            && m_d->cs->colorDepthId() == Float32BitsColorDepthID
            && m_d->cs->colorDepthId() == Float64BitsColorDepthID
            && m_d->cs->colorModelId() != LABAColorModelID
            && m_d->cs->colorModelId() != CMYKAColorModelID) {
652
        for (int ch = 0; ch<maxvalue.size(); ch++) {
653 654
            KoChannelInfo *channel = m_d->cs->channels()[ch];
            maxvalue[ch] = m_d->displayRenderer->maxVisibleFloatValue(channel);
655
            channelValues[ch] = channelValues[ch]/(maxvalue[ch]);
656
            channelValuesDisplay[KoChannelInfo::displayPositionToChannelIndex(ch, m_d->cs->channels())] = qBound((float)0.0,channelValues[ch], (float)1.0);
657 658 659
        }
    } else {
        for (int i =0; i<channelValues.size();i++) {
660
            channelValuesDisplay[KoChannelInfo::displayPositionToChannelIndex(i, m_d->cs->channels())] = qBound((float)0.0,channelValues[i], (float)1.0);
661 662
        }
    }
663
    QPointF coordinates(0.0,0.0);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
664 665 666 667 668 669 670 671
    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
672
    if (m_d->model != ColorModel::Channel && c.colorSpace()->colorModelId().id() == "RGBA") {
673 674
        if (c.colorSpace()->colorModelId().id() == "RGBA") {
            if (m_d->model == ColorModel::HSV){
675
                QVector <float> inbetween(3);
676
                RGBToHSV(channelValuesDisplay[0],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2]);
677
                inbetween = convertvectorqrealTofloat(getHSX(convertvectorfloatToqreal(inbetween)));
Wolthera van Hövell's avatar
Wolthera van Hövell committed
678
                coordinates.setX(inbetween[m_d->channel1]/huedivider);
679
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
680
                    coordinates.setY(inbetween[m_d->channel2]/huedivider2);
681
                }
682 683
            } else if (m_d->model == ColorModel::HSL) {
                QVector <float> inbetween(3);
684
                RGBToHSL(channelValuesDisplay[0],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2]);
685
                inbetween = convertvectorqrealTofloat(getHSX(convertvectorfloatToqreal(inbetween)));
Wolthera van Hövell's avatar
Wolthera van Hövell committed
686
                coordinates.setX(inbetween[m_d->channel1]/huedivider);
687
                if (m_d->dimension == Dimensions::twodimensional) {
Wolthera van Hövell's avatar
Wolthera van Hövell committed
688
                    coordinates.setY(inbetween[m_d->channel2]/huedivider2);
689
                }
690
            } else if (m_d->model == ColorModel::HSI) {
691
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValuesDisplay);
692
                QVector <qreal> inbetween(3);
693
                RGBToHSI(channelValuesDisplay[0],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2]);
694
                inbetween = getHSX(inbetween);
695 696 697 698
                coordinates.setX(inbetween[m_d->channel1]);
                if (m_d->dimension == Dimensions::twodimensional) {
                    coordinates.setY(inbetween[m_d->channel2]);
                }
699
            } else if (m_d->model == ColorModel::HSY) {
700
                QVector <qreal> luma = m_d->cs->lumaCoefficients();
701
                QVector <qreal> chan2 = convertvectorfloatToqreal(channelValuesDisplay);
702
                QVector <qreal> inbetween(3);
703
                RGBToHSY(channelValuesDisplay[0],channelValuesDisplay[1], channelValuesDisplay[2], &inbetween[0], &inbetween[1], &inbetween[2], luma[0], luma[1], luma[2]);
704
                inbetween = getHSX(inbetween);
705 706 707 708
                coordinates.setX(inbetween[m_d->channel1]);
                if (m_d->dimension == Dimensions::twodimensional) {
                    coordinates.setY(inbetween[m_d->channel2]);
                }
709 710
            }
        }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
711
    } else {
712
        coordinates.setX(channelValuesDisplay[m_d->channel1]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
713
        if (m_d->dimension == Dimensions::twodimensional) {
714
            coordinates.setY(channelValuesDisplay[m_d->channel2]);
Wolthera van Hövell's avatar
Wolthera van Hövell committed
715
        }
716 717 718 719
    }
    return coordinates;
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
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;
}

738 739
void KisVisualColorSelectorShape::mousePressEvent(QMouseEvent *e)
{
740
    m_d->mousePressActive = true;
741
    QPointF coordinates = convertWidgetCoordinateToShapeCoordinate(e->pos());
742
    KoColor col = convertShapeCoordinateToKoColor(coordinates, true);
743 744 745
    setColor(col);
    Q_EMIT sigNewColor(col);
    m_d->updateTimer->start();
746
}
747 748

void KisVisualColorSelectorShape::mouseMoveEvent(QMouseEvent *e)
749
{
750 751
    if (m_d->mousePressActive==true && this->mask().contains(e->pos())) {
        QPointF coordinates = convertWidgetCoordinateToShapeCoordinate(e->pos());
752
        KoColor col = convertShapeCoordinateToKoColor(coordinates, true);
753 754 755 756 757 758 759 760 761
        setColor(col);
        if (!m_d->updateTimer->isActive()) {
            Q_EMIT sigNewColor(col);
            m_d->updateTimer->start();
        }
    } else {
        e->ignore();
    }
}
762

763 764 765
void KisVisualColorSelectorShape::mouseReleaseEvent(QMouseEvent *)
{
    m_d->mousePressActive = false;
766 767 768 769 770
}
void KisVisualColorSelectorShape::paintEvent(QPaintEvent*)
{
    QPainter painter(this);

771 772
    //check if old and new colors differ.

773
    if (m_d->imagesNeedUpdate) {
774 775 776
        setMask(getMaskMap());
    }
    drawCursor();
777
    painter.drawImage(0,0,m_d->fullSelector);
778 779 780 781 782 783 784 785 786 787 788 789
}

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

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

790
void KisVisualColorSelectorShape::setFullImage(QImage full)
791 792 793 794 795 796 797 798
{
    m_d->fullSelector = full;
}
KoColor KisVisualColorSelectorShape::getCurrentColor()
{
    return m_d->currentColor;
}

799
QVector <qreal> KisVisualColorSelectorShape::getHSX(QVector<qreal> hsx, bool wrangler)
800
{
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    QVector <qreal> ihsx = hsx;
    if (!wrangler){
        if (m_d->model==HSV){
            if (hsx[2]<=0.0) {
                ihsx[1] = m_d->sat;
            }
        } else {
            if ((hsx[2]<=0.0 || hsx[2]>=1.0)) {
                ihsx[1] = m_d->sat;
            }
        }
        if ((hsx[1]<0.0 || hsx[0]<0.0)){
            ihsx[0]=m_d->hue;
        }
    } else {
        ihsx[0]=m_d->hue;
        ihsx[1]=m_d->sat;
818
    }
819
    return ihsx;
820 821 822 823
}

void KisVisualColorSelectorShape::setHSX(QVector<qreal> hsx)
{
824 825 826 827 828 829 830 831 832 833
    m_d->sat = hsx[1];
    m_d->hue = hsx[0];
}

QVector <int> KisVisualColorSelectorShape::getChannels()
{
    QVector <int> channels(2);
    channels[0] = m_d->channel1;
    channels[1] = m_d->channel2;
    return channels;
834 835
}

836 837 838 839 840 841 842
/*-----------Rectangle Shape------------*/

KisVisualRectangleSelectorShape::KisVisualRectangleSelectorShape(QWidget *parent,
                                                                 Dimensions dimension,
                                                                 ColorModel model,
                                                                 const KoColorSpace *cs,
                                                                 int channel1, int channel2,
843
                                                                 const KoColorDisplayRendererInterface *displayRenderer,
844
                                                                 int width,
845
                                                                 singelDTypes d)
846
    : KisVisualColorSelectorShape(parent, dimension, model, cs, channel1, channel2, displayRenderer)
847 848
{
    m_type = d;
849
    m_barWidth = width;
850 851 852 853 854 855 856
}

KisVisualRectangleSelectorShape::~KisVisualRectangleSelectorShape()
{

}

857
void KisVisualRectangleSelectorShape::setBorderWidth(int width)
858 859 860 861
{
    m_barWidth = width;
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
QRect KisVisualRectangleSelectorShape::getSpaceForSquare(QRect geom)
{
    QPointF tl;
    QPointF br;

    if (m_type==KisVisualRectangleSelectorShape::vertical) {
        br = geom.bottomRight();
        tl = QPoint(geom.topLeft().x()+m_barWidth, geom.topLeft().y());
    } else if (m_type==KisVisualRectangleSelectorShape::horizontal) {
        br = geom.bottomRight();
        tl = QPoint(geom.topLeft().x(), geom.topLeft().y()+m_barWidth);
    } else {
        tl = QPointF (geom.topLeft().x()+m_barWidth, geom.topLeft().y()+m_barWidth);
        br = QPointF (geom.bottomRight().x()-m_barWidth, geom.bottomRight().y()-m_barWidth);

    }
    QRect a(tl.toPoint(), br.toPoint());
    QRect r(a.left(), a.top(), qMin(a.height(), a.width()), qMin(a.height(), a.width()));
    return r;
}

QRect KisVisualRectangleSelectorShape::getSpaceForCircle(QRect geom)
{
    return getSpaceForSquare(geom);
}

QRect KisVisualRectangleSelectorShape::getSpaceForTriangle(QRect geom)
{
    return getSpaceForSquare(geom);
}

893 894
QPointF KisVisualRectangleSelectorShape::convertShapeCoordinateToWidgetCoordinate(QPointF coordinate)
{
895 896
    qreal x = m_barWidth/2;
    qreal y = m_barWidth/2;
897
    KisVisualColorSelectorShape::Dimensions dimension = getDimensions();
898 899 900 901 902
    if (dimension == KisVisualColorSelectorShape::onedimensional) {
        if ( m_type == KisVisualRectangleSelectorShape::vertical) {
            y = coordinate.x()*height();
        } else if (m_type == KisVisualRectangleSelectorShape::horizontal) {
            x = coordinate.x()*width();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
903
        } else if (m_type == KisVisualRectangleSelectorShape::border) {
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

            QRectF innerRect(m_barWidth/2, m_barWidth/2, width()-m_barWidth, height()-m_barWidth);
            QPointF left (innerRect.left(),innerRect.center().y());
            QList <QLineF> polygonLines;
            polygonLines.append(QLineF(left, innerRect.topLeft()));
            polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
            polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
            polygonLines.append(QLineF(innerRect.bottomRight(), innerRect.bottomLeft()));
            polygonLines.append(QLineF(innerRect.bottomLeft(), left));

            qreal totalLength =0.0;
            Q_FOREACH(QLineF line, polygonLines) {
                totalLength += line.length();
            }

            qreal length = coordinate.x()*totalLength;
            QPointF intersect(x,y);
            Q_FOREACH(QLineF line, polygonLines) {
                if (line.length()>length && length>0){
                    intersect = line.pointAt(length/line.length());

                }
                length-=line.length();
            }
            x = qRound(intersect.x());
            y = qRound(intersect.y());

Wolthera van Hövell's avatar
Wolthera van Hövell committed
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
        } else /*if (m_type == KisVisualRectangleSelectorShape::borderMirrored)*/  {

            QRectF innerRect(m_barWidth/2, m_barWidth/2, width()-m_barWidth, height()-m_barWidth);
            QPointF bottom (innerRect.center().x(), innerRect.bottom());
            QList <QLineF> polygonLines;
            polygonLines.append(QLineF(bottom, innerRect.bottomLeft()));
            polygonLines.append(QLineF(innerRect.bottomLeft(), innerRect.topLeft()));
            polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
            polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
            polygonLines.append(QLineF(innerRect.bottomRight(), bottom));

            qreal totalLength =0.0;
            Q_FOREACH(QLineF line, polygonLines) {
                totalLength += line.length();
            }

            qreal length = coordinate.x()*(totalLength/2);
            QPointF intersect(x,y);
            if (coordinate.y()==1) {
                for (int i = polygonLines.size()-1; i==0; i--) {
                    QLineF line = polygonLines.at(i);
                    if (line.length()>length && length>0){
                        intersect = line.pointAt(length/line.length());

                    }
                    length-=line.length();
                }
            } else {
                Q_FOREACH(QLineF line, polygonLines) {
                    if (line.length()>length && length>0){
                        intersect = line.pointAt(length/line.length());

                    }
                    length-=line.length();
                }
            }
            x = qRound(intersect.x());
            y = qRound(intersect.y());

970
        }
971 972 973 974 975 976 977 978 979 980 981 982 983
    } 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();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
984 985
    if (getMaskMap().contains(coordinate)) {
        if (dimension == KisVisualColorSelectorShape::onedimensional ) {
986 987 988 989
            if (m_type == KisVisualRectangleSelectorShape::vertical) {
                x = (qreal)coordinate.y()/(qreal)height();
            } else if (m_type == KisVisualRectangleSelectorShape::horizontal) {
                x = (qreal)coordinate.x()/(qreal)width();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
990
            } else if (m_type == KisVisualRectangleSelectorShape::border) {
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
                //border

                QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
                QPointF left (innerRect.left(),innerRect.center().y());
                QList <QLineF> polygonLines;
                polygonLines.append(QLineF(left, innerRect.topLeft()));
                polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
                polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
                polygonLines.append(QLineF(innerRect.bottomRight(), innerRect.bottomLeft()));
                polygonLines.append(QLineF(innerRect.bottomLeft(), left));

                QLineF radius(coordinate, this->geometry().center());
                QPointF intersect(0.5,0.5);
                qreal length = 0.0;
                qreal totalLength = 0.0;
                bool foundIntersect = false;
Wolthera van Hövell's avatar
Wolthera van Hövell committed
1007
                Q_FOREACH(QLineF line, polygonLines) {
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
                    if (line.intersect(radius,&intersect)==QLineF::BoundedIntersection && foundIntersect==false)
                    {
                        foundIntersect = true;
                        length+=QLineF(line.p1(), intersect).length();

                    }
                    if (foundIntersect==false) {
                        length+=line.length();
                    }
                    totalLength+=line.length();
                }

                x = length/totalLength;

Wolthera van Hövell's avatar
Wolthera van Hövell committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
            } else /*if (m_type == KisVisualRectangleSelectorShape::borderMirrored)*/  {
                //border

                QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
                QPointF bottom (innerRect.center().x(), innerRect.bottom());
                QList <QLineF> polygonLines;
                polygonLines.append(QLineF(bottom, innerRect.bottomLeft()));
                polygonLines.append(QLineF(innerRect.bottomLeft(), innerRect.topLeft()));
                polygonLines.append(QLineF(innerRect.topLeft(), innerRect.topRight()));
                polygonLines.append(QLineF(innerRect.topRight(), innerRect.bottomRight()));
                polygonLines.append(QLineF(innerRect.bottomRight(), bottom));

                QLineF radius(coordinate, this->geometry().center());
                QPointF intersect(0.5,0.5);
                qreal length = 0.0;
                qreal totalLength = 0.0;
                bool foundIntersect = false;
                Q_FOREACH(QLineF line, polygonLines) {
                    if (line.intersect(radius,&intersect)==QLineF::BoundedIntersection && foundIntersect==false)
                    {
                        foundIntersect = true;
                        length+=QLineF(line.p1(), intersect).length();

                    }
                    if (foundIntersect==false) {
                        length+=line.length();
                    }
                    totalLength+=line.length();
                }
                int halflength = totalLength/2;

                if (length>halflength) {
                    x = (halflength - (length-halflength))/halflength;
                    y = 1.0;
                } else {
                    x = length/halflength;
                    y = 0.0;
                }
1060 1061 1062 1063 1064
            }
        }
        else {
            x = (qreal)coordinate.x()/(qreal)width();
            y = (qreal)coordinate.y()/(qreal)height();
Wolthera van Hövell's avatar
Wolthera van Hövell committed
1065 1066
        }
    }
1067 1068 1069 1070 1071 1072
    return QPointF(x, y);
}

QRegion KisVisualRectangleSelectorShape::getMaskMap()
{
    QRegion mask = QRegion(0,0,width(),height());
Wolthera van Hövell's avatar
Wolthera van Hövell committed
1073
    if (m_type==KisVisualRectangleSelectorShape::border || m_type==KisVisualRectangleSelectorShape::borderMirrored) {
1074 1075
        mask = mask.subtracted(QRegion(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2)));
    }
1076 1077
    return mask;
}
1078 1079 1080 1081
void KisVisualRectangleSelectorShape::resizeEvent(QResizeEvent *)
{
    forceImageUpdate();
}
1082 1083 1084

void KisVisualRectangleSelectorShape::drawCursor()
{
1085
    QPointF cursorPoint = convertShapeCoordinateToWidgetCoordinate(getCursorPosition());
1086
    QImage fullSelector = getImageMap();
1087
    QColor col = getColorFromConverter(getCurrentColor());
1088 1089 1090 1091 1092 1093
    QPainter painter;
    painter.begin(&fullSelector);
    painter.setRenderHint(QPainter::Antialiasing);
    //QPainterPath path;
    QBrush fill;
    fill.setStyle(Qt::SolidPattern);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

    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);
    }
Wolthera van Hövell's avatar
Wolthera van Hövell committed
1107 1108
    QRectF innerRect(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2));
    if (getDimensions() == KisVisualColorSelectorShape::onedimensional && m_type!=KisVisualRectangleSelectorShape::border && m_type!=KisVisualRectangleSelectorShape::borderMirrored) {
1109 1110 1111 1112 1113
        painter.setPen(Qt::white);
        fill.setColor(Qt::white);
        painter.setBrush(fill);
        painter.drawRect(rect);
        //set filter conversion!
1114
        fill.setColor(col);
1115 1116 1117 1118 1119 1120
        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);

Wolthera van Hövell's avatar
Wolthera van Hövell committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    }else if(m_type==KisVisualRectangleSelectorShape::borderMirrored){
        painter.setPen(Qt::white);
        fill.setColor(Qt::white);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
        QPoint mirror(innerRect.center().x()+(innerRect.center().x()-cursorPoint.x()),cursorPoint.y());
        painter.drawEllipse(mirror, cursorwidth, cursorwidth);
        fill.setColor(col);
        painter.setPen(Qt::black);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth-1, cursorwidth-1);
        painter.drawEllipse(mirror, cursorwidth-1, cursorwidth-1);

1134 1135 1136 1137 1138
    } else {
        painter.setPen(Qt::white);
        fill.setColor(Qt::white);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth, cursorwidth);
1139
        fill.setColor(col);
1140 1141 1142 1143
        painter.setPen(Qt::black);
        painter.setBrush(fill);
        painter.drawEllipse(cursorPoint, cursorwidth-1.0, cursorwidth-1.0);
    }
1144 1145 1146
    painter.end();
    setFullImage(fullSelector);
}
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

//----------------Elliptical--------------------------//
KisVisualEllipticalSelectorShape::KisVisualEllipticalSelectorShape(QWidget *parent,
                                                                 Dimensions dimension,
                                                                 ColorModel model,
                                                                 const KoColorSpace *cs,
                                                                 int channel1, int channel2,
                                                                 const KoColorDisplayRendererInterface *displayRenderer,
                                                                 int borwidth,
                                                                 singelDTypes d)
    : KisVisualColorSelectorShape(parent, dimension, model, cs, channel1, channel2, displayRenderer)
{
    m_type = d;
    m_barWidth = borwidth;
}

KisVisualEllipticalSelectorShape::~KisVisualEllipticalSelectorShape()
{

}

QSize KisVisualEllipticalSelectorShape::sizeHint() const
{
    return QSize(180,180);
}
1172
void KisVisualEllipticalSelectorShape::setBorderWidth(int width)
1173 1174 1175 1176
{
    m_barWidth = width;
}

1177 1178
QRect KisVisualEllipticalSelectorShape::getSpaceForSquare(QRect geom)
{
1179 1180 1181
    int sizeValue = qMin(width(),height());
    QRect b(geom.left(), geom.top(), sizeValue, sizeValue);
    QLineF radius(b.center(), QPointF(b.left()+m_barWidth, b.center().y()) );
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
    radius.setAngle(135);
    QPointF tl = radius.p2();
    radius.setAngle(315);
    QPointF br = radius.p2();
    QRect r(tl.toPoint(), br.toPoint());
    return r;
}

QRect KisVisualEllipticalSelectorShape::getSpaceForCircle(QRect geom)
{
1192 1193 1194 1195
    int sizeValue = qMin(width(),height());
    QRect b(geom.left(), geom.top(), sizeValue, sizeValue);
    QPointF tl = QPointF (b.topLeft().x()+m_barWidth, b.topLeft().y()+m_barWidth);
    QPointF br = QPointF (b.bottomRight().x()-m_barWidth, b.bottomRight().y()-m_barWidth);
1196 1197 1198 1199 1200 1201
    QRect r(tl.toPoint(), br.toPoint());
    return r;
}

QRect KisVisualEllipticalSelectorShape::getSpaceForTriangle(QRect geom)
{
1202 1203 1204
    int sizeValue = qMin(width(),height());
    QRect b(geom.left(), geom.top(), sizeValue, sizeValue);
    QLineF radius(b.center(), QPointF(b.left()+m_barWidth, b.center().y()) );
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    radius.setAngle(90);//point at yellowgreen :)
    QPointF t = radius.p2();
    radius.setAngle(330);//point to purple :)
    QPointF br = radius.p2();
    radius.setAngle(210);//point to cerulean :)
    QPointF bl = radius.p2();
    QPointF tl = QPoint(bl.x(),t.y());
    QRect r(tl.toPoint(), br.toPoint());
    return r;
}

1216 1217
QPointF KisVisualEllipticalSelectorShape::convertShapeCoordinateToWidgetCoordinate(QPointF coordinate)
{
1218 1219 1220 1221 1222 1223 1224
    qreal x;
    qreal y;
    qreal a = (qreal)width()*0.5;
    QPointF center(a, a);
    QLineF line(center, QPoint((m_barWidth*0.5),a));
    qreal angle = coordinate.x()*360.0;
    angle = fmod(angle+180.0,360.0);
1225 1226 1227 1228 1229 1230
    angle = 180.0-angle;
    angle = angle+180.0;
    if (m_type==KisVisualEllipticalSelectorShape::borderMirrored) {
        angle = (coordinate.x()/2)*360.0;
        angle = fmod((angle+90.0), 360.0);
    }
1231 1232
    line.setAngle(angle);
    if (getDimensions()!=KisVisualColorSelectorShape::onedimensional) {
1233
        line.setLength(coordinate.y()*a);
1234
    }
1235 1236
    x = qRound(line.p2().x());
    y = qRound(line.p2().y());
1237 1238 1239 1240 1241 1242 1243 1244
    return QPointF(x,y);
}

QPointF KisVisualEllipticalSelectorShape::convertWidgetCoordinateToShapeCoordinate(QPoint coordinate)
{
    //default implementation:
    qreal x = 0.5;
    qreal y = 1.0;
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
    QRect total(0, 0, width(), height());
    QLineF line(total.center(), coordinate);
    qreal a = total.width()/2;
    qreal angle;

    if (m_type!=KisVisualEllipticalSelectorShape::borderMirrored){
        angle = fmod((line.angle()+180.0), 360.0);
        angle = 180.0-angle;
        angle = angle+180.0;
        x = angle/360.0;
        if (getDimensions()==KisVisualColorSelectorShape::twodimensional) {
            y = qBound(0.0,line.length()/a, 1.0);
1257 1258
        }

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
    } else {
        angle = fmod((line.angle()+270.0), 360.0);
        if (angle>180.0) {
            angle = 180.0-angle;
            angle = angle+180;
        }
        x = (angle/360.0)*2;
        if (getDimensions()==KisVisualColorSelectorShape::twodimensional) {
            y = line.length()/a;
        }
1269
    }
1270 1271


1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
    return QPointF(x, y);
}

QRegion KisVisualEllipticalSelectorShape::getMaskMap()
{
    QRegion mask = QRegion(0,0,width(),height(), QRegion::Ellipse);
    if (getDimensions()==KisVisualColorSelectorShape::onedimensional) {
        mask = mask.subtracted(QRegion(m_barWidth, m_barWidth, width()-(m_barWidth*2), height()-(m_barWidth*2), QRegion::Ellipse));
    }
    return mask;
}

1284 1285 1286 1287 1288
void KisVisualEllipticalSelectorShape::resizeEvent(QResizeEvent *)
{
    forceImageUpdate();
}

1289 1290 1291
void KisVisualEllipticalSelectorShape::drawCursor()
{
    QPointF cursorPoint = convertShapeCoordinateToWidgetCoordinate(getCursorPosition());
1292
    QImage fullSelector = getImageMap();
1293 1294 1295 1296
    QColor col = getColorFromConverter(getCurrentColor());
    QPainter painter;
    painter.begin(&fullSelector);
    painter.setRenderHint(QPainter::Antialiasing);
1297 1298

    painter.save();
1299
    painter.setCompositionMode(QPainter::CompositionMode_Clear);
1300 1301 1302
    QPen pen;
    pen.setWidth(5);
    painter.setPen(pen);
1303
    painter.drawEllipse(QRect(0,0,width(),height()));
1304 1305 1306 1307 1308
    if (getDimensions()==KisVisualColorSelectorShape::onedimensional) {
        painter.drawEllipse(QRect(this->geometry().top()+m_barWidth, this->geometry().left()+m_barWidth, this->geometry().width()-(m_barWidth*2), this->geometry().height()-(m_barWidth*2)));
    }
    painter.restore();

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319