]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
Add a color picker widget to pick a color on the screen, without having to open the...
[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 <QPushButton>
25 #include <QDesktopWidget>
26
27 #include <KApplication>
28 #include <KColorDialog>
29 #include <KIcon>
30 #include <KLocalizedString>
31
32 #ifdef Q_WS_X11
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include <QX11Info>
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     QPushButton *button = new QPushButton(this);
65     button->setIcon(KIcon("color-picker"));
66     button->setToolTip(i18n("Pick a color on the screen"));
67     connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
68 }
69
70 ColorPickerWidget::~ColorPickerWidget()
71 {
72 #ifdef Q_WS_X11
73     if (m_filterActive && kapp)
74         kapp->removeX11EventFilter(m_filter);
75 #endif
76 }
77
78 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent* event)
79 {
80     if (m_filterActive) {
81         closeEventFilter();
82         emit colorPicked(KColorDialog::grabColor(event->globalPos()));
83         return;
84     }
85     QWidget::mouseReleaseEvent(event);
86 }
87
88 void ColorPickerWidget::keyPressEvent(QKeyEvent* event)
89 {
90     if (m_filterActive) {
91         if (event->key() == Qt::Key_Escape)
92             closeEventFilter();
93         event->accept();
94         return;
95     }
96     QWidget::keyPressEvent(event);
97 }
98
99 void ColorPickerWidget::slotSetupEventFilter()
100 {
101     m_filterActive = true;
102 #ifdef Q_WS_X11
103     m_filter = new KCDPickerFilter(this);
104     kapp->installX11EventFilter(m_filter);
105 #endif
106     grabMouse(Qt::CrossCursor);
107     grabKeyboard();
108 }
109
110 void ColorPickerWidget::closeEventFilter()
111 {
112     m_filterActive = false;
113 #ifdef Q_WS_X11
114     kapp->removeX11EventFilter(m_filter);
115     delete m_filter;
116     m_filter = 0;
117 #endif
118     releaseMouse();
119     releaseKeyboard();
120 }
121
122 #include "colorpickerwidget.moc"