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>
41 MyFrame::MyFrame(QWidget* parent) :
44 setFrameStyle(QFrame::Box | QFrame::Plain);
45 setWindowOpacity(0.5);
46 setWindowFlags(Qt::FramelessWindowHint);
50 void MyFrame::hideEvent ( QHideEvent * event )
52 QFrame::hideEvent(event);
53 // We need a timer here since hiding the frame will trigger a monitor refresh timer that will
54 // repaint the monitor after 70 ms.
55 QTimer::singleShot(250, this, SIGNAL(getColor()));
59 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
67 QHBoxLayout *layout = new QHBoxLayout(this);
68 layout->setContentsMargins(0, 0, 0, 0);
70 QToolButton *button = new QToolButton(this);
71 button->setIcon(KIcon("color-picker"));
72 button->setToolTip("<p>" + i18n("Pick a color on the screen. By pressing the mouse button and then moving your mouse you can select a section of the screen from which to get an average color.") + "</p>");
73 button->setAutoRaise(true);
74 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
76 layout->addWidget(button);
77 setFocusPolicy(Qt::StrongFocus);
79 m_grabRectFrame = new MyFrame();
80 m_grabRectFrame->hide();
83 ColorPickerWidget::~ColorPickerWidget()
85 delete m_grabRectFrame;
86 if (m_filterActive) removeEventFilter(this);
89 void ColorPickerWidget::slotGetAverageColor()
91 disconnect(m_grabRectFrame, SIGNAL(getColor()), this, SLOT(slotGetAverageColor()));
92 m_grabRect = m_grabRect.normalized();
94 int numPixel = m_grabRect.width() * m_grabRect.height();
100 // only show message for larger rects because of the overhead displayMessage creates
101 if (numPixel > 40000)
102 emit displayMessage(i18n("Requesting color information..."), 0);
105 Only getting the image once for the whole rect
106 results in a vast speed improvement.
109 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
110 m_image = XGetImage(QX11Info::display(), root, m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height(), -1, ZPixmap);
112 QWidget *desktop = QApplication::desktop();
113 m_image = QPixmap::grabWindow(desktop->winId(), m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height()).toImage();
116 for (int x = 0; x < m_grabRect.width(); ++x) {
117 for (int y = 0; y < m_grabRect.height(); ++y) {
118 QColor color = grabColor(QPoint(x, y), false);
120 sumG += color.green();
121 sumB += color.blue();
124 // Warning: slows things down, so don't do it for every pixel (the inner for loop)
125 if (numPixel > 40000)
126 emit displayMessage(i18n("Requesting color information..."), (int)(x * m_grabRect.height() / (qreal)numPixel * 100));
130 XDestroyImage(m_image);
134 if (numPixel > 40000)
135 emit displayMessage(i18n("Calculated average color for rectangle."), -1);
137 emit colorPicked(QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel));
140 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
142 if (event->button() != Qt::LeftButton) {
148 if (m_filterActive) {
149 m_grabRect = QRect(event->globalPos(), QSize(0, 0));
150 m_grabRectFrame->setGeometry(m_grabRect);
151 m_grabRectFrame->show();
154 QWidget::mousePressEvent(event);
157 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
159 if (m_filterActive) {
162 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
163 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
165 if (m_grabRect.width() * m_grabRect.height() == 0) {
166 m_grabRectFrame->hide();
167 emit colorPicked(grabColor(event->globalPos()));
169 // delay because m_grabRectFrame does not hide immediately
170 connect(m_grabRectFrame, SIGNAL(getColor()), this, SLOT(slotGetAverageColor()));
171 m_grabRectFrame->hide();
176 QWidget::mouseReleaseEvent(event);
179 void ColorPickerWidget::mouseMoveEvent(QMouseEvent* event)
181 if (m_filterActive) {
182 m_grabRect.setWidth(event->globalX() - m_grabRect.x());
183 m_grabRect.setHeight(event->globalY() - m_grabRect.y());
184 m_grabRectFrame->setGeometry(m_grabRect.normalized());
186 QWidget::mouseMoveEvent(event);
189 void ColorPickerWidget::slotSetupEventFilter()
191 m_filterActive = true;
193 installEventFilter(this);
194 grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
198 void ColorPickerWidget::closeEventFilter()
200 m_filterActive = false;
203 removeEventFilter(this);
206 bool ColorPickerWidget::eventFilter(QObject *object, QEvent *event)
208 // Close color picker on any key press
209 if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) {
211 event->setAccepted(true);
214 return QObject::eventFilter(object, event);
218 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
222 we use the X11 API directly in this case as we are not getting back a valid
223 return from QPixmap::grabWindow in the case where the application is using
226 if( !qApp->desktop()->geometry().contains( p ))
228 unsigned long xpixel;
229 if (m_image == NULL) {
230 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
231 m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
232 xpixel = XGetPixel(m_image, 0, 0);
234 xpixel = XGetPixel(m_image, p.x(), p.y());
237 XDestroyImage(m_image);
242 xcol.flags = DoRed | DoGreen | DoBlue;
243 XQueryColor(QX11Info::display(),
244 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
246 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
248 if (m_image.isNull()) {
249 QWidget *desktop = QApplication::desktop();
250 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
251 QImage i = pm.toImage();
252 return i.pixel(0, 0);
254 return m_image.pixel(p.x(), p.y());
259 #include "colorpickerwidget.moc"