]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
Color Picker: Also speed up picking a region for non X11 Desktops
[kdenlive] / src / colorpickerwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "colorpickerwidget.h"
22
23 #include <QMouseEvent>
24 #include <QHBoxLayout>
25 #include <QPushButton>
26 #include <QLabel>
27 #include <QSpinBox>
28 #include <QDesktopWidget>
29
30 #include <KApplication>
31 #include <KIcon>
32 #include <KLocalizedString>
33
34 #ifdef Q_WS_X11
35 #include <X11/Xutil.h>
36 #include <fixx11h.h>
37
38 class KCDPickerFilter: public QWidget
39 {
40 public:
41     KCDPickerFilter(QWidget* parent): QWidget(parent) {}
42
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);
48             return true;
49         }
50         return false;
51     }
52 };
53 #endif 
54
55
56 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
57         QWidget(parent),
58         m_filterActive(false)
59 {
60 #ifdef Q_WS_X11
61     m_filter = 0;
62 #endif
63     m_image = 0;
64
65     QHBoxLayout *layout = new QHBoxLayout(this);
66
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()));
71
72     m_size = new QSpinBox(this);
73     m_size->setMinimum(1);
74     // Use qMax here, to make it possible to get the average for the whole screen
75     m_size->setMaximum(qMax(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
76     m_size->setValue(1);
77
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);
82 }
83
84 ColorPickerWidget::~ColorPickerWidget()
85 {
86 #ifdef Q_WS_X11
87     if (m_filterActive && kapp)
88         kapp->removeX11EventFilter(m_filter);
89 #endif
90 }
91
92 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
93 {
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
100     int sumR = 0;
101     int sumG = 0;
102     int sumB = 0;
103
104     /*
105      Only getting the image once for the whole rect
106      results in a vast speed improvement.
107     */
108 #ifdef Q_WS_X11
109     Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
110     m_image = XGetImage(QX11Info::display(), root, x0, y0, x1 - x0, y1 - y0, -1, ZPixmap);
111 #else
112     QWidget *desktop = QApplication::desktop();
113     m_image = QPixmap::grabWindow(desktop->winId(), x0, y0, x1 - x0, y1 - y0).toImage();
114 #endif
115
116     for (int i = x0; i < x1; ++i) {
117         for (int j = y0; j < y1; ++j) {
118             QColor color;
119             color = grabColor(QPoint(i - x0, j - y0), false);
120             sumR += color.red();
121             sumG += color.green();
122             sumB += color.blue();
123         }
124     }
125
126 #ifdef Q_WS_X11
127     XDestroyImage(m_image);
128 #endif
129     m_image = 0;
130
131     int numPixel = (x1 - x0) * (y1 - y0);
132     return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
133 }
134
135 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
136 {
137     if (event->button() != Qt::LeftButton) {
138         closeEventFilter();
139         event->accept();
140         return;
141     }
142     QWidget::mousePressEvent(event);
143 }
144
145 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
146 {
147     if (m_filterActive) {
148         closeEventFilter();
149
150         if (m_size->value() == 1)
151             emit colorPicked(grabColor(event->globalPos()));
152         else
153             emit colorPicked(averagePickedColor(event->globalPos()));
154
155         return;
156     }
157     QWidget::mouseReleaseEvent(event);
158 }
159
160 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
161 {
162     if (m_filterActive) {
163         // "special keys" (non letter, numeral) do not work, so close for every key
164         //if (event->key() == Qt::Key_Escape)
165         closeEventFilter();
166         event->accept();
167         return;
168     }
169     QWidget::keyPressEvent(event);
170 }
171
172 void ColorPickerWidget::slotSetupEventFilter()
173 {
174     m_filterActive = true;
175 #ifdef Q_WS_X11
176     m_filter = new KCDPickerFilter(this);
177     kapp->installX11EventFilter(m_filter);
178 #endif
179     if (m_size->value() == 1)
180         grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
181     else
182         grabMouse(Qt::CrossCursor);
183     grabKeyboard();
184 }
185
186 void ColorPickerWidget::closeEventFilter()
187 {
188     m_filterActive = false;
189 #ifdef Q_WS_X11
190     kapp->removeX11EventFilter(m_filter);
191     delete m_filter;
192     m_filter = 0;
193 #endif
194     releaseMouse();
195     releaseKeyboard();
196 }
197
198 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
199 {
200 #ifdef Q_WS_X11
201     /*
202      we use the X11 API directly in this case as we are not getting back a valid
203      return from QPixmap::grabWindow in the case where the application is using
204      an argb visual
205     */
206     if( !qApp->desktop()->geometry().contains( p ))
207         return QColor();
208     unsigned long xpixel;
209     if (m_image == 0) {
210         Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
211         m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
212         xpixel = XGetPixel(m_image, 0, 0);
213     } else {
214         xpixel = XGetPixel(m_image, p.x(), p.y());
215     }
216     if (destroyImage) {
217         XDestroyImage(m_image);
218         m_image = 0;
219     }
220     XColor xcol;
221     xcol.pixel = xpixel;
222     xcol.flags = DoRed | DoGreen | DoBlue;
223     XQueryColor(QX11Info::display(),
224                 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
225                 &xcol);
226     return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
227 #else
228     if (m_image == 0) {
229         QWidget *desktop = QApplication::desktop();
230         QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
231         QImage i = pm.toImage();
232         return i.pixel(0, 0);
233     } else {
234         return m_image.pixel(p.x(), p.y());
235     }
236 #endif
237 }
238
239 #include "colorpickerwidget.moc"