kwin Library API Documentation

WebButton.cpp

00001 /*
00002   'Web' kwin client
00003 
00004   Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>
00005 
00006   This program is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010 
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015 
00016   You should have received a copy of the GNU General Public License
00017   along with this program; see the file COPYING.  If not, write to
00018   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019   Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qcursor.h>
00023 #include <qpainter.h>
00024 #include <qtooltip.h>
00025 #include <qapplication.h>
00026 
00027 #include "WebButton.h"
00028 #include "Web.h"
00029 
00030 namespace Web {
00031 
00032 WebButton::WebButton(QWidget * parent, const QString& tip, WebClient* deco)
00033   : QButton (parent, 0, 0),
00034     mouseOver_  (false),
00035     mouseDown_  (false),
00036     position_   (Mid),
00037     shape_      (false),
00038     deco_       (deco)
00039 {
00040   setTipText(tip);
00041   setCursor(ArrowCursor);
00042   setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
00043   setBackgroundMode(NoBackground);
00044 }
00045 
00046 WebButton::~WebButton()
00047 {
00048   // Empty.
00049 }
00050 
00051   void
00052 WebButton::setShape(bool b)
00053 {
00054   shape_ = b;
00055   repaint();
00056 }
00057 
00058   void
00059 WebButton::mousePressEvent(QMouseEvent * e)
00060 {
00061   mouseDown_ = true;
00062   repaint();
00063   QButton::mousePressEvent(e);
00064   e->accept();
00065 }
00066 
00067   void
00068 WebButton::mouseReleaseEvent(QMouseEvent * e)
00069 {
00070   mouseDown_ = false;
00071   repaint();
00072 
00073   KDecorationFactory* f = deco_->factory();
00074   if (rect().contains(e->pos()))
00075   {
00076     clickEvent(e->button());
00077   }
00078   if( !f->exists( deco_ )) // decoration was destroyed
00079     return;
00080   QButton::mouseReleaseEvent(e);
00081   e->accept();
00082 }
00083 
00084   void
00085 WebButton::enterEvent(QEvent * e)
00086 {
00087   mouseOver_ = true;
00088   repaint();
00089   QButton::enterEvent(e);
00090 }
00091 
00092   void
00093 WebButton::leaveEvent(QEvent * e)
00094 {
00095   mouseOver_ = false;
00096   repaint();
00097   QButton::leaveEvent(e);
00098 }
00099 
00100   void
00101 WebButton::paintEvent(QPaintEvent *)
00102 {
00103   QPen highlightPen;
00104 
00105   if (mouseDown_)
00106     highlightPen = QPen(colorGroup().light());
00107 
00108   else
00109   {
00110     if (mouseOver_)
00111       highlightPen = QPen(colorGroup().highlight());
00112     else
00113       highlightPen = QPen(NoPen);
00114   }
00115 
00116   QPainter p(this);
00117 
00118   p.fillRect(rect(), colorGroup().background());
00119   switch ( position_ )
00120   {
00121     case Left:
00122       {
00123         // Draw edge.
00124 
00125         p.setPen(Qt::black);
00126 
00127         p.drawLine(0, 0, width(), 0);
00128         p.drawLine(0, 1, 0, height() - 1);
00129         if (shape_)
00130         {
00131           p.drawPoint(3, 1);
00132           p.drawPoint(4, 1);
00133           p.drawPoint(2, 2);
00134           p.drawPoint(1, 3);
00135           p.drawPoint(1, 4);
00136         }
00137         // Draw highlight.
00138 
00139         p.setBrush(NoBrush);
00140         p.setPen(highlightPen);
00141 
00142         if (shape_)
00143           p.setClipRegion(QRegion(rect()) - QRect(0, 0, 6, 6));
00144 
00145         p.drawRect(2, 2, width() - 4, height() - 4);
00146         if (shape_)
00147         {
00148           p.setClipRect(rect());
00149           p.drawPoint(4, 3);
00150           p.drawPoint(5, 3);
00151           p.drawPoint(3, 4);
00152           p.drawPoint(3, 5);
00153         }
00154       }
00155 
00156       break;
00157 
00158     case Right:
00159       {
00160         // Draw edge.
00161 
00162         p.setPen(Qt::black);
00163         p.drawLine(0, 0, width(), 0);
00164         p.drawLine(width() - 1, 1, width() - 1, height() - 1);
00165         if (shape_)
00166         {
00167           p.drawPoint(width() - 5, 1);
00168           p.drawPoint(width() - 4, 1);
00169           p.drawPoint(width() - 3, 2);
00170           p.drawPoint(width() - 2, 3);
00171           p.drawPoint(width() - 2, 4);
00172         }
00173         // Draw highlight.
00174 
00175         p.setBrush(NoBrush);
00176         p.setPen(highlightPen);
00177 
00178         if (shape_)
00179           p.setClipRegion(QRegion(rect()) - QRect(width() - 6, 0, 6, 6));
00180 
00181         p.drawRect(2, 2, width() - 4, height() - 4);
00182         if (shape_)
00183         {
00184           p.setClipRect(rect());
00185           p.drawPoint(width() - 5, 3);
00186           p.drawPoint(width() - 6, 3);
00187           p.drawPoint(width() - 4, 4);
00188           p.drawPoint(width() - 4, 5);
00189         }
00190       }
00191 
00192       break;
00193 
00194     case Mid:
00195     default:
00196       {
00197         // Draw edge.
00198 
00199         p.setPen(Qt::black);
00200         p.drawLine(0, 0, width(), 0);
00201 
00202         // Draw highlight.
00203 
00204         p.setBrush(NoBrush);
00205         p.setPen(highlightPen);
00206 
00207         p.drawRect(2, 2, width() - 4, height() - 4);
00208       }
00209 
00210       break;
00211   }
00212 
00213   // Draw icon.
00214 
00215   QPoint center(rect().center());
00216 
00217   int bwby2(bitmap_.width() / 2);    // Bitmap Width BY 2
00218   int bhby2(bitmap_.height() / 2);   // Bitmap Height BY 2
00219 
00220   p.setBrush(NoBrush);
00221   p.setPen(Qt::black);
00222 
00223   p.drawPixmap(center.x() - bwby2 + 1, center.y() - bhby2 + 1, bitmap_);
00224 }
00225 
00226   QSize
00227 WebButton::sizeHint() const
00228 {
00229   return QSize(16, 16);
00230 }
00231 
00232   QSize
00233 WebButton::minimumSizeHint() const
00234 {
00235   return QSize(14, 14);
00236 }
00237 
00238   void
00239 WebButton::setBitmap(const QBitmap & b)
00240 {
00241   bitmap_ = b;
00242   repaint();
00243 }
00244 
00245   void
00246 WebButton::setPosition(Position p)
00247 {
00248   if (  QApplication::reverseLayout() )
00249   {
00250       if ( p == Left )
00251           position_ = Right;
00252       else if ( p == Right )
00253           position_ = Left;
00254   }
00255   else
00256       position_ = p;
00257   repaint();
00258 }
00259 
00260   void
00261 WebButton::resizeEvent(QResizeEvent *)
00262 {
00263   repaint();
00264 }
00265 
00266 void
00267 WebButton::setTipText(const QString &tip) {
00268   if(KDecoration::options()->showTooltips()) {
00269     QToolTip::remove(this );
00270     QToolTip::add(this, tip );
00271   }
00272 }
00273 
00274 }
00275 
00276 #include "WebButton.moc"
00277 // vim:ts=2:sw=2:tw=78:set et:
KDE Logo
This file is part of the documentation for kwin Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jun 14 01:19:59 2006 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003