]> git.sesse.net Git - kdenlive/commitdiff
Add a color picker widget to pick a color on the screen, without having to open the...
authorTill Theato <root@ttill.de>
Thu, 29 Jul 2010 22:16:33 +0000 (22:16 +0000)
committerTill Theato <root@ttill.de>
Thu, 29 Jul 2010 22:16:33 +0000 (22:16 +0000)
svn path=/trunk/kdenlive/; revision=4662

src/CMakeLists.txt
src/choosecolorwidget.cpp [new file with mode: 0644]
src/choosecolorwidget.h [new file with mode: 0644]
src/colorpickerwidget.cpp [new file with mode: 0644]
src/colorpickerwidget.h [new file with mode: 0644]
src/effectstackedit.cpp

index dff5a77f83840431d969cb29b57f4233340f4024..24fe467e1fe1b89fd583d62fedd118af1dacaf9a 100644 (file)
@@ -210,6 +210,8 @@ set(kdenlive_SRCS
   colorcorrection/vectorscopegenerator.cpp
   colorcorrection/waveformgenerator.cpp
   razorgroupcommand.cpp
+  colorpickerwidget.cpp
+  choosecolorwidget.cpp
 )
 
 
diff --git a/src/choosecolorwidget.cpp b/src/choosecolorwidget.cpp
new file mode 100644 (file)
index 0000000..fce036f
--- /dev/null
@@ -0,0 +1,56 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
+
+#include "choosecolorwidget.h"
+#include "colorpickerwidget.h"
+
+#include <QLabel>
+#include <QHBoxLayout>
+
+#include <KColorButton>
+#include <KLocalizedString>
+
+
+ChooseColorWidget::ChooseColorWidget(QString text, QColor color, QWidget *parent) :
+        QWidget(parent)
+{
+    QHBoxLayout *layout = new QHBoxLayout(this);
+    layout->addWidget(new QLabel(text));
+    m_button = new KColorButton(color, this);
+    layout->addWidget(m_button);
+    ColorPickerWidget *picker = new ColorPickerWidget(this);
+    layout->addWidget(picker);
+
+    connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
+    connect(m_button, SIGNAL(clicked(bool)), this, SIGNAL(modified()));
+}
+
+QColor ChooseColorWidget::getColor()
+{
+    return m_button->color();
+}
+
+void ChooseColorWidget::setColor(QColor color)
+{
+    m_button->setColor(color);
+    emit modified();
+}
+
+#include "choosecolorwidget.moc"
diff --git a/src/choosecolorwidget.h b/src/choosecolorwidget.h
new file mode 100644 (file)
index 0000000..a379e5c
--- /dev/null
@@ -0,0 +1,59 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
+
+#ifndef CHOOSECOLORWIDGET_H
+#define CHOOSECOLORWIDGET_H
+
+#include <QtCore>
+#include <QWidget>
+
+class KColorButton;
+
+/**
+ * @class ChooseColorWidget
+ * @brief Provides options to choose a color.
+ * @author Till Theato
+ */
+
+class ChooseColorWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    /** @brief Sets up the widget.
+    * @param text (optional) What the color will be used for
+    * @param color (optional) initial color */
+    ChooseColorWidget(QString text = QString(), QColor color = QColor(), QWidget* parent = 0);
+
+    /** @brief Gets the choosen color. */
+    QColor getColor();
+
+private:
+    KColorButton *m_button;
+
+private slots:
+    /** @brief Updates the different color choosing options to have all selected @param color. */
+    void setColor(QColor color);
+
+signals:
+    /** @brief Emitted whenever a different color was choosen. */
+    void modified();
+};
+
+#endif
diff --git a/src/colorpickerwidget.cpp b/src/colorpickerwidget.cpp
new file mode 100644 (file)
index 0000000..66f828d
--- /dev/null
@@ -0,0 +1,122 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
+
+#include "colorpickerwidget.h"
+
+#include <QMouseEvent>
+#include <QPushButton>
+#include <QDesktopWidget>
+
+#include <KApplication>
+#include <KColorDialog>
+#include <KIcon>
+#include <KLocalizedString>
+
+#ifdef Q_WS_X11
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <QX11Info>
+#include <fixx11h.h>
+
+class KCDPickerFilter: public QWidget
+{
+public:
+    KCDPickerFilter(QWidget* parent): QWidget(parent) {}
+
+    virtual bool x11Event(XEvent* event) {
+        if (event->type == ButtonRelease) {
+            QMouseEvent e(QEvent::MouseButtonRelease, QPoint(),
+            QPoint(event->xmotion.x_root, event->xmotion.y_root) , Qt::NoButton, Qt::NoButton, Qt::NoModifier);
+            QApplication::sendEvent(parentWidget(), &e);
+            return true;
+        }
+        return false;
+    }
+};
+#endif 
+
+
+ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
+        QWidget(parent),
+        m_filterActive(false)
+{
+#ifdef Q_WS_X11
+    m_filter = 0;
+#endif
+
+    QPushButton *button = new QPushButton(this);
+    button->setIcon(KIcon("color-picker"));
+    button->setToolTip(i18n("Pick a color on the screen"));
+    connect(button, SIGNAL(clicked()), this, SLOT(slotSetupEventFilter()));
+}
+
+ColorPickerWidget::~ColorPickerWidget()
+{
+#ifdef Q_WS_X11
+    if (m_filterActive && kapp)
+        kapp->removeX11EventFilter(m_filter);
+#endif
+}
+
+void ColorPickerWidget::mouseReleaseEvent(QMouseEvent* event)
+{
+    if (m_filterActive) {
+        closeEventFilter();
+        emit colorPicked(KColorDialog::grabColor(event->globalPos()));
+        return;
+    }
+    QWidget::mouseReleaseEvent(event);
+}
+
+void ColorPickerWidget::keyPressEvent(QKeyEvent* event)
+{
+    if (m_filterActive) {
+        if (event->key() == Qt::Key_Escape)
+            closeEventFilter();
+        event->accept();
+        return;
+    }
+    QWidget::keyPressEvent(event);
+}
+
+void ColorPickerWidget::slotSetupEventFilter()
+{
+    m_filterActive = true;
+#ifdef Q_WS_X11
+    m_filter = new KCDPickerFilter(this);
+    kapp->installX11EventFilter(m_filter);
+#endif
+    grabMouse(Qt::CrossCursor);
+    grabKeyboard();
+}
+
+void ColorPickerWidget::closeEventFilter()
+{
+    m_filterActive = false;
+#ifdef Q_WS_X11
+    kapp->removeX11EventFilter(m_filter);
+    delete m_filter;
+    m_filter = 0;
+#endif
+    releaseMouse();
+    releaseKeyboard();
+}
+
+#include "colorpickerwidget.moc"
diff --git a/src/colorpickerwidget.h b/src/colorpickerwidget.h
new file mode 100644 (file)
index 0000000..f85e0f4
--- /dev/null
@@ -0,0 +1,69 @@
+/***************************************************************************
+ *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
+ ***************************************************************************/
+
+
+#ifndef COLORPICKERWIDGET_H
+#define COLORPICKERWIDGET_H
+
+#include <QtCore>
+#include <QWidget>
+
+#ifdef Q_WS_X11
+class KCDPickerFilter;
+#endif
+
+/**
+ * @class ColorPickerWidget
+ * @brief A widget to pick a color anywhere on the screen.
+ * @author Till Theato
+ *
+ * The code is based on the color picker in KColorDialog. 
+ */
+
+class ColorPickerWidget : public QWidget
+{
+    Q_OBJECT
+
+public:
+    /** @brief Sets up the widget. */
+    ColorPickerWidget(QWidget *parent = 0);
+    /** @brief Makes sure the event filter is removed. */
+    virtual ~ColorPickerWidget();
+
+protected:
+    virtual void mouseReleaseEvent(QMouseEvent *event);
+    virtual void keyPressEvent(QKeyEvent *event);
+
+private:
+    /** @brief Closes the event filter and makes mouse and keyboard work again on other widgets/windows. */
+    void closeEventFilter();
+    bool m_filterActive;
+#ifdef Q_WS_X11
+    KCDPickerFilter *m_filter;
+#endif 
+
+private slots:
+    /** @brief Sets up an event filter for picking a color. */
+    void slotSetupEventFilter();
+
+signals:
+    void colorPicked(QColor);
+};
+
+#endif
index a496a6ec229de690cc876f0ad6797e56975d5151..e7c38fb072746d9b366a351e943c9ab7cc328869 100644 (file)
@@ -31,6 +31,7 @@
 #include "profilesdialog.h"
 #include "kis_curve_widget.h"
 #include "kis_cubic_curve.h"
+#include "choosecolorwidget.h"
 
 #include <KDebug>
 #include <KLocale>
@@ -285,16 +286,13 @@ void EffectStackEdit::transferParamDesc(const QDomElement d, int pos, int in, in
                 m_keyframeEditor->addParameter(pa);
             }
         } else if (type == "color") {
-            Colorval *cval = new Colorval;
-            cval->setupUi(toFillin);
+            if (value.startsWith('#'))
+                value = value.replace('#', "0x");
             bool ok;
-            if (value.startsWith('#')) value = value.replace('#', "0x");
-            cval->kcolorbutton->setColor(value.toUInt(&ok, 16));
-            //kDebug() << "color: " << value << ", " << value.toUInt(&ok, 16);
-            cval->label->setText(paramName);
-            m_valueItems[paramName] = cval;
-            connect(cval->kcolorbutton, SIGNAL(clicked()) , this, SLOT(collectAllParameters()));
-            m_uiItems.append(cval);
+            ChooseColorWidget *choosecolor = new ChooseColorWidget(paramName, QColor(value.toUInt(&ok, 16)), this);
+            m_vbox->addWidget(choosecolor);
+            m_valueItems[paramName] = choosecolor;
+            connect(choosecolor, SIGNAL(modified()) , this, SLOT(collectAllParameters()));
         } else if (type == "position") {
             int pos = value.toInt();
             if (d.attribute("id") == "fadein" || d.attribute("id") == "fade_from_black") {
@@ -532,8 +530,8 @@ void EffectStackEdit::collectAllParameters()
             QCheckBox *box = ((Boolval*)m_valueItems.value(paramName))->checkBox;
             setValue = box->checkState() == Qt::Checked ? "1" : "0" ;
         } else if (type == "color") {
-            KColorButton *color = ((Colorval*)m_valueItems.value(paramName))->kcolorbutton;
-            setValue = color->color().name();
+            ChooseColorWidget *choosecolor = ((ChooseColorWidget*)m_valueItems.value(paramName));
+            setValue = choosecolor->getColor().name();
         } else if (type == "complex") {
             ComplexParameter *complex = ((ComplexParameter*)m_valueItems.value(paramName));
             namenode.item(i) = complex->getParamDesc();