]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
480ed8d13a61064e4ba856d8f34e8aa7a23530f1
[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 <QToolButton>
26 #include <QLabel>
27 #include <QSpinBox>
28 #include <QDesktopWidget>
29 #include <QFrame>
30
31 #include <KApplication>
32 #include <KIcon>
33 #include <KLocalizedString>
34
35 #ifdef Q_WS_X11
36 #include <X11/Xutil.h>
37 #include <fixx11h.h>
38
39 class KCDPickerFilter: public QWidget
40 {
41 public:
42     KCDPickerFilter(QWidget* parent): QWidget(parent) {}
43
44     virtual bool x11Event(XEvent* event) {
45         if (event->type == ButtonRelease) {
46             QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
47             QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
48             QApplication::sendEvent(parentWidget(), &e);
49             return true;
50         }
51         return false;
52     }
53 };
54 #endif 
55
56
57 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
58         QWidget(parent),
59         m_filterActive(false)
60 {
61 #ifdef Q_WS_X11
62     m_filter = 0;
63     m_image = NULL;
64 #endif
65
66     QHBoxLayout *layout = new QHBoxLayout(this);
67     layout->setContentsMargins(0, 0, 0, 0);
68
69     QToolButton *button = new QToolButton(this);
70     button->setIcon(KIcon("color-picker"));
71     // TODO: make translatable after 0.8.2 release
72     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."));
73     button->setAutoRaise(true);
74     connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
75
76     layout->addWidget(button);
77
78     m_grabRectFrame = new QFrame();
79     m_grabRectFrame->setFrameStyle(QFrame::Box | QFrame::Plain);
80     m_grabRectFrame->setWindowOpacity(0.5);
81     m_grabRectFrame->setWindowFlags(Qt::FramelessWindowHint);
82     m_grabRectFrame->hide();
83 }
84
85 ColorPickerWidget::~ColorPickerWidget()
86 {
87     delete m_grabRectFrame;
88 #ifdef Q_WS_X11
89     if (m_filterActive && kapp)
90         kapp->removeX11EventFilter(m_filter);
91 #endif
92 }
93
94 void ColorPickerWidget::slotGetAverageColor()
95 {
96     m_grabRect = m_grabRect.normalized();
97
98     int numPixel = m_grabRect.width() * m_grabRect.height();
99
100     int sumR = 0;
101     int sumG = 0;
102     int sumB = 0;
103
104     // only show message for larger rects because of the overhead displayMessage creates
105     if (numPixel > 40000)
106         emit displayMessage(i18n("Requesting color information..."), 0);
107
108     /*
109      Only getting the image once for the whole rect
110      results in a vast speed improvement.
111     */
112 #ifdef Q_WS_X11
113     Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
114     m_image = XGetImage(QX11Info::display(), root, m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height(), -1, ZPixmap);
115 #else
116     QWidget *desktop = QApplication::desktop();
117     m_image = QPixmap::grabWindow(desktop->winId(), m_grabRect.x(), m_grabRect.y(), m_grabRect.width(), m_grabRect.height()).toImage();
118 #endif
119
120     for (int x = 0; x < m_grabRect.width(); ++x) {
121         for (int y = 0; y < m_grabRect.height(); ++y) {
122             QColor color = grabColor(QPoint(x, y), false);
123             sumR += color.red();
124             sumG += color.green();
125             sumB += color.blue();
126         }
127
128         // Warning: slows things down, so don't do it for every pixel (the inner for loop)
129         if (numPixel > 40000)
130             emit displayMessage(i18n("Requesting color information..."), (int)(x * m_grabRect.height() / (qreal)numPixel * 100));
131     }
132
133 #ifdef Q_WS_X11
134     XDestroyImage(m_image);
135     m_image = NULL;
136 #endif
137
138     if (numPixel > 40000)
139         emit displayMessage(i18n("Calculated average color for rectangle."), -1);
140
141     emit colorPicked(QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel));
142 }
143
144 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
145 {
146     if (event->button() != Qt::LeftButton) {
147         closeEventFilter();
148         event->accept();
149         return;
150     }
151
152     if (m_filterActive) {
153         m_grabRect = QRect(event->globalPos(), QSize(0, 0));
154         m_grabRectFrame->setGeometry(m_grabRect);
155         m_grabRectFrame->show();
156     }
157
158     QWidget::mousePressEvent(event);
159 }
160
161 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
162 {
163     if (m_filterActive) {
164         m_grabRectFrame->hide();
165
166         closeEventFilter();
167
168         m_grabRect.setWidth(event->globalX() - m_grabRect.x());
169         m_grabRect.setHeight(event->globalY() - m_grabRect.y());
170
171         if (m_grabRect.width() * m_grabRect.height() == 0) {
172             emit colorPicked(grabColor(event->globalPos()));
173         } else {
174             // delay because m_grabRectFrame does not hide immediately
175             QTimer::singleShot(50, this, SLOT(slotGetAverageColor()));
176         }
177
178         return;
179     }
180     QWidget::mouseReleaseEvent(event);
181 }
182
183 void ColorPickerWidget::mouseMoveEvent(QMouseEvent* event)
184 {
185     if (m_filterActive) {
186         m_grabRect.setWidth(event->globalX() - m_grabRect.x());
187         m_grabRect.setHeight(event->globalY() - m_grabRect.y());
188         m_grabRectFrame->setGeometry(m_grabRect.normalized());
189     }
190     QWidget::mouseMoveEvent(event);
191 }
192
193 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
194 {
195     if (m_filterActive) {
196         // "special keys" (non letter, numeral) do not work, so close for every key
197         //if (event->key() == Qt::Key_Escape)
198         closeEventFilter();
199         event->accept();
200         return;
201     }
202     QWidget::keyPressEvent(event);
203 }
204
205 void ColorPickerWidget::slotSetupEventFilter()
206 {
207     m_filterActive = true;
208 #ifdef Q_WS_X11
209     m_filter = new KCDPickerFilter(this);
210     kapp->installX11EventFilter(m_filter);
211 #endif
212     grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
213     grabKeyboard();
214 }
215
216 void ColorPickerWidget::closeEventFilter()
217 {
218     m_filterActive = false;
219 #ifdef Q_WS_X11
220     kapp->removeX11EventFilter(m_filter);
221     delete m_filter;
222     m_filter = 0;
223 #endif
224     releaseMouse();
225     releaseKeyboard();
226 }
227
228 QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
229 {
230 #ifdef Q_WS_X11
231     /*
232      we use the X11 API directly in this case as we are not getting back a valid
233      return from QPixmap::grabWindow in the case where the application is using
234      an argb visual
235     */
236     if( !qApp->desktop()->geometry().contains( p ))
237         return QColor();
238     unsigned long xpixel;
239     if (m_image == NULL) {
240         Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
241         m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
242         xpixel = XGetPixel(m_image, 0, 0);
243     } else {
244         xpixel = XGetPixel(m_image, p.x(), p.y());
245     }
246     if (destroyImage) {
247         XDestroyImage(m_image);
248         m_image = 0;
249     }
250     XColor xcol;
251     xcol.pixel = xpixel;
252     xcol.flags = DoRed | DoGreen | DoBlue;
253     XQueryColor(QX11Info::display(),
254                 DefaultColormap(QX11Info::display(), QX11Info::appScreen()),
255                 &xcol);
256     return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
257 #else
258     if (m_image.isNull()) {
259         QWidget *desktop = QApplication::desktop();
260         QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
261         QImage i = pm.toImage();
262         return i.pixel(0, 0);
263     } else {
264         return m_image.pixel(p.x(), p.y());
265     }
266 #endif
267 }
268
269 #include "colorpickerwidget.moc"