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