]> git.sesse.net Git - kdenlive/blob - src/colorpickerwidget.cpp
Make parameter name visible again in color chooser 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 <QPushButton>
26 #include <QLabel>
27 #include <QSpinBox>
28 #include <QDesktopWidget>
29
30 #include <KApplication>
31 #include <KColorDialog>
32 #include <KIcon>
33 #include <KLocalizedString>
34 #include <KDebug>
35
36 #ifdef Q_WS_X11
37 #include <X11/Xlib.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 #endif
64
65     QHBoxLayout *layout = new QHBoxLayout(this);
66
67     QPushButton *button = new QPushButton(this);
68     button->setIcon(KIcon("color-picker"));
69     button->setToolTip(i18n("Pick a color on the screen"));
70     connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
71
72     m_size = new QSpinBox(this);
73     m_size->setMinimum(1);
74     //m_size->setMaximum(qMin(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
75     m_size->setMaximum(100);
76     m_size->setValue(1);
77
78     layout->addWidget(button);
79     layout->addStretch(1);
80     layout->addWidget(new QLabel(i18n("Width of square to pick color from:")));
81     layout->addWidget(m_size);
82 }
83
84 ColorPickerWidget::~ColorPickerWidget()
85 {
86 #ifdef Q_WS_X11
87     if (m_filterActive && kapp)
88         kapp->removeX11EventFilter(m_filter);
89 #endif
90 }
91
92 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
93 {
94     int size = m_size->value();
95     int x0 = qMax(0, pos.x() - size / 2); 
96     int y0 = qMax(0, pos.y() - size / 2);
97     int x1 = qMin(qApp->desktop()->geometry().width(), pos.x() + size / 2);
98     int y1 = qMin(qApp->desktop()->geometry().height(), pos.y() + size / 2);
99
100     int sumR = 0;
101     int sumG = 0;
102     int sumB = 0;
103
104     for (int i = x0; i < x1; ++i) {
105         for (int j = y0; j < y1; ++j) {
106             QColor color = KColorDialog::grabColor(QPoint(i, j));
107             sumR += color.red();
108             sumG += color.green();
109             sumB += color.blue();
110         }
111     }
112
113     int numPixel = (x1 - x0) * (y1 - y0);
114     return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
115 }
116
117 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
118 {
119     if (event->button() != Qt::LeftButton) {
120         closeEventFilter();
121         event->accept();
122         return;
123     }
124     QWidget::mousePressEvent(event);
125 }
126
127 void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
128 {
129     if (m_filterActive) {
130         closeEventFilter();
131
132         if (m_size->value() == 1)
133             emit colorPicked(KColorDialog::grabColor(event->globalPos()));
134         else
135             emit colorPicked(averagePickedColor(event->globalPos()));
136
137         return;
138     }
139     QWidget::mouseReleaseEvent(event);
140 }
141
142 void ColorPickerWidget::keyPressEvent(QKeyEvent *event)
143 {
144     if (m_filterActive) {
145         // "special keys" (non letter, numeral) do not work, so close for every key
146         //if (event->key() == Qt::Key_Escape)
147         closeEventFilter();
148         event->accept();
149         return;
150     }
151     QWidget::keyPressEvent(event);
152 }
153
154 void ColorPickerWidget::slotSetupEventFilter()
155 {
156     m_filterActive = true;
157 #ifdef Q_WS_X11
158     m_filter = new KCDPickerFilter(this);
159     kapp->installX11EventFilter(m_filter);
160 #endif
161     if (m_size->value() == 1)
162         grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
163     else
164         grabMouse(Qt::CrossCursor);
165     grabKeyboard();
166 }
167
168 void ColorPickerWidget::closeEventFilter()
169 {
170     m_filterActive = false;
171 #ifdef Q_WS_X11
172     kapp->removeX11EventFilter(m_filter);
173     delete m_filter;
174     m_filter = 0;
175 #endif
176     releaseMouse();
177     releaseKeyboard();
178 }
179
180 #include "colorpickerwidget.moc"