Back to index

kdeartwork  4.3.2
Button.cpp
Go to the documentation of this file.
00001 /*
00002   RISC OS KWin client
00003 
00004   Copyright 2000
00005     Rik Hemsley <rik@kde.org>
00006 
00007   This program is free software; you can redistribute it and/or
00008   modify it under the terms of the GNU General Public
00009   License as published by the Free Software Foundation; either
00010   version 2 of the License, or (at your option) any later version.
00011 
00012   This program is distributed in the hope that it will be useful,
00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   General Public License for more details.
00016 
00017   You should have received a copy of the GNU General Public License
00018   along with this program; see the file COPYING.  If not, write to
00019   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020   Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <qtooltip.h>
00024 //Added by qt3to4:
00025 #include <QMouseEvent>
00026 #include <QPixmap>
00027 #include <QBitmap>
00028 #include <QPaintEvent>
00029 #include "Button.h"
00030 #include "Static.h"
00031 
00032 namespace RiscOS
00033 {
00034 
00035 Button::Button(QWidget *parent, const QString& tip,
00036                const Qt::MouseButtons realizeButtons)
00037   : QWidget(parent, "Button", 0),
00038     realizeButtons_(realizeButtons),
00039     lastButton_(Qt::NoButton),
00040     alignment_(Left),
00041     down_     (false),
00042     active_   (false)
00043 {
00044    this->setToolTip( tip);
00045    setBackgroundColor(Qt::black);
00046 
00047    setFixedSize(Static::instance()->titleHeight() - 1,
00048                 Static::instance()->titleHeight());
00049 }
00050 
00051 Button::~Button()
00052 {
00053   // Empty.
00054 }
00055 
00056 void Button::setAlignment(Alignment a)
00057 {
00058    alignment_ = a;
00059    repaint();
00060 }
00061 
00062 void Button::setActive(bool b)
00063 {
00064    active_ = b;
00065    repaint();
00066 }
00067 
00068 Button::Alignment Button::alignment() const
00069 {
00070    return alignment_;
00071 }
00072 
00073 void Button::mousePressEvent(QMouseEvent *e)
00074 {
00075    down_ = true;
00076    lastButton_ = e->button();
00077    repaint();
00078 
00079    QMouseEvent me(e->type(), e->pos(), e->globalPos(),
00080                   (e->button()&realizeButtons_) ? Qt::LeftButton : Qt::NoButton,
00081                   e->state());
00082    QWidget::mousePressEvent(&me);
00083 }
00084 
00085 void Button::mouseReleaseEvent(QMouseEvent *e)
00086 {
00087    down_ = false;
00088    lastButton_ = e->button();
00089    repaint();
00090    QMouseEvent me(e->type(), e->pos(), e->globalPos(),
00091                   (e->button()&realizeButtons_) ? Qt::LeftButton : Qt::NoButton,
00092                   e->state());
00093    QWidget::mouseReleaseEvent(&me);
00094 }
00095 
00096 void Button::setPixmap(const QPixmap &p)
00097 {
00098    if (QPixmap::defaultDepth() <= 8)
00099       aPixmap_ = iPixmap_ = p;
00100    else
00101    {
00102       QRgb light;
00103       QRgb* data = NULL;
00104       QRgb w = qRgb(255, 255, 255);
00105 
00106       QImage aTx(p.convertToImage());
00107       QImage iTx(aTx.copy());
00108 
00109       const KDecorationOptions* options = KDecoration::options();
00110       light = options->color(KDecoration::ColorButtonBg, true).light(150).rgb();
00111 
00112       if (light == qRgb(0, 0, 0))
00113          light = qRgb(228, 228, 228);
00114 
00115       data = (QRgb *)aTx.bits();
00116 
00117       for (int x = 0; x < 144; x++)
00118          if (data[x] == w)
00119             data[x] = light;
00120 
00121       light = options->color(KDecoration::ColorButtonBg, false).light(150).rgb();
00122 
00123       if (light == qRgb(0, 0, 0))
00124          light = qRgb(228, 228, 228);
00125 
00126       data = (QRgb *)iTx.bits();
00127 
00128       for (int x = 0; x < 144; x++)
00129          if (data[x] == w)
00130             data[x] = light;
00131 
00132       aPixmap_.convertFromImage(aTx);
00133       iPixmap_.convertFromImage(iTx);
00134 
00135       if (!p.mask().isNull())
00136       {
00137          aPixmap_.setMask(p.mask());
00138          iPixmap_.setMask(p.mask());
00139       }
00140    }
00141    repaint();
00142 }
00143 
00144 void Button::paintEvent(QPaintEvent *)
00145 {
00146    bitBlt(this, alignment_ == Left ? 1 : 0, 0,
00147           &Static::instance()->buttonBase(active_, down_));
00148 
00149    int i = width() / 2 - 6;
00150 
00151    bitBlt(this, alignment_ == Left ? i + 1 : i,
00152           i + 1, active_ ? &aPixmap_ : &iPixmap_);
00153 }
00154 
00155 } // End namespace
00156 
00157 // vim:ts=2:sw=2:tw=78
00158 #include "Button.moc"