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 <QHBoxLayout>
25 #include <QToolButton>
28 #include <QDesktopWidget>
31 #include <KApplication>
33 #include <KLocalizedString>
36 #include <X11/Xutil.h>
39 class KCDPickerFilter: public QWidget
42 KCDPickerFilter(QWidget* parent): QWidget(parent) {}
44 virtual bool x11Event(XEvent* event) {
45 if (event->type == ButtonRelease) {
46 QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
47 QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
48 QApplication::sendEvent(parentWidget(), &e);
57 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
66 QHBoxLayout *layout = new QHBoxLayout(this);
67 layout->setContentsMargins(0, 0, 0, 0);
69 QToolButton *button = new QToolButton(this);
70 button->setIcon(KIcon("color-picker"));
71 // TODO: make translatable after 0.8.2 release
72 button->setToolTip(i18n("Pick a color on the screen") + QString("\nBy pressing the mouse button and then moving your mouse you can select a section of the screen from which to get an average color."));
73 button->setAutoRaise(true);
74 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
76 layout->addWidget(button);
78 m_grabRectFrame = new QFrame();
79 m_grabRectFrame->setFrameStyle(QFrame::Box | QFrame::Plain);
80 m_grabRectFrame->setWindowOpacity(0.5);
81 m_grabRectFrame->setWindowFlags(Qt::FramelessWindowHint);
82 m_grabRectFrame->hide();
85 ColorPickerWidget::~ColorPickerWidget()
87 delete m_grabRectFrame;
89 if (m_filterActive && kapp)
90 kapp->removeX11EventFilter(m_filter);
94 void ColorPickerWidget::slotGetAverageColor()
96 m_grabRect = m_grabRect.normalized();
98 int numPixel = m_grabRect.width() * m_grabRect.height();
104 // only show message for larger rects because of the overhead displayMessage creates
105 if (numPixel > 40000)
106 emit displayMessage(i18n("Requesting color information..."), 0);
109 Only getting the image once for the whole rect
110 results in a vast speed improvement.
113 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
114 m_image = XGetImage(QX11Info::display(), root, m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height(), -1, ZPixmap);
116 QWidget *desktop = QApplication::desktop();
117 m_image = QPixmap::grabWindow(desktop->winId(), m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height()).toImage();
120 for (int x = 0; x < m_grabRect.width(); ++x) {
121 for (int y = 0; y < m_grabRect.height(); ++y) {
122 QColor color = grabColor(QPoint(x, y), false);
124 sumG += color.green();
125 sumB += color.blue();
128 // Warning: slows things down, so don't do it for every pixel (the inner for loop)
129 if (numPixel > 40000)
130 emit displayMessage(i18n("Requesting color information..."), (int)(x * m_grabRect.height() / (qreal)numPixel * 100));
134 XDestroyImage(m_image);
138 if (numPixel > 40000)
139 emit displayMessage(i18n("Calculated average color for rectangle."), -1);
141 emit colorPicked(QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel));
144 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
146 if (event->button() != Qt::LeftButton) {
152 if (m_filterActive) {
153 m_grabRect = QRect(event->globalPos(), QSize(0, 0));
154 m_grabRectFrame->setGeometry(m_grabRect);
155 m_grabRectFrame->show();
158 QWidget::mousePressEvent(event);
161 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
163 if (m_filterActive) {
164 m_grabRectFrame->hide();
168 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
169 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
171 if (m_grabRect.width() * m_grabRect.height() == 0) {
172 emit colorPicked(grabColor(event->globalPos()));
174 // delay because m_grabRectFrame does not hide immediately
175 QTimer::singleShot(50, this, SLOT(slotGetAverageColor()));
180 QWidget::mouseReleaseEvent(event);
183 void ColorPickerWidget::mouseMoveEvent(QMouseEvent* event)
185 if (m_filterActive) {
186 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
187 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
188 m_grabRectFrame->setGeometry(m_grabRect.normalized());
190 QWidget::mouseMoveEvent(event);
193 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
195 if (m_filterActive) {
196 // "special keys" (non letter, numeral) do not work, so close for every key
197 //if (event->key() == Qt::Key_Escape)
202 QWidget::keyPressEvent(event);
205 void ColorPickerWidget::slotSetupEventFilter()
207 m_filterActive = true;
209 m_filter = new KCDPickerFilter(this);
210 kapp->installX11EventFilter(m_filter);
212 grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
216 void ColorPickerWidget::closeEventFilter()
218 m_filterActive = false;
220 kapp->removeX11EventFilter(m_filter);
228 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
232 we use the X11 API directly in this case as we are not getting back a valid
233 return from QPixmap::grabWindow in the case where the application is using
236 if( !qApp->desktop()->geometry().contains( p ))
238 unsigned long xpixel;
239 if (m_image == NULL) {
240 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
241 m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
242 xpixel = XGetPixel(m_image, 0, 0);
244 xpixel = XGetPixel(m_image, p.x(), p.y());
247 XDestroyImage(m_image);
252 xcol.flags = DoRed | DoGreen | DoBlue;
253 XQueryColor(QX11Info::display(),
254 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
256 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
258 if (m_image.isNull()) {
259 QWidget *desktop = QApplication::desktop();
260 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
261 QImage i = pm.toImage();
262 return i.pixel(0, 0);
264 return m_image.pixel(p.x(), p.y());
269 #include "colorpickerwidget.moc"