00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
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
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 { }
00165
00166 #include "kcolorbutton.moc"