diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-06-08 13:53:00 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-06-12 12:59:31 +0000 |
commit | 6715627602c049f83ae223ad89a4a0f837ca3c00 (patch) | |
tree | 47d6427987aa61e655fa776ec8f9532f46fc70ed /src/imports/nativestyle/items | |
parent | 6e1fed645d6461f5d1f49f91a8ce32b66638a004 (diff) |
NativeStyle: add ScrollBar
Change-Id: Ied2055866a67798ce60105e7251740a3e66b38db
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/nativestyle/items')
-rw-r--r-- | src/imports/nativestyle/items/items.pri | 2 | ||||
-rw-r--r-- | src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp | 93 | ||||
-rw-r--r-- | src/imports/nativestyle/items/qquickstyleitemscrollbar.h | 72 |
3 files changed, 167 insertions, 0 deletions
diff --git a/src/imports/nativestyle/items/items.pri b/src/imports/nativestyle/items/items.pri index 1bb311fd..bbe6523b 100644 --- a/src/imports/nativestyle/items/items.pri +++ b/src/imports/nativestyle/items/items.pri @@ -12,6 +12,7 @@ HEADERS += \ $$PWD/qquickstyleitemframe.h \ $$PWD/qquickstyleitemtextarea.h \ $$PWD/qquickstyleitemcombobox.h \ + $$PWD/qquickstyleitemscrollbar.h \ SOURCES += \ $$PWD/qquickstyleitem.cpp \ @@ -25,3 +26,4 @@ SOURCES += \ $$PWD/qquickstyleitemframe.cpp \ $$PWD/qquickstyleitemtextarea.cpp \ $$PWD/qquickstyleitemcombobox.cpp \ + $$PWD/qquickstyleitemscrollbar.cpp \ diff --git a/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp b/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp new file mode 100644 index 00000000..7a462082 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemscrollbar.cpp @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qquickstyleitemscrollbar.h" + +QFont QQuickStyleItemScrollBar::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemScrollBar::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto scrollBar = control<QQuickScrollBar>(); + connect(scrollBar, &QQuickScrollBar::orientationChanged, this, &QQuickStyleItem::markImageDirty); + connect(scrollBar, &QQuickScrollBar::pressedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemScrollBar::calculateGeometry() +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ScrollBar, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.layoutRect = style()->subElementRect(QStyle::SE_ScrollBarLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_ScrollBar, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemScrollBar::paintEvent(QPainter *painter) +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_ScrollBar, &styleOption, painter); +} + +void QQuickStyleItemScrollBar::initStyleOption(QStyleOptionSlider &styleOption) +{ + initStyleOptionBase(styleOption); + auto scrollBar = control<QQuickScrollBar>(); + + styleOption.subControls = m_subControl == Groove ? QStyle::SC_ScrollBarGroove : QStyle::SC_ScrollBarSlider; + styleOption.activeSubControls = QStyle::SC_None; + styleOption.orientation = scrollBar->orientation(); + + if (scrollBar->isPressed()) + styleOption.state |= QStyle::State_Sunken; + + // The following values will let the handle fill 100% of the + // groove / imageSize. But when the handle is resized by + // QQuickScrollBar, it will end up with the correct size visually. + styleOption.pageStep = 1000; + styleOption.minimum = 0; + styleOption.maximum = 1; + styleOption.sliderValue = 0; +} diff --git a/src/imports/nativestyle/items/qquickstyleitemscrollbar.h b/src/imports/nativestyle/items/qquickstyleitemscrollbar.h new file mode 100644 index 00000000..e181d4ec --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemscrollbar.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QQUICKSTYLEITEMSCROLLBAR_H +#define QQUICKSTYLEITEMSCROLLBAR_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickscrollbar_p.h> + +class QQuickStyleItemScrollBar : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + + QML_NAMED_ELEMENT(ScrollBar) + +public: + enum SubControl { + Groove = 1, + Handle, + }; + Q_ENUM(SubControl) + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSlider &styleOption); + +private: + SubControl m_subControl = Groove; +}; + +#endif // QQUICKSTYLEITEMSCROLLBAR_H |