File indexing completed on 2024-04-28 11:36:24
0001 /* This file is part of the KDE libraries 0002 Copyright (C) 2001 Frerich Raabe <raabe@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License version 2 as published by the Free Software Foundation. 0007 0008 This library is distributed in the hope that it will be useful, 0009 but WITHOUT ANY WARRANTY; without even the implied warranty of 0010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0011 Library General Public License for more details. 0012 0013 You should have received a copy of the GNU Library General Public License 0014 along with this library; see the file COPYING.LIB. If not, write to 0015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0016 Boston, MA 02110-1301, USA. 0017 */ 0018 0019 #include "karrowbutton.h" 0020 0021 #include <QStyle> 0022 #include <QStyleOptionFrame> 0023 #include <QPainter> 0024 0025 class KArrowButtonPrivate 0026 { 0027 public: 0028 Qt::ArrowType arrow; 0029 }; 0030 0031 KArrowButton::KArrowButton(QWidget *parent, Qt::ArrowType arrow) 0032 : QPushButton(parent), d(new KArrowButtonPrivate()) 0033 { 0034 d->arrow = arrow; 0035 } 0036 0037 KArrowButton::~KArrowButton() 0038 { 0039 delete d; 0040 } 0041 0042 QSize KArrowButton::sizeHint() const 0043 { 0044 return QSize(12, 12); 0045 } 0046 0047 void KArrowButton::setArrowType(Qt::ArrowType a) 0048 { 0049 if (d->arrow != a) { 0050 d->arrow = a; 0051 repaint(); 0052 } 0053 } 0054 Qt::ArrowType KArrowButton::arrowType() const 0055 { 0056 return d->arrow; 0057 } 0058 0059 void KArrowButton::paintEvent(QPaintEvent *) 0060 { 0061 QPainter p(this); 0062 const unsigned int arrowSize = 8; 0063 const unsigned int margin = 2; 0064 0065 QStyleOptionFrame opt; 0066 opt.init(this); 0067 opt.lineWidth = 2; 0068 opt.midLineWidth = 0; 0069 0070 p.fillRect(rect(), palette().brush(QPalette::Window)); 0071 0072 style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this); 0073 0074 if (d->arrow == Qt::NoArrow) { 0075 return; 0076 } 0077 0078 if (static_cast<unsigned int>(width()) < arrowSize + margin || 0079 static_cast<unsigned int>(height()) < arrowSize + margin) { 0080 return; // don't draw arrows if we are too small 0081 } 0082 0083 unsigned int x = 0, y = 0; 0084 if (d->arrow == Qt::DownArrow) { 0085 x = (width() - arrowSize) / 2; 0086 y = height() - (arrowSize + margin); 0087 } else if (d->arrow == Qt::UpArrow) { 0088 x = (width() - arrowSize) / 2; 0089 y = margin; 0090 } else if (d->arrow == Qt::RightArrow) { 0091 x = width() - (arrowSize + margin); 0092 y = (height() - arrowSize) / 2; 0093 } else { // arrowType == LeftArrow 0094 x = margin; 0095 y = (height() - arrowSize) / 2; 0096 } 0097 0098 if (isDown()) { 0099 x++; 0100 y++; 0101 } 0102 0103 QStyle::PrimitiveElement e = QStyle::PE_IndicatorArrowLeft; 0104 switch (d->arrow) { 0105 case Qt::LeftArrow: e = QStyle::PE_IndicatorArrowLeft; break; 0106 case Qt::RightArrow: e = QStyle::PE_IndicatorArrowRight; break; 0107 case Qt::UpArrow: e = QStyle::PE_IndicatorArrowUp; break; 0108 case Qt::DownArrow: e = QStyle::PE_IndicatorArrowDown; break; 0109 case Qt::NoArrow: Q_ASSERT(0); break; 0110 } 0111 0112 opt.state |= QStyle::State_Enabled; 0113 opt.rect = QRect(x, y, arrowSize, arrowSize); 0114 0115 style()->drawPrimitive(e, &opt, &p, this); 0116 } 0117 0118 #include "moc_karrowbutton.cpp"