summaryrefslogtreecommitdiffstats
path: root/src/widgets/styles/qwindowsstyle.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/styles/qwindowsstyle.cpp')
-rw-r--r--src/widgets/styles/qwindowsstyle.cpp186
1 files changed, 58 insertions, 128 deletions
diff --git a/src/widgets/styles/qwindowsstyle.cpp b/src/widgets/styles/qwindowsstyle.cpp
index 08134b5e12..bae182b467 100644
--- a/src/widgets/styles/qwindowsstyle.cpp
+++ b/src/widgets/styles/qwindowsstyle.cpp
@@ -88,26 +88,6 @@ enum QSliderDirection { SlUp, SlDown, SlLeft, SlRight };
QWindowsStylePrivate::QWindowsStylePrivate() = default;
-qreal QWindowsStylePrivate::appDevicePixelRatio()
-{
- return qApp->devicePixelRatio();
-}
-
-bool QWindowsStylePrivate::isDarkMode()
-{
- bool result = false;
-#ifdef Q_OS_WIN
- using QWindowsApplication = QNativeInterface::Private::QWindowsApplication;
- // Windows only: Return whether dark mode style support is desired and
- // dark mode is in effect.
- if (auto windowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration())) {
- result = windowsApp->isDarkMode()
- && windowsApp->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle);
- }
-#endif
- return result;
-}
-
// Returns \c true if the toplevel parent of \a widget has seen the Alt-key
bool QWindowsStylePrivate::hasSeenAlt(const QWidget *widget) const
{
@@ -132,19 +112,20 @@ bool QWindowsStyle::eventFilter(QObject *o, QEvent *e)
widget = widget->window();
// Alt has been pressed - find all widgets that care
- QList<QWidget *> l = widget->findChildren<QWidget *>();
+ const QList<QWidget *> children = widget->findChildren<QWidget *>();
auto ignorable = [](QWidget *w) {
return w->isWindow() || !w->isVisible()
|| w->style()->styleHint(SH_UnderlineShortcut, nullptr, w);
};
- l.removeIf(ignorable);
// Update states before repainting
d->seenAlt.append(widget);
d->alt_down = true;
// Repaint all relevant widgets
- for (int pos = 0; pos < l.size(); ++pos)
- l.at(pos)->update();
+ for (QWidget *w : children) {
+ if (!ignorable(w))
+ w->update();
+ }
}
break;
case QEvent::KeyRelease:
@@ -154,9 +135,9 @@ bool QWindowsStyle::eventFilter(QObject *o, QEvent *e)
// Update state and repaint the menu bars.
d->alt_down = false;
#if QT_CONFIG(menubar)
- QList<QMenuBar *> l = widget->findChildren<QMenuBar *>();
- for (int i = 0; i < l.size(); ++i)
- l.at(i)->update();
+ const QList<QMenuBar *> menuBars = widget->findChildren<QMenuBar *>();
+ for (QWidget *w : menuBars)
+ w->update();
#endif
}
break;
@@ -276,28 +257,33 @@ void QWindowsStyle::polish(QPalette &pal)
int QWindowsStylePrivate::pixelMetricFromSystemDp(QStyle::PixelMetric pm, const QStyleOption *, const QWidget *widget)
{
#if defined(Q_OS_WIN)
+ // The pixel metrics are in device indepentent pixels;
+ // hardcode DPI to 1x 96 DPI.
+ const int dpi = 96;
+
switch (pm) {
case QStyle::PM_DockWidgetFrameWidth:
- return GetSystemMetrics(SM_CXFRAME);
-
- case QStyle::PM_TitleBarHeight:
- if (widget && (widget->windowType() == Qt::Tool)) {
- // MS always use one less than they say
- return GetSystemMetrics(SM_CYSMCAPTION) - 1;
- }
- return GetSystemMetrics(SM_CYCAPTION) - 1;
+ return GetSystemMetricsForDpi(SM_CXFRAME, dpi);
+
+ case QStyle::PM_TitleBarHeight: {
+ const int resizeBorderThickness =
+ GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) + GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
+ if (widget && (widget->windowType() == Qt::Tool))
+ return GetSystemMetricsForDpi(SM_CYSMCAPTION, dpi) + resizeBorderThickness;
+ return GetSystemMetricsForDpi(SM_CYCAPTION, dpi) + resizeBorderThickness;
+ }
case QStyle::PM_ScrollBarExtent:
{
NONCLIENTMETRICS ncm;
- ncm.cbSize = FIELD_OFFSET(NONCLIENTMETRICS, lfMessageFont) + sizeof(LOGFONT);
- if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0))
+ ncm.cbSize = sizeof(NONCLIENTMETRICS);
+ if (SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0, dpi))
return qMax(ncm.iScrollHeight, ncm.iScrollWidth);
}
break;
case QStyle::PM_MdiSubWindowFrameWidth:
- return GetSystemMetrics(SM_CYFRAME);
+ return GetSystemMetricsForDpi(SM_CYFRAME, dpi);
default:
break;
@@ -369,25 +355,10 @@ static QScreen *screenOf(const QWidget *w)
}
// Calculate the overall scale factor to obtain Qt Device Independent
-// Pixels from a native Windows size. Divide by devicePixelRatio
-// and account for secondary screens with differing logical DPI.
+// Pixels from a native Windows size.
qreal QWindowsStylePrivate::nativeMetricScaleFactor(const QWidget *widget)
{
- const QPlatformScreen *screen = screenOf(widget)->handle();
- const qreal scale = screen ? (screen->logicalDpi().first / screen->logicalBaseDpi().first)
- : QWindowsStylePrivate::appDevicePixelRatio();
- qreal result = qreal(1) / scale;
- if (QGuiApplicationPrivate::screen_list.size() > 1) {
- const QScreen *primaryScreen = QGuiApplication::primaryScreen();
- const QScreen *screen = screenOf(widget);
- if (screen != primaryScreen) {
- const qreal primaryLogicalDpi = primaryScreen->handle()->logicalDpi().first;
- const qreal logicalDpi = screen->handle()->logicalDpi().first;
- if (!qFuzzyCompare(primaryLogicalDpi, logicalDpi))
- result *= logicalDpi / primaryLogicalDpi;
- }
- }
- return result;
+ return qreal(1) / QHighDpiScaling::factor(screenOf(widget));
}
/*!
@@ -397,7 +368,7 @@ int QWindowsStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QW
{
int ret = QWindowsStylePrivate::pixelMetricFromSystemDp(pm, opt, widget);
if (ret != QWindowsStylePrivate::InvalidMetric)
- return qRound(qreal(ret) * QWindowsStylePrivate::nativeMetricScaleFactor(widget));
+ return ret;
ret = QWindowsStylePrivate::fixedPixelMetric(pm);
if (ret != QWindowsStylePrivate::InvalidMetric)
@@ -407,7 +378,7 @@ int QWindowsStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QW
switch (pm) {
case PM_MaximumDragDistance:
- ret = QCommonStyle::pixelMetric(PM_MaximumDragDistance);
+ ret = QCommonStyle::pixelMetric(PM_MaximumDragDistance, opt, widget);
if (ret == -1)
ret = 60;
break;
@@ -464,46 +435,6 @@ int QWindowsStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QW
QPixmap QWindowsStyle::standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt,
const QWidget *widget) const
{
-#if defined(Q_OS_WIN)
- QPixmap desktopIcon;
- switch(standardPixmap) {
- case SP_DriveCDIcon:
- case SP_DriveDVDIcon:
- case SP_DriveNetIcon:
- case SP_DriveHDIcon:
- case SP_DriveFDIcon:
- case SP_FileIcon:
- case SP_FileLinkIcon:
- case SP_DirLinkIcon:
- case SP_DirClosedIcon:
- case SP_DesktopIcon:
- case SP_ComputerIcon:
- case SP_DirOpenIcon:
- case SP_FileDialogNewFolder:
- case SP_DirHomeIcon:
- case SP_TrashIcon:
- case SP_VistaShield:
- if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
- QPlatformTheme::StandardPixmap sp = static_cast<QPlatformTheme::StandardPixmap>(standardPixmap);
- desktopIcon = theme->standardPixmap(sp, QSizeF(16, 16));
- }
- break;
- case SP_MessageBoxInformation:
- case SP_MessageBoxWarning:
- case SP_MessageBoxCritical:
- case SP_MessageBoxQuestion:
- if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
- QPlatformTheme::StandardPixmap sp = static_cast<QPlatformTheme::StandardPixmap>(standardPixmap);
- desktopIcon = theme->standardPixmap(sp, QSizeF());
- }
- break;
- default:
- break;
- }
- if (!desktopIcon.isNull()) {
- return desktopIcon;
- }
-#endif // Q_OS_WIN
return QCommonStyle::standardPixmap(standardPixmap, opt, widget);
}
@@ -514,9 +445,14 @@ int QWindowsStyle::styleHint(StyleHint hint, const QStyleOption *opt, const QWid
int ret = 0;
switch (hint) {
- case SH_EtchDisabledText:
- ret = d_func()->isDarkMode() ? 0 : 1;
+ case SH_EtchDisabledText: {
+ const QPalette pal = opt ? opt->palette
+ : widget ? widget->palette()
+ : QPalette();
+ ret = pal.window().color().lightness() > pal.text().color().lightness()
+ ? 1 : 0;
break;
+ }
case SH_Slider_SnapToValue:
case SH_PrintDialog_RightAlignButtons:
case SH_FontDialog_SelectAssociatedText:
@@ -571,12 +507,12 @@ int QWindowsStyle::styleHint(StyleHint hint, const QStyleOption *opt, const QWid
ret = 1;
}
}
-#ifndef QT_NO_ACCESSIBILITY
+#if QT_CONFIG(accessibility)
if (!ret && opt && opt->type == QStyleOption::SO_MenuItem
&& QStyleHelper::isInstanceOf(opt->styleObject, QAccessible::MenuItem)
&& opt->styleObject->property("_q_showUnderlined").toBool())
ret = 1;
-#endif // QT_NO_ACCESSIBILITY
+#endif // QT_CONFIG(accessibility)
break;
}
#endif // Q_OS_WIN
@@ -820,7 +756,7 @@ void QWindowsStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt,
}
#endif // QT_CONFIG(itemviews)
if (!(opt->state & State_Off)) {
- QPointF points[6];
+ std::array<QPointF, 6> points;
qreal scaleh = opt->rect.width() / 12.0;
qreal scalev = opt->rect.height() / 12.0;
points[0] = { opt->rect.x() + qreal(3.5) * scaleh, opt->rect.y() + qreal(5.5) * scalev };
@@ -831,7 +767,7 @@ void QWindowsStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt,
points[5] = { points[4].x() - 4 * scaleh, points[4].y() + 4 * scalev };
p->setPen(QPen(opt->palette.text().color(), 0));
p->setBrush(opt->palette.text().color());
- p->drawPolygon(points, 6);
+ p->drawPolygon(points.data(), static_cast<int>(points.size()));
}
if (doRestore)
p->restore();
@@ -1583,11 +1519,8 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
default:
break;
}
- if (opt->direction == Qt::RightToLeft){ //reverse layout changes the order of Beginning/end
- bool tmp = paintLeftBorder;
- paintRightBorder=paintLeftBorder;
- paintLeftBorder=tmp;
- }
+ if (opt->direction == Qt::RightToLeft) //reverse layout changes the order of Beginning/end
+ std::swap(paintLeftBorder, paintRightBorder);
break;
case Qt::RightToolBarArea :
switch (toolbar->positionOfLine){
@@ -1799,7 +1732,7 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
titleRect.height(), titleRect.width());
}
proxy()->drawItemText(p, titleRect,
- Qt::AlignLeft | Qt::AlignVCenter, palette,
+ Qt::AlignLeft | Qt::AlignVCenter | Qt::TextHideMnemonic, palette,
dwOpt->state & State_Enabled, dwOpt->title,
floating ? (active ? QPalette::BrightText : QPalette::Window) : QPalette::WindowText);
p->setFont(oldFont);
@@ -1962,39 +1895,36 @@ void QWindowsStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComp
QSliderDirection dir;
if (orient == Qt::Horizontal)
- if (tickAbove)
- dir = SlUp;
- else
- dir = SlDown;
+ dir = tickAbove ? SlUp : SlDown;
else
- if (tickAbove)
- dir = SlLeft;
- else
- dir = SlRight;
-
- QPolygon a;
+ dir = tickAbove ? SlLeft : SlRight;
+ std::array<QPoint, 5> points;
int d = 0;
switch (dir) {
case SlUp:
- y1 = y1 + wi/2;
+ y1 = y1 + wi / 2;
d = (wi + 1) / 2 - 1;
- a.setPoints(5, x1,y1, x1,y2, x2,y2, x2,y1, x1+d,y1-d);
+ points = {QPoint(x1, y1), QPoint(x1, y2), QPoint(x2, y2),
+ QPoint(x2, y1), QPoint(x1 + d, y1 - d)};
break;
case SlDown:
- y2 = y2 - wi/2;
+ y2 = y2 - wi / 2;
d = (wi + 1) / 2 - 1;
- a.setPoints(5, x1,y1, x1,y2, x1+d,y2+d, x2,y2, x2,y1);
+ points = {QPoint(x1, y1), QPoint(x1, y2), QPoint(x1 + d, y2 + d),
+ QPoint(x2, y2), QPoint(x2, y1)};
break;
case SlLeft:
d = (he + 1) / 2 - 1;
- x1 = x1 + he/2;
- a.setPoints(5, x1,y1, x1-d,y1+d, x1,y2, x2,y2, x2,y1);
+ x1 = x1 + he / 2;
+ points = {QPoint(x1, y1), QPoint(x1 - d, y1 + d), QPoint(x1,y2),
+ QPoint(x2, y2), QPoint(x2, y1)};
break;
case SlRight:
d = (he + 1) / 2 - 1;
- x2 = x2 - he/2;
- a.setPoints(5, x1,y1, x1,y2, x2,y2, x2+d,y1+d, x2,y1);
+ x2 = x2 - he / 2;
+ points = {QPoint(x1, y1), QPoint(x1, y2), QPoint(x2, y2),
+ QPoint(x2 + d, y1 + d), QPoint(x2, y1)};
break;
}
@@ -2004,7 +1934,7 @@ void QWindowsStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComp
Qt::BGMode oldMode = p->backgroundMode();
p->setBackgroundMode(Qt::OpaqueMode);
p->drawRect(x1, y1, x2-x1+1, y2-y1+1);
- p->drawPolygon(a);
+ p->drawPolygon(points.data(), static_cast<int>(points.size()));
p->setBrush(oldBrush);
p->setBackgroundMode(oldMode);