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>
26 #include <KApplication>
27 #include <KColorDialog>
29 #include <KLocalizedString>
35 class KCDPickerFilter: public QWidget
38 KCDPickerFilter(QWidget* parent): QWidget(parent) {}
40 virtual bool x11Event(XEvent* event) {
41 if (event->type == ButtonRelease) {
42 QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
43 QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
44 QApplication::sendEvent(parentWidget(), &e);
53 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
61 QPushButton *button = new QPushButton(this);
62 button->setIcon(KIcon("color-picker"));
63 button->setToolTip(i18n("Pick a color on the screen"));
64 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
67 ColorPickerWidget::~ColorPickerWidget()
70 if (m_filterActive && kapp)
71 kapp->removeX11EventFilter(m_filter);
75 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
77 if (event->button() != Qt::LeftButton) {
82 QWidget::mousePressEvent(event);
85 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
89 // does not work this way
90 //if (event->button() == Qt::LeftButton)
91 emit colorPicked(KColorDialog::grabColor(event->globalPos()));
94 QWidget::mouseReleaseEvent(event);
97 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
100 // "special keys" (non letter, numeral) do not work, so close for every key
101 //if (event->key() == Qt::Key_Escape)
106 QWidget::keyPressEvent(event);
109 void ColorPickerWidget::slotSetupEventFilter()
111 m_filterActive = true;
113 m_filter = new KCDPickerFilter(this);
114 kapp->installX11EventFilter(m_filter);
116 grabMouse(Qt::CrossCursor);
120 void ColorPickerWidget::closeEventFilter()
122 m_filterActive = false;
124 kapp->removeX11EventFilter(m_filter);
132 #include "colorpickerwidget.moc"