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>
34 #include <KLocalizedString>
37 #include <X11/Xutil.h>
42 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
50 QHBoxLayout *layout = new QHBoxLayout(this);
51 layout->setContentsMargins(0, 0, 0, 0);
53 QToolButton *button = new QToolButton(this);
54 button->setIcon(KIcon("color-picker"));
55 // TODO: make translatable after 0.8.2 release
56 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."));
57 button->setAutoRaise(true);
58 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
60 layout->addWidget(button);
61 setFocusPolicy(Qt::StrongFocus);
63 m_grabRectFrame = new QFrame();
64 m_grabRectFrame->setFrameStyle(QFrame::Box | QFrame::Plain);
65 m_grabRectFrame->setWindowOpacity(0.5);
66 m_grabRectFrame->setWindowFlags(Qt::FramelessWindowHint);
67 m_grabRectFrame->hide();
70 ColorPickerWidget::~ColorPickerWidget()
72 delete m_grabRectFrame;
73 if (m_filterActive) removeEventFilter(this);
76 void ColorPickerWidget::slotGetAverageColor()
78 m_grabRect = m_grabRect.normalized();
80 int numPixel = m_grabRect.width() * m_grabRect.height();
86 // only show message for larger rects because of the overhead displayMessage creates
88 emit displayMessage(i18n("Requesting color information..."), 0);
91 Only getting the image once for the whole rect
92 results in a vast speed improvement.
95 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
96 m_image = XGetImage(QX11Info::display(), root, m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height(), -1, ZPixmap);
98 QWidget *desktop = QApplication::desktop();
99 m_image = QPixmap::grabWindow(desktop->winId(), m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height()).toImage();
102 for (int x = 0; x < m_grabRect.width(); ++x) {
103 for (int y = 0; y < m_grabRect.height(); ++y) {
104 QColor color = grabColor(QPoint(x, y), false);
106 sumG += color.green();
107 sumB += color.blue();
110 // Warning: slows things down, so don't do it for every pixel (the inner for loop)
111 if (numPixel > 40000)
112 emit displayMessage(i18n("Requesting color information..."), (int)(x * m_grabRect.height() / (qreal)numPixel * 100));
116 XDestroyImage(m_image);
120 if (numPixel > 40000)
121 emit displayMessage(i18n("Calculated average color for rectangle."), -1);
123 emit colorPicked(QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel));
126 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
128 if (event->button() != Qt::LeftButton) {
134 if (m_filterActive) {
135 m_grabRect = QRect(event->globalPos(), QSize(0, 0));
136 m_grabRectFrame->setGeometry(m_grabRect);
137 m_grabRectFrame->show();
140 QWidget::mousePressEvent(event);
143 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
145 if (m_filterActive) {
146 m_grabRectFrame->hide();
150 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
151 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
153 if (m_grabRect.width() * m_grabRect.height() == 0) {
154 emit colorPicked(grabColor(event->globalPos()));
156 // delay because m_grabRectFrame does not hide immediately
157 QTimer::singleShot(50, this, SLOT(slotGetAverageColor()));
162 QWidget::mouseReleaseEvent(event);
165 void ColorPickerWidget::mouseMoveEvent(QMouseEvent* event)
167 if (m_filterActive) {
168 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
169 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
170 m_grabRectFrame->setGeometry(m_grabRect.normalized());
172 QWidget::mouseMoveEvent(event);
175 void ColorPickerWidget::slotSetupEventFilter()
177 m_filterActive = true;
179 installEventFilter(this);
180 grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
184 void ColorPickerWidget::closeEventFilter()
186 m_filterActive = false;
189 removeEventFilter(this);
192 bool ColorPickerWidget::eventFilter(QObject *object, QEvent *event)
194 // Close color picker on any key press
195 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
197 event->setAccepted(true);
200 return QObject::eventFilter(object, event);
204 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
208 we use the X11 API directly in this case as we are not getting back a valid
209 return from QPixmap::grabWindow in the case where the application is using
212 if( !qApp->desktop()->geometry().contains( p ))
214 unsigned long xpixel;
215 if (m_image == NULL) {
216 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
217 m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
218 xpixel = XGetPixel(m_image, 0, 0);
220 xpixel = XGetPixel(m_image, p.x(), p.y());
223 XDestroyImage(m_image);
228 xcol.flags = DoRed | DoGreen | DoBlue;
229 XQueryColor(QX11Info::display(),
230 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
232 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
234 if (m_image.isNull()) {
235 QWidget *desktop = QApplication::desktop();
236 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
237 QImage i = pm.toImage();
238 return i.pixel(0, 0);
240 return m_image.pixel(p.x(), p.y());
245 #include "colorpickerwidget.moc"