kdualcolorbutton.cpp Source File (kdeui Library)

| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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     }
    00181 }
    00182
    00183 void KDualColorButton::mousePressEvent(QMouseEvent *ev)
    00184 {
    00185     QRect fgRect, bgRect;
    00186     metrics(fgRect, bgRect);
    00187     mPos = ev->pos();
    00188     tmpColor = curColor;
    00189     dragFlag = false;
    00190     if(fgRect.contains(mPos)){
    00191         curColor = Foreground;
    00192         miniCtlFlag = false;
    00193     }
    00194     else if(bgRect.contains(mPos)){
    00195         curColor = Background;
    00196         miniCtlFlag = false;
    00197    }
    00198     else if(ev->pos().x() > fgRect.width()){
    00199         // We handle the swap and reset controls as soon as the mouse is
    00200         // is pressed and ignore further events on this click (mosfet).
    00201         QBrush c = fg;
    00202         fg = bg;
    00203         bg = c;
    00204         emit fgChanged(fg.color());
    00205         emit bgChanged(bg.color());
    00206         miniCtlFlag = true;
    00207     }
    00208     else if(ev->pos().x() < bgRect.x()){
    00209         fg.setColor(Qt::black);
    00210         bg.setColor(Qt::white);
    00211         emit fgChanged(fg.color());
    00212         emit bgChanged(bg.color());
    00213         miniCtlFlag = true;
    00214     }
    00215     repaint(false);
    00216 }
    00217
    00218
    00219 void KDualColorButton::mouseMoveEvent(QMouseEvent *ev)
    00220 {
    00221     if(!miniCtlFlag){
    00222         int delay = KGlobalSettings::dndEventDelay();
    00223         if(ev->x() >= mPos.x()+delay || ev->x() <= mPos.x()-delay ||
    00224            ev->y() >= mPos.y()+delay || ev->y() <= mPos.y()-delay) {
    00225             KColorDrag *d = KColorDrag::makeDrag( curColor == Foreground ?
    00226                                                   fg.color() : bg.color(),
    00227                                                   this);
    00228             d->dragCopy();
    00229             dragFlag = true;
    00230         }
    00231     }
    00232 }
    00233
    00234 void KDualColorButton::mouseReleaseEvent(QMouseEvent *ev)
    00235 {
    00236     if(!miniCtlFlag){
    00237         QRect fgRect, bgRect;
    00238
    00239         metrics(fgRect, bgRect);
    00240         if(dragFlag)
    00241             curColor = tmpColor;
    00242         else if(fgRect.contains(ev->pos()) && curColor == Foreground){
    00243             if(tmpColor == Background){
    00244                 curColor = Foreground;
    00245                 emit currentChanged(Foreground);
    00246             }
    00247             else{
    00248                 QColor newColor = fg.color();
    00249                 if(KColorDialog::getColor(newColor, d->dialogParent) != QDialog::Rejected){
    00250                     fg.setColor(newColor);
    00251                     emit fgChanged(newColor);
    00252                 }
    00253             }
    00254         }
    00255         else if(bgRect.contains(ev->pos()) && curColor == Background){
    00256             if(tmpColor == Foreground){
    00257                 curColor = Background;
    00258                 emit currentChanged(Background);
    00259             }
    00260             else{
    00261                 QColor newColor = bg.color();
    00262                 if(KColorDialog::getColor(newColor, d->dialogParent) != QDialog::Rejected){
    00263                     bg.setColor(newColor);
    00264                     emit bgChanged(newColor);
    00265                 }
    00266             }
    00267         }
    00268         repaint(false);
    00269         dragFlag = false;
    00270     }
    00271     else
    00272         miniCtlFlag = false;
    00273 }
    00274
    00275 void KDualColorButton::virtual_hook( int, void* )
    00276 { /*BASE::virtual_hook( id, data );*/ }
    00277
    00278 #include "kdualcolorbutton.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:56:39 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001