kdeui Library API Documentation

kdualcolorbutton.cpp

00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Daniel M. Duley <mosfet@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00016 Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include "kdualcolorbutton.h" 00020 #include "kcolordialog.h" 00021 #include "kcolordrag.h" 00022 #include "dcolorarrow.xbm" 00023 #include "dcolorreset.xpm" 00024 #include <kglobalsettings.h> 00025 #include <qpainter.h> 00026 #include <qbitmap.h> 00027 #include <qdrawutil.h> 00028 00029 class KDualColorButton::KDualColorPrivate 00030 { 00031 public: 00032 QWidget* dialogParent; 00033 }; 00034 00035 KDualColorButton::KDualColorButton(QWidget *parent, const char *name, QWidget* dialogParent) 00036 : QWidget(parent, name), 00037 d (new KDualColorPrivate) 00038 { 00039 d->dialogParent = dialogParent; 00040 00041 arrowBitmap = new QBitmap(dcolorarrow_width, dcolorarrow_height, 00042 (const unsigned char *)dcolorarrow_bits, true); 00043 arrowBitmap->setMask(*arrowBitmap); // heh 00044 resetPixmap = new QPixmap((const char **)dcolorreset_xpm); 00045 fg = QBrush(Qt::black, SolidPattern); 00046 bg = QBrush(Qt::white, SolidPattern); 00047 curColor = Foreground; 00048 dragFlag = false; 00049 miniCtlFlag = false; 00050 if(sizeHint().isValid()) 00051 setMinimumSize(sizeHint()); 00052 setAcceptDrops(true); 00053 } 00054 00055 KDualColorButton::KDualColorButton(const QColor &fgColor, const QColor &bgColor, 00056 QWidget *parent, const char *name, QWidget* dialogParent) 00057 : QWidget(parent, name), 00058 d (new KDualColorPrivate) 00059 { 00060 d->dialogParent = dialogParent; 00061 00062 arrowBitmap = new QBitmap(dcolorarrow_width, dcolorarrow_height, 00063 (const unsigned char *)dcolorarrow_bits, true); 00064 arrowBitmap->setMask(*arrowBitmap); 00065 resetPixmap = new QPixmap((const char **)dcolorreset_xpm); 00066 fg = QBrush(fgColor, SolidPattern); 00067 bg = QBrush(bgColor, SolidPattern); 00068 curColor = Foreground; 00069 dragFlag = false; 00070 miniCtlFlag = false; 00071 if(sizeHint().isValid()) 00072 setMinimumSize(sizeHint()); 00073 setAcceptDrops(true); 00074 } 00075 00076 KDualColorButton::~KDualColorButton() 00077 { 00078 delete d; 00079 delete arrowBitmap; 00080 delete resetPixmap; 00081 } 00082 00083 QColor KDualColorButton::foreground() const 00084 { 00085 return(fg.color()); 00086 } 00087 00088 QColor KDualColorButton::background() const 00089 { 00090 return(bg.color()); 00091 } 00092 00093 KDualColorButton::DualColor KDualColorButton::current() const 00094 { 00095 return(curColor); 00096 } 00097 00098 QColor KDualColorButton::currentColor() const 00099 { 00100 return(curColor == Background ? bg.color() : fg.color()); 00101 } 00102 00103 QSize KDualColorButton::sizeHint() const 00104 { 00105 return(QSize(34, 34)); 00106 } 00107 00108 void KDualColorButton::setForeground(const QColor &c) 00109 { 00110 fg = QBrush(c, SolidPattern); 00111 repaint(false); 00112 00113 emit fgChanged(fg.color()); 00114 } 00115 00116 void KDualColorButton::setBackground(const QColor &c) 00117 { 00118 bg = QBrush(c, SolidPattern); 00119 repaint(false); 00120 00121 emit bgChanged(bg.color()); 00122 } 00123 00124 void KDualColorButton::setCurrentColor(const QColor &c) 00125 { 00126 if(curColor == Background) 00127 bg = QBrush(c, SolidPattern); 00128 else 00129 fg = QBrush(c, SolidPattern); 00130 repaint(false); 00131 } 00132 00133 void KDualColorButton::setCurrent(DualColor s) 00134 { 00135 curColor = s; 00136 repaint(false); 00137 } 00138 00139 void KDualColorButton::metrics(QRect &fgRect, QRect &bgRect) 00140 { 00141 fgRect = QRect(0, 0, width()-14, height()-14); 00142 bgRect = QRect(14, 14, width()-14, height()-14); 00143 } 00144 00145 void KDualColorButton::paintEvent(QPaintEvent *) 00146 { 00147 QRect fgRect, bgRect; 00148 QPainter p(this); 00149 00150 metrics(fgRect, bgRect); 00151 QBrush defBrush = colorGroup().brush(QColorGroup::Button); 00152 00153 qDrawShadeRect(&p, bgRect, colorGroup(), curColor == Background, 2, 0, 00154 isEnabled() ? &bg : &defBrush); 00155 qDrawShadeRect(&p, fgRect, colorGroup(), curColor == Foreground, 2, 0, 00156 isEnabled() ? &fg : &defBrush); 00157 p.setPen(colorGroup().shadow()); 00158 p.drawPixmap(fgRect.right()+2, 0, *arrowBitmap); 00159 p.drawPixmap(0, fgRect.bottom()+2, *resetPixmap); 00160 00161 } 00162 00163 void KDualColorButton::dragEnterEvent(QDragEnterEvent *ev) 00164 { 00165 ev->accept(isEnabled() && KColorDrag::canDecode(ev)); 00166 } 00167 00168 void KDualColorButton::dropEvent(QDropEvent *ev) 00169 { 00170 QColor c; 00171 if(KColorDrag::decode(ev, c)){ 00172 if(curColor == Foreground){ 00173 fg.setColor(c); 00174 emit fgChanged(c); 00175 } 00176 else{ 00177 bg.setColor(c); 00178 emit(bgChanged(c)); 00179 } 00180 repaint(false); 00181 } 00182 } 00183 00184 void KDualColorButton::mousePressEvent(QMouseEvent *ev) 00185 { 00186 QRect fgRect, bgRect; 00187 metrics(fgRect, bgRect); 00188 mPos = ev->pos(); 00189 tmpColor = curColor; 00190 dragFlag = false; 00191 if(fgRect.contains(mPos)){ 00192 curColor = Foreground; 00193 miniCtlFlag = false; 00194 } 00195 else if(bgRect.contains(mPos)){ 00196 curColor = Background; 00197 miniCtlFlag = false; 00198 } 00199 else if(ev->pos().x() > fgRect.width()){ 00200 // We handle the swap and reset controls as soon as the mouse is 00201 // is pressed and ignore further events on this click (mosfet). 00202 QBrush c = fg; 00203 fg = bg; 00204 bg = c; 00205 emit fgChanged(fg.color()); 00206 emit bgChanged(bg.color()); 00207 miniCtlFlag = true; 00208 } 00209 else if(ev->pos().x() < bgRect.x()){ 00210 fg.setColor(Qt::black); 00211 bg.setColor(Qt::white); 00212 emit fgChanged(fg.color()); 00213 emit bgChanged(bg.color()); 00214 miniCtlFlag = true; 00215 } 00216 repaint(false); 00217 } 00218 00219 00220 void KDualColorButton::mouseMoveEvent(QMouseEvent *ev) 00221 { 00222 if(!miniCtlFlag){ 00223 int delay = KGlobalSettings::dndEventDelay(); 00224 if(ev->x() >= mPos.x()+delay || ev->x() <= mPos.x()-delay || 00225 ev->y() >= mPos.y()+delay || ev->y() <= mPos.y()-delay) { 00226 KColorDrag *d = new KColorDrag( curColor == Foreground ? 00227 fg.color() : bg.color(), 00228 this); 00229 d->dragCopy(); 00230 dragFlag = true; 00231 } 00232 } 00233 } 00234 00235 void KDualColorButton::mouseReleaseEvent(QMouseEvent *ev) 00236 { 00237 if(!miniCtlFlag){ 00238 QRect fgRect, bgRect; 00239 00240 metrics(fgRect, bgRect); 00241 if(dragFlag) 00242 curColor = tmpColor; 00243 else if(fgRect.contains(ev->pos()) && curColor == Foreground){ 00244 if(tmpColor == Background){ 00245 curColor = Foreground; 00246 emit currentChanged(Foreground); 00247 } 00248 else{ 00249 QColor newColor = fg.color(); 00250 if(KColorDialog::getColor(newColor, d->dialogParent) != QDialog::Rejected){ 00251 fg.setColor(newColor); 00252 emit fgChanged(newColor); 00253 } 00254 } 00255 } 00256 else if(bgRect.contains(ev->pos()) && curColor == Background){ 00257 if(tmpColor == Foreground){ 00258 curColor = Background; 00259 emit currentChanged(Background); 00260 } 00261 else{ 00262 QColor newColor = bg.color(); 00263 if(KColorDialog::getColor(newColor, d->dialogParent) != QDialog::Rejected){ 00264 bg.setColor(newColor); 00265 emit bgChanged(newColor); 00266 } 00267 } 00268 } 00269 repaint(false); 00270 dragFlag = false; 00271 } 00272 else 00273 miniCtlFlag = false; 00274 } 00275 00276 void KDualColorButton::virtual_hook( int, void* ) 00277 { /*BASE::virtual_hook( id, data );*/ } 00278 00279 #include "kdualcolorbutton.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 12 15:08:15 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003