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