karrowbutton.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 #include "karrowbutton.h"
00020 
00021 #include <qstyle.h>
00022 #include <qpainter.h>
00023 
00024 class KArrowButtonPrivate
00025 {
00026     public:
00027         Qt::ArrowType arrow;
00028 };
00029 
00030 KArrowButton::KArrowButton(QWidget *parent, Qt::ArrowType arrow,
00031         const char *name)
00032     : QPushButton(parent, name)
00033 {
00034     d = new KArrowButtonPrivate();
00035     d->arrow = arrow;
00036 }
00037 
00038 KArrowButton::~KArrowButton()
00039 {
00040     delete d;
00041 }
00042 
00043 QSize KArrowButton::sizeHint() const
00044 {
00045     return QSize( 12, 12 );
00046 }
00047 
00048 void KArrowButton::setArrowType(Qt::ArrowType a)
00049 {
00050     if (d->arrow != a) {
00051         d->arrow = a;
00052         repaint();
00053     }
00054 }
00055 
00056 void KArrowButton::drawButton(QPainter *p)
00057 {
00058     const unsigned int arrowSize = 8;
00059     const unsigned int margin = 2;
00060     
00061         p->fillRect( rect(), colorGroup().brush( QColorGroup::Background ) );
00062     style().drawPrimitive( QStyle::PE_Panel, p, QRect( 0, 0, width(), height() ),
00063                    colorGroup(), 
00064                    isDown() ? QStyle::Style_Sunken : QStyle::Style_Default,
00065                    QStyleOption( 2, 0 ) );
00066 
00067     if (static_cast<unsigned int>(width()) < arrowSize + margin ||
00068         static_cast<unsigned int>(height()) < arrowSize + margin)
00069         return; 
00070 
00071     unsigned int x = 0, y = 0;
00072     if (d->arrow == DownArrow) {
00073         x = (width() - arrowSize) / 2;
00074         y = height() - (arrowSize + margin);
00075     } else if (d->arrow == UpArrow) {
00076         x = (width() - arrowSize) / 2;
00077         y = margin;
00078     } else if (d->arrow == RightArrow) {
00079         x = width() - (arrowSize + margin);
00080         y = (height() - arrowSize) / 2;
00081     } else { 
00082         x = margin;
00083         y = (height() - arrowSize) / 2;
00084     }
00085 
00086     if (isDown()) {
00087         x++;
00088         y++;
00089     }
00090 
00091     QStyle::PrimitiveElement e = QStyle::PE_ArrowLeft;
00092     switch (d->arrow)
00093     {
00094         case Qt::LeftArrow: e = QStyle::PE_ArrowLeft; break;
00095         case Qt::RightArrow: e = QStyle::PE_ArrowRight; break;
00096         case Qt::UpArrow: e = QStyle::PE_ArrowUp; break;
00097         case Qt::DownArrow: e = QStyle::PE_ArrowDown; break;
00098     }
00099     int flags = QStyle::Style_Enabled;
00100     if ( isDown() )
00101         flags |= QStyle::Style_Down;
00102     style().drawPrimitive( e, p, QRect( QPoint( x, y ), QSize( arrowSize, arrowSize ) ),
00103                    colorGroup(), flags );
00104 }
00105 
00106 void KArrowButton::virtual_hook( int, void* )
00107 {  }
00108 
00109 #include "karrowbutton.moc"
 
This file is part of the documentation for kdeui Library Version 3.2.0.