• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • colors
kcolorbutton.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Martin Jones (mjones@kde.org)
3  Copyright (C) 1999 Cristian Tibirna (ctibirna@kde.org)
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 <config.h>
24 
25 #include <QtCore/QPointer>
26 #include <QtGui/QPainter>
27 #include <QtGui/qdrawutil.h>
28 #include <QtGui/QApplication>
29 #include <QtGui/QClipboard>
30 #include <QtGui/QStyle>
31 #include <kglobalsettings.h>
32 #include <kstandardshortcut.h>
33 #include <QMouseEvent>
34 #include <QStyleOptionButton>
35 #include "kcolordialog.h"
36 #include "kcolorhelpers_p.h"
37 #include "kcolormimedata.h"
38 #include "kdebug.h"
39 #include "kwindowsystem.h"
40 
41 using KDEPrivate::fillOpaqueRect;
42 
43 class KColorButton::KColorButtonPrivate
44 {
45 public:
46  KColorButtonPrivate(KColorButton *q);
47 
48  void _k_chooseColor();
49  void _k_colorChosen();
50 
51  KColorButton *q;
52  QColor m_defaultColor;
53  bool m_bdefaultColor : 1;
54  bool m_alphaChannel : 1;
55 
56  QColor col;
57  QPoint mPos;
58 
59  QWeakPointer<KColorDialog> dialogPtr;
60 
61  void initStyleOption(QStyleOptionButton* opt) const;
62 };
63 
64 KColorButton::KColorButtonPrivate::KColorButtonPrivate(KColorButton *q)
65  : q(q)
66 {
67  m_bdefaultColor = false;
68  m_alphaChannel = false;
69  q->setAcceptDrops(true);
70 
71  connect(q, SIGNAL(clicked()), q, SLOT(_k_chooseColor()));
72 }
73 
74 KColorButton::KColorButton( QWidget *parent )
75  : QPushButton( parent )
76  , d( new KColorButtonPrivate(this) )
77 {
78 }
79 
80 KColorButton::KColorButton( const QColor &c, QWidget *parent )
81  : QPushButton( parent )
82  , d( new KColorButtonPrivate(this) )
83 {
84  d->col = c;
85 }
86 
87 KColorButton::KColorButton( const QColor &c, const QColor &defaultColor, QWidget *parent )
88  : QPushButton( parent )
89  , d( new KColorButtonPrivate(this) )
90 {
91  d->col = c;
92  setDefaultColor(defaultColor);
93 }
94 
95 KColorButton::~KColorButton()
96 {
97  delete d;
98 }
99 
100 QColor KColorButton::color() const
101 {
102  return d->col;
103 }
104 
105 void KColorButton::setColor( const QColor &c )
106 {
107  if ( d->col != c ) {
108  d->col = c;
109  update();
110  emit changed( d->col );
111  }
112 }
113 
114 void KColorButton::setAlphaChannelEnabled( bool alpha )
115 {
116  d->m_alphaChannel = alpha;
117 }
118 
119 bool KColorButton::isAlphaChannelEnabled() const
120 {
121  return d->m_alphaChannel;
122 }
123 
124 QColor KColorButton::defaultColor() const
125 {
126  return d->m_defaultColor;
127 }
128 
129 void KColorButton::setDefaultColor( const QColor &c )
130 {
131  d->m_bdefaultColor = c.isValid();
132  d->m_defaultColor = c;
133 }
134 
135 void KColorButton::KColorButtonPrivate::initStyleOption(QStyleOptionButton* opt) const
136 {
137  opt->initFrom(q);
138  opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
139  opt->features = QStyleOptionButton::None;
140  if (q->isDefault())
141  opt->features |= QStyleOptionButton::DefaultButton;
142  opt->text.clear();
143  opt->icon = QIcon();
144 }
145 
146 void KColorButton::paintEvent( QPaintEvent* )
147 {
148  QPainter painter(this);
149  QStyle *style = QWidget::style();
150 
151  //First, we need to draw the bevel.
152  QStyleOptionButton butOpt;
153  d->initStyleOption(&butOpt);
154  style->drawControl( QStyle::CE_PushButtonBevel, &butOpt, &painter, this );
155 
156  //OK, now we can muck around with drawing out pretty little color box
157  //First, sort out where it goes
158  QRect labelRect = style->subElementRect( QStyle::SE_PushButtonContents,
159  &butOpt, this );
160  int shift = style->pixelMetric( QStyle::PM_ButtonMargin, &butOpt, this ) / 2;
161  labelRect.adjust(shift, shift, -shift, -shift);
162  int x, y, w, h;
163  labelRect.getRect(&x, &y, &w, &h);
164 
165  if (isChecked() || isDown()) {
166  x += style->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &butOpt, this );
167  y += style->pixelMetric( QStyle::PM_ButtonShiftVertical, &butOpt, this );
168  }
169 
170  QColor fillCol = isEnabled() ? d->col : palette().color(backgroundRole());
171  qDrawShadePanel( &painter, x, y, w, h, palette(), true, 1, NULL);
172  if ( fillCol.isValid() ) {
173  fillOpaqueRect(&painter, QRect( x+1, y+1, w-2, h-2), fillCol );
174  }
175 
176  if ( hasFocus() ) {
177  QRect focusRect = style->subElementRect( QStyle::SE_PushButtonFocusRect, &butOpt, this );
178  QStyleOptionFocusRect focusOpt;
179  focusOpt.init(this);
180  focusOpt.rect = focusRect;
181  focusOpt.backgroundColor = palette().background().color();
182  style->drawPrimitive( QStyle::PE_FrameFocusRect, &focusOpt, &painter, this );
183  }
184 }
185 
186 QSize KColorButton::sizeHint() const
187 {
188  QStyleOptionButton opt;
189  d->initStyleOption(&opt);
190  return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
191  expandedTo(QApplication::globalStrut());
192 }
193 
194 QSize KColorButton::minimumSizeHint() const
195 {
196  QStyleOptionButton opt;
197  d->initStyleOption(&opt);
198  return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
199  expandedTo(QApplication::globalStrut());
200 }
201 
202 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
203 {
204  event->setAccepted( KColorMimeData::canDecode( event->mimeData()) && isEnabled());
205 }
206 
207 void KColorButton::dropEvent( QDropEvent *event)
208 {
209  QColor c=KColorMimeData::fromMimeData( event->mimeData());
210  if (c.isValid()) {
211  setColor(c);
212  }
213 }
214 
215 void KColorButton::keyPressEvent( QKeyEvent *e )
216 {
217  int key = e->key() | e->modifiers();
218 
219  if ( KStandardShortcut::copy().contains( key ) ) {
220  QMimeData *mime=new QMimeData;
221  KColorMimeData::populateMimeData(mime,color());
222  QApplication::clipboard()->setMimeData( mime, QClipboard::Clipboard );
223  }
224  else if ( KStandardShortcut::paste().contains( key ) ) {
225  QColor color=KColorMimeData::fromMimeData( QApplication::clipboard()->mimeData( QClipboard::Clipboard ));
226  setColor( color );
227  }
228  else
229  QPushButton::keyPressEvent( e );
230 }
231 
232 void KColorButton::mousePressEvent( QMouseEvent *e)
233 {
234  d->mPos = e->pos();
235  QPushButton::mousePressEvent(e);
236 }
237 
238 void KColorButton::mouseMoveEvent( QMouseEvent *e)
239 {
240  if( (e->buttons() & Qt::LeftButton) &&
241  (e->pos()-d->mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
242  {
243  KColorMimeData::createDrag(color(),this)->start();
244  setDown(false);
245  }
246 }
247 
248 void KColorButton::KColorButtonPrivate::_k_chooseColor()
249 {
250  KColorDialog *dialog = dialogPtr.data();
251  if (dialog) {
252  dialog->show();
253  KWindowSystem::forceActiveWindow(dialog->winId());
254  return;
255  }
256 
257  dialog = new KColorDialog(q);
258  dialog->setColor(q->color());
259  if (m_bdefaultColor) {
260  dialog->setDefaultColor(m_defaultColor);
261  }
262  dialog->setAlphaChannelEnabled(m_alphaChannel);
263  dialog->setAttribute(Qt::WA_DeleteOnClose);
264  dialog->setButtons(KDialog::Ok | KDialog::Cancel);
265  connect(dialog, SIGNAL(applyClicked()), q, SLOT(_k_colorChosen()));
266  connect(dialog, SIGNAL(accepted()), q, SLOT(_k_colorChosen()));
267  dialogPtr = dialog;
268  dialog->show();
269 }
270 
271 void KColorButton::KColorButtonPrivate::_k_colorChosen()
272 {
273  KColorDialog *dialog = dialogPtr.data();
274  if (!dialog) {
275  return;
276  }
277 
278  if (dialog->color().isValid()) {
279  q->setColor(dialog->color());
280  } else if (m_bdefaultColor) {
281  q->setColor(m_defaultColor);
282  }
283 }
284 
285 #include "kcolorbutton.moc"
QColor
KColorButton::changed
void changed(const QColor &newColor)
Emitted when the color of the widget is changed, either with setColor() or via user selection...
kdebug.h
KColorButton::setAlphaChannelEnabled
void setAlphaChannelEnabled(bool alpha)
When set to true, allow the user to change the alpha component of the color.
Definition: kcolorbutton.cpp:114
KColorButton::setColor
void setColor(const QColor &c)
Sets the current color to c.
Definition: kcolorbutton.cpp:105
kcolorbutton.h
KColorDialog::setAlphaChannelEnabled
void setAlphaChannelEnabled(bool alpha)
When set to true, the user is allowed to change the alpha component of the color. ...
Definition: kcolordialog.cpp:1376
kglobalsettings.h
KColorDialog::setDefaultColor
void setDefaultColor(const QColor &defaultCol)
Call this to make the dialog show a "Default Color" checkbox.
Definition: kcolordialog.cpp:1343
KColorMimeData::canDecode
bool canDecode(const QMimeData *mimeData)
Returns true if the MIME data mimeData contains a color object.
Definition: kcolormimedata.cpp:36
KDialog::Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected) ...
Definition: kdialog.h:144
QWidget
KColorButton::sizeHint
QSize sizeHint() const
Definition: kcolorbutton.cpp:186
QPushButton
KWindowSystem::forceActiveWindow
static void forceActiveWindow(WId win, long time=0)
Sets window win to be the active window.
Definition: kwindowsystem_mac.cpp:366
KColorButton::color
QColor color() const
Returns the currently chosen color.
kcolormimedata.h
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
KStandardShortcut::copy
const KShortcut & copy()
Copy selected area into the clipboard.
Definition: kstandardshortcut.cpp:335
KDialog::Ok
Show Ok button. (this button accept()s the dialog; result set to QDialog::Accepted) ...
Definition: kdialog.h:141
kstandardshortcut.h
KColorMimeData::createDrag
QDrag * createDrag(const QColor &color, QWidget *dragsource)
Creates a color drag object.
Definition: kcolormimedata.cpp:61
KColorButton::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Definition: kcolorbutton.cpp:215
KColorDialog::color
QColor color
Definition: kcolordialog.h:215
KColorButton::setDefaultColor
void setDefaultColor(const QColor &c)
Sets the default color to c.
Definition: kcolorbutton.cpp:129
KColorButton::~KColorButton
virtual ~KColorButton()
Definition: kcolorbutton.cpp:95
KColorMimeData::fromMimeData
QColor fromMimeData(const QMimeData *mimeData)
Decodes the MIME data mimeData and returns the resulting color.
Definition: kcolormimedata.cpp:50
KColorMimeData::populateMimeData
void populateMimeData(QMimeData *mimeData, const QColor &color)
Sets the color and text representation fields for the specified color in the mimedata object: applica...
Definition: kcolormimedata.cpp:29
KColorButton
A pushbutton to display or allow user selection of a color.
Definition: kcolorbutton.h:37
QPoint
KColorDialog
A color selection dialog.
Definition: kcolordialog.h:210
kcolordialog.h
QRect
KColorButton::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: kcolorbutton.cpp:238
kwindowsystem.h
KColorButton::isAlphaChannelEnabled
bool isAlphaChannelEnabled() const
Returns true if the user is allowed to change the alpha component.
Definition: kcolorbutton.cpp:119
KColorButton::color
QColor color
Definition: kcolorbutton.h:40
KColorButton::KColorButton
KColorButton(QWidget *parent=0)
Creates a color button.
Definition: kcolorbutton.cpp:74
QSize
KColorButton::dropEvent
virtual void dropEvent(QDropEvent *)
Definition: kcolorbutton.cpp:207
KColorDialog::setColor
void setColor(const QColor &col)
Preselects a color.
Definition: kcolordialog.cpp:1484
KColorButton::paintEvent
virtual void paintEvent(QPaintEvent *pe)
Definition: kcolorbutton.cpp:146
KDEPrivate::fillOpaqueRect
void fillOpaqueRect(QPainter *painter, const QRect &rect, const QBrush &brush)
Definition: kcolorhelpers.cpp:28
KGlobalSettings::dndEventDelay
static int dndEventDelay()
Returns a threshold in pixels for drag & drop operations.
Definition: kglobalsettings.cpp:227
KColorButton::defaultColor
QColor defaultColor() const
Returns the default color or an invalid color if no default color is set.
KColorButton::dragEnterEvent
virtual void dragEnterEvent(QDragEnterEvent *)
Definition: kcolorbutton.cpp:202
KColorButton::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
Definition: kcolorbutton.cpp:232
KColorButton::minimumSizeHint
QSize minimumSizeHint() const
Definition: kcolorbutton.cpp:194
KStandardShortcut::paste
const KShortcut & paste()
Paste contents of clipboard at mouse/cursor position.
Definition: kstandardshortcut.cpp:336
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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