• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • krita API Reference
  • KDE Home
  • Contact Us
 

KritaWidgets

  • sources
  • kfour-appscomplete
  • krita
  • libs
  • widgets
KoToolBoxScrollArea_p.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Alvin Wong <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #ifndef KO_TOOLBOX_SCROLL_AREA_H
8 #define KO_TOOLBOX_SCROLL_AREA_H
9 
10 #include "KoToolBox_p.h"
11 #include "KoToolBoxLayout_p.h"
12 
13 #include <QScrollArea>
14 #include <QScrollBar>
15 #include <QScroller>
16 #include <QStyleOption>
17 #include <QToolButton>
18 #include <KisKineticScroller.h>
19 
20 class KoToolBoxScrollArea : public QScrollArea
21 {
22  Q_OBJECT
23 public:
24  KoToolBoxScrollArea(KoToolBox *toolBox, QWidget *parent)
25  : QScrollArea(parent)
26  , m_toolBox(toolBox)
27  , m_orientation(Qt::Vertical)
28  , m_scrollPrev(new QToolButton(this))
29  , m_scrollNext(new QToolButton(this))
30  {
31  setFrameShape(QFrame::NoFrame);
32  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
33  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
34 
35  m_toolBox->setOrientation(m_orientation);
36  setWidget(m_toolBox);
37 
38  m_scrollPrev->setAutoRepeat(true);
39  m_scrollPrev->setAutoFillBackground(true);
40  m_scrollPrev->setFocusPolicy(Qt::NoFocus);
41  connect(m_scrollPrev, &QToolButton::clicked, this, &KoToolBoxScrollArea::doScrollPrev);
42  m_scrollNext->setAutoRepeat(true);
43  m_scrollNext->setAutoFillBackground(true);
44  m_scrollNext->setFocusPolicy(Qt::NoFocus);
45  connect(m_scrollNext, &QToolButton::clicked, this, &KoToolBoxScrollArea::doScrollNext);
46 
47  QScroller *scroller = KisKineticScroller::createPreconfiguredScroller(this);
48  if (!scroller) {
49  QScroller::grabGesture(viewport(), QScroller::MiddleMouseButtonGesture);
50  QScroller *scroller = QScroller::scroller(viewport());
51  QScrollerProperties sp = scroller->scrollerProperties();
52 
53  sp.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.0);
54  sp.setScrollMetric(QScrollerProperties::OvershootDragResistanceFactor, 0.1);
55  sp.setScrollMetric(QScrollerProperties::OvershootDragDistanceFactor, 0.1);
56  sp.setScrollMetric(QScrollerProperties::OvershootScrollDistanceFactor, 0.0);
57  sp.setScrollMetric(QScrollerProperties::OvershootScrollTime, 0.4);
58 
59  scroller->setScrollerProperties(sp);
60  }
61  connect(scroller, SIGNAL(stateChanged(QScroller::State)), this, SLOT(slotScrollerStateChange(QScroller::State)));
62  }
63 
64  void setOrientation(Qt::Orientation orientation)
65  {
66  if (orientation == m_orientation) {
67  return;
68  }
69  m_orientation = orientation;
70  m_toolBox->setOrientation(orientation);
71  layoutItems();
72  }
73 
74  Qt::Orientation orientation() const
75  {
76  return m_orientation;
77  }
78 
79  QSize minimumSizeHint() const override
80  {
81  return m_toolBox->minimumSizeHint();
82  }
83 
84  QSize sizeHint() const override
85  {
86  return m_toolBox->sizeHint();
87  }
88 
89 public Q_SLOTS:
90  void slotScrollerStateChange(QScroller::State state){ KisKineticScroller::updateCursor(this, state); }
91 
92 protected:
93  bool event(QEvent *event) override
94  {
95  if (event->type() == QEvent::LayoutRequest) {
96  // LayoutRequest can be triggered by icon changes, so resize the toolbox
97  layoutItems();
98  // The toolbox might have changed the sizeHint and minimumSizeHint
99  updateGeometry();
100  }
101  return QScrollArea::event(event);
102  }
103 
104  void resizeEvent(QResizeEvent *event) override
105  {
106  layoutItems();
107  QScrollArea::resizeEvent(event);
108  updateScrollButtons();
109  }
110 
111  void wheelEvent(QWheelEvent *event) override
112  {
113  if (m_orientation == Qt::Vertical) {
114  QApplication::sendEvent(verticalScrollBar(), event);
115  } else {
116  QApplication::sendEvent(horizontalScrollBar(), event);
117  }
118  }
119 
120  void scrollContentsBy(int dx, int dy) override
121  {
122  QScrollArea::scrollContentsBy(dx, dy);
123  updateScrollButtons();
124  }
125 
126 private Q_SLOTS:
127  void doScrollPrev()
128  {
129  if (m_orientation == Qt::Vertical) {
130  verticalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepSub);
131  } else {
132  horizontalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepSub);
133  }
134  }
135 
136  void doScrollNext()
137  {
138  if (m_orientation == Qt::Vertical) {
139  verticalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
140  } else {
141  horizontalScrollBar()->triggerAction(QAbstractSlider::SliderSingleStepAdd);
142  }
143  }
144 
145 private:
146  int scrollButtonWidth() const
147  {
148  QStyleOption opt;
149  opt.init(this);
150  return style()->pixelMetric(QStyle::PM_TabBarScrollButtonWidth, &opt, this);
151  }
152 
153  void layoutItems()
154  {
155  const KoToolBoxLayout *l = m_toolBox->toolBoxLayout();
156  QSize newSize = viewport()->size();
157  if (m_orientation == Qt::Vertical) {
158  newSize.setHeight(l->heightForWidth(newSize.width()));
159  } else {
160  newSize.setWidth(l->widthForHeight(newSize.height()));
161  }
162  m_toolBox->resize(newSize);
163 
164  updateScrollButtons();
165  }
166 
167  void updateScrollButtons()
168  {
169  // We move the scroll buttons outside the widget rect instead of setting
170  // the visibility, because setting the visibility triggers a relayout
171  // of QAbstractScrollArea, which occasionally causes an offset bug when
172  // QScroller performs the overshoot animation. (Overshoot is done by
173  // moving the viewport widget, but the viewport position is reset during
174  // a relayout.)
175  const int scrollButtonWidth = this->scrollButtonWidth();
176  if (m_orientation == Qt::Vertical) {
177  m_scrollPrev->setArrowType(Qt::UpArrow);
178  m_scrollPrev->setEnabled(verticalScrollBar()->value() != verticalScrollBar()->minimum());
179  if (m_scrollPrev->isEnabled()) {
180  m_scrollPrev->setGeometry(0, 0, width(), scrollButtonWidth);
181  } else {
182  m_scrollPrev->setGeometry(-width(), 0, width(), scrollButtonWidth);
183  }
184  m_scrollNext->setArrowType(Qt::DownArrow);
185  m_scrollNext->setEnabled(verticalScrollBar()->value() != verticalScrollBar()->maximum());
186  if (m_scrollNext->isEnabled()) {
187  m_scrollNext->setGeometry(0, height() - scrollButtonWidth, width(), scrollButtonWidth);
188  } else {
189  m_scrollNext->setGeometry(-width(), height() - scrollButtonWidth, width(), scrollButtonWidth);
190  }
191  } else {
192  m_scrollPrev->setArrowType(Qt::LeftArrow);
193  m_scrollPrev->setEnabled(horizontalScrollBar()->value() != horizontalScrollBar()->minimum());
194  if (m_scrollPrev->isEnabled()) {
195  m_scrollPrev->setGeometry(0, 0, scrollButtonWidth, height());
196  } else {
197  m_scrollPrev->setGeometry(0, -height(), scrollButtonWidth, height());
198  }
199  m_scrollNext->setArrowType(Qt::RightArrow);
200  m_scrollNext->setEnabled(horizontalScrollBar()->value() != horizontalScrollBar()->maximum());
201  if (m_scrollNext->isEnabled()) {
202  m_scrollNext->setGeometry(width() - scrollButtonWidth, 0, scrollButtonWidth, height());
203  } else {
204  m_scrollNext->setGeometry(width() - scrollButtonWidth, -height(), scrollButtonWidth, height());
205  }
206  }
207  }
208 
209  KoToolBox *m_toolBox;
210  Qt::Orientation m_orientation;
211 
212  QToolButton *m_scrollPrev;
213  QToolButton *m_scrollNext;
214 };
215 
216 #endif // KO_TOOLBOX_SCROLL_AREA_H
QWheelEvent
QAbstractScrollArea::verticalScrollBar
QScrollBar * verticalScrollBar() const
KoToolBoxScrollArea::scrollContentsBy
void scrollContentsBy(int dx, int dy) override
Definition: KoToolBoxScrollArea_p.h:120
KoToolBoxScrollArea::minimumSizeHint
QSize minimumSizeHint() const override
Definition: KoToolBoxScrollArea_p.h:79
QWidget::setFocusPolicy
void setFocusPolicy(Qt::FocusPolicy policy)
KoToolBoxScrollArea::KoToolBoxScrollArea
KoToolBoxScrollArea(KoToolBox *toolBox, QWidget *parent)
Definition: KoToolBoxScrollArea_p.h:24
QAbstractButton::clicked
void clicked(bool checked)
QWidget
KoToolBox
KoToolBox is a dock widget that can order tools according to type and priority.
Definition: KoToolBox_p.h:37
KoToolBoxLayout
Definition: KoToolBoxLayout_p.h:232
QToolButton::setArrowType
void setArrowType(Qt::ArrowType type)
KoToolBox_p.h
QSize
KoToolBoxScrollArea::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: KoToolBoxScrollArea_p.h:104
QScrollArea::scrollContentsBy
virtual void scrollContentsBy(int dx, int dy)
QSize::width
int width() const
KoToolBoxScrollArea::sizeHint
QSize sizeHint() const override
Definition: KoToolBoxScrollArea_p.h:84
QFrame::setFrameShape
void setFrameShape(Shape)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KoToolBox::setOrientation
void setOrientation(Qt::Orientation orientation)
Set the orientation of the layout to orientation.
Definition: KoToolBox.cpp:244
KoToolBoxLayout::widthForHeight
int widthForHeight(int height) const
For calculating the width from height by KoToolBoxScrollArea.
Definition: KoToolBoxLayout_p.h:334
KoToolBoxScrollArea::orientation
Qt::Orientation orientation() const
Definition: KoToolBoxScrollArea_p.h:74
QSize::setWidth
void setWidth(int width)
QStyle::pixelMetric
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const=0
QAbstractScrollArea::setHorizontalScrollBarPolicy
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
QAbstractSlider::triggerAction
void triggerAction(SliderAction action)
QWidget::style
QStyle * style() const
QSize::height
int height() const
KoToolBox::toolBoxLayout
KoToolBoxLayout * toolBoxLayout() const
Definition: KoToolBox.cpp:325
QCoreApplication::sendEvent
bool sendEvent(QObject *receiver, QEvent *event)
QToolButton
KoToolBoxScrollArea::setOrientation
void setOrientation(Qt::Orientation orientation)
Definition: KoToolBoxScrollArea_p.h:64
QScrollArea
QScrollArea::resizeEvent
virtual void resizeEvent(QResizeEvent *)
KoToolBoxScrollArea::event
bool event(QEvent *event) override
Definition: KoToolBoxScrollArea_p.h:93
QStyleOption
QAbstractScrollArea::horizontalScrollBar
QScrollBar * horizontalScrollBar() const
QWidget::setEnabled
void setEnabled(bool)
QWidget::minimumSizeHint
minimumSizeHint
KoToolBoxLayout_p.h
QWidget::updateGeometry
void updateGeometry()
QWidget::height
int height() const
QEvent
QResizeEvent
KoToolBoxLayout::heightForWidth
int heightForWidth(int width) const override
Definition: KoToolBoxLayout_p.h:308
QWidget::sizeHint
sizeHint
QStyleOption::init
void init(const QWidget *widget)
QWidget::setAutoFillBackground
void setAutoFillBackground(bool enabled)
QWidget::setGeometry
void setGeometry(int x, int y, int w, int h)
KoToolBoxScrollArea::slotScrollerStateChange
void slotScrollerStateChange(QScroller::State state)
Definition: KoToolBoxScrollArea_p.h:90
QWidget::width
int width() const
KoToolBoxScrollArea::wheelEvent
void wheelEvent(QWheelEvent *event) override
Definition: KoToolBoxScrollArea_p.h:111
QAbstractButton::setAutoRepeat
void setAutoRepeat(bool)
QScrollArea::event
virtual bool event(QEvent *e)
QAbstractScrollArea::viewport
QWidget * viewport() const
QAbstractScrollArea::setVerticalScrollBarPolicy
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
QWidget::size
size
KoToolBoxScrollArea
Definition: KoToolBoxScrollArea_p.h:20
QObject::parent
QObject * parent() const
QSize::setHeight
void setHeight(int height)
QScrollArea::setWidget
void setWidget(QWidget *widget)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 11:48:23 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KritaWidgets

Skip menu "KritaWidgets"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

krita API Reference

Skip menu "krita API Reference"
  • libs
  •   KritaBasicFlakes
  •   brush
  •   KritaUndo2
  •   KritaFlake
  •   image
  •   KritaPlugin
  •   Krita
  •   KritaPigment
  •   KritaResources
  •   KritaStore
  •   ui
  •   KritaWidgets
  •   KritaWidgetUtils
  • plugins
  •   Assitants
  •   Extensions
  •   Filters
  •   Generators
  •   Formats
  •           src
  •   PaintOps
  •     libpaintop

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal