diff options
Diffstat (limited to 'src/widgets/styles/qstylehelper.cpp')
-rw-r--r-- | src/widgets/styles/qstylehelper.cpp | 141 |
1 files changed, 79 insertions, 62 deletions
diff --git a/src/widgets/styles/qstylehelper.cpp b/src/widgets/styles/qstylehelper.cpp index 0b910d46df..b4616b8c24 100644 --- a/src/widgets/styles/qstylehelper.cpp +++ b/src/widgets/styles/qstylehelper.cpp @@ -1,45 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** 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 https://www.qt.io/terms-conditions. For further -** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include <qstyleoption.h> #include <qpainter.h> #include <qpixmapcache.h> +#include <private/qhighdpiscaling_p.h> +#include <private/qguiapplication_p.h> #include <private/qmath_p.h> #include <private/qstyle_p.h> #include <qmath.h> @@ -49,6 +15,7 @@ #include <qabstractscrollarea.h> #include <qwindow.h> +#include <qmetaobject.h> #include "qstylehelper_p.h" #include <qstringbuilder.h> @@ -58,39 +25,82 @@ Q_GUI_EXPORT int qt_defaultDpiX(); namespace QStyleHelper { -QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) +static inline bool usePixmapCache(const QStyleOption *opt) { + if (QWidget *widget = qobject_cast<QWidget *>(opt->styleObject)) + return !widget->testAttribute(Qt::WA_StyleSheetTarget); + return true; +} + +QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size, qreal dpr) +{ + if (!usePixmapCache(option)) + return {}; + const QStyleOptionComplex *complexOption = qstyleoption_cast<const QStyleOptionComplex *>(option); QString tmp = key % HexString<uint>(option->state) % HexString<uint>(option->direction) % HexString<uint>(complexOption ? uint(complexOption->activeSubControls) : 0u) % HexString<quint64>(option->palette.cacheKey()) % HexString<uint>(size.width()) - % HexString<uint>(size.height()); + % HexString<uint>(size.height()) + % HexString<qreal>(dpr); #if QT_CONFIG(spinbox) if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) { tmp = tmp % HexString<uint>(spinBox->buttonSymbols) % HexString<uint>(spinBox->stepEnabled) - % QLatin1Char(spinBox->frame ? '1' : '0'); ; + % QChar(spinBox->frame ? u'1' : u'0'); } #endif // QT_CONFIG(spinbox) return tmp; } -qreal dpiScaled(qreal value) +#ifdef Q_OS_DARWIN +static const qreal qstyleBaseDpi = 72; +#else +static const qreal qstyleBaseDpi = 96; +#endif + +Q_WIDGETS_EXPORT qreal dpi(const QStyleOption *option) { -#ifdef Q_OS_MAC - // On mac the DPI is always 72 so we should not scale it - return value; +#ifndef Q_OS_DARWIN + // Prioritize the application override, except for on macOS where + // we have historically not supported the AA_Use96Dpi flag. + if (QCoreApplication::testAttribute(Qt::AA_Use96Dpi)) + return 96; +#endif + + // Expect that QStyleOption::QFontMetrics::QFont has the correct DPI set + if (option) + return option->fontMetrics.fontDpi(); + + // Fall back to historical Qt behavior: hardocded 72 DPI on mac, + // primary screen DPI on other platforms. +#ifdef Q_OS_DARWIN + return qstyleBaseDpi; #else - static const qreal scale = qreal(qt_defaultDpiX()) / 96.0; - return value * scale; + return qt_defaultDpiX(); #endif } -#ifndef QT_NO_ACCESSIBILITY +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, qreal dpi) +{ + return value * dpi / qstyleBaseDpi; +} + +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QPaintDevice *device) +{ + return dpiScaled(value, device->logicalDpiX()); +} + +Q_WIDGETS_EXPORT qreal dpiScaled(qreal value, const QStyleOption *option) +{ + return dpiScaled(value, dpi(option)); +} + +#if QT_CONFIG(accessibility) bool isInstanceOf(QObject *obj, QAccessible::Role role) { bool match = false; @@ -103,7 +113,7 @@ bool isInstanceOf(QObject *obj, QAccessible::Role role) bool hasAncestor(QObject *obj, QAccessible::Role role) { bool found = false; - QObject *parent = obj ? obj->parent() : 0; + QObject *parent = obj ? obj->parent() : nullptr; while (parent && !found) { if (isInstanceOf(parent, role)) found = true; @@ -111,7 +121,7 @@ bool hasAncestor(QObject *obj, QAccessible::Role role) } return found; } -#endif // QT_NO_ACCESSIBILITY +#endif // QT_CONFIG(accessibility) #if QT_CONFIG(dial) @@ -146,7 +156,7 @@ static QPointF calcRadialPos(const QStyleOptionSlider *dial, qreal offset) qreal len = r - QStyleHelper::calcBigLineSize(r) - 3; qreal back = offset * len; QPointF pos(QPointF(xc + back * qCos(a), yc - back * qSin(a))); - return pos; + return pos + dial->rect.topLeft(); } qreal angle(const QPointF &p1, const QPointF &p2) @@ -192,7 +202,7 @@ QPolygonF calcLines(const QStyleOptionSlider *dial) qreal xc = width / 2 + 0.5; qreal yc = height / 2 + 0.5; const int ns = dial->tickInterval; - if (!ns) // Invalid values may be set by Qt Designer. + if (!ns) // Invalid values may be set by Qt Widgets Designer. return poly; int notches = (dial->maximum + ns - 1 - dial->minimum) / ns; if (notches <= 0) @@ -219,7 +229,7 @@ QPolygonF calcLines(const QStyleOptionSlider *dial) poly[2 * i + 1] = QPointF(xc + (r - 1) * c, yc -(r - 1) * s); } } - return poly; + return poly.translated(dial->rect.topLeft()); } // This will draw a nice and shiny QDial for us. We don't want @@ -227,7 +237,7 @@ QPolygonF calcLines(const QStyleOptionSlider *dial) void drawDial(const QStyleOptionSlider *option, QPainter *painter) { - QPalette pal = option->palette; + const QPalette pal = option->palette; QColor buttonColor = pal.button().color(); const int width = option->rect.width(); const int height = option->rect.height(); @@ -241,24 +251,31 @@ void drawDial(const QStyleOptionSlider *option, QPainter *painter) // Draw notches if (option->subControls & QStyle::SC_DialTickmarks) { - painter->setPen(option->palette.dark().color().darker(120)); + const bool inverted = pal.window().color().lightness() < pal.text().color().lightness() + && pal.light().color().lightness() > pal.dark().color().lightness(); + const QColor notchColor = inverted ? pal.light().color().lighter(120) + : pal.dark().color().darker(120); + painter->setPen(notchColor); painter->drawLines(QStyleHelper::calcLines(option)); } + // setting color before BEGIN_STYLE_PIXMAPCACHE since + // otherwise it is not set when the image is in the cache + buttonColor.setHsv(buttonColor .hue(), + qMin(140, buttonColor .saturation()), + qMax(180, buttonColor.value())); + // Cache dial background BEGIN_STYLE_PIXMAPCACHE(QString::fromLatin1("qdial")); p->setRenderHint(QPainter::Antialiasing); const qreal d_ = r / 6; - const qreal dx = option->rect.x() + d_ + (width - 2 * r) / 2 + 1; - const qreal dy = option->rect.y() + d_ + (height - 2 * r) / 2 + 1; + const qreal dx = d_ + (width - 2 * r) / 2 + 1; + const qreal dy = d_ + (height - 2 * r) / 2 + 1; QRectF br = QRectF(dx + 0.5, dy + 0.5, int(r * 2 - 2 * d_ - 2), int(r * 2 - 2 * d_ - 2)); - buttonColor.setHsv(buttonColor .hue(), - qMin(140, buttonColor .saturation()), - qMax(180, buttonColor.value())); if (enabled) { // Drop shadow @@ -310,7 +327,7 @@ void drawDial(const QStyleOptionSlider *option, QPainter *painter) QPointF dp = calcRadialPos(option, qreal(0.70)); buttonColor = buttonColor.lighter(104); - buttonColor.setAlphaF(qreal(0.8)); + buttonColor.setAlphaF(0.8f); const qreal ds = r/qreal(7.0); QRectF dialRect(dp.x() - ds, dp.y() - ds, 2*ds, 2*ds); QRadialGradient dialGradient(dialRect.center().x() + dialRect.width()/2, @@ -347,7 +364,7 @@ void drawBorderPixmap(const QPixmap &pixmap, QPainter *painter, const QRect &rec QRect(left, 0, size.width() -right - left, top)); //top-left - if(left > 0) + if (left > 0) painter->drawPixmap(QRect(rect.left(), rect.top(), left, top), pixmap, QRect(0, 0, left, top)); |