1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include "colorpickerwidget.h"
23 #include <QMouseEvent>
24 #include <QPushButton>
25 #include <QDesktopWidget>
27 #include <KApplication>
28 #include <KColorDialog>
30 #include <KLocalizedString>
34 #include <X11/Xutil.h>
38 class KCDPickerFilter: public QWidget
41 KCDPickerFilter(QWidget* parent): QWidget(parent) {}
43 virtual bool x11Event(XEvent* event) {
44 if (event->type == ButtonRelease) {
45 QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
46 QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
47 QApplication::sendEvent(parentWidget(), &e);
56 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
64 QPushButton *button = new QPushButton(this);
65 button->setIcon(KIcon("color-picker"));
66 button->setToolTip(i18n("Pick a color on the screen"));
67 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
70 ColorPickerWidget::~ColorPickerWidget()
73 if (m_filterActive && kapp)
74 kapp->removeX11EventFilter(m_filter);
78 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent* event)
82 emit colorPicked(KColorDialog::grabColor(event->globalPos()));
85 QWidget::mouseReleaseEvent(event);
88 void ColorPickerWidget::keyPressEvent(QKeyEvent* event)
91 if (event->key() == Qt::Key_Escape)
96 QWidget::keyPressEvent(event);
99 void ColorPickerWidget::slotSetupEventFilter()
101 m_filterActive = true;
103 m_filter = new KCDPickerFilter(this);
104 kapp->installX11EventFilter(m_filter);
106 grabMouse(Qt::CrossCursor);
110 void ColorPickerWidget::closeEventFilter()
112 m_filterActive = false;
114 kapp->removeX11EventFilter(m_filter);
122 #include "colorpickerwidget.moc"