• Skip to content
  • Skip to link menu
Brand

API Documentation

  1. KDE API Reference
  2. The KDE Frameworks
  3. KWidgetsAddons
  • KDE Home
  • Contact Us

Quick Links

Skip menu "KWidgetsAddons"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Dependencies
  • Related Pages

Class Picker

About

Addons to QtWidgets

Maintainer
Christoph Feck
Supported platforms
Android, FreeBSD, Linux, MacOSX, Windows
Community
IRC: #kde-devel on Freenode
Mailing list: kde-frameworks-devel
Use with CMake
find_package(KF5WidgetsAddons)
target_link_libraries(yourapp KF5::WidgetsAddons)
Use with QMake
QT += KWidgetsAddons 
Clone
git clone git://anongit.kde.org/kwidgetsaddons.git
Browse source
KWidgetsAddons on cgit.kde.org

KWidgetsAddons

  • frameworks
  • frameworks
  • kwidgetsaddons
  • src
kcolorbutton.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Martin Jones ([email protected])
3  Copyright (C) 1999 Cristian Tibirna ([email protected])
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kcolorbutton.h"
22 
23 #include <QPointer>
24 #include <QPainter>
25 #include <qdrawutil.h>
26 #include <QApplication>
27 #include <QColorDialog>
28 #include <QClipboard>
29 #include <QMimeData>
30 #include <QDrag>
31 #include <QStyle>
32 #include <QMouseEvent>
33 #include <QStyleOptionButton>
34 
35 class Q_DECL_HIDDEN KColorButton::KColorButtonPrivate
36 {
37 public:
38  KColorButtonPrivate(KColorButton *q);
39 
40  void _k_chooseColor();
41  void _k_colorChosen();
42 
43  KColorButton *q;
44  QColor m_defaultColor;
45  bool m_bdefaultColor : 1;
46  bool m_alphaChannel : 1;
47 
48  QColor col;
49  QPoint mPos;
50 
51  QPointer<QColorDialog> dialogPtr;
52 
53  void initStyleOption(QStyleOptionButton *opt) const;
54 };
55 
57 // Functions duplicated from KColorMimeData
58 // Should be kept in sync
59 void _k_populateMimeData(QMimeData *mimeData, const QColor &color)
60 {
61  mimeData->setColorData(color);
62  mimeData->setText(color.name());
63 }
64 
65 bool _k_canDecode(const QMimeData *mimeData)
66 {
67  if (mimeData->hasColor()) {
68  return true;
69  }
70  if (mimeData->hasText()) {
71  const QString colorName = mimeData->text();
72  if ((colorName.length() >= 4) && (colorName[0] == QLatin1Char('#'))) {
73  return true;
74  }
75  }
76  return false;
77 }
78 
79 QColor _k_fromMimeData(const QMimeData *mimeData)
80 {
81  if (mimeData->hasColor()) {
82  return mimeData->colorData().value<QColor>();
83  }
84  if (_k_canDecode(mimeData)) {
85  return QColor(mimeData->text());
86  }
87  return QColor();
88 }
89 
90 QDrag *_k_createDrag(const QColor &color, QObject *dragsource)
91 {
92  QDrag *drag = new QDrag(dragsource);
93  QMimeData *mime = new QMimeData;
94  _k_populateMimeData(mime, color);
95  drag->setMimeData(mime);
96  QPixmap colorpix(25, 20);
97  colorpix.fill(color);
98  QPainter p(&colorpix);
99  p.setPen(Qt::black);
100  p.drawRect(0, 0, 24, 19);
101  p.end();
102  drag->setPixmap(colorpix);
103  drag->setHotSpot(QPoint(-5, -7));
104  return drag;
105 }
107 
108 KColorButton::KColorButtonPrivate::KColorButtonPrivate(KColorButton *q)
109  : q(q)
110 {
111  m_bdefaultColor = false;
112  m_alphaChannel = false;
113  q->setAcceptDrops(true);
114 
115  connect(q, &KColorButton::clicked, q, [this]() { _k_chooseColor(); });
116 }
117 
118 KColorButton::KColorButton(QWidget *parent)
119  : QPushButton(parent)
120  , d(new KColorButtonPrivate(this))
121 {
122 }
123 
124 KColorButton::KColorButton(const QColor &c, QWidget *parent)
125  : QPushButton(parent)
126  , d(new KColorButtonPrivate(this))
127 {
128  d->col = c;
129 }
130 
131 KColorButton::KColorButton(const QColor &c, const QColor &defaultColor, QWidget *parent)
132  : QPushButton(parent)
133  , d(new KColorButtonPrivate(this))
134 {
135  d->col = c;
136  setDefaultColor(defaultColor);
137 }
138 
139 KColorButton::~KColorButton()
140 {
141  delete d;
142 }
143 
144 QColor KColorButton::color() const
145 {
146  return d->col;
147 }
148 
149 void KColorButton::setColor(const QColor &c)
150 {
151  if (d->col != c) {
152  d->col = c;
153  update();
154  emit changed(d->col);
155  }
156 }
157 
158 void KColorButton::setAlphaChannelEnabled(bool alpha)
159 {
160  d->m_alphaChannel = alpha;
161 }
162 
163 bool KColorButton::isAlphaChannelEnabled() const
164 {
165  return d->m_alphaChannel;
166 }
167 
168 QColor KColorButton::defaultColor() const
169 {
170  return d->m_defaultColor;
171 }
172 
173 void KColorButton::setDefaultColor(const QColor &c)
174 {
175  d->m_bdefaultColor = c.isValid();
176  d->m_defaultColor = c;
177 }
178 
179 void KColorButton::KColorButtonPrivate::initStyleOption(QStyleOptionButton *opt) const
180 {
181  opt->initFrom(q);
182  opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
183  opt->features = QStyleOptionButton::None;
184  if (q->isDefault()) {
185  opt->features |= QStyleOptionButton::DefaultButton;
186  }
187  opt->text.clear();
188  opt->icon = QIcon();
189 }
190 
191 void KColorButton::paintEvent(QPaintEvent *)
192 {
193  QPainter painter(this);
194  QStyle *style = QWidget::style();
195 
196  //First, we need to draw the bevel.
197  QStyleOptionButton butOpt;
198  d->initStyleOption(&butOpt);
199  style->drawControl(QStyle::CE_PushButtonBevel, &butOpt, &painter, this);
200 
201  //OK, now we can muck around with drawing out pretty little color box
202  //First, sort out where it goes
203  QRect labelRect = style->subElementRect(QStyle::SE_PushButtonContents,
204  &butOpt, this);
205  int shift = style->pixelMetric(QStyle::PM_ButtonMargin, &butOpt, this) / 2;
206  labelRect.adjust(shift, shift, -shift, -shift);
207  int x, y, w, h;
208  labelRect.getRect(&x, &y, &w, &h);
209 
210  if (isChecked() || isDown()) {
211  x += style->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &butOpt, this);
212  y += style->pixelMetric(QStyle::PM_ButtonShiftVertical, &butOpt, this);
213  }
214 
215  QColor fillCol = isEnabled() ? d->col : palette().color(backgroundRole());
216  qDrawShadePanel(&painter, x, y, w, h, palette(), true, 1, nullptr);
217  if (fillCol.isValid()) {
218  const QRect rect(x + 1, y + 1, w - 2, h - 2);
219  if (fillCol.alpha() < 255) {
220  QPixmap chessboardPattern(16, 16);
221  QPainter patternPainter(&chessboardPattern);
222  patternPainter.fillRect(0, 0, 8, 8, Qt::black);
223  patternPainter.fillRect(8, 8, 8, 8, Qt::black);
224  patternPainter.fillRect(0, 8, 8, 8, Qt::white);
225  patternPainter.fillRect(8, 0, 8, 8, Qt::white);
226  patternPainter.end();
227  painter.fillRect(rect, QBrush(chessboardPattern));
228  }
229  painter.fillRect(rect, fillCol);
230  }
231 
232  if (hasFocus()) {
233  QRect focusRect = style->subElementRect(QStyle::SE_PushButtonFocusRect, &butOpt, this);
234  QStyleOptionFocusRect focusOpt;
235  focusOpt.init(this);
236  focusOpt.rect = focusRect;
237  focusOpt.backgroundColor = palette().window().color();
238  style->drawPrimitive(QStyle::PE_FrameFocusRect, &focusOpt, &painter, this);
239  }
240 }
241 
242 QSize KColorButton::sizeHint() const
243 {
244  QStyleOptionButton opt;
245  d->initStyleOption(&opt);
246  return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
247  expandedTo(QApplication::globalStrut());
248 }
249 
250 QSize KColorButton::minimumSizeHint() const
251 {
252  QStyleOptionButton opt;
253  d->initStyleOption(&opt);
254  return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
255  expandedTo(QApplication::globalStrut());
256 }
257 
258 void KColorButton::dragEnterEvent(QDragEnterEvent *event)
259 {
260  event->setAccepted(_k_canDecode(event->mimeData()) && isEnabled());
261 }
262 
263 void KColorButton::dropEvent(QDropEvent *event)
264 {
265  QColor c = _k_fromMimeData(event->mimeData());
266  if (c.isValid()) {
267  setColor(c);
268  }
269 }
270 
271 void KColorButton::keyPressEvent(QKeyEvent *e)
272 {
273  int key = e->key() | e->modifiers();
274 
275  if (QKeySequence::keyBindings(QKeySequence::Copy).contains(key)) {
276  QMimeData *mime = new QMimeData;
277  _k_populateMimeData(mime, color());
278  QApplication::clipboard()->setMimeData(mime, QClipboard::Clipboard);
279  } else if (QKeySequence::keyBindings(QKeySequence::Paste).contains(key)) {
280  QColor color = _k_fromMimeData(QApplication::clipboard()->mimeData(QClipboard::Clipboard));
281  setColor(color);
282  } else {
283  QPushButton::keyPressEvent(e);
284  }
285 }
286 
287 void KColorButton::mousePressEvent(QMouseEvent *e)
288 {
289  d->mPos = e->pos();
290  QPushButton::mousePressEvent(e);
291 }
292 
293 void KColorButton::mouseMoveEvent(QMouseEvent *e)
294 {
295  if ((e->buttons() & Qt::LeftButton) &&
296  (e->pos() - d->mPos).manhattanLength() > QApplication::startDragDistance()) {
297  _k_createDrag(color(), this)->exec();
298  setDown(false);
299  }
300 }
301 
302 void KColorButton::KColorButtonPrivate::_k_chooseColor()
303 {
304  QColorDialog *dialog = dialogPtr.data();
305  if (dialog) {
306  dialog->show();
307  dialog->raise();
308  dialog->activateWindow();
309  return;
310  }
311 
312  dialog = new QColorDialog(q);
313  dialog->setCurrentColor(q->color());
314  dialog->setOption(QColorDialog::ShowAlphaChannel, m_alphaChannel);
315  dialog->setAttribute(Qt::WA_DeleteOnClose);
316  connect(dialog, SIGNAL(accepted()), q, SLOT(_k_colorChosen()));
317  dialogPtr = dialog;
318  dialog->show();
319 }
320 
321 void KColorButton::KColorButtonPrivate::_k_colorChosen()
322 {
323  QColorDialog *dialog = dialogPtr.data();
324  if (!dialog) {
325  return;
326  }
327 
328  if (dialog->selectedColor().isValid()) {
329  q->setColor(dialog->selectedColor());
330  } else if (m_bdefaultColor) {
331  q->setColor(m_defaultColor);
332  }
333 }
334 
335 #include "moc_kcolorbutton.cpp"
QAbstractButton::isDown
bool isDown() const
KColorButton::changed
void changed(const QColor &newColor)
Emitted when the color of the widget is changed, either with setColor() or via user selection...
QDrag::setHotSpot
void setHotSpot(const QPoint &hotspot)
QWidget
QKeyEvent::modifiers
Qt::KeyboardModifiers modifiers() const
QWidget::palette
const QPalette & palette() const
QPainter::end
bool end()
QDropEvent::mimeData
const QMimeData * mimeData() const
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
QColorDialog::setOption
void setOption(ColorDialogOption option, bool on)
KColorButton::setAlphaChannelEnabled
void setAlphaChannelEnabled(bool alpha)
When set to true, allow the user to change the alpha component of the color.
Definition: kcolorbutton.cpp:158
QDrag::setMimeData
void setMimeData(QMimeData *data)
QColor::name
QString name() const
KColorButton::setColor
void setColor(const QColor &c)
Sets the current color to c.
Definition: kcolorbutton.cpp:149
QWidget::style
QStyle * style() const
QStyle::pixelMetric
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const =0
QMimeData::setColorData
void setColorData(const QVariant &color)
QDrag::setPixmap
void setPixmap(const QPixmap &pixmap)
QPalette::color
const QColor & color(ColorGroup group, ColorRole role) const
KColorButton::KColorButton
KColorButton(QWidget *parent=nullptr)
Creates a color button.
Definition: kcolorbutton.cpp:118
QPointer< QColorDialog >
QWidget::y
int y() const
QWidget::setAttribute
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QVariant::value
T value() const
QBrush
QWidget::hasFocus
bool hasFocus() const
QPoint
QMouseEvent
QMouseEvent::buttons
Qt::MouseButtons buttons() const
QMimeData::hasText
bool hasText() const
QMimeData
QWidget::update
void update()
QStyleOptionButton
QApplication::globalStrut
QSize globalStrut()
QStyleOption::initFrom
void initFrom(const QWidget *widget)
KColorButton::color
QColor color() const
Returns the currently chosen color.
QBrush::color
const QColor & color() const
QDrag::exec
Qt::DropAction exec(Qt::DropActions supportedActions)
QRect
QWidget::isEnabled
bool isEnabled() const
QMimeData::text
QString text() const
QWidget::x
int x() const
QObject
QStyle
QWidget::backgroundRole
QPalette::ColorRole backgroundRole() const
QDropEvent
QPainter
QDrag
QWidget::raise
void raise()
QAbstractButton::clicked
void clicked(bool checked)
QColorDialog
QColorDialog::setCurrentColor
void setCurrentColor(const QColor &color)
QMimeData::setText
void setText(const QString &text)
QString
QColor
QAbstractButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
QClipboard::setMimeData
void setMimeData(QMimeData *src, Mode mode)
QMimeData::colorData
QVariant colorData() const
QColor::alpha
int alpha() const
QWidget::rect
QRect rect() const
QPixmap
QWidget::setAcceptDrops
void setAcceptDrops(bool on)
QStyleOptionFocusRect
QKeyEvent::key
int key() const
QSize
QColorDialog::selectedColor
QColor selectedColor() const
QLatin1Char
KColorButton::setDefaultColor
void setDefaultColor(const QColor &c)
Sets the default color to c.
Definition: kcolorbutton.cpp:173
QAbstractButton::isChecked
bool isChecked() const
QKeyEvent
KColorButton
A pushbutton to display or allow user selection of a color.
Definition: kcolorbutton.h:39
QDragEnterEvent
QWidget::activateWindow
void activateWindow()
QStyle::drawControl
virtual void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
QRect::adjust
void adjust(int dx1, int dy1, int dx2, int dy2)
QPushButton::event
virtual bool event(QEvent *e)
QString::length
int length() const
QStyleOption::init
void init(const QWidget *widget)
QStyle::drawPrimitive
virtual void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const =0
KColorButton::isAlphaChannelEnabled
bool isAlphaChannelEnabled() const
Returns true if the user is allowed to change the alpha component.
Definition: kcolorbutton.cpp:163
QPushButton
QKeySequence::keyBindings
QList< QKeySequence > keyBindings(StandardKey key)
QWidget::show
void show()
QStyle::subElementRect
virtual QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const =0
QMouseEvent::pos
QPoint pos() const
QPushButton::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
QPaintEvent
QPalette::window
const QBrush & window() const
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::parent
QObject * parent() const
QPushButton::isDefault
bool isDefault() const
QGuiApplication::clipboard
QClipboard * clipboard()
QRect::getRect
void getRect(int *x, int *y, int *width, int *height) const
KColorButton::defaultColor
QColor defaultColor() const
Returns the default color or an invalid color if no default color is set.
QStyle::sizeFromContents
virtual QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const =0
QIcon
QApplication::startDragDistance
int startDragDistance()
QColor::isValid
bool isValid() const
QMimeData::hasColor
bool hasColor() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Dec 9 2019 01:43:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal