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 <QPushButton>
28 #include <QDesktopWidget>
30 #include <KApplication>
32 #include <KLocalizedString>
35 #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) :
65 QHBoxLayout *layout = new QHBoxLayout(this);
67 QPushButton *button = new QPushButton(this);
68 button->setIcon(KIcon("color-picker"));
69 button->setToolTip(i18n("Pick a color on the screen"));
70 connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
72 m_size = new QSpinBox(this);
73 m_size->setMinimum(1);
74 // Use qMin here, as we might run into troubles with the cursor otherwise.
75 m_size->setMaximum(qMin(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
78 layout->addWidget(button);
79 layout->addStretch(1);
80 layout->addWidget(new QLabel(i18n("Width of square to pick color from:")));
81 layout->addWidget(m_size);
84 ColorPickerWidget::~ColorPickerWidget()
87 if (m_filterActive && kapp)
88 kapp->removeX11EventFilter(m_filter);
92 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
94 int size = m_size->value();
95 int x0 = qMax(0, pos.x() - size / 2);
96 int y0 = qMax(0, pos.y() - size / 2);
97 int x1 = qMin(qApp->desktop()->geometry().width(), pos.x() + size / 2);
98 int y1 = qMin(qApp->desktop()->geometry().height(), pos.y() + size / 2);
99 int numPixel = (x1 - x0) * (y1 - y0);
105 // only show message for size > 200 because for smaller values it slows down to much
107 emit displayMessage(i18n("Requesting color information..."), 0);
110 Only getting the image once for the whole rect
111 results in a vast speed improvement.
114 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
115 m_image = XGetImage(QX11Info::display(), root, x0, y0, x1 - x0, y1 - y0, -1, ZPixmap);
117 QWidget *desktop = QApplication::desktop();
118 m_image = QPixmap::grabWindow(desktop->winId(), x0, y0, x1 - x0, y1 - y0).toImage();
121 for (int i = x0; i <= x1; ++i) {
122 for (int j = y0; j <= y1; ++j) {
124 color = grabColor(QPoint(i - x0, j - y0), false);
126 sumG += color.green();
127 sumB += color.blue();
130 // Warning: slows things down, so don't do it for every pixel (the inner for loop)
132 emit displayMessage(i18n("Requesting color information..."), (int)(((i - x0) * (y1 - y0)) / (qreal)numPixel * 100));
136 XDestroyImage(m_image);
141 emit displayMessage(i18n("Calculated average color for rectangle."), -1);
143 return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
146 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
148 if (event->button() != Qt::LeftButton) {
153 QWidget::mousePressEvent(event);
156 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
158 if (m_filterActive) {
161 if (m_size->value() == 1)
162 emit colorPicked(grabColor(event->globalPos()));
164 emit colorPicked(averagePickedColor(event->globalPos()));
168 QWidget::mouseReleaseEvent(event);
171 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
173 if (m_filterActive) {
174 // "special keys" (non letter, numeral) do not work, so close for every key
175 //if (event->key() == Qt::Key_Escape)
180 QWidget::keyPressEvent(event);
183 void ColorPickerWidget::slotSetupEventFilter()
185 m_filterActive = true;
187 m_filter = new KCDPickerFilter(this);
188 kapp->installX11EventFilter(m_filter);
190 if (m_size->value() < 10)
191 grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
193 grabMouse(QCursor(KIcon("kdenlive-select-all").pixmap(m_size->value(), m_size->value())));
197 void ColorPickerWidget::closeEventFilter()
199 m_filterActive = false;
201 kapp->removeX11EventFilter(m_filter);
209 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
213 we use the X11 API directly in this case as we are not getting back a valid
214 return from QPixmap::grabWindow in the case where the application is using
217 if( !qApp->desktop()->geometry().contains( p ))
219 unsigned long xpixel;
221 Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
222 m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
223 xpixel = XGetPixel(m_image, 0, 0);
225 xpixel = XGetPixel(m_image, p.x(), p.y());
228 XDestroyImage(m_image);
233 xcol.flags = DoRed | DoGreen | DoBlue;
234 XQueryColor(QX11Info::display(),
235 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
237 return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
240 QWidget *desktop = QApplication::desktop();
241 QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
242 QImage i = pm.toImage();
243 return i.pixel(0, 0);
245 return m_image.pixel(p.x(), p.y());
250 #include "colorpickerwidget.moc"