kdeui Library API Documentation

kcolorbutton.cpp

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <config.h>
00022 
00023 #include <qpainter.h>
00024 #include <qdrawutil.h>
00025 #include <qapplication.h>
00026 #include <qstyle.h>
00027 #include <kglobalsettings.h>
00028 #include "kcolordialog.h"
00029 #include "kcolorbutton.h"
00030 #include "kcolordrag.h"
00031 
00032 class KColorButton::KColorButtonPrivate
00033 {
00034 public:
00035     bool m_bdefaultColor;
00036     QColor m_defaultColor;
00037 };
00038 
00039 KColorButton::KColorButton( QWidget *parent, const char *name )
00040   : QPushButton( parent, name )
00041 {
00042     d = new KColorButtonPrivate;
00043     d->m_bdefaultColor = false;
00044     d->m_defaultColor = QColor();
00045   setAcceptDrops( true);
00046 
00047   // 2000-10-15 (putzer): fixes broken keyboard usage
00048   connect (this, SIGNAL(clicked()), this, SLOT(chooseColor()));
00049 }
00050 
00051 KColorButton::KColorButton( const QColor &c, QWidget *parent,
00052                 const char *name )
00053   : QPushButton( parent, name ), col(c)
00054 {
00055     d = new KColorButtonPrivate;
00056     d->m_bdefaultColor = false;
00057     d->m_defaultColor = QColor();
00058     setAcceptDrops( true);
00059 
00060   // 2000-10-15 (putzer): fixes broken keyboard usage
00061   connect (this, SIGNAL(clicked()), this, SLOT(chooseColor()));
00062 }
00063 
00064 KColorButton::KColorButton( const QColor &c, const QColor &defaultColor, QWidget *parent,
00065                 const char *name )
00066   : QPushButton( parent, name ), col(c)
00067 {
00068     d = new KColorButtonPrivate;
00069     d->m_bdefaultColor = true;
00070     d->m_defaultColor = defaultColor;
00071     setAcceptDrops( true);
00072 
00073   // 2000-10-15 (putzer): fixes broken keyboard usage
00074   connect (this, SIGNAL(clicked()), this, SLOT(chooseColor()));
00075 }
00076 
00077 KColorButton::~KColorButton()
00078 {
00079     delete d;
00080 }
00081 
00082 void KColorButton::setColor( const QColor &c )
00083 {
00084   if ( col != c ) {
00085     col = c;
00086     repaint( false );
00087     emit changed( col );
00088   }
00089 }
00090 
00091 void KColorButton::drawButtonLabel( QPainter *painter )
00092 {
00093   QRect r = style().subRect( QStyle::SR_PushButtonContents, this );
00094   int l = r.x();
00095   int t = r.y();
00096   int w = r.width();
00097   int h = r.height();
00098   int b = 5;
00099 
00100   QColor fillCol = isEnabled() ? col : backgroundColor();
00101 
00102   if ( isDown() ) {
00103     qDrawShadePanel( painter, l+b+1, t+b+1, w-b*2, h-b*2, colorGroup(), 1, 1, 0 );
00104     b++;
00105     if ( fillCol.isValid() )
00106       painter->fillRect( l+b+1, t+b+1, w-b*2, h-b*2, fillCol );
00107   } else {
00108     qDrawShadePanel( painter, l+b, t+b, w-b*2, h-b*2, colorGroup(), 1, 1, 0 );
00109     b++;
00110     if ( fillCol.isValid() )
00111       painter->fillRect( l+b, t+b, w-b*2, h-b*2, fillCol );
00112   }
00113 }
00114 
00115 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
00116 {
00117   event->accept( KColorDrag::canDecode( event) && isEnabled());
00118 }
00119 
00120 void KColorButton::dropEvent( QDropEvent *event)
00121 {
00122   QColor c;
00123   if( KColorDrag::decode( event, c)) {
00124     setColor(c);
00125   }
00126 }
00127 
00128 void KColorButton::mousePressEvent( QMouseEvent *e)
00129 {
00130   mPos = e->pos();
00131   QPushButton::mousePressEvent(e);
00132 }
00133 
00134 void KColorButton::mouseMoveEvent( QMouseEvent *e)
00135 {
00136   if( (e->state() & LeftButton) &&
00137     (e->pos()-mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
00138   {
00139     // Drag color object
00140     KColorDrag *dg = KColorDrag::makeDrag( color(), this);
00141     dg->dragCopy();
00142     setDown(false);
00143   }
00144 }
00145 
00146 void KColorButton::chooseColor()
00147 {
00148   QColor c = color();
00149   if ( d->m_bdefaultColor )
00150   {
00151       if( KColorDialog::getColor( c, d->m_defaultColor, this ) != QDialog::Rejected ) {
00152           setColor( c );
00153       }
00154   }
00155   else
00156   {
00157       if( KColorDialog::getColor( c, this ) != QDialog::Rejected ) {
00158           setColor( c );
00159       }
00160   }
00161 }
00162 
00163 void KColorButton::virtual_hook( int, void* )
00164 { /*BASE::virtual_hook( id, data );*/ }
00165 
00166 #include "kcolorbutton.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:02 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001