From 15c485914c3122e97cd8fe1271c0202d2d658c13 Mon Sep 17 00:00:00 2001 From: Till Theato Date: Sat, 31 Jul 2010 13:14:38 +0000 Subject: [PATCH] Color Picker: Implement picking average color in rect. Warning: Very slow for now. svn path=/trunk/kdenlive/; revision=4668 --- src/choosecolorwidget.cpp | 11 +++++--- src/colorpickerwidget.cpp | 56 ++++++++++++++++++++++++++++++++++++--- src/colorpickerwidget.h | 4 +++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/choosecolorwidget.cpp b/src/choosecolorwidget.cpp index fce036f9..e65a8def 100644 --- a/src/choosecolorwidget.cpp +++ b/src/choosecolorwidget.cpp @@ -22,7 +22,8 @@ #include "colorpickerwidget.h" #include -#include +#include +#include #include #include @@ -31,11 +32,13 @@ 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))); diff --git a/src/colorpickerwidget.cpp b/src/colorpickerwidget.cpp index 8b7bc2d4..b2b567ad 100644 --- a/src/colorpickerwidget.cpp +++ b/src/colorpickerwidget.cpp @@ -21,7 +21,11 @@ #include "colorpickerwidget.h" #include +#include #include +#include +#include +#include #include #include @@ -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(); } diff --git a/src/colorpickerwidget.h b/src/colorpickerwidget.h index ffdda863..fce8d789 100644 --- a/src/colorpickerwidget.h +++ b/src/colorpickerwidget.h @@ -24,6 +24,7 @@ #include #include +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 -- 2.39.2