]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
Use Icon color-picker as cursor for color picker
[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
26 #include <KApplication>
27 #include <KColorDialog>
28 #include <KIcon>
29 #include <KLocalizedString>
30 #include <KDebug>
31
32 #ifdef Q_WS_X11
33 #include <X11/Xlib.h>
34
35 class KCDPickerFilter: public QWidget
36 {
37 public:
38     KCDPickerFilter(QWidget* parent): QWidget(parent) {}
39
40     virtual bool x11Event(XEvent* event) {
41         if (event->type == ButtonRelease) {
42             QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
43             QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
44             QApplication::sendEvent(parentWidget(), &e);
45             return true;
46         }
47         return false;
48     }
49 };
50 #endif 
51
52
53 ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
54         QWidget(parent),
55         m_filterActive(false)
56 {
57 #ifdef Q_WS_X11
58     m_filter = 0;
59 #endif
60
61     QPushButton *button = new QPushButton(this);
62     button->setIcon(KIcon("color-picker"));
63     button->setToolTip(i18n("Pick a color on the screen"));
64     connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
65 }
66
67 ColorPickerWidget::~ColorPickerWidget()
68 {
69 #ifdef Q_WS_X11
70     if (m_filterActive && kapp)
71         kapp->removeX11EventFilter(m_filter);
72 #endif
73 }
74
75 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
76 {
77     if (event->button() != Qt::LeftButton) {
78         closeEventFilter();
79         event->accept();
80         return;
81     }
82     QWidget::mousePressEvent(event);
83 }
84
85 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
86 {
87     if (m_filterActive) {
88         closeEventFilter();
89         // does not work this way
90         //if (event->button() == Qt::LeftButton)
91         emit colorPicked(KColorDialog::grabColor(event->globalPos() - QPoint(11, -10)));
92         return;
93     }
94     QWidget::mouseReleaseEvent(event);
95 }
96
97 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
98 {
99     if (m_filterActive) {
100         // "special keys" (non letter, numeral) do not work, so close for every key
101         //if (event->key() == Qt::Key_Escape)
102         closeEventFilter();
103         event->accept();
104         return;
105     }
106     QWidget::keyPressEvent(event);
107 }
108
109 void ColorPickerWidget::slotSetupEventFilter()
110 {
111     m_filterActive = true;
112 #ifdef Q_WS_X11
113     m_filter = new KCDPickerFilter(this);
114     kapp->installX11EventFilter(m_filter);
115 #endif
116     grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22)));
117     grabKeyboard();
118 }
119
120 void ColorPickerWidget::closeEventFilter()
121 {
122     m_filterActive = false;
123 #ifdef Q_WS_X11
124     kapp->removeX11EventFilter(m_filter);
125     delete m_filter;
126     m_filter = 0;
127 #endif
128     releaseMouse();
129     releaseKeyboard();
130 }
131
132 #include "colorpickerwidget.moc"