• Skip to content
  • Skip to link menu
KDE 4.6 API Reference
  • KDE API Reference
  • kdelibs
  • 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 
00040 using KDEPrivate::fillOpaqueRect;
00041 
00042 class KColorButton::KColorButtonPrivate
00043 {
00044 public:
00045     KColorButtonPrivate(KColorButton *q);
00046 
00047     void _k_chooseColor();
00048 
00049     KColorButton *q;
00050     QColor m_defaultColor;
00051     bool m_bdefaultColor : 1;
00052     bool m_alphaChannel : 1;
00053 
00054     QColor col;
00055     QPoint mPos;
00056 
00057     void initStyleOption(QStyleOptionButton* opt) const;    
00058 };
00059 
00060 KColorButton::KColorButtonPrivate::KColorButtonPrivate(KColorButton *q)
00061     : q(q)
00062 {
00063   m_bdefaultColor = false;
00064   m_alphaChannel = false;
00065   q->setAcceptDrops(true);
00066 
00067   connect(q, SIGNAL(clicked()), q, SLOT(_k_chooseColor()));
00068 }
00069 
00070 KColorButton::KColorButton( QWidget *parent )
00071   : QPushButton( parent )
00072   , d( new KColorButtonPrivate(this) )
00073 {
00074 }
00075 
00076 KColorButton::KColorButton( const QColor &c, QWidget *parent )
00077   : QPushButton( parent )
00078   , d( new KColorButtonPrivate(this) )
00079 {
00080   d->col = c;
00081 }
00082 
00083 KColorButton::KColorButton( const QColor &c, const QColor &defaultColor, QWidget *parent )
00084   : QPushButton( parent )
00085   , d( new KColorButtonPrivate(this) )
00086 {
00087   d->col = c;
00088   setDefaultColor(defaultColor);
00089 }
00090 
00091 KColorButton::~KColorButton()
00092 {
00093   delete d;
00094 }
00095 
00096 QColor KColorButton::color() const
00097 {
00098   return d->col;
00099 }
00100 
00101 void KColorButton::setColor( const QColor &c )
00102 {
00103   if ( d->col != c ) {
00104     d->col = c;
00105     update();
00106     emit changed( d->col );
00107   }
00108 }
00109 
00110 void KColorButton::setAlphaChannelEnabled( bool alpha )
00111 {
00112     d->m_alphaChannel = alpha;
00113 }
00114 
00115 bool KColorButton::isAlphaChannelEnabled() const
00116 {
00117     return d->m_alphaChannel;
00118 }
00119 
00120 QColor KColorButton::defaultColor() const
00121 {
00122   return d->m_defaultColor;
00123 }
00124 
00125 void KColorButton::setDefaultColor( const QColor &c )
00126 {
00127   d->m_bdefaultColor = c.isValid();
00128   d->m_defaultColor = c;
00129 }
00130 
00131 void KColorButton::KColorButtonPrivate::initStyleOption(QStyleOptionButton* opt) const
00132 {
00133     opt->initFrom(q);
00134     opt->state |= q->isDown() ? QStyle::State_Sunken : QStyle::State_Raised;
00135     opt->features = QStyleOptionButton::None;
00136     if (q->isDefault())
00137       opt->features |= QStyleOptionButton::DefaultButton;
00138     opt->text.clear();
00139     opt->icon = QIcon();
00140 }
00141 
00142 void KColorButton::paintEvent( QPaintEvent* )
00143 {
00144   QPainter painter(this);
00145   QStyle *style = QWidget::style();
00146 
00147   //First, we need to draw the bevel.
00148   QStyleOptionButton butOpt;
00149   d->initStyleOption(&butOpt);
00150   style->drawControl( QStyle::CE_PushButtonBevel, &butOpt, &painter, this );
00151 
00152   //OK, now we can muck around with drawing out pretty little color box
00153   //First, sort out where it goes
00154   QRect labelRect = style->subElementRect( QStyle::SE_PushButtonContents,
00155       &butOpt, this );
00156   int shift = style->pixelMetric( QStyle::PM_ButtonMargin, &butOpt, this ) / 2;
00157   labelRect.adjust(shift, shift, -shift, -shift);
00158   int x, y, w, h;
00159   labelRect.getRect(&x, &y, &w, &h);
00160 
00161   if (isChecked() || isDown()) {
00162     x += style->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &butOpt, this );
00163     y += style->pixelMetric( QStyle::PM_ButtonShiftVertical, &butOpt, this );
00164   }
00165 
00166   QColor fillCol = isEnabled() ? d->col : palette().color(backgroundRole());
00167   qDrawShadePanel( &painter, x, y, w, h, palette(), true, 1, NULL);
00168   if ( fillCol.isValid() ) {
00169     fillOpaqueRect(&painter, QRect( x+1, y+1, w-2, h-2), fillCol );
00170   }
00171 
00172   if ( hasFocus() ) {
00173     QRect focusRect = style->subElementRect( QStyle::SE_PushButtonFocusRect, &butOpt, this );
00174     QStyleOptionFocusRect focusOpt;
00175     focusOpt.init(this);
00176     focusOpt.rect            = focusRect;
00177     focusOpt.backgroundColor = palette().background().color();
00178     style->drawPrimitive( QStyle::PE_FrameFocusRect, &focusOpt, &painter, this );
00179   }
00180 }
00181 
00182 QSize KColorButton::sizeHint() const
00183 {
00184     QStyleOptionButton opt;
00185     d->initStyleOption(&opt);
00186     return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(40, 15), this).
00187         expandedTo(QApplication::globalStrut());
00188 }
00189 
00190 QSize KColorButton::minimumSizeHint() const
00191 {
00192     QStyleOptionButton opt;
00193     d->initStyleOption(&opt);
00194     return style()->sizeFromContents(QStyle::CT_PushButton, &opt, QSize(3, 3), this).
00195         expandedTo(QApplication::globalStrut());
00196 }
00197 
00198 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
00199 {
00200   event->setAccepted( KColorMimeData::canDecode( event->mimeData()) && isEnabled());
00201 }
00202 
00203 void KColorButton::dropEvent( QDropEvent *event)
00204 {
00205   QColor c=KColorMimeData::fromMimeData( event->mimeData());
00206   if (c.isValid()) {
00207     setColor(c);
00208   }
00209 }
00210 
00211 void KColorButton::keyPressEvent( QKeyEvent *e )
00212 {
00213   int key = e->key() | e->modifiers();
00214 
00215   if ( KStandardShortcut::copy().contains( key ) ) {
00216     QMimeData *mime=new QMimeData;
00217     KColorMimeData::populateMimeData(mime,color());
00218     QApplication::clipboard()->setMimeData( mime, QClipboard::Clipboard );
00219   }
00220   else if ( KStandardShortcut::paste().contains( key ) ) {
00221     QColor color=KColorMimeData::fromMimeData( QApplication::clipboard()->mimeData( QClipboard::Clipboard ));
00222     setColor( color );
00223   }
00224   else
00225     QPushButton::keyPressEvent( e );
00226 }
00227 
00228 void KColorButton::mousePressEvent( QMouseEvent *e)
00229 {
00230   d->mPos = e->pos();
00231   QPushButton::mousePressEvent(e);
00232 }
00233 
00234 void KColorButton::mouseMoveEvent( QMouseEvent *e)
00235 {
00236   if( (e->buttons() & Qt::LeftButton) &&
00237     (e->pos()-d->mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
00238   {
00239     KColorMimeData::createDrag(color(),this)->start();
00240     setDown(false);
00241   }
00242 }
00243 
00244 void KColorButton::KColorButtonPrivate::_k_chooseColor()
00245 {
00246     QPointer<KColorDialog> dialog = new KColorDialog(q, true);
00247     dialog->setColor(q->color());
00248     if (m_bdefaultColor) {
00249         dialog->setDefaultColor(m_defaultColor);
00250     }
00251     dialog->setAlphaChannelEnabled(m_alphaChannel);
00252     if (dialog->exec() != QDialog::Rejected) {
00253         if (dialog->color().isValid()) {
00254             q->setColor(dialog->color());
00255         } else if (m_bdefaultColor) {
00256             q->setColor(m_defaultColor);
00257         }
00258     }
00259     delete dialog;
00260 }
00261 
00262 
00263 #include "kcolorbutton.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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
Generated for kdelibs by doxygen 1.7.3
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal