~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to tools/qtconfig/colorbutton.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the qtconfig application of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "colorbutton.h"
 
30
 
 
31
#include <qapplication.h>
 
32
#include <qevent.h>
 
33
#include <qcolordialog.h>
 
34
#include <qpainter.h>
 
35
#include <q3dragobject.h>
 
36
#include <qstyle.h>
 
37
#include <qstyleoption.h>
 
38
 
 
39
ColorButton::ColorButton(QWidget *parent)
 
40
    : QAbstractButton(parent), mousepressed(false)
 
41
{
 
42
    setAcceptDrops(true);
 
43
    col = Qt::black;
 
44
    connect(this, SIGNAL(clicked()), SLOT(changeColor()));
 
45
}
 
46
 
 
47
 
 
48
ColorButton::ColorButton(const QColor &c, QWidget *parent)
 
49
    : QAbstractButton(parent)
 
50
{
 
51
    setAcceptDrops(true);
 
52
    col = c;
 
53
    connect(this, SIGNAL(clicked()), SLOT(changeColor()));
 
54
}
 
55
 
 
56
 
 
57
void ColorButton::setColor(const QColor &c)
 
58
{
 
59
    col = c;
 
60
    update();
 
61
}
 
62
 
 
63
 
 
64
void ColorButton::changeColor()
 
65
{
 
66
    QColor c = QColorDialog::getColor(col, qApp->activeWindow());
 
67
 
 
68
    if (c.isValid()) {
 
69
        setColor(c);
 
70
        emit colorChanged(color());
 
71
    }
 
72
}
 
73
 
 
74
 
 
75
QSize ColorButton::sizeHint() const
 
76
{
 
77
    return QSize(40, 25);
 
78
}
 
79
 
 
80
 
 
81
QSize ColorButton::minimumSizeHint() const
 
82
{
 
83
    return QSize(40, 25);
 
84
}
 
85
 
 
86
 
 
87
void ColorButton::drawButton(QPainter *p)
 
88
{
 
89
    QStyleOptionButton buttonOptions;
 
90
    buttonOptions.init(this);
 
91
    buttonOptions.features = QStyleOptionButton::None;
 
92
    buttonOptions.rect = rect();
 
93
    buttonOptions.palette = palette();
 
94
    buttonOptions.state = (isDown() ? QStyle::State_Sunken : QStyle::State_Raised);
 
95
    style()->drawPrimitive(QStyle::PE_PanelButtonBevel, &buttonOptions, p, this);
 
96
 
 
97
    p->save();
 
98
    drawButtonLabel(p);
 
99
    p->restore();
 
100
 
 
101
    QStyleOptionFocusRect frectOptions;
 
102
    frectOptions.init(this);
 
103
    frectOptions.rect = style()->subElementRect(QStyle::SE_PushButtonFocusRect, &buttonOptions, this);
 
104
    if (hasFocus())
 
105
        style()->drawPrimitive(QStyle::PE_FrameFocusRect, &frectOptions, p, this);
 
106
}
 
107
 
 
108
 
 
109
void ColorButton::drawButtonLabel(QPainter *p)
 
110
{
 
111
    QPalette::ColorGroup cg =
 
112
        (isEnabled() ? (hasFocus() ? QPalette::Active : QPalette::Inactive) : QPalette::Disabled);
 
113
 
 
114
    p->setPen(palette().color(cg, QPalette::ButtonText));
 
115
    p->setBrush(col);
 
116
    p->drawRect(width() / 4, height() / 4, width() / 2 - 1, height() / 2 - 1);
 
117
}
 
118
 
 
119
 
 
120
void ColorButton::dragEnterEvent(QDragEnterEvent *e)
 
121
{
 
122
    if (! Q3ColorDrag::canDecode(e)) {
 
123
        e->ignore();
 
124
        return;
 
125
    }
 
126
}
 
127
 
 
128
 
 
129
void ColorButton::dragMoveEvent(QDragMoveEvent *e)
 
130
{
 
131
    if (! Q3ColorDrag::canDecode(e)) {
 
132
        e->ignore();
 
133
        return;
 
134
    }
 
135
 
 
136
    e->accept();
 
137
}
 
138
 
 
139
 
 
140
void ColorButton::dropEvent(QDropEvent *e)
 
141
{
 
142
    if (! Q3ColorDrag::canDecode(e)) {
 
143
        e->ignore();
 
144
        return;
 
145
    }
 
146
 
 
147
    QColor c;
 
148
    Q3ColorDrag::decode(e, c);
 
149
    setColor(c);
 
150
    emit colorChanged(color());
 
151
}
 
152
 
 
153
 
 
154
void ColorButton::mousePressEvent(QMouseEvent *e)
 
155
{
 
156
    presspos = e->pos();
 
157
    mousepressed = true;
 
158
    QAbstractButton::mousePressEvent(e);
 
159
}
 
160
 
 
161
 
 
162
void ColorButton::mouseReleaseEvent(QMouseEvent *e)
 
163
{
 
164
    mousepressed = false;
 
165
    QAbstractButton::mouseReleaseEvent(e);
 
166
}
 
167
 
 
168
 
 
169
void ColorButton::mouseMoveEvent(QMouseEvent *e)
 
170
{
 
171
    if (! mousepressed)
 
172
        return;
 
173
 
 
174
    if ((presspos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
 
175
        mousepressed = false;
 
176
        setDown(false);
 
177
 
 
178
        Q3ColorDrag *cd = new Q3ColorDrag(color(), this);
 
179
        cd->dragCopy();
 
180
    }
 
181
}
 
182
 
 
183
void ColorButton::paintEvent(QPaintEvent *)
 
184
{
 
185
    QPainter p(this);
 
186
    drawButton(&p);
 
187
}
 
188
 
 
189