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

KDEUI

kcolorbutton.cpp
Go to the documentation of this file.
00001 /*  This file is part of the KDE libraries
00002     Copyright (C) 1997 Martin Jones (mjones@kde.org)
00003     Copyright (C) 1999 Cristian Tibirna (ctibirna@kde.org)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kcolorbutton.h"
00022 
00023 #include <config.h>
00024 
00025 #include <QtCore/QPointer>
00026 #include <QtGui/QPainter>
00027 #include <QtGui/qdrawutil.h>
00028 #include <QtGui/QApplication>
00029 #include <QtGui/QClipboard>
00030 #include <QtGui/QStyle>
00031 #include <kglobalsettings.h>
00032 #include <kstandardshortcut.h>
00033 #include <QMouseEvent>
00034 #include <QStyleOptionButton>
00035 #include "kcolordialog.h"
00036 #include "kcolorhelpers_p.h"
00037 #include "kcolormimedata.h"
00038 #include "kdebug.h"
00039 #include "kwindowsystem.h"
00040 
00041 using KDEPrivate::fillOpaqueRect;
00042 
00043 class KColorButton::KColorButtonPrivate
00044 {
00045 public:
00046     KColorButtonPrivate(KColorButton *q);
00047 
00048     void _k_chooseColor();
00049     void _k_colorChosen();
00050 
00051     KColorButton *q;
00052     QColor m_defaultColor;
00053     bool m_bdefaultColor : 1;
00054     bool m_alphaChannel : 1;
00055 
00056     QColor col;
00057     QPoint mPos;
00058 
00059     QWeakPointer<KColorDialog> dialogPtr;
00060 
00061     void initStyleOption(QStyleOptionButton* opt) const;    
00062 };
00063 
00064 KColorButton::KColorButtonPrivate::KColorButtonPrivate(KColorButton *q)
00065     : q(q)
00066 {
00067   m_bdefaultColor = false;
00068   m_alphaChannel = false;
00069   q->setAcceptDrops(true);
00070 
00071   connect(q, SIGNAL(clicked()), q, SLOT(_k_chooseColor()));
00072 }
00073 
00074 KColorButton::KColorButton( QWidget *parent )
00075   : QPushButton( parent )
00076   , d( new KColorButtonPrivate(this) )
00077 {
00078 }
00079 
00080 KColorButton::KColorButton( const QColor &c, QWidget *parent )
00081   : QPushButton( parent )
00082   , d( new KColorButtonPrivate(this) )
00083 {
00084   d->col = c;
00085 }
00086 
00087 KColorButton::KColorButton( const QColor &c, const QColor &defaultColor, QWidget *parent )
00088   : QPushButton( parent )
00089   , d( new KColorButtonPrivate(this) )
00090 {
00091   d->col = c;
00092   setDefaultColor(defaultColor);
00093 }
00094 
00095 KColorButton::~KColorButton()
00096 {
00097   delete d;
00098 }
00099 
00100 QColor KColorButton::color() const
00101 {
00102   return d->col;
00103 }
00104 
00105 void KColorButton::setColor( const QColor &c )
00106 {
00107   if ( d->col != c ) {
00108     d->col = c;
00109     update();
00110     emit changed( d->col );
00111   }
00112 }
00113 
00114 void KColorButton::setAlphaChannelEnabled( bool alpha )
00115 {
00116     d->m_alphaChannel = alpha;
00117 }
00118 
00119 bool KColorButton::isAlphaChannelEnabled() const
00120 {
00121     return d->m_alphaChannel;
00122 }
00123 
00124 QColor KColorButton::defaultColor() const
00125 {
00126   return d->m_defaultColor;
00127 }
00128 
00129 void KColorButton::setDefaultColor( const QColor &c )
00130 {
00131   d->m_bdefaultColor = c.isValid();
00132   d->m_defaultColor = c;
00133 }
00134 
00135 void KColorButton::KColorButtonPrivate::initStyleOption(QStyleOptionButton* opt) const
00136 {
00137     opt->initFrom(q);
00138     opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
00139     opt->features = QStyleOptionButton::None;
00140     if (q->isDefault())
00141       opt->features |= QStyleOptionButton::DefaultButton;
00142     opt->text.clear();
00143     opt->icon = QIcon();
00144 }
00145 
00146 void KColorButton::paintEvent( QPaintEvent* )
00147 {
00148   QPainter painter(this);
00149   QStyle *style = QWidget::style();
00150 
00151   //First, we need to draw the bevel.
00152   QStyleOptionButton butOpt;
00153   d->initStyleOption(&butOpt);
00154   style->drawControl( QStyle::CE_PushButtonBevel, &butOpt, &painter, this );
00155 
00156   //OK, now we can muck around with drawing out pretty little color box
00157   //First, sort out where it goes
00158   QRect labelRect = style->subElementRect( QStyle::SE_PushButtonContents,
00159       &butOpt, this );
00160   int shift = style->pixelMetric( QStyle::PM_ButtonMargin, &butOpt, this ) / 2;
00161   labelRect.adjust(shift, shift, -shift, -shift);
00162   int x, y, w, h;
00163   labelRect.getRect(&x, &y, &w, &h);
00164 
00165   if (isChecked() || isDown()) {
00166     x += style->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &butOpt, this );
00167     y += style->pixelMetric( QStyle::PM_ButtonShiftVertical, &butOpt, this );
00168   }
00169 
00170   QColor fillCol = isEnabled() ? d->col : palette().color(backgroundRole());
00171   qDrawShadePanel( &painter, x, y, w, h, palette(), true, 1, NULL);
00172   if ( fillCol.isValid() ) {
00173     fillOpaqueRect(&painter, QRect( x+1, y+1, w-2, h-2), fillCol );
00174   }
00175 
00176   if ( hasFocus() ) {
00177     QRect focusRect = style->subElementRect( QStyle::SE_PushButtonFocusRect, &butOpt, this );
00178     QStyleOptionFocusRect focusOpt;
00179     focusOpt.init(this);
00180     focusOpt.rect            = focusRect;
00181     focusOpt.backgroundColor = palette().background().color();
00182     style->drawPrimitive( QStyle::PE_FrameFocusRect, &focusOpt, &painter, this );
00183   }
00184 }
00185 
00186 QSize KColorButton::sizeHint() const
00187 {
00188     QStyleOptionButton opt;
00189     d->initStyleOption(&opt);
00190     return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
00191         expandedTo(QApplication::globalStrut());
00192 }
00193 
00194 QSize KColorButton::minimumSizeHint() const
00195 {
00196     QStyleOptionButton opt;
00197     d->initStyleOption(&opt);
00198     return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
00199         expandedTo(QApplication::globalStrut());
00200 }
00201 
00202 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
00203 {
00204   event->setAccepted( KColorMimeData::canDecode( event->mimeData()) && isEnabled());
00205 }
00206 
00207 void KColorButton::dropEvent( QDropEvent *event)
00208 {
00209   QColor c=KColorMimeData::fromMimeData( event->mimeData());
00210   if (c.isValid()) {
00211     setColor(c);
00212   }
00213 }
00214 
00215 void KColorButton::keyPressEvent( QKeyEvent *e )
00216 {
00217   int key = e->key() | e->modifiers();
00218 
00219   if ( KStandardShortcut::copy().contains( key ) ) {
00220     QMimeData *mime=new QMimeData;
00221     KColorMimeData::populateMimeData(mime,color());
00222     QApplication::clipboard()->setMimeData( mime, QClipboard::Clipboard );
00223   }
00224   else if ( KStandardShortcut::paste().contains( key ) ) {
00225     QColor color=KColorMimeData::fromMimeData( QApplication::clipboard()->mimeData( QClipboard::Clipboard ));
00226     setColor( color );
00227   }
00228   else
00229     QPushButton::keyPressEvent( e );
00230 }
00231 
00232 void KColorButton::mousePressEvent( QMouseEvent *e)
00233 {
00234   d->mPos = e->pos();
00235   QPushButton::mousePressEvent(e);
00236 }
00237 
00238 void KColorButton::mouseMoveEvent( QMouseEvent *e)
00239 {
00240   if( (e->buttons() & Qt::LeftButton) &&
00241     (e->pos()-d->mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
00242   {
00243     KColorMimeData::createDrag(color(),this)->start();
00244     setDown(false);
00245   }
00246 }
00247 
00248 void KColorButton::KColorButtonPrivate::_k_chooseColor()
00249 {
00250     KColorDialog *dialog = dialogPtr.data();
00251     if (dialog) {
00252         dialog->show();
00253         KWindowSystem::forceActiveWindow(dialog->winId());
00254         return;
00255     }
00256 
00257     dialog = new KColorDialog(q);
00258     dialog->setColor(q->color());
00259     if (m_bdefaultColor) {
00260         dialog->setDefaultColor(m_defaultColor);
00261     }
00262     dialog->setAlphaChannelEnabled(m_alphaChannel);
00263     dialog->setAttribute(Qt::WA_DeleteOnClose);
00264     dialog->setButtons(KDialog::Ok | KDialog::Cancel);
00265     connect(dialog, SIGNAL(applyClicked()), q, SLOT(_k_colorChosen()));
00266     connect(dialog, SIGNAL(okClicked()), q, SLOT(_k_colorChosen()));
00267     dialogPtr = dialog;
00268     dialog->show();
00269 }
00270 
00271 void KColorButton::KColorButtonPrivate::_k_colorChosen()
00272 {
00273     KColorDialog *dialog = dialogPtr.data();
00274     if (!dialog) {
00275         return;
00276     }
00277 
00278     if (dialog->color().isValid()) {
00279         q->setColor(dialog->color());
00280     } else if (m_bdefaultColor) {
00281         q->setColor(m_defaultColor);
00282     }
00283 }
00284 
00285 #include "kcolorbutton.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:53:01 by doxygen 1.8.0 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-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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