Commit cb718f86 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Fix color picker offset, live preview of picked color in the button

Related to #800 #864
parent 49f47704
......@@ -26,8 +26,11 @@
#include <QMouseEvent>
#include <QTimer>
#include <QToolButton>
#include <QStylePainter>
#include <QStyleOptionFocusRect>
#include <klocalizedstring.h>
#ifdef Q_WS_X11
#include <X11/Xutil.h>
#include <fixx11h.h>
......@@ -52,6 +55,7 @@ void MyFrame::hideEvent(QHideEvent *event)
ColorPickerWidget::ColorPickerWidget(QWidget *parent)
: QWidget(parent)
, m_mouseColor(Qt::transparent)
{
#ifdef Q_WS_X11
......@@ -72,6 +76,7 @@ ColorPickerWidget::ColorPickerWidget(QWidget *parent)
layout->addWidget(button);
setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
m_grabRectFrame = new MyFrame();
m_grabRectFrame->hide();
......@@ -85,6 +90,21 @@ ColorPickerWidget::~ColorPickerWidget()
}
}
void ColorPickerWidget::paintEvent(QPaintEvent *event)
{
QStylePainter painter(this);
QStyleOptionComplex option;
option.initFrom(this);
if (m_filterActive) {
QRect r = option.rect;
int margin = r.height() / 8;
r.adjust(margin, 4 * margin, -margin, -margin);
painter.fillRect(r, m_mouseColor);
}
painter.drawComplexControl(QStyle::CC_ToolButton, option);
}
void ColorPickerWidget::slotGetAverageColor()
{
disconnect(m_grabRectFrame, SIGNAL(getColor()), this, SLOT(slotGetAverageColor()));
......@@ -169,6 +189,9 @@ void ColorPickerWidget::mouseReleaseEvent(QMouseEvent *event)
void ColorPickerWidget::mouseMoveEvent(QMouseEvent *event)
{
// Draw live rectangle of current color under mouse
m_mouseColor = grabColor(QCursor::pos(), true);
update();
if (m_filterActive) {
m_grabRect.setWidth(event->globalX() - m_grabRect.x());
m_grabRect.setHeight(event->globalY() - m_grabRect.y());
......@@ -182,7 +205,7 @@ void ColorPickerWidget::slotSetupEventFilter()
m_filterActive = true;
setFocus();
installEventFilter(this);
grabMouse(QCursor(QIcon::fromTheme(QStringLiteral("color-picker")).pixmap(32, 32), 16, 2));
grabMouse(QCursor(QIcon::fromTheme(QStringLiteral("color-picker")).pixmap(32, 32), 4, 28));
grabKeyboard();
}
......
......@@ -64,6 +64,7 @@ protected:
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
bool eventFilter(QObject *object, QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
private:
/** @brief Closes the event filter and makes mouse and keyboard work again on other widgets/windows. */
......@@ -78,6 +79,7 @@ private:
bool m_filterActive{false};
QRect m_grabRect;
QFrame *m_grabRectFrame;
QColor m_mouseColor;
#ifdef Q_WS_X11
XImage *m_image;
#else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment