kis_screen_color_picker.cpp 7.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*
 * 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 <QDesktopWidget>
#include <QGuiApplication>
#include <QApplication>
#include <QScreen>
#include <QColor>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QWindow>
#include <QTimer>

#include "kis_icon.h"
#include "kis_screen_color_picker.h"

struct KisScreenColorPicker::Private
{

    QPushButton *screenColorPickerButton = 0;
    QLabel *lblScreenColorInfo = 0;

    KoColor currentColor = KoColor();
    KoColor beforeScreenColorPicking = KoColor();

    KisScreenColorPickingEventFilter *colorPickingEventFilter = 0;

#ifdef Q_OS_WIN32
    QTimer *updateTimer = 0;
    QWindow dummyTransparentWindow;
#endif
};

KisScreenColorPicker::KisScreenColorPicker(QWidget *parent) : QWidget(parent), m_d(new Private)
{
    QVBoxLayout *layout = new QVBoxLayout();
    this->setLayout(layout);
    m_d->screenColorPickerButton = new QPushButton();
    m_d->screenColorPickerButton->setIcon(kisIcon("krita_tool_color_picker"));
56
    m_d->screenColorPickerButton->setMinimumHeight(25);
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    this->layout()->addWidget(m_d->screenColorPickerButton);
    m_d->lblScreenColorInfo = new QLabel(QLatin1String("\n"));
    this->layout()->addWidget(m_d->lblScreenColorInfo);
    connect(m_d->screenColorPickerButton, SIGNAL(clicked()), SLOT(pickScreenColor()));
#ifdef Q_OS_WIN32
    m_d->updateTimer = new QTimer(q);
    m_d->dummyTransparentWindow.resize(1, 1);
    m_d->dummyTransparentWindow.setFlags(Qt::Tool | Qt::FramelessWindowHint);
    connect(m_d->updateTimer, SIGNAL(timeout()), SLOT(updateColorPicking()));
#endif
}

KisScreenColorPicker::~KisScreenColorPicker()
{
}

KoColor KisScreenColorPicker::currentColor()
{
    return m_d->currentColor;
}

void KisScreenColorPicker::pickScreenColor()
{
    if (!m_d->colorPickingEventFilter)
        m_d->colorPickingEventFilter = new KisScreenColorPickingEventFilter(this);
    this->installEventFilter(m_d->colorPickingEventFilter);
    // If user pushes Escape, the last color before picking will be restored.
    m_d->beforeScreenColorPicking = currentColor();
#ifndef QT_NO_CURSOR
    grabMouse(Qt::CrossCursor);
#else
    grabMouse();
#endif

#ifdef Q_OS_WIN32 // excludes WinCE and WinRT
    // On Windows mouse tracking doesn't work over other processes's windows
    m_d->updateTimer->start(30);

    // HACK: Because mouse grabbing doesn't work across processes, we have to have a dummy,
    // invisible window to catch the mouse click, otherwise we will click whatever we clicked
    // and loose focus.
    m_d->dummyTransparentWindow.show();
#endif
    grabKeyboard();
    /* With setMouseTracking(true) the desired color can be more precisely picked up,
     * and continuously pushing the mouse button is not necessary.
     */
    setMouseTracking(true);

    //emit to the rest of the dialog to disable.
    Q_EMIT sigPleaseDisableEverything(true);
    m_d->screenColorPickerButton->setDisabled(true);

    const QPoint globalPos = QCursor::pos();
    setCurrentColor(grabScreenColor(globalPos));
    updateColorLabelText(globalPos);
}

void KisScreenColorPicker::setCurrentColor(KoColor c)
{
    m_d->currentColor = c;
}

KoColor KisScreenColorPicker::grabScreenColor(const QPoint &p)
{
    const QDesktopWidget *desktop = QApplication::desktop();
    const QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber())->grabWindow(desktop->winId(),
                                                                                              p.x(), p.y(), 1, 1);
    QImage i = pixmap.toImage();
    KoColor col = KoColor();
    col.fromQColor(QColor::fromRgb(i.pixel(0, 0)));
    return col;
}

void KisScreenColorPicker::updateColorLabelText(const QPoint &globalPos)
{
    KoColor col = grabScreenColor(globalPos);
    QString colname = KoColor::toQString(col);
    m_d->lblScreenColorInfo->setText(tr("Cursor at %1 , %2, color: %3 \nPress ESC to cancel")
                                     .arg(globalPos.x())
                                     .arg(globalPos.y())
                                     .arg(colname));
}

bool KisScreenColorPicker::handleColorPickingMouseMove(QMouseEvent *e)
{
    // If the cross is visible the grabbed color will be black most of the times
    //cp->setCrossVisible(!cp->geometry().contains(e->pos()));


    continueUpdateColorPicking(e->globalPos());
    return true;
}

bool KisScreenColorPicker::handleColorPickingMouseButtonRelease(QMouseEvent *e)
{
    setCurrentColor(grabScreenColor(e->globalPos()));
    Q_EMIT sigNewColorPicked(currentColor());
    releaseColorPicking();
    return true;
}

bool KisScreenColorPicker::handleColorPickingKeyPress(QKeyEvent *e)
{
    //Q_Q(QColorDialog);
    if (e->matches(QKeySequence::Cancel)) {
        releaseColorPicking();
        setCurrentColor(m_d->beforeScreenColorPicking);
    } else if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
        setCurrentColor(grabScreenColor(QCursor::pos()));
        releaseColorPicking();
    }
    e->accept();
    return true;
}

void KisScreenColorPicker::releaseColorPicking()
{
    //Q_Q(QColorDialog);
    //cp->setCrossVisible(true);
    removeEventFilter(m_d->colorPickingEventFilter);
    releaseMouse();
#ifdef Q_OS_WIN32
    m_d->updateTimer->stop();
    m_d->dummyTransparentWindow.setVisible(false);
#endif
    releaseKeyboard();
    setMouseTracking(false);
    m_d->lblScreenColorInfo->setText(QLatin1String("\n"));
    //emit enable signal
    Q_EMIT sigPleaseDisableEverything(false);
    m_d->screenColorPickerButton->setDisabled(false);
}

void KisScreenColorPicker::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
}

void KisScreenColorPicker::updateColorPicking()
{
#ifndef QT_NO_CURSOR
    //Q_Q(QColorDialog);
    static QPoint lastGlobalPos;
    QPoint newGlobalPos = QCursor::pos();
    if (lastGlobalPos == newGlobalPos)
        return;
    lastGlobalPos = newGlobalPos;

    if (!rect().contains(mapFromGlobal(newGlobalPos))) { // Inside the dialog mouse tracking works, handleColorPickingMouseMove will be called
        continueUpdateColorPicking(newGlobalPos);
#ifdef Q_OS_WIN32
        m_dummyTransparentWindow.setPosition(newGlobalPos);
#endif
    }
#endif // ! QT_NO_CURSOR
}

void KisScreenColorPicker::continueUpdateColorPicking(const QPoint &globalPos)
{
    const KoColor color = grabScreenColor(globalPos);
    // QTBUG-39792, do not change standard, custom color selectors while moving as
    // otherwise it is not possible to pre-select a custom cell for assignment.
    setCurrentColor(color);
    updateColorLabelText(globalPos);

}

// Event filter to be installed on the dialog while in color-picking mode.
KisScreenColorPickingEventFilter::KisScreenColorPickingEventFilter(KisScreenColorPicker *w, QObject *parent) : QObject(parent), m_w(w)
{}

bool KisScreenColorPickingEventFilter::eventFilter(QObject *, QEvent *event)
{
    switch (event->type()) {
    case QEvent::MouseMove:
        return m_w->handleColorPickingMouseMove(static_cast<QMouseEvent *>(event));
    case QEvent::MouseButtonRelease:
        return m_w->handleColorPickingMouseButtonRelease(static_cast<QMouseEvent *>(event));
    case QEvent::KeyPress:
        return m_w->handleColorPickingKeyPress(static_cast<QKeyEvent *>(event));
    default:
        break;
    }
    return false;
}


#include "kis_screen_color_picker.moc"