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   int x, y, w, h;
00094   QRect r = style().subRect( QStyle::SR_PushButtonContents, this );
00095   r.rect(&x, &y, &w, &h);
00096 
00097   int margin = style().pixelMetric( QStyle::PM_ButtonMargin, this );
00098   x += margin;
00099   y += margin;
00100   w -= 2*margin;
00101   h -= 2*margin;
00102 
00103   if (isOn() || isDown()) {
00104     x += style().pixelMetric( QStyle::PM_ButtonShiftHorizontal, this );
00105     y += style().pixelMetric( QStyle::PM_ButtonShiftVertical, this );
00106   }
00107 
00108   QColor fillCol = isEnabled() ? col : backgroundColor();
00109   qDrawShadePanel( painter, x, y, w, h, colorGroup(), true, 1, NULL);
00110   if ( fillCol.isValid() )
00111     painter->fillRect( x+1, y+1, w-2, h-2, fillCol );
00112 
00113   if ( hasFocus() ) {
00114     QRect focusRect = style().subRect( QStyle::SR_PushButtonFocusRect, this );
00115     style().drawPrimitive( QStyle::PE_FocusRect, painter, focusRect, colorGroup() );
00116   }
00117 }
00118 
00119 QSize KColorButton::sizeHint() const
00120 {
00121   return style().sizeFromContents(QStyle::CT_PushButton, this, QSize(40, 15)).
00122         expandedTo(QApplication::globalStrut());
00123 }
00124 
00125 void KColorButton::dragEnterEvent( QDragEnterEvent *event)
00126 {
00127   event->accept( KColorDrag::canDecode( event) && isEnabled());
00128 }
00129 
00130 void KColorButton::dropEvent( QDropEvent *event)
00131 {
00132   QColor c;
00133   if( KColorDrag::decode( event, c)) {
00134     setColor(c);
00135   }
00136 }
00137 
00138 void KColorButton::mousePressEvent( QMouseEvent *e)
00139 {
00140   mPos = e->pos();
00141   QPushButton::mousePressEvent(e);
00142 }
00143 
00144 void KColorButton::mouseMoveEvent( QMouseEvent *e)
00145 {
00146   if( (e->state() & LeftButton) &&
00147     (e->pos()-mPos).manhattanLength() > KGlobalSettings::dndEventDelay() )
00148   {
00149     // Drag color object
00150     KColorDrag *dg = new KColorDrag( color(), this);
00151     dg->dragCopy();
00152     setDown(false);
00153   }
00154 }
00155 
00156 void KColorButton::chooseColor()
00157 {
00158   QColor c = color();
00159   if ( d->m_bdefaultColor )
00160   {
00161       if( KColorDialog::getColor( c, d->m_defaultColor, this ) != QDialog::Rejected ) {
00162           setColor( c );
00163       }
00164   }
00165   else
00166   {
00167       if( KColorDialog::getColor( c, this ) != QDialog::Rejected ) {
00168           setColor( c );
00169       }
00170   }
00171 }
00172 
00173 void KColorButton::virtual_hook( int, void* )
00174 { /*BASE::virtual_hook( id, data );*/ }
00175 
00176 #include "kcolorbutton.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed May 12 09:04:40 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003