]> git.sesse.net Git - kdenlive/blobdiff - src/colorpickerwidget.cpp
Add animation feature to Slideshow Clip.
[kdenlive] / src / colorpickerwidget.cpp
index 2cf12c3bca0def4ee2b083377e8104ddc76e46e2..acd1d26982a8be5378c146f2395f5582abaa6b66 100644 (file)
@@ -59,8 +59,8 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
 {
 #ifdef Q_WS_X11
     m_filter = 0;
+    m_image = NULL;
 #endif
-    m_image = 0;
 
     QHBoxLayout *layout = new QHBoxLayout(this);
 
@@ -71,8 +71,8 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent) :
 
     m_size = new QSpinBox(this);
     m_size->setMinimum(1);
-    // Use qMax here, to make it possible to get the average for the whole screen
-    m_size->setMaximum(qMax(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
+    // Use qMin here, as we might run into troubles with the cursor otherwise.
+    m_size->setMaximum(qMin(qApp->desktop()->geometry().width(), qApp->desktop()->geometry().height()));
     m_size->setValue(1);
 
     layout->addWidget(button);
@@ -92,15 +92,27 @@ ColorPickerWidget::~ColorPickerWidget()
 QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
 {
     int size = m_size->value();
-    int x0 = qMax(0, pos.x() - size / 2); 
+    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);
 
+    // take care of loss when dividing odd sizes
+    if (size % 2 != 0) {
+        if (x1 < qApp->desktop()->geometry().width()) ++x1;
+        if (y1 < qApp->desktop()->geometry().height()) ++y1;
+    }
+
+    int numPixel = (x1 - x0) * (y1 - y0);
+
     int sumR = 0;
     int sumG = 0;
     int sumB = 0;
 
+    // only show message for size > 200 because for smaller values it slows down to much
+    if (size > 200)
+        emit displayMessage(i18n("Requesting color information..."), 0);
+
     /*
      Only getting the image once for the whole rect
      results in a vast speed improvement.
@@ -121,14 +133,20 @@ QColor ColorPickerWidget::averagePickedColor(const QPoint pos)
             sumG += color.green();
             sumB += color.blue();
         }
+
+        // Warning: slows things down, so don't do it for every pixel (the inner for loop)
+        if (size > 200)
+            emit displayMessage(i18n("Requesting color information..."), (int)(((i - x0) * (y1 - y0)) / (qreal)numPixel * 100));
     }
 
 #ifdef Q_WS_X11
     XDestroyImage(m_image);
+    m_image = NULL;
 #endif
-    m_image = 0;
 
-    int numPixel = (x1 - x0) * (y1 - y0);
+    if (size > 200)
+        emit displayMessage(i18n("Calculated average color for rectangle."), -1);
+
     return QColor(sumR / numPixel, sumG / numPixel, sumB / numPixel);
 }
 
@@ -176,10 +194,10 @@ void ColorPickerWidget::slotSetupEventFilter()
     m_filter = new KCDPickerFilter(this);
     kapp->installX11EventFilter(m_filter);
 #endif
-    if (m_size->value() == 1)
+    if (m_size->value() < 10)
         grabMouse(QCursor(KIcon("color-picker").pixmap(22, 22), 0, 21));
     else
-        grabMouse(Qt::CrossCursor);
+        grabMouse(QCursor(KIcon("kdenlive-select-all").pixmap(m_size->value(), m_size->value())));
     grabKeyboard();
 }
 
@@ -206,7 +224,7 @@ QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
     if( !qApp->desktop()->geometry().contains( p ))
         return QColor();
     unsigned long xpixel;
-    if (m_image == 0) {
+    if (m_image == NULL) {
         Window root = RootWindow(QX11Info::display(), QX11Info::appScreen());
         m_image = XGetImage(QX11Info::display(), root, p.x(), p.y(), 1, 1, -1, ZPixmap);
         xpixel = XGetPixel(m_image, 0, 0);
@@ -225,7 +243,7 @@ QColor ColorPickerWidget::grabColor(const QPoint &p, bool destroyImage)
                 &xcol);
     return QColor::fromRgbF(xcol.red / 65535.0, xcol.green / 65535.0, xcol.blue / 65535.0);
 #else
-    if (m_image == 0) {
+    if (m_image.isNull()) {
         QWidget *desktop = QApplication::desktop();
         QPixmap pm = QPixmap::grabWindow(desktop->winId(), p.x(), p.y(), 1, 1);
         QImage i = pm.toImage();