]> git.sesse.net Git - kdenlive/commitdiff
Color Picker: Implement picking average color in rect. Warning: Very slow for now.
authorTill Theato <root@ttill.de>
Sat, 31 Jul 2010 13:14:38 +0000 (13:14 +0000)
committerTill Theato <root@ttill.de>
Sat, 31 Jul 2010 13:14:38 +0000 (13:14 +0000)
svn path=/trunk/kdenlive/; revision=4668

src/choosecolorwidget.cpp
src/colorpickerwidget.cpp
src/colorpickerwidget.h

index fce036f916f7a67786a1f9634151266cf35760b1..e65a8defb292462b99526690d09770d31e6d589e 100644 (file)
@@ -22,7 +22,8 @@
 #include "colorpickerwidget.h"
 
 #include <QLabel>
-#include <QHBoxLayout>
+#include <QVBoxLayout>
+#include <QGroupBox>
 
 #include <KColorButton>
 #include <KLocalizedString>
 ChooseColorWidget::ChooseColorWidget(QString text, QColor color, QWidget *parent) :
         QWidget(parent)
 {
-    QHBoxLayout *layout = new QHBoxLayout(this);
-    layout->addWidget(new QLabel(text));
+    //QGroupBox *box = new QGroupBox(text, this);
+    QVBoxLayout *layout = new QVBoxLayout(this);
+
     m_button = new KColorButton(color, this);
-    layout->addWidget(m_button);
     ColorPickerWidget *picker = new ColorPickerWidget(this);
+
+    layout->addWidget(m_button);
     layout->addWidget(picker);
 
     connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
index 8b7bc2d4bf2c36e8b9066714c52ea1affbb1d304..b2b567ad6781e07d28861e86b3be8c174fcfdeab 100644 (file)
 #include "colorpickerwidget.h"
 
 #include <QMouseEvent>
+#include <QHBoxLayout>
 #include <QPushButton>
+#include <QLabel>
+#include <QSpinBox>
+#include <QDesktopWidget>
 
 #include <KApplication>
 #include <KColorDialog>
@@ -58,10 +62,23 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
     m_filter = 0;
 #endif
 
+    QHBoxLayout *layout = new QHBoxLayout(this);
+
     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()));
+
+    m_size = new QSpinBox(this);
+    m_size->setMinimum(1);
+    //m_size->setMaximum(qMin(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
+    m_size->setMaximum(100);
+    m_size->setValue(1);
+
+    layout->addWidget(button);
+    layout->addStretch(1);
+    layout->addWidget(new QLabel(i18n("Width of rect to pick color from:")));
+    layout->addWidget(m_size);
 }
 
 ColorPickerWidget::~ColorPickerWidget()
@@ -72,6 +89,31 @@ ColorPickerWidget::~ColorPickerWidget()
 #endif
 }
 
+QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
+{
+    int size = m_size->value();
+    int x0 = qMax(0, pos.x() - size / 2); 
+    int y0 = qMax(0, pos.y() - size / 2);
+    int x1 = qMin(qApp->desktop()->geometry().width(), pos.x() + size / 2);
+    int y1 = qMin(qApp->desktop()->geometry().height(), pos.y() + size / 2);
+
+    int sumR = 0;
+    int sumG = 0;
+    int sumB = 0;
+
+    for (int i = x0; i < x1; ++i) {
+        for (int j = y0; j < y1; ++j) {
+            QColor color = KColorDialog::grabColor(QPoint(i, j));
+            sumR += color.red();
+            sumG += color.green();
+            sumB += color.blue();
+        }
+    }
+
+    int numPixel = (x1 - x0) * (y1 - y0);
+    return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
+}
+
 void ColorPickerWidget::mousePressEvent(QMouseEvent* event)
 {
     if (event->button() != Qt::LeftButton) {
@@ -86,9 +128,12 @@ void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
 {
     if (m_filterActive) {
         closeEventFilter();
-        // does not work this way
-        //if (event->button() == Qt::LeftButton)
-        emit colorPicked(KColorDialog::grabColor(event->globalPos() - QPoint(11, -10)));
+
+        if (m_size->value() == 1)
+            emit colorPicked(KColorDialog::grabColor(event->globalPos()));
+        else
+            emit colorPicked(averagePickedColor(event->globalPos()));
+
         return;
     }
     QWidget::mouseReleaseEvent(event);
@@ -113,7 +158,10 @@ void ColorPickerWidget::slotSetupEventFilter()
     m_filter = new KCDPickerFilter(this);
     kapp->installX11EventFilter(m_filter);
 #endif
-    grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22)));
+    if (m_size->value() == 1)
+        grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
+    else
+        grabMouse(Qt::CrossCursor);
     grabKeyboard();
 }
 
index ffdda863dbe98e33e8ce3e48bdde49d0c7f694c9..fce8d789c7e4a6a93c4c2a3de1af12aa6aee3b13 100644 (file)
@@ -24,6 +24,7 @@
 #include <QtCore>
 #include <QWidget>
 
+class QSpinBox;
 #ifdef Q_WS_X11
 class KCDPickerFilter;
 #endif
@@ -54,7 +55,10 @@ protected:
 private:
     /** @brief Closes the event filter and makes mouse and keyboard work again on other widgets/windows. */
     void closeEventFilter();
+    /** @brief Calculates the average color for a rect around @param pos with m_size->value() as width. */
+    QColor averagePickedColor(const QPoint pos);
     bool m_filterActive;
+    QSpinBox *m_size;
 #ifdef Q_WS_X11
     KCDPickerFilter *m_filter;
 #endif