00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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_ ))
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
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
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
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
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
00198
00199 p.setPen(Qt::black);
00200 p.drawLine(0, 0, width(), 0);
00201
00202
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
00214
00215 QPoint center(rect().center());
00216
00217 int bwby2(bitmap_.width() / 2);
00218 int bhby2(bitmap_.height() / 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