KDEUI
karrowbutton.cpp
Go to the documentation of this file.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>
00022 #include <QStyleOptionFrame>
00023 #include <QPainter>
00024
00025 #include <assert.h>
00026
00027 class KArrowButtonPrivate
00028 {
00029 public:
00030 Qt::ArrowType arrow;
00031 };
00032
00033 KArrowButton::KArrowButton(QWidget *parent, Qt::ArrowType arrow)
00034 : QPushButton(parent),d(new KArrowButtonPrivate())
00035 {
00036 d->arrow = arrow;
00037 }
00038
00039 KArrowButton::~KArrowButton()
00040 {
00041 delete d;
00042 }
00043
00044 QSize KArrowButton::sizeHint() const
00045 {
00046 return QSize( 12, 12 );
00047 }
00048
00049 void KArrowButton::setArrowType(Qt::ArrowType a)
00050 {
00051 if (d->arrow != a) {
00052 d->arrow = a;
00053 repaint();
00054 }
00055 }
00056 Qt::ArrowType KArrowButton::arrowType() const
00057 {
00058 return d->arrow;
00059 }
00060
00061 void KArrowButton::paintEvent(QPaintEvent*)
00062 {
00063 QPainter p(this);
00064 const unsigned int arrowSize = 8;
00065 const unsigned int margin = 2;
00066
00067 QStyleOptionFrame opt;
00068 opt.init(this);
00069 opt.lineWidth = 2;
00070 opt.midLineWidth = 0;
00071
00072 p.fillRect( rect(), palette().brush( QPalette::Background ) );
00073
00074 style()->drawPrimitive( QStyle::PE_Frame, &opt, &p, this);
00075
00076 if (d->arrow == Qt::NoArrow)
00077 return;
00078
00079 if (static_cast<unsigned int>(width()) < arrowSize + margin ||
00080 static_cast<unsigned int>(height()) < arrowSize + margin)
00081 return;
00082
00083 unsigned int x = 0, y = 0;
00084 if (d->arrow == Qt::DownArrow) {
00085 x = (width() - arrowSize) / 2;
00086 y = height() - (arrowSize + margin);
00087 } else if (d->arrow == Qt::UpArrow) {
00088 x = (width() - arrowSize) / 2;
00089 y = margin;
00090 } else if (d->arrow == Qt::RightArrow) {
00091 x = width() - (arrowSize + margin);
00092 y = (height() - arrowSize) / 2;
00093 } else {
00094 x = margin;
00095 y = (height() - arrowSize) / 2;
00096 }
00097
00098 if (isDown()) {
00099 x++;
00100 y++;
00101 }
00102
00103 QStyle::PrimitiveElement e = QStyle::PE_IndicatorArrowLeft;
00104 switch (d->arrow)
00105 {
00106 case Qt::LeftArrow: e = QStyle::PE_IndicatorArrowLeft; break;
00107 case Qt::RightArrow: e = QStyle::PE_IndicatorArrowRight; break;
00108 case Qt::UpArrow: e = QStyle::PE_IndicatorArrowUp; break;
00109 case Qt::DownArrow: e = QStyle::PE_IndicatorArrowDown; break;
00110 case Qt::NoArrow: assert( 0 ); break;
00111 }
00112
00113 opt.state |= QStyle::State_Enabled;
00114 opt.rect = QRect( x, y, arrowSize, arrowSize);
00115
00116 style()->drawPrimitive( e, &opt, &p, this );
00117 }
00118
00119 #include "karrowbutton.moc"