Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "colorbutton.h"
43
44#include <QApplication>
45#include <QtEvents>
46#include <QColorDialog>
47#include <QPainter>
48#include <QMimeData>
49#include <QStyle>
50#include <QStyleOption>
51
52QT_BEGIN_NAMESPACE
53
54ColorButton::ColorButton(QWidget *parent)
55 : QAbstractButton(parent)
56 , col(Qt::black)
57 , mousepressed(false)
58{
59 setAcceptDrops(true);
60 connect(this, SIGNAL(clicked()), SLOT(changeColor()));
61}
62
63
64ColorButton::ColorButton(const QColor &c, QWidget *parent)
65 : QAbstractButton(parent)
66 , col(c)
67{
68 setAcceptDrops(true);
69 connect(this, SIGNAL(clicked()), SLOT(changeColor()));
70}
71
72
73void ColorButton::setColor(const QColor &c)
74{
75 col = c;
76 update();
77}
78
79
80void ColorButton::changeColor()
81{
82 QColor c = QColorDialog::getColor(col, qApp->activeWindow());
83
84 if (c.isValid()) {
85 setColor(c);
86 emit colorChanged(color());
87 }
88}
89
90
91QSize ColorButton::sizeHint() const
92{
93 return QSize(40, 25);
94}
95
96
97QSize ColorButton::minimumSizeHint() const
98{
99 return QSize(40, 25);
100}
101
102
103void ColorButton::drawButton(QPainter *p)
104{
105 QStyleOptionButton buttonOptions;
106 buttonOptions.init(this);
107 buttonOptions.features = QStyleOptionButton::None;
108 buttonOptions.rect = rect();
109 buttonOptions.palette = palette();
110 buttonOptions.state = (isDown() ? QStyle::State_Sunken : QStyle::State_Raised);
111 style()->drawPrimitive(QStyle::PE_PanelButtonBevel, &buttonOptions, p, this);
112
113 p->save();
114 drawButtonLabel(p);
115 p->restore();
116
117 QStyleOptionFocusRect frectOptions;
118 frectOptions.init(this);
119 frectOptions.rect = style()->subElementRect(QStyle::SE_PushButtonFocusRect, &buttonOptions, this);
120 if (hasFocus())
121 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &frectOptions, p, this);
122}
123
124
125void ColorButton::drawButtonLabel(QPainter *p)
126{
127 QPalette::ColorGroup cg =
128 (isEnabled() ? (hasFocus() ? QPalette::Active : QPalette::Inactive) : QPalette::Disabled);
129
130 p->setPen(palette().color(cg, QPalette::ButtonText));
131 p->setBrush(col);
132 p->drawRect(width() / 4, height() / 4, width() / 2 - 1, height() / 2 - 1);
133}
134
135
136void ColorButton::dragEnterEvent(QDragEnterEvent *e)
137{
138 if (!e->mimeData()->hasColor()) {
139 e->ignore();
140 return;
141 }
142}
143
144
145void ColorButton::dragMoveEvent(QDragMoveEvent *e)
146{
147 if (!e->mimeData()->hasColor()) {
148 e->ignore();
149 return;
150 }
151
152 e->accept();
153}
154
155
156void ColorButton::dropEvent(QDropEvent *e)
157{
158 if (!e->mimeData()->hasColor()) {
159 e->ignore();
160 return;
161 }
162
163 QColor c = qvariant_cast<QColor>(e->mimeData()->colorData());
164 setColor(c);
165 emit colorChanged(color());
166}
167
168
169void ColorButton::mousePressEvent(QMouseEvent *e)
170{
171 presspos = e->pos();
172 mousepressed = true;
173 QAbstractButton::mousePressEvent(e);
174}
175
176
177void ColorButton::mouseReleaseEvent(QMouseEvent *e)
178{
179 mousepressed = false;
180 QAbstractButton::mouseReleaseEvent(e);
181}
182
183
184void ColorButton::mouseMoveEvent(QMouseEvent *e)
185{
186 if (!mousepressed)
187 return;
188
189 if ((presspos - e->pos()).manhattanLength() > QApplication::startDragDistance()) {
190 mousepressed = false;
191 setDown(false);
192
193 QDrag *drag = new QDrag(this);
194 QMimeData *data = new QMimeData;
195 data->setColorData(color());
196 drag->setMimeData(data);
197 drag->start(Qt::CopyAction);
198 }
199}
200
201void ColorButton::paintEvent(QPaintEvent *)
202{
203 QPainter p(this);
204 drawButton(&p);
205}
206
207QT_END_NAMESPACE
208

Warning: That file was not part of the compilation database. It may have many parsing errors.