summaryrefslogtreecommitdiffstats
path: root/kdeui/kdualcolorbutton.h
blob: eb4a71ab2e8ded6f7273915b4c306bf41e374027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* This file is part of the KDE libraries
   Copyright (C) 1999 Daniel M. Duley <mosfet@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#ifndef __KDUALCOLORBTN_H
#define __KDUALCOLORBTN_H

class QBitmap;
#include <qbrush.h>
#include <qwidget.h>

#include <kdelibs_export.h>

/**
 * @short A widget for selecting two related colors.
 *
 * KDualColorButton allows the user to select two cascaded colors (usually a
 * foreground and background color). Other features include drag and drop
 * from other KDE color widgets, a reset to black and white control, and a
 * swap colors control.
 *
 * When the user clicks on the foreground or background rectangle the
 * rectangle is first sunken and the currentChanged() signal is emitted.
 * Further clicks will present a color dialog and emit either the fgChanged()
 * or bgChanged() if a new color is selected.
 *
 * Note: With drag and drop when dropping a color the current selected color
 * will be set, while when dragging a color it will use whatever color
 * rectangle the mouse was pressed inside.
 *
 * \image html kdualcolorbutton.png "KDE Dual Color Button"
 *
 * @author Daniel M. Duley <mosfet@kde.org>
 */
class KDEUI_EXPORT KDualColorButton : public QWidget
{
    Q_OBJECT
    Q_ENUMS( DualColor )
    Q_PROPERTY( QColor foreground READ foreground WRITE setForeground )
    Q_PROPERTY( QColor background READ background WRITE setBackground )
    Q_PROPERTY( QColor currentColor READ currentColor WRITE setCurrentColor STORED false DESIGNABLE false )
    Q_PROPERTY( DualColor current READ current WRITE setCurrent )

public:

    enum DualColor { Foreground, Background };
    /**
     * Constructs a new KDualColorButton using the default black and white
     * colors.
     *
     * As of KDE 3.5.1, sets the dialog parent to the same as "parent" if that
     * argument is non-null and the dialogParent argument is null.
     */
    KDualColorButton(QWidget *parent=0, const char *name=0, QWidget* dialogParent=0);

    /**
     * Constructs a new KDualColorButton with the supplied foreground and
     * background colors.
     */
    KDualColorButton(const QColor &fgColor, const QColor &bgColor,
                     QWidget *parent=0, const char *name=0, QWidget* dialogParent=0);

    ~KDualColorButton();
    /**
     * Returns the current foreground color.
     */
    QColor foreground() const;
    /**
     * Returns the current background color.
     */
    QColor background() const;
    /**
     * Returns the current color item selected by the user.
     */
    DualColor current() const;
    /**
     * Returns the color of the selected item.
     */
    QColor currentColor() const;
    /**
     * Returns the minimum size needed to display the widget and all its
     * controls.
     */
    virtual QSize sizeHint() const;
public slots:
    /**
     * Sets the foreground color.
     */
    void setForeground(const QColor &c);
    /**
     * Sets the background color.
     */
    void setBackground(const QColor &c);
    /**
     * Sets the current selected color item.
     */
    void setCurrent(DualColor s);
    /**
     * Sets the color of the selected item.
     */
    void setCurrentColor(const QColor &c);
signals:
    /**
     * Emitted when the foreground color is changed.
     */
    void fgChanged(const QColor &c);
    /**
     * Emitted when the background color is changed.
     */
    void bgChanged(const QColor &c);
    /**
     * Emitted when the user changes the current color selection.
     */
    void currentChanged(KDualColorButton::DualColor s);
protected:
    /**
     * Sets the supplied rectangles to the proper size and position for the
     * current widget size. You can reimplement this to change the layout
     * of the widget. Restrictions are that the swap control will always
     * be at the top right, the reset control will always be at the bottom
     * left, and you must leave at least a 14x14 space in those corners.
     */
    virtual void metrics(QRect &fgRect, QRect &bgRect);
    virtual void paintEvent(QPaintEvent *ev);
    virtual void mousePressEvent(QMouseEvent *ev);
    virtual void mouseMoveEvent(QMouseEvent *ev);
    virtual void mouseReleaseEvent(QMouseEvent *ev);
    // Dnd
    virtual void dragEnterEvent(QDragEnterEvent *ev);
    virtual void dropEvent(QDropEvent *ev);
private:
    QBitmap *arrowBitmap;
    QPixmap *resetPixmap;
    QBrush fg, bg;
    QPoint mPos;
    bool dragFlag, miniCtlFlag;
    DualColor curColor, tmpColor;

protected:
    virtual void virtual_hook( int id, void* data );
private:
    class KDualColorPrivate;
    KDualColorPrivate *d;
};

#endif