diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-06-15 15:11:29 +0200 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2020-06-17 09:42:31 +0000 |
commit | a2f664e5fbaf7d879e45510cb259e91fad15e030 (patch) | |
tree | d652c533d3ff978d3dd62c060c92e9b5a35aa3f9 /src | |
parent | 34435e087ade5073fdee067ce62debdfe8564c41 (diff) |
Native style: add ProgressBar
Change-Id: Ie48002b083615afd4f8f8ab3b1303a8f5c39f81b
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/imports/controls/macos/ProgressBar.qml | 60 | ||||
-rw-r--r-- | src/imports/controls/macos/macos.pri | 1 | ||||
-rw-r--r-- | src/imports/nativestyle/controls/DefaultProgressBar.qml | 65 | ||||
-rw-r--r-- | src/imports/nativestyle/controls/controls.pri | 1 | ||||
-rw-r--r-- | src/imports/nativestyle/items/items.pri | 2 | ||||
-rw-r--r-- | src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp | 97 | ||||
-rw-r--r-- | src/imports/nativestyle/items/qquickstyleitemprogressbar.h | 72 | ||||
-rw-r--r-- | src/imports/nativestyle/qmldir | 1 | ||||
-rw-r--r-- | src/imports/nativestyle/qstyle/mac/qquickmacstyle_mac.mm | 96 | ||||
-rw-r--r-- | src/imports/nativestyle/qstyle/qquickcommonstyle.cpp | 7 | ||||
-rw-r--r-- | src/imports/nativestyle/qstyle/qquickstyleoption.h | 2 |
11 files changed, 326 insertions, 78 deletions
diff --git a/src/imports/controls/macos/ProgressBar.qml b/src/imports/controls/macos/ProgressBar.qml new file mode 100644 index 00000000..6c4c9c22 --- /dev/null +++ b/src/imports/controls/macos/ProgressBar.qml @@ -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$ +** +****************************************************************************/ + +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.ProgressBar { + id: control + + implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset, + implicitContentWidth + leftPadding + rightPadding) + implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset, + implicitContentHeight + topPadding + bottomPadding) + + font.pixelSize: nativeIndicator ? indicator.styleFont(control).pixelSize : undefined + + background: NativeStyle.ProgressBar { + control: control + // Note: on macOS, the groove will paint both the background and the contents + subControl: NativeStyle.ProgressBar.Groove + useNinePatchImage: false + } + +} diff --git a/src/imports/controls/macos/macos.pri b/src/imports/controls/macos/macos.pri index e80afb0a..aeafdcb3 100644 --- a/src/imports/controls/macos/macos.pri +++ b/src/imports/controls/macos/macos.pri @@ -10,3 +10,4 @@ QML_FILES += \ $$PWD/TextArea.qml \ $$PWD/ComboBox.qml \ $$PWD/ScrollBar.qml \ + $$PWD/ProgressBar.qml \ diff --git a/src/imports/nativestyle/controls/DefaultProgressBar.qml b/src/imports/nativestyle/controls/DefaultProgressBar.qml new file mode 100644 index 00000000..e86f9410 --- /dev/null +++ b/src/imports/nativestyle/controls/DefaultProgressBar.qml @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** 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.Templates 2.12 as T +import QtQuick.Controls 2.12 +import QtQuick.Controls.impl 2.12 +import QtQuick.NativeStyle 6.0 as NativeStyle + +T.ProgressBar { + 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: nativeIndicator ? indicator.styleFont(control).pixelSize : undefined + + background: NativeStyle.ProgressBar { + control: control + subControl: NativeStyle.ProgressBar.Groove + } + + contentItem: NativeStyle.ProgressBar { + control: control + subControl: NativeStyle.ProgressBar.Contents + useNinePatchImage: false + } +} diff --git a/src/imports/nativestyle/controls/controls.pri b/src/imports/nativestyle/controls/controls.pri index b1f5ee0b..55f84703 100644 --- a/src/imports/nativestyle/controls/controls.pri +++ b/src/imports/nativestyle/controls/controls.pri @@ -10,3 +10,4 @@ QML_FILES += \ $$PWD/DefaultTextArea.qml \ $$PWD/DefaultComboBox.qml \ $$PWD/DefaultScrollBar.qml \ + $$PWD/DefaultProgressBar.qml \ diff --git a/src/imports/nativestyle/items/items.pri b/src/imports/nativestyle/items/items.pri index bbe6523b..a5c33b86 100644 --- a/src/imports/nativestyle/items/items.pri +++ b/src/imports/nativestyle/items/items.pri @@ -13,6 +13,7 @@ HEADERS += \ $$PWD/qquickstyleitemtextarea.h \ $$PWD/qquickstyleitemcombobox.h \ $$PWD/qquickstyleitemscrollbar.h \ + $$PWD/qquickstyleitemprogressbar.h \ SOURCES += \ $$PWD/qquickstyleitem.cpp \ @@ -27,3 +28,4 @@ SOURCES += \ $$PWD/qquickstyleitemtextarea.cpp \ $$PWD/qquickstyleitemcombobox.cpp \ $$PWD/qquickstyleitemscrollbar.cpp \ + $$PWD/qquickstyleitemprogressbar.cpp \ diff --git a/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp b/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp new file mode 100644 index 00000000..3429b5ac --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemprogressbar.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** 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 "qquickstyleitemprogressbar.h" + +QFont QQuickStyleItemProgressBar::styleFont(QQuickItem *control) +{ + return style()->font(QStyle::CE_ProgressBarLabel, controlSize(control)); +} + +void QQuickStyleItemProgressBar::connectToControl() +{ + QQuickStyleItem::connectToControl(); + auto progressBar = control<QQuickProgressBar>(); + connect(progressBar, &QQuickProgressBar::fromChanged, this, &QQuickStyleItem::markImageDirty); + connect(progressBar, &QQuickProgressBar::toChanged, this, &QQuickStyleItem::markImageDirty); + connect(progressBar, &QQuickProgressBar::positionChanged, this, &QQuickStyleItem::markImageDirty); +} + +StyleItemGeometry QQuickStyleItemProgressBar::calculateGeometry() +{ + QStyleOptionProgressBar styleOption; + initStyleOption(styleOption); + + StyleItemGeometry geometry; + geometry.minimumSize = style()->sizeFromContents(QStyle::CT_ProgressBar, &styleOption, QSize(0, 0)); + geometry.implicitSize = geometry.minimumSize; + styleOption.rect = QRect(QPoint(0, 0), geometry.implicitSize); + geometry.contentRect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption); + geometry.layoutRect = style()->subElementRect(QStyle::SE_ProgressBarLayoutItem, &styleOption); + geometry.ninePatchMargins = style()->ninePatchMargins(QStyle::CE_ProgressBar, &styleOption, geometry.minimumSize); + + return geometry; +} + +void QQuickStyleItemProgressBar::paintEvent(QPainter *painter) +{ + QStyleOptionProgressBar styleOption; + initStyleOption(styleOption); + if (m_subControl == Groove) { + styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarGroove, &styleOption); + style()->drawControl(QStyle::CE_ProgressBarGroove, &styleOption, painter); + } else { + styleOption.rect = style()->subElementRect(QStyle::SE_ProgressBarContents, &styleOption); + style()->drawControl(QStyle::CE_ProgressBarContents, &styleOption, painter); + } +} + +void QQuickStyleItemProgressBar::initStyleOption(QStyleOptionProgressBar &styleOption) +{ + initStyleOptionBase(styleOption); + auto progressBar = control<QQuickProgressBar>(); + + styleOption.state = QStyle::State_Horizontal; + + if (progressBar->isIndeterminate()) { + styleOption.minimum = 0; + styleOption.maximum = 0; + } else { + styleOption.minimum = progressBar->from(); + styleOption.maximum = progressBar->to(); + styleOption.progress = progressBar->value(); + } +} diff --git a/src/imports/nativestyle/items/qquickstyleitemprogressbar.h b/src/imports/nativestyle/items/qquickstyleitemprogressbar.h new file mode 100644 index 00000000..f6281f0f --- /dev/null +++ b/src/imports/nativestyle/items/qquickstyleitemprogressbar.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 QQUICKSTYLEITEMPROGRESSBAR_H +#define QQUICKSTYLEITEMPROGRESSBAR_H + +#include "qquickstyleitem.h" +#include <QtQuickTemplates2/private/qquickprogressbar_p.h> + +class QQuickStyleItemProgressBar : public QQuickStyleItem +{ + Q_OBJECT + + Q_PROPERTY(SubControl subControl MEMBER m_subControl) + + QML_NAMED_ELEMENT(ProgressBar) + +public: + enum SubControl { + Groove = 1, + Contents, + }; + Q_ENUM(SubControl) + + QFont styleFont(QQuickItem *control) override; + +protected: + void connectToControl() override; + void paintEvent(QPainter *painter) override; + StyleItemGeometry calculateGeometry() override; + +private: + void initStyleOption(QStyleOptionProgressBar &styleOption); + +private: + SubControl m_subControl = Groove; +}; + +#endif // QQUICKSTYLEITEMPROGRESSBAR_H diff --git a/src/imports/nativestyle/qmldir b/src/imports/nativestyle/qmldir index 1e62db50..799a9609 100644 --- a/src/imports/nativestyle/qmldir +++ b/src/imports/nativestyle/qmldir @@ -14,3 +14,4 @@ DefaultFrame 6.0 controls/DefaultFrame.qml DefaultTextArea 6.0 controls/DefaultTextArea.qml DefaultComboBox 6.0 controls/DefaultComboBox.qml DefaultScrollBar 6.0 controls/DefaultScrollBar.qml +DefaultProgressBar 6.0 controls/DefaultProgressBar.qml diff --git a/src/imports/nativestyle/qstyle/mac/qquickmacstyle_mac.mm b/src/imports/nativestyle/qstyle/mac/qquickmacstyle_mac.mm index 5101680c..6ad3be5c 100644 --- a/src/imports/nativestyle/qstyle/mac/qquickmacstyle_mac.mm +++ b/src/imports/nativestyle/qstyle/mac/qquickmacstyle_mac.mm @@ -1422,72 +1422,16 @@ QStyleHelper::WidgetSizePolicy QMacStylePrivate::effectiveAquaSizeConstrain(cons QStyleHelper::WidgetSizePolicy QMacStylePrivate::aquaSizeConstrain(const QStyleOption *option, QStyle::ContentsType ct, QSize szHint, QSize *insz) const { -#if defined(QMAC_QAQUASTYLE_SIZE_CONSTRAIN) || defined(DEBUG_SIZE_CONSTRAINT) - if (option) { - if (option->state & QStyle::State_Small) - return QStyleHelper::SizeSmall; - if (option->state & QStyle::State_Mini) - return QStyleHelper::SizeMini; - } + if (!option) + return QStyleHelper::SizeLarge; - if (insz) - *insz = QSize(); - if (qEnvironmentVariableIsSet("QWIDGET_ALL_SMALL")) + if (option->state & QStyle::State_Small) return QStyleHelper::SizeSmall; - if (qEnvironmentVariableIsSet("QWIDGET_ALL_MINI")) + if (option->state & QStyle::State_Mini) return QStyleHelper::SizeMini; - return QStyleHelper::SizeDefault; - - QSize large = qt_aqua_get_known_size(ct, option, szHint, QStyleHelper::SizeLarge), - small = qt_aqua_get_known_size(ct, option, szHint, QStyleHelper::SizeSmall), - mini = qt_aqua_get_known_size(ct, option, szHint, QStyleHelper::SizeMini); - bool guess_size = false; - QStyleHelper::WidgetSizePolicy ret = QStyleHelper::SizeDefault; - QStyleHelper::WidgetSizePolicy wsp = QStyleHelper::widgetSizePolicy(option); - if (wsp == QStyleHelper::SizeDefault) - guess_size = true; - else if (wsp == QStyleHelper::SizeMini) - ret = QStyleHelper::SizeMini; - else if (wsp == QStyleHelper::SizeSmall) - ret = QStyleHelper::SizeSmall; - else if (wsp == QStyleHelper::SizeLarge) - ret = QStyleHelper::SizeLarge; - if (guess_size) - ret = qt_aqua_guess_size(large, small, mini); - - QSize *sz = 0; - if (ret == QStyleHelper::SizeSmall) - sz = &small; - else if (ret == QStyleHelper::SizeLarge) - sz = &large; - else if (ret == QStyleHelper::SizeMini) - sz = &mini; - if (insz) - *insz = sz ? *sz : QSize(-1, -1); -#ifdef DEBUG_SIZE_CONSTRAINT - if (sz) { - const char *size_desc = "Unknown"; - if (sz == &small) - size_desc = "Small"; - else if (sz == &large) - size_desc = "Large"; - else if (sz == &mini) - size_desc = "Mini"; - qDebug("%s - %s: %s taken (%d, %d) [%d, %d]", - widg ? widg->objectName().toLatin1().constData() : "*Unknown*", - widg ? widg->metaObject()->className() : "*Unknown*", size_desc, widg->width(), widg->height(), - sz->width(), sz->height()); - } -#endif - return ret; -#else - if (insz) - *insz = QSize(); - Q_UNUSED(widg); - Q_UNUSED(ct); - Q_UNUSED(szHint); - return QStyleHelper::SizeDefault; -#endif + + return QStyleHelper::SizeLarge; + } uint qHash(const QMacStylePrivate::CocoaControl &cw, uint seed = 0) @@ -4155,10 +4099,9 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter } break; case CE_ProgressBarLabel: - case CE_ProgressBarGroove: - // Do nothing. All done in CE_ProgressBarContents. Only keep these for proxy style overrides. - break; case CE_ProgressBarContents: + break; + case CE_ProgressBarGroove: if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) { const bool isIndeterminate = (pb->minimum == 0 && pb->maximum == 0); const bool inverted = pb->invertedAppearance; @@ -4169,7 +4112,8 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter QRect rect = pb->rect; const CGRect cgRect = rect.toCGRect(); - const auto aquaSize = d->effectiveAquaSizeConstrain(opt); + const auto aquaSize = d->aquaSizeConstrain(opt); + // const QProgressStyleAnimation *animation = qobject_cast<QProgressStyleAnimation*>(d->animation(opt->styleObject)); QIndeterminateProgressIndicator *ipi = nil; // if (isIndeterminate || animation) @@ -4644,16 +4588,18 @@ QRect QMacStyle::subElementRect(SubElement sr, const QStyleOption *opt) const rect = opt->rect; setLayoutItemMargins(+1, 0 /* SHOULD be -1, done for alignment */, 0, 0 /* SHOULD be -1, done for alignment */, &rect, opt->direction); break; - case SE_ProgressBarLayoutItem: { - rect = opt->rect; - int bottom = SIZE(3, 8, 8); - if (opt->state & State_Horizontal) { - rect.adjust(0, +1, 0, -bottom); - } else { - setLayoutItemMargins(+1, 0, -bottom, 0, &rect, opt->direction); + case SE_ProgressBarLayoutItem: + if (const QStyleOptionProgressBar *pb = qstyleoption_cast<const QStyleOptionProgressBar *>(opt)) { + const bool isIndeterminate = (pb->minimum == 0 && pb->maximum == 0); + rect = opt->rect; + + if (isIndeterminate) { + rect.adjust(1, 2, -1, -2); + } else { + rect.adjust(1, 1, -1, -2); + } } break; - } case SE_PushButtonLayoutItem: if (const QStyleOptionButton *buttonOpt = qstyleoption_cast<const QStyleOptionButton *>(opt)) { diff --git a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp index e8dadb9e..17af8763 100644 --- a/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp +++ b/src/imports/nativestyle/qstyle/qquickcommonstyle.cpp @@ -4673,11 +4673,16 @@ QSize QCommonStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, c sz = QSize(w, w); } break; + case CT_ProgressBar: + if (sz.isNull()) { + // Special case: return minimum nine patch image size + sz = QSize(10, 10); + } + break; case CT_ScrollBar: case CT_MenuBar: case CT_Menu: case CT_MenuBarItem: - case CT_ProgressBar: case CT_TabBarTab: // just return the contentsSize for now Q_FALLTHROUGH(); diff --git a/src/imports/nativestyle/qstyle/qquickstyleoption.h b/src/imports/nativestyle/qstyle/qquickstyleoption.h index dc92f849..c41f557c 100644 --- a/src/imports/nativestyle/qstyle/qquickstyleoption.h +++ b/src/imports/nativestyle/qstyle/qquickstyleoption.h @@ -357,8 +357,6 @@ protected: QStyleOptionProgressBar(int version); }; -typedef Q_DECL_DEPRECATED QStyleOptionProgressBar QStyleOptionProgressBarV2; - class QStyleOptionMenuItem : public QStyleOption { public: |