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 } 00065 00066 void 00067 WebButton::mouseReleaseEvent(QMouseEvent * e) 00068 { 00069 mouseDown_ = false; 00070 repaint(); 00071 00072 KDecorationFactory* f = deco_->factory(); 00073 if (rect().contains(e->pos())) 00074 { 00075 clickEvent(e->button()); 00076 } 00077 if( !f->exists( deco_ )) // decoration was destroyed 00078 return; 00079 QButton::mouseReleaseEvent(e); 00080 } 00081 00082 void 00083 WebButton::enterEvent(QEvent * e) 00084 { 00085 mouseOver_ = true; 00086 repaint(); 00087 QButton::enterEvent(e); 00088 } 00089 00090 void 00091 WebButton::leaveEvent(QEvent * e) 00092 { 00093 mouseOver_ = false; 00094 repaint(); 00095 QButton::leaveEvent(e); 00096 } 00097 00098 void 00099 WebButton::paintEvent(QPaintEvent *) 00100 { 00101 QPen highlightPen; 00102 00103 if (mouseDown_) 00104 highlightPen = QPen(colorGroup().light()); 00105 00106 else 00107 { 00108 if (mouseOver_) 00109 highlightPen = QPen(colorGroup().highlight()); 00110 else 00111 highlightPen = QPen(NoPen); 00112 } 00113 00114 QPainter p(this); 00115 00116 p.fillRect(rect(), colorGroup().background()); 00117 switch ( position_ ) 00118 { 00119 case Left: 00120 { 00121 // Draw edge. 00122 00123 p.setPen(Qt::black); 00124 00125 p.drawLine(0, 0, width(), 0); 00126 p.drawLine(0, 1, 0, height() - 1); 00127 if (shape_) 00128 { 00129 p.drawPoint(3, 1); 00130 p.drawPoint(4, 1); 00131 p.drawPoint(2, 2); 00132 p.drawPoint(1, 3); 00133 p.drawPoint(1, 4); 00134 } 00135 // Draw highlight. 00136 00137 p.setBrush(NoBrush); 00138 p.setPen(highlightPen); 00139 00140 if (shape_) 00141 p.setClipRegion(QRegion(rect()) - QRect(0, 0, 6, 6)); 00142 00143 p.drawRect(2, 2, width() - 4, height() - 4); 00144 if (shape_) 00145 { 00146 p.setClipRect(rect()); 00147 p.drawPoint(4, 3); 00148 p.drawPoint(5, 3); 00149 p.drawPoint(3, 4); 00150 p.drawPoint(3, 5); 00151 } 00152 } 00153 00154 break; 00155 00156 case Right: 00157 { 00158 // Draw edge. 00159 00160 p.setPen(Qt::black); 00161 p.drawLine(0, 0, width(), 0); 00162 p.drawLine(width() - 1, 1, width() - 1, height() - 1); 00163 if (shape_) 00164 { 00165 p.drawPoint(width() - 5, 1); 00166 p.drawPoint(width() - 4, 1); 00167 p.drawPoint(width() - 3, 2); 00168 p.drawPoint(width() - 2, 3); 00169 p.drawPoint(width() - 2, 4); 00170 } 00171 // Draw highlight. 00172 00173 p.setBrush(NoBrush); 00174 p.setPen(highlightPen); 00175 00176 if (shape_) 00177 p.setClipRegion(QRegion(rect()) - QRect(width() - 6, 0, 6, 6)); 00178 00179 p.drawRect(2, 2, width() - 4, height() - 4); 00180 if (shape_) 00181 { 00182 p.setClipRect(rect()); 00183 p.drawPoint(width() - 5, 3); 00184 p.drawPoint(width() - 6, 3); 00185 p.drawPoint(width() - 4, 4); 00186 p.drawPoint(width() - 4, 5); 00187 } 00188 } 00189 00190 break; 00191 00192 case Mid: 00193 default: 00194 { 00195 // Draw edge. 00196 00197 p.setPen(Qt::black); 00198 p.drawLine(0, 0, width(), 0); 00199 00200 // Draw highlight. 00201 00202 p.setBrush(NoBrush); 00203 p.setPen(highlightPen); 00204 00205 p.drawRect(2, 2, width() - 4, height() - 4); 00206 } 00207 00208 break; 00209 } 00210 00211 // Draw icon. 00212 00213 QPoint center(rect().center()); 00214 00215 int bwby2(bitmap_.width() / 2); // Bitmap Width BY 2 00216 int bhby2(bitmap_.height() / 2); // Bitmap Height BY 2 00217 00218 p.setBrush(NoBrush); 00219 p.setPen(Qt::black); 00220 00221 p.drawPixmap(center.x() - bwby2 + 1, center.y() - bhby2 + 1, bitmap_); 00222 } 00223 00224 QSize 00225 WebButton::sizeHint() const 00226 { 00227 return QSize(16, 16); 00228 } 00229 00230 QSize 00231 WebButton::minimumSizeHint() const 00232 { 00233 return QSize(14, 14); 00234 } 00235 00236 void 00237 WebButton::setBitmap(const QBitmap & b) 00238 { 00239 bitmap_ = b; 00240 repaint(); 00241 } 00242 00243 void 00244 WebButton::setPosition(Position p) 00245 { 00246 if ( QApplication::reverseLayout() ) 00247 { 00248 if ( p == Left ) 00249 position_ = Right; 00250 else if ( p == Right ) 00251 position_ = Left; 00252 } 00253 else 00254 position_ = p; 00255 repaint(); 00256 } 00257 00258 void 00259 WebButton::resizeEvent(QResizeEvent *) 00260 { 00261 repaint(); 00262 } 00263 00264 void 00265 WebButton::setTipText(const QString &tip) { 00266 if(KDecoration::options()->showTooltips()) { 00267 QToolTip::remove(this ); 00268 QToolTip::add(this, tip ); 00269 } 00270 } 00271 00272 } 00273 00274 #include "WebButton.moc" 00275 // vim:ts=2:sw=2:tw=78:set et:
KDE Logo
This file is part of the documentation for kwin Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Dec 16 19:08:42 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003