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 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
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 { }
00175
00176 #include "kcolorbutton.moc"