summaryrefslogtreecommitdiffstats
path: root/lib/kofficeui/KoTooluButton.h
blob: b933eeee989fddacfae6bede90b93a8c8943c469 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* This file is part of the KDE project
   Copyright (C) 2002 Werner Trobin <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   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 _kotoolbutton_h_
#define _kotoolbutton_h_

#include <ktoolbarbutton.h>
#include <qmap.h>
#include <qpoint.h>

class QPopupMenu;

class KoColorPanel : public QWidget
{
    Q_OBJECT
public:
    KoColorPanel( QWidget* parent = 0, const char* name = 0 );
    virtual ~KoColorPanel();

    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;

    enum MenuStyle { Plain, CustomColors };
    static QPopupMenu* createColorPopup( MenuStyle style, const QColor& defaultColor,
                                         const QObject* receiver, const char* slot,
                                         QWidget* parent, const char* name );

public slots:
    void clear();
    void insertColor( const QColor& color );
    void insertColor( const QColor& color, const QString& toolTip );
    void insertDefaultColors();

signals:
    void colorSelected( const QColor& color );

protected:
    virtual void mousePressEvent( QMouseEvent* e );
    virtual void mouseReleaseEvent( QMouseEvent* e );
    virtual void mouseMoveEvent( QMouseEvent* e );
    virtual void paintEvent( QPaintEvent* e );
    virtual void keyPressEvent( QKeyEvent* e );
    virtual void focusInEvent( QFocusEvent* e );
    virtual void dragEnterEvent( QDragEnterEvent* e );
    virtual void dropEvent( QDropEvent* e );

private:
    // The position of the 16x16 tiles in "tile steps"
    struct Position {
        Position() : x( -1 ), y( -1 ) {}
        Position( short x_, short y_ ) : x( x_ ), y( y_ ) {}

        short x;
        short y;
    };
    friend bool operator<( const KoColorPanel::Position& lhs, const KoColorPanel::Position& rhs );

    void finalizeInsertion( const Position& pos );
    bool insertColor( const QColor& color, bool checking );
    bool insertColor( const QColor& color, const QString& toolTip, bool checking );
    bool isAvailable( const QColor& color );

    Position mapToPosition( const QPoint& point ) const;
    QColor mapToColor( const QPoint& point ) const;
    QColor mapToColor( const Position& position ) const;
    QRect mapFromPosition( const Position& position ) const;
    Position validPosition( const Position& position );

    int lines() const;
    void paintArea( const QRect& rect, int& startRow, int& endRow, int& startCol, int& endCol ) const;
    void updateFocusPosition( const Position& newPosition );
    void paint( const Position& position );
    void init();

    Position m_nextPosition, m_focusPosition;
    QMap<Position, QColor> m_colorMap;
    QPoint m_pressedPos;
    bool m_defaultsAdded;
};

// Needed for the use of KoColorPanel::Position in QMap
bool operator<( const KoColorPanel::Position& lhs, const KoColorPanel::Position& rhs );


// A tiny class needed to emit the correct signal when the default
// color item in the color-panel popup is activated. Additionally
// it's used to provide the color select dialog and manages the recent
// colors... hacky
class KoColorPopupProxy : public QObject
{
    Q_OBJECT
public:
    KoColorPopupProxy( const QColor& defaultColor, KoColorPanel* recentColors, QObject* parent, const char* name );
    virtual ~KoColorPopupProxy() {}

    void setRecentColorPanel( KoColorPanel* recentColors );

public slots:
    void slotDefaultColor();
    void slotMoreColors();

signals:
    void colorSelected( const QColor& color );

private:
    QColor m_defaultColor;
    KoColorPanel* m_recentColors;
};


// Parts of the code are from KToolBarButton
class KoToolButton : public KToolBarButton
{
    Q_OBJECT
public:
    /**
     * Construct a button with an icon loaded by the button itself.
     * This will trust the button to load the correct icon with the
     * correct size.
     *
     * @param icon   Name of icon to load (may be absolute or relative)
     * @param id     Id of this button
     * @param parent This button's parent
     * @param name   This button's internal name
     * @param txt    This button's text (in a tooltip or otherwise)
     */
    KoToolButton( const QString& icon, int id, QWidget* parent,
                  const char* name = 0L, const QString& txt = QString::null,
                  KInstance* _instance = KGlobal::instance() );

    /**
     * Construct a button with an existing pixmap.  It is not
     * recommended that you use this as the internal icon loading code
     * will almost always get it "right".
     *
     * @param icon   Name of icon to load (may be absolute or relative)
     * @param id     Id of this button
     * @param parent This button's parent
     * @param name   This button's internal name
     * @param txt    This button's text (in a tooltip or otherwise)
     */
    KoToolButton( const QPixmap& pixmap, int id, QWidget* parent,
                  const char* name = 0L, const QString& txt = QString::null );

    virtual ~KoToolButton();

    virtual QSize sizeHint() const;
    virtual QSize minimumSizeHint() const;
    virtual QSize minimumSize() const;

public slots:
    void colorSelected( const QColor& color );

protected:
    virtual void drawButton(QPainter *p);
    virtual bool eventFilter( QObject* o, QEvent* e );

private:
    void init();
    void buttonShift( int& dx, int& dy );
    bool hitArrow( const QPoint& pos );

    QPopupMenu* m_popup;
    bool m_arrowPressed;
};

#endif // _kotoolbutton_h_