]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
Fix small regression to previous fix for treating m_image as pointer.
[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
64     QHBoxLayout *layout = new QHBoxLayout(this);
65
66     QPushButton *button = new QPushButton(this);
67     button->setIcon(KIcon("color-picker"));
68     button->setToolTip(i18n("Pick a color on the screen"));
69     connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
70
71     m_size = new QSpinBox(this);
72     m_size->setMinimum(1);
73     // Use qMin here, as we might run into troubles with the cursor otherwise.
74     m_size->setMaximum(qMin(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
75     m_size->setValue(1);
76
77     layout->addWidget(button);
78     layout->addStretch(1);
79     layout->addWidget(new QLabel(i18n("Width of square to pick color from:")));
80     layout->addWidget(m_size);
81 }
82
83 ColorPickerWidget::~ColorPickerWidget()
84 {
85 #ifdef Q_WS_X11
86     if (m_filterActive && kapp)
87         kapp->removeX11EventFilter(m_filter);
88 #endif
89 }
90
91 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
92 {
93     int size = m_size->value();
94     int x0 = qMax(0, pos.x() - size / 2);
95     int y0 = qMax(0, pos.y() - size / 2);
96     int x1 = qMin(qApp->desktop()->geometry().width(), pos.x() + size / 2);
97     int y1 = qMin(qApp->desktop()->geometry().height(), pos.y() + size / 2);
98
99     // take care of loss when dividing odd sizes
100     if (size % 2 != 0) {
101         if (x1 < qApp->desktop()->geometry().width()) ++x1;
102         if (y1 < qApp->desktop()->geometry().height()) ++y1;
103     }
104
105     int numPixel = (x1 - x0) * (y1 - y0);
106
107     int sumR = 0;
108     int sumG = 0;
109     int sumB = 0;
110
111     // only show message for size > 200 because for smaller values it slows down to much
112     if (size > 200)
113         emit displayMessage(i18n("Requesting color information..."), 0);
114
115     /*
116      Only getting the image once for the whole rect
117      results in a vast speed improvement.
118     */
119 #ifdef Q_WS_X11
120     Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
121     m_image = XGetImage(QX11Info::display(), root, x0, y0, x1 - x0, y1 - y0, -1, ZPixmap);
122 #else
123     QWidget *desktop = QApplication::desktop();
124     m_image = QPixmap::grabWindow(desktop->winId(), x0, y0, x1 - x0, y1 - y0).toImage();
125 #endif
126
127     for (int i = x0; i < x1; ++i) {
128         for (int j = y0; j < y1; ++j) {
129             QColor color;
130             color = grabColor(QPoint(i - x0, j - y0), false);
131             sumR += color.red();
132             sumG += color.green();
133             sumB += color.blue();
134         }
135
136         // Warning: slows things down, so don't do it for every pixel (the inner for loop)
137         if (size > 200)
138             emit displayMessage(i18n("Requesting color information..."), (int)(((i - x0) * (y1 - y0)) / (qreal)numPixel * 100));
139     }
140
141 #ifdef Q_WS_X11
142     XDestroyImage(m_image);
143 #endif
144
145     if (size > 200)
146         emit displayMessage(i18n("Calculated average color for rectangle."), -1);
147
148     return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
149 }
150
151 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
152 {
153     if (event->button() != Qt::LeftButton) {
154         closeEventFilter();
155         event->accept();
156         return;
157     }
158     QWidget::mousePressEvent(event);
159 }
160
161 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
162 {
163     if (m_filterActive) {
164         closeEventFilter();
165
166         if (m_size->value() == 1)
167             emit colorPicked(grabColor(event->globalPos()));
168         else
169             emit colorPicked(averagePickedColor(event->globalPos()));
170
171         return;
172     }
173     QWidget::mouseReleaseEvent(event);
174 }
175
176 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
177 {
178     if (m_filterActive) {
179         // "special keys" (non letter, numeral) do not work, so close for every key
180         //if (event->key() == Qt::Key_Escape)
181         closeEventFilter();
182         event->accept();
183         return;
184     }
185     QWidget::keyPressEvent(event);
186 }
187
188 void ColorPickerWidget::slotSetupEventFilter()
189 {
190     m_filterActive = true;
191 #ifdef Q_WS_X11
192     m_filter = new KCDPickerFilter(this);
193     kapp->installX11EventFilter(m_filter);
194 #endif
195     if (m_size->value() < 10)
196         grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
197     else
198         grabMouse(QCursor(KIcon("kdenlive-select-all").pixmap(m_size->value(), m_size->value())));
199     grabKeyboard();
200 }
201
202 void ColorPickerWidget::closeEventFilter()
203 {
204     m_filterActive = false;
205 #ifdef Q_WS_X11
206     kapp->removeX11EventFilter(m_filter);
207     delete m_filter;
208     m_filter = 0;
209 #endif
210     releaseMouse();
211     releaseKeyboard();
212 }
213
214 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
215 {
216 #ifdef Q_WS_X11
217     /*
218      we use the X11 API directly in this case as we are not getting back a valid
219      return from QPixmap::grabWindow in the case where the application is using
220      an argb visual
221     */
222     if( !qApp->desktop()->geometry().contains( p ))
223         return QColor();
224     unsigned long xpixel;
225     if (m_image == 0) {
226         Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
227         m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
228         xpixel = XGetPixel(m_image, 0, 0);
229     } else {
230         xpixel = XGetPixel(m_image, p.x(), p.y());
231     }
232     if (destroyImage) {
233         XDestroyImage(m_image);
234         m_image = 0;
235     }
236     XColor xcol;
237     xcol.pixel = xpixel;
238     xcol.flags = DoRed | DoGreen | DoBlue;
239     XQueryColor(QX11Info::display(),
240                 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
241                 &xcol);
242     return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
243 #else
244     if (m_image.isNull()) {
245         QWidget *desktop = QApplication::desktop();
246         QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
247         QImage i = pm.toImage();
248         return i.pixel(0, 0);
249     } else {
250         return m_image.pixel(p.x(), p.y());
251     }
252 #endif
253 }
254
255 #include "colorpickerwidget.moc"