KDELibs4Support

karrowbutton.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001 Frerich Raabe <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "karrowbutton.h"
20 
21 #include <QStyle>
22 #include <QStyleOptionFrame>
23 #include <QPainter>
24 
25 class KArrowButtonPrivate
26 {
27 public:
28  Qt::ArrowType arrow;
29 };
30 
32  : QPushButton(parent), d(new KArrowButtonPrivate())
33 {
34  d->arrow = arrow;
35 }
36 
38 {
39  delete d;
40 }
41 
43 {
44  return QSize(12, 12);
45 }
46 
48 {
49  if (d->arrow != a) {
50  d->arrow = a;
51  repaint();
52  }
53 }
55 {
56  return d->arrow;
57 }
58 
60 {
61  QPainter p(this);
62  const unsigned int arrowSize = 8;
63  const unsigned int margin = 2;
64 
66  opt.init(this);
67  opt.lineWidth = 2;
68  opt.midLineWidth = 0;
69 
70  p.fillRect(rect(), palette().brush(QPalette::Window));
71 
72  style()->drawPrimitive(QStyle::PE_Frame, &opt, &p, this);
73 
74  if (d->arrow == Qt::NoArrow) {
75  return;
76  }
77 
78  if (static_cast<unsigned int>(width()) < arrowSize + margin ||
79  static_cast<unsigned int>(height()) < arrowSize + margin) {
80  return; // don't draw arrows if we are too small
81  }
82 
83  unsigned int x = 0, y = 0;
84  if (d->arrow == Qt::DownArrow) {
85  x = (width() - arrowSize) / 2;
86  y = height() - (arrowSize + margin);
87  } else if (d->arrow == Qt::UpArrow) {
88  x = (width() - arrowSize) / 2;
89  y = margin;
90  } else if (d->arrow == Qt::RightArrow) {
91  x = width() - (arrowSize + margin);
92  y = (height() - arrowSize) / 2;
93  } else { // arrowType == LeftArrow
94  x = margin;
95  y = (height() - arrowSize) / 2;
96  }
97 
98  if (isDown()) {
99  x++;
100  y++;
101  }
102 
104  switch (d->arrow) {
107  case Qt::UpArrow: e = QStyle::PE_IndicatorArrowUp; break;
109  case Qt::NoArrow: Q_ASSERT(0); break;
110  }
111 
112  opt.state |= QStyle::State_Enabled;
113  opt.rect = QRect(x, y, arrowSize, arrowSize);
114 
115  style()->drawPrimitive(e, &opt, &p, this);
116 }
117 
bool isDown() const const
ArrowType
const QPalette & palette() const const
void fillRect(const QRectF &rectangle, const QBrush &brush)
QStyle * style() const const
QSize sizeHint() const override
Reimplemented from QPushButton.
int y() const const
void setArrowType(Qt::ArrowType a)
Defines in what direction the arrow is pointing to.
KArrowButton(QWidget *parent=nullptr, Qt::ArrowType arrow=Qt::UpArrow)
Constructs an arrow button.
int width() const const
virtual ~KArrowButton()
Destructor.
void paintEvent(QPaintEvent *) override
Reimplemented from QPushButton.
int x() const const
QRect rect() const const
void repaint()
void init(const QWidget *widget)
virtual void drawPrimitive(QStyle::PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const const =0
Qt::ArrowType arrowType() const
Returns the arrow type.
int height() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:56:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.