File indexing completed on 2024-04-14 04:47:27

0001 /*
0002     SPDX-FileCopyrightText: 2010 Till Theato <root@ttill.de>
0003 
0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QFrame>
0010 #include <QPoint>
0011 #include <QWidget>
0012 
0013 class QFrame;
0014 #ifdef Q_WS_X11
0015 #include <X11/Xlib.h>
0016 #endif
0017 
0018 class MyFrame : public QFrame
0019 {
0020     Q_OBJECT
0021 public:
0022     explicit MyFrame(QWidget *parent = nullptr);
0023 
0024 protected:
0025     void hideEvent(QHideEvent *event) override;
0026 
0027 Q_SIGNALS:
0028     void getColor();
0029 };
0030 
0031 /** @class ColorPickerWidget
0032     @brief A widget to pick a color anywhere on the screen.
0033     The code is partially based on the color picker in KColorDialog.
0034     @author Till Theato
0035  */
0036 class ColorPickerWidget : public QWidget
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041     /** @brief Sets up the widget. */
0042     explicit ColorPickerWidget(QWidget *parent = nullptr);
0043     /** @brief Makes sure the event filter is removed. */
0044     ~ColorPickerWidget() override;
0045 
0046 protected:
0047     void mousePressEvent(QMouseEvent *event) override;
0048     void mouseReleaseEvent(QMouseEvent *event) override;
0049     void mouseMoveEvent(QMouseEvent *event) override;
0050     bool eventFilter(QObject *object, QEvent *event) override;
0051     void paintEvent(QPaintEvent *event) override;
0052 
0053 private:
0054     /** @brief Closes the event filter and makes mouse and keyboard work again on other widgets/windows. */
0055     void closeEventFilter();
0056 
0057     /** @brief Color of the screen at point @param p.
0058     * @param p Position of color requested
0059     * @param destroyImage (optional) Whether or not to keep the XImage in m_image
0060                           (needed for fast processing of rects) */
0061     QColor grabColor(const QPoint &p, bool destroyImage = true);
0062 
0063     bool m_filterActive{false};
0064     bool m_useDBus{true};
0065     QRect m_grabRect;
0066     QPoint m_clickPoint;
0067     QFrame *m_grabRectFrame;
0068     QColor m_mouseColor;
0069 #ifdef Q_WS_X11
0070     XImage *m_image;
0071 #else
0072     QImage m_image;
0073 #endif
0074 
0075 private Q_SLOTS:
0076     /** @brief Sets up an event filter for picking a color. */
0077     void slotSetupEventFilter();
0078 
0079     /** @brief Calculates the average color for the pixels in the rect m_grabRect and emits colorPicked. */
0080     void slotGetAverageColor();
0081 
0082     /** @brief Send a DBus message to the Freedesktop portal to request a color picker operation */
0083     void grabColorDBus();
0084     /** @brief To be called by the DBus connection when a response comes in */
0085     void gotColorResponse(uint response, const QVariantMap &results);
0086 
0087 Q_SIGNALS:
0088     /** @brief Signal fired when a new color has been picked */
0089     void colorPicked(const QColor &);
0090     /** @brief When user wants to pick a color, it's better to disable filter so we get proper color values. */
0091     void disableCurrentFilter(bool);
0092 };