KDEUI
karrowbutton.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001 Frerich Raabe <raabe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 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; // don't draw arrows if we are too small 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 { // arrowType == LeftArrow 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"
KDE 4.6 API Reference