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 56 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
|
/*
* Copyright 2015 Kai Uwe Broulik <kde@privat.broulik.de>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>
*/
#include "grabwidget.h"
#include <QDebug>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusMetaType>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QApplication>
#include <QClipboard>
#include <QDesktopWidget>
#include <QEvent>
#include <QMouseEvent>
#include <QScreen>
#include <QWidget>
#include <KWindowSystem>
Q_DECLARE_METATYPE(QColor)
QDBusArgument &operator<< (QDBusArgument &argument, const QColor &color)
{
argument.beginStructure();
argument << color.rgba();
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &argument, QColor &color)
{
argument.beginStructure();
QRgb rgba;
argument >> rgba;
argument.endStructure();
color = QColor::fromRgba(rgba);
return argument;
}
Grabber::Grabber(QObject *parent)
: QObject(parent)
{
}
Grabber::~Grabber() = default;
void Grabber::setColor(const QColor &color)
{
if (m_color == color) {
return;
}
m_color = color;
emit colorChanged();
}
X11Grabber::X11Grabber(QObject *parent)
: Grabber(parent)
, m_grabWidget(new QWidget(nullptr, Qt::BypassWindowManagerHint))
{
m_grabWidget->move(-5000, -5000);
}
X11Grabber::~X11Grabber()
{
delete m_grabWidget;
}
void X11Grabber::pick()
{
// TODO pretend the mouse went somewhere else to prevent the tooltip from spawning
m_grabWidget->show();
m_grabWidget->installEventFilter(this);
m_grabWidget->grabMouse(Qt::CrossCursor);
}
bool X11Grabber::eventFilter(QObject *watched, QEvent *event)
{
if (watched == m_grabWidget && event->type() == QEvent::MouseButtonRelease) {
m_grabWidget->removeEventFilter(this);
m_grabWidget->hide();
m_grabWidget->releaseMouse();
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (me->button() == Qt::LeftButton) {
const QPoint pos = me->globalPos();
const QDesktopWidget *desktop = QApplication::desktop();
const QPixmap pixmap = QGuiApplication::screens().at(desktop->screenNumber())->grabWindow(desktop->winId(),
pos.x(), pos.y(), 1, 1);
if (!pixmap.isNull()) {
QImage i = pixmap.toImage();
QColor color(i.pixel(0, 0));
setColor(color);
}
}
}
return QObject::eventFilter(watched, event);
}
KWinWaylandGrabber::KWinWaylandGrabber(QObject *parent)
: Grabber(parent)
{
qDBusRegisterMetaType<QColor>();
}
KWinWaylandGrabber::~KWinWaylandGrabber() = default;
void KWinWaylandGrabber::pick()
{
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
QStringLiteral("/ColorPicker"),
QStringLiteral("org.kde.kwin.ColorPicker"),
QStringLiteral("pick"));
auto call = QDBusConnection::sessionBus().asyncCall(msg);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this,
[this] (QDBusPendingCallWatcher *watcher) {
watcher->deleteLater();
QDBusPendingReply<QColor> reply = *watcher;
if (!reply.isError()) {
setColor(reply.value());
}
}
);
}
GrabWidget::GrabWidget(QObject *parent)
: QObject(parent)
{
if (KWindowSystem::isPlatformX11()) {
m_grabber = new X11Grabber(this);
} else if (KWindowSystem::isPlatformWayland()) {
m_grabber = new KWinWaylandGrabber(this);
}
if (m_grabber) {
connect(m_grabber, &Grabber::colorChanged, this, &GrabWidget::currentColorChanged);
}
}
GrabWidget::~GrabWidget() = default;
QColor GrabWidget::currentColor() const
{
return m_grabber ? m_grabber->color() : QColor();
}
void GrabWidget::pick()
{
if (m_grabber) {
m_grabber->pick();
}
}
void GrabWidget::copyToClipboard(const QString &text)
{
QApplication::clipboard()->setText(text);
}
|