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