From bf727efa7aa9a4738ae86a2c699832129813c670 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Thu, 25 Jun 2020 13:34:30 +0200 Subject: Native style: add Dial Change-Id: Iad09b9f6080994528d65dcddc9eda49dbba356f7 Reviewed-by: Mitch Curtis --- .../quickcontrols2/desktopgallery/CustomDials.qml | 87 ++++++++++++++++ examples/quickcontrols2/desktopgallery/Dials.qml | 66 +++++++++++++ .../desktopgallery/desktopgallery.pro | 2 + examples/quickcontrols2/desktopgallery/main.qml | 2 + src/imports/controls/macos/Dial.qml | 43 ++++++++ src/imports/controls/macos/macos.pri | 1 + src/imports/nativestyle/controls/DefaultDial.qml | 59 +++++++++++ src/imports/nativestyle/controls/controls.pri | 1 + src/imports/nativestyle/items/items.pri | 2 + .../nativestyle/items/qquickstyleitemdial.cpp | 110 +++++++++++++++++++++ .../nativestyle/items/qquickstyleitemdial.h | 60 +++++++++++ src/imports/nativestyle/qmldir | 1 + .../nativestyle/qstyle/qquickcommonstyle.cpp | 2 + src/imports/nativestyle/qstyle/qquickstyle.h | 1 + 14 files changed, 437 insertions(+) create mode 100644 examples/quickcontrols2/desktopgallery/CustomDials.qml create mode 100644 examples/quickcontrols2/desktopgallery/Dials.qml create mode 100644 src/imports/controls/macos/Dial.qml create mode 100644 src/imports/nativestyle/controls/DefaultDial.qml create mode 100644 src/imports/nativestyle/items/qquickstyleitemdial.cpp create mode 100644 src/imports/nativestyle/items/qquickstyleitemdial.h diff --git a/examples/quickcontrols2/desktopgallery/CustomDials.qml b/examples/quickcontrols2/desktopgallery/CustomDials.qml new file mode 100644 index 00000000..c1a0ac4e --- /dev/null +++ b/examples/quickcontrols2/desktopgallery/CustomDials.qml @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick dial1s 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$ +** +****************************************************************************/ + +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Controls.impl 2.12 +import QtQuick.Layouts 2.15 + +ControlContainer { + id: container + title: "Dials" + + Row { + spacing: container.rowSpacing + + Dial { + id: dial1 + width: 50 + height: 50 + from: 0 + to: 10 + value: 5 + + background: DialImpl { + implicitWidth: 184 + implicitHeight: 184 + color: "darkgray" + progress: dial1.position + opacity: dial1.enabled ? 1 : 0.3 + } + + handle: ColorImage { + x: dial1.background.x + dial1.background.width / 2 - width / 2 + y: dial1.background.y + dial1.background.height / 2 - height / 2 + width: 14 + height: 10 + color: "green" + source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/images/dial-indicator.png" + antialiasing: true + opacity: dial1.enabled ? 1 : 0.3 + transform: [ + Translate { + y: -Math.min(dial1.background.width, dial1.background.height) * 0.4 + dial1.handle.height / 2 + }, + Rotation { + angle: dial1.angle + origin.x: dial1.handle.width / 2 + origin.y: dial1.handle.height / 2 + } + ] + } + } + } +} diff --git a/examples/quickcontrols2/desktopgallery/Dials.qml b/examples/quickcontrols2/desktopgallery/Dials.qml new file mode 100644 index 00000000..56dff2a4 --- /dev/null +++ b/examples/quickcontrols2/desktopgallery/Dials.qml @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 2.15 + +ControlContainer { + id: container + title: "Dials" + + Row { + spacing: container.rowSpacing + + Dial { + width: 50 + height: 50 + from: 0 + to: 10 + value: 5 + } + + Dial { + width: 50 + height: 50 + from: 0 + to: 10 + value: 5 + stepSize: 1 + property int qqc2_style_tickPosition: 1 + } + } +} diff --git a/examples/quickcontrols2/desktopgallery/desktopgallery.pro b/examples/quickcontrols2/desktopgallery/desktopgallery.pro index cabc9d7c..6c6112db 100644 --- a/examples/quickcontrols2/desktopgallery/desktopgallery.pro +++ b/examples/quickcontrols2/desktopgallery/desktopgallery.pro @@ -30,6 +30,8 @@ QML_FILES = \ CustomScrollBars.qml \ ProgressBars.qml \ CustomProgressBars.qml \ + Dials.qml \ + CustomDials.qml \ OTHER_FILES += $$QML_FILES RESOURCES += $$QML_FILES diff --git a/examples/quickcontrols2/desktopgallery/main.qml b/examples/quickcontrols2/desktopgallery/main.qml index 5bdbd71f..6cf7a646 100644 --- a/examples/quickcontrols2/desktopgallery/main.qml +++ b/examples/quickcontrols2/desktopgallery/main.qml @@ -86,6 +86,7 @@ ApplicationWindow { TextFields { } TextAreas { } ComboBoxes { } + Dials { } Frames { } ProgressBars { } ScrollBars { } @@ -112,6 +113,7 @@ ApplicationWindow { CustomTextFields { } CustomTextAreas { } CustomComboBoxes { } + CustomDials { } CustomFrames { } CustomProgressBars { } CustomScrollBars { } diff --git a/src/imports/controls/macos/Dial.qml b/src/imports/controls/macos/Dial.qml new file mode 100644 index 00000000..d21bfbed --- /dev/null +++ b/src/imports/controls/macos/Dial.qml @@ -0,0 +1,43 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Controls.impl 2.12 +import QtQuick.NativeStyle 6.0 as NativeStyle + +NativeStyle.DefaultDial { +} diff --git a/src/imports/controls/macos/macos.pri b/src/imports/controls/macos/macos.pri index aeafdcb3..98474a92 100644 --- a/src/imports/controls/macos/macos.pri +++ b/src/imports/controls/macos/macos.pri @@ -11,3 +11,4 @@ QML_FILES += \ $$PWD/ComboBox.qml \ $$PWD/ScrollBar.qml \ $$PWD/ProgressBar.qml \ + $$PWD/Dial.qml \ diff --git a/src/imports/nativestyle/controls/DefaultDial.qml b/src/imports/nativestyle/controls/DefaultDial.qml new file mode 100644 index 00000000..d4fc3b98 --- /dev/null +++ b/src/imports/nativestyle/controls/DefaultDial.qml @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Controls.impl 2.12 +import QtQuick.Templates 2.12 as T +import QtQuick.NativeStyle 6.0 as NativeStyle + +T.Dial { + id: control + + readonly property bool nativeBackground: background instanceof NativeStyle.StyleItem + + implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, + implicitContentWidth + leftPadding + rightPadding) + implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, + implicitContentHeight + topPadding + bottomPadding) + + font.pixelSize: nativeBackground ? background.styleFont(control).pixelSize : undefined + + background: NativeStyle.Dial { + control: control + useNinePatchImage: false + } +} diff --git a/src/imports/nativestyle/controls/controls.pri b/src/imports/nativestyle/controls/controls.pri index 55f84703..8675989e 100644 --- a/src/imports/nativestyle/controls/controls.pri +++ b/src/imports/nativestyle/controls/controls.pri @@ -11,3 +11,4 @@ QML_FILES += \ $$PWD/DefaultComboBox.qml \ $$PWD/DefaultScrollBar.qml \ $$PWD/DefaultProgressBar.qml \ + $$PWD/DefaultDial.qml \ diff --git a/src/imports/nativestyle/items/items.pri b/src/imports/nativestyle/items/items.pri index a5c33b86..f9caca1c 100644 --- a/src/imports/nativestyle/items/items.pri +++ b/src/imports/nativestyle/items/items.pri @@ -14,6 +14,7 @@ HEADERS += \ $$PWD/qquickstyleitemcombobox.h \ $$PWD/qquickstyleitemscrollbar.h \ $$PWD/qquickstyleitemprogressbar.h \ + $$PWD/qquickstyleitemdial.h \ SOURCES += \ $$PWD/qquickstyleitem.cpp \ @@ -29,3 +30,4 @@ SOURCES += \ $$PWD/qquickstyleitemcombobox.cpp \ $$PWD/qquickstyleitemscrollbar.cpp \ $$PWD/qquickstyleitemprogressbar.cpp \ + $$PWD/qquickstyleitemdial.cpp \ diff --git a/src/imports/nativestyle/items/qquickstyleitemdial.cpp b/src/imports/nativestyle/items/qquickstyleitemdial.cpp new file mode 100644 index 00000000..f214fb2e --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemdial.cpp @@ -0,0 +1,110 @@ +/**************************************************************************** +** +** 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 "qquickstyleitemdial.h" + +QFont QQuickStyleItemDial::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemDial::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto dial = control(); + connect(dial, &QQuickDial::fromChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::toChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::positionChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::valueChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::stepSizeChanged, this, &QQuickStyleItem::markImageDirty); + connect(dial, &QQuickDial::pressedChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemDial::calculateGeometry() +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_Dial, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + geometry.layoutRect = style()->subElementRect(QStyle::SE_SliderLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CC_Dial, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemDial::paintEvent(QPainter *painter) +{ + QStyleOptionSlider styleOption; + initStyleOption(styleOption); + style()->drawComplexControl(QStyle::CC_Dial, &styleOption, painter); +} + +void QQuickStyleItemDial::initStyleOption(QStyleOptionSlider &styleOption) +{ + initStyleOptionBase(styleOption); + auto dial = control(); + + styleOption.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; + styleOption.activeSubControls = QStyle::SC_None; + styleOption.tickInterval = dial->stepSize(); + styleOption.dialWrapping = dial->wrap(); + styleOption.upsideDown = true; + + if (dial->isPressed()) + styleOption.state |= QStyle::State_Sunken; + + if (dial->stepSize() == 0) { + styleOption.minimum = 0; + styleOption.maximum = 10000; + styleOption.sliderPosition = dial->position() * styleOption.maximum; + } else { + styleOption.minimum = dial->from(); + styleOption.maximum = dial->to(); + styleOption.sliderPosition = dial->value(); + } + + // TODO: add proper API for tickmarks + const int index = dial->metaObject()->indexOfProperty("qqc2_style_tickPosition"); + if (index != -1) { + const int tickPosition = dial->metaObject()->property(index).read(dial).toInt(); + styleOption.tickPosition = QStyleOptionSlider::TickPosition(tickPosition); + if (styleOption.tickPosition != QStyleOptionSlider::NoTicks) + styleOption.subControls |= QStyle::SC_DialTickmarks; + } + +} diff --git a/src/imports/nativestyle/items/qquickstyleitemdial.h b/src/imports/nativestyle/items/qquickstyleitemdial.h new file mode 100644 index 00000000..3eefa734 --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemdial.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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 QQUICKSTYLEITEMDIAL_H +#define QQUICKSTYLEITEMDIAL_H + +#include "qquickstyleitem.h" +#include + +class QQuickStyleItemDial : public QQuickStyleItem +{ + Q_OBJECT + QML_NAMED_ELEMENT(Dial) + +public: + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionSlider &styleOption); +}; + +#endif // QQUICKSTYLEITEMDIAL_H diff --git a/src/imports/nativestyle/qmldir b/src/imports/nativestyle/qmldir index 799a9609..3b6ce6c5 100644 --- a/src/imports/nativestyle/qmldir +++ b/src/imports/nativestyle/qmldir @@ -15,3 +15,4 @@ DefaultTextArea 6.0 controls/DefaultTextArea.qml DefaultComboBox 6.0 controls/DefaultComboBox.qml DefaultScrollBar 6.0 controls/DefaultScrollBar.qml DefaultProgressBar 6.0 controls/DefaultProgressBar.qml +DefaultDial 6.0 controls/DefaultDial.qml diff --git a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp index 16e05abd..41f12545 100644 --- a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp +++ b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp @@ -4666,6 +4666,8 @@ QSize QCommonStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, c if (const QStyleOptionSlider *option = qstyleoption_cast(opt)) sz = subControlRect(QStyle::CC_Slider, option, QStyle::SC_SliderHandle).size(); break; + case CT_Dial: + sz = QSize(20, 20); case CT_Frame: if (const QStyleOptionFrame *option = qstyleoption_cast(opt)) { const int ninePatchSplit = 1; diff --git a/src/imports/nativestyle/qstyle/qquickstyle.h b/src/imports/nativestyle/qstyle/qquickstyle.h index 8fd3a5ae..4a2e5287 100644 --- a/src/imports/nativestyle/qstyle/qquickstyle.h +++ b/src/imports/nativestyle/qstyle/qquickstyle.h @@ -543,6 +543,7 @@ public: CT_Menu, CT_TabBarTab, CT_Slider, + CT_Dial, CT_ScrollBar, CT_LineEdit, CT_SpinBox, -- cgit v1.2.3