Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qstyle.h"
5#include "qapplication.h"
6#include "qpainter.h"
7#include "qwidget.h"
8#include "qbitmap.h"
9#include "qpixmapcache.h"
10#include "qstyleoption.h"
11#include "private/qstyle_p.h"
12#include "private/qguiapplication_p.h"
13#include <qpa/qplatformtheme.h>
14#ifndef QT_NO_DEBUG
15#include "qdebug.h"
16#endif
17
18#include <limits.h>
19#include <algorithm>
20
22
23static const int MaxBits = 8 * sizeof(QSizePolicy::ControlType);
24
25static int unpackControlTypes(QSizePolicy::ControlTypes controls, QSizePolicy::ControlType *array)
26{
27 if (!controls)
28 return 0;
29
30 // optimization: exactly one bit is set
31 if (qPopulationCount(uint(controls)) == 1) {
32 array[0] = QSizePolicy::ControlType(uint(controls));
33 return 1;
34 }
35
36 int count = 0;
37 for (int i = 0; i < MaxBits; ++i) {
38 if (uint bit = (controls & (0x1 << i)))
40 }
41 return count;
42}
43
360 : QObject(*new QStylePrivate)
361{
362 Q_D(QStyle);
363 d->proxyStyle = this;
364}
365
372 : QObject(dd)
373{
374 Q_D(QStyle);
375 d->proxyStyle = this;
378 "StandardPixmap in QPlatformTheme and QStyle out of sync");
379}
380
385{
386}
387
397{
398 Q_D(const QStyle);
399 return d->name;
400}
401
406void QStyle::setName(const QString &name)
407{
408 Q_D(QStyle);
409 d->name = name;
410}
411
436void QStyle::polish(QWidget * /* widget */)
437{
438}
439
455void QStyle::unpolish(QWidget * /* widget */)
456{
457}
458
466{
467}
468
476{
477}
478
488void QStyle::polish(QPalette & /* pal */)
489{
490}
491
511 const QString &text) const
512{
514 int x, y, w, h;
515 rect.getRect(&x, &y, &w, &h);
516 if (!text.isEmpty()) {
517 result = metrics.boundingRect(x, y, w, h, alignment, text);
519 result.setWidth(result.width()+1);
520 result.setHeight(result.height()+1);
521 }
522 } else {
523 result = QRect(x, y, w, h);
524 }
525 return result;
526}
527
535{
537 int x, y, w, h;
538 rect.getRect(&x, &y, &w, &h);
539
540 QSizeF pixmapSize = pixmap.deviceIndependentSize();
541 const int pixmapWidth = pixmapSize.width();
542 const int pixmapHeight = pixmapSize.height();
543
545 y += h/2 - pixmapHeight/2;
547 y += h - pixmapHeight;
549 x += w - pixmapWidth;
551 x += w/2 - pixmapWidth/2;
553 x += w - pixmapWidth;
554 result = QRect(x, y, pixmapWidth, pixmapHeight);
555 return result;
556}
557
575 bool enabled, const QString& text, QPalette::ColorRole textRole) const
576{
577 if (text.isEmpty())
578 return;
579 QPen savedPen;
580 if (textRole != QPalette::NoRole) {
581 savedPen = painter->pen();
582 painter->setPen(QPen(pal.brush(textRole), savedPen.widthF()));
583 }
584 if (!enabled) {
586 QRect br;
589 return;
590 } else if (proxy()->styleHint(SH_EtchDisabledText)) {
591 QPen pen = painter->pen();
592 painter->setPen(pal.light().color());
593 painter->drawText(rect.adjusted(1, 1, 1, 1), alignment, text);
594 painter->setPen(pen);
595 }
596 }
598 if (textRole != QPalette::NoRole)
599 painter->setPen(savedPen);
600}
601
613 const QPixmap &pixmap) const
614{
615 qreal scale = pixmap.devicePixelRatio();
617 QRect inter = aligned.intersected(rect);
618
619 painter->drawPixmap(inter.x(), inter.y(), pixmap, inter.x() - aligned.x(), inter.y() - aligned.y(), qRound(inter.width() * scale), qRound(inter.height() *scale));
620}
621
2145{
2147 return logicalRect;
2148 QRect rect = logicalRect;
2149 rect.translate(2 * (boundingRect.right() - logicalRect.right()) +
2150 logicalRect.width() - boundingRect.width(), 0);
2151 return rect;
2152}
2153
2164{
2166 return logicalPos;
2167 return QPoint(boundingRect.right() - logicalPos.x(), logicalPos.y());
2168}
2169
2175{
2177 int x = rectangle.x();
2178 int y = rectangle.y();
2179 int w = size.width();
2180 int h = size.height();
2182 y += rectangle.size().height()/2 - h/2;
2184 y += rectangle.size().height() - h;
2186 x += rectangle.size().width() - w;
2188 x += rectangle.size().width()/2 - w/2;
2189 return QRect(x, y, w, h);
2190}
2191
2204{
2206}
2207
2223int QStyle::sliderPositionFromValue(int min, int max, int logicalValue, int span, bool upsideDown)
2224{
2225 if (span <= 0 || max <= min)
2226 return 0;
2227 if (logicalValue < min)
2228 return upsideDown ? span : 0;
2229 if (logicalValue > max)
2230 return upsideDown ? 0 : span;
2231
2232 const uint range = qint64(max) - min;
2233 const uint p = upsideDown ? qint64(max) - logicalValue : qint64(logicalValue) - min;
2234
2235 if (range > (uint)INT_MAX/4096) {
2236 double dpos = (double(p))/(double(range)/span);
2237 return int(dpos);
2238 } else if (range > (uint)span) {
2239 return (2 * p * span + range) / (2*range);
2240 } else {
2241 uint div = span / range;
2242 uint mod = span % range;
2243 return p * div + (2 * p * mod + range) / (2 * range);
2244 }
2245 // equiv. to (p * span) / range + 0.5
2246 // no overflow because of this implicit assumption:
2247 // span <= 4096
2248}
2249
2268int QStyle::sliderValueFromPosition(int min, int max, int pos, int span, bool upsideDown)
2269{
2270 if (span <= 0 || pos <= 0)
2271 return upsideDown ? max : min;
2272 if (pos >= span)
2273 return upsideDown ? min : max;
2274
2275 const qint64 range = qint64(max) - min;
2276
2277 if ((uint)span > range) {
2278 const int tmp = (2 * range * pos + span) / (qint64(2) * span);
2279 return upsideDown ? max - tmp : tmp + min;
2280 } else {
2281 const qint64 div = range / span;
2282 const qint64 mod = range % span;
2283 const int tmp = pos * div + (2 * mod * pos + span) / (qint64(2) * span);
2284 return upsideDown ? max - tmp : tmp + min;
2285 }
2286 // equiv. to min + (pos*range)/span + 0.5
2287 // no overflow because of this implicit assumption:
2288 // pos <= span < sqrt(INT_MAX+0.0625)+0.25 ~ sqrt(INT_MAX)
2289}
2290
2303{
2304 QColor background = QColor(0xd4, 0xd0, 0xc8); // win 2000 grey
2305
2306 QColor light(background.lighter());
2307 QColor dark(background.darker());
2308 QColor mid(Qt::gray);
2309 QPalette palette(Qt::black, background, light, dark, mid, Qt::black, Qt::white);
2311 palette.setBrush(QPalette::Disabled, QPalette::Text, dark);
2313 palette.setBrush(QPalette::Disabled, QPalette::Base, background);
2314 return palette;
2315}
2316
2372int QStyle::combinedLayoutSpacing(QSizePolicy::ControlTypes controls1,
2373 QSizePolicy::ControlTypes controls2, Qt::Orientation orientation,
2375{
2378 int count1 = unpackControlTypes(controls1, array1);
2379 int count2 = unpackControlTypes(controls2, array2);
2380 int result = -1;
2381
2382 for (int i = 0; i < count1; ++i) {
2383 for (int j = 0; j < count2; ++j) {
2384 int spacing = layoutSpacing(array1[i], array2[j], orientation, option, widget);
2386 }
2387 }
2388 return result;
2389}
2390
2401const QStyle * QStyle::proxy() const
2402{
2403 Q_D(const QStyle);
2404 return d->proxyStyle == this ? this : d->proxyStyle->proxy();
2405}
2406
2407/* \internal
2408
2409 This function sets the base style that style calls will be
2410 redirected to. Note that ownership is not transferred. \a style
2411 must be a valid pointer (not nullptr).
2412*/
2413void QStyle::setProxy(QStyle *style)
2414{
2415 Q_D(QStyle);
2416 Q_ASSERT(style);
2417 d->proxyStyle = style;
2418}
2419
2420//Windows and KDE allow menus to cover the taskbar, while GNOME and macOS don't
2422{
2424 return theme && theme->themeHint(QPlatformTheme::UseFullScreenForPopupMenu).toBool();
2425}
2426
2428
2429#include "moc_qstyle.cpp"
The QApplication class manages the GUI application's control flow and main settings.
\inmodule QtGui
Definition qbrush.h:30
const QColor & color() const
Returns the brush color.
Definition qbrush.h:121
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
QColor darker(int f=200) const noexcept
Definition qcolor.cpp:2857
QColor lighter(int f=150) const noexcept
Definition qcolor.cpp:2812
Definition qflags.h:17
\reentrant \inmodule QtGui
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
static QPlatformTheme * platformTheme()
static bool isRightToLeft()
Returns true if the application's layout direction is Qt::RightToLeft; otherwise returns false.
Qt::LayoutDirection layoutDirection
the default layout direction for this application
\inmodule QtCore
Definition qobject.h:90
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
const QBrush & background() const
Returns the current background brush.
const QPen & pen() const
Returns the painter's current pen.
void setPen(const QColor &color)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void drawText(const QPointF &p, const QString &s)
Draws the given text with the currently defined text direction, beginning at the given position.
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device.
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
The QPalette class contains color groups for each widget state.
Definition qpalette.h:19
const QBrush & brush(ColorGroup cg, ColorRole cr) const
Returns the brush in the specified color group, used for the given color role.
Definition qpalette.cpp:794
const QBrush & light() const
Returns the light brush of the current color group.
Definition qpalette.h:84
@ Disabled
Definition qpalette.h:48
@ ButtonText
Definition qpalette.h:51
@ WindowText
Definition qpalette.h:50
\inmodule QtGui
Definition qpen.h:25
qreal widthF() const
Returns the pen width with floating point precision.
Definition qpen.cpp:598
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
\inmodule QtCore\reentrant
Definition qpoint.h:23
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:127
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:132
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:715
constexpr qreal right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:498
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr int height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:238
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:184
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:241
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:235
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:187
constexpr int right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:178
\inmodule QtCore
Definition qsize.h:207
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:321
constexpr qreal height() const noexcept
Returns the height.
Definition qsize.h:324
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
The QStyleOption class stores the parameters used by QStyle functions.
static bool useFullScreenForPopup()
Definition qstyle.cpp:2421
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.
Definition qstyle.h:29
virtual void polish(QWidget *widget)
Initializes the appearance of the given widget.
Definition qstyle.cpp:436
virtual QRect itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const
Returns the area within the given rectangle in which to draw the specified pixmap according to the de...
Definition qstyle.cpp:534
virtual QPalette standardPalette() const
Returns the style's standard palette.
Definition qstyle.cpp:2302
static Qt::Alignment visualAlignment(Qt::LayoutDirection direction, Qt::Alignment alignment)
Transforms an alignment of Qt::AlignLeft or Qt::AlignRight without Qt::AlignAbsolute into Qt::AlignLe...
Definition qstyle.cpp:2203
@ SH_EtchDisabledText
Definition qstyle.h:583
@ SH_DitherDisabledText
Definition qstyle.h:584
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=nullptr, const QWidget *widget=nullptr, QStyleHintReturn *returnData=nullptr) const =0
Returns an integer representing the specified style hint for the given widget described by the provid...
virtual ~QStyle()
Destroys the style object.
Definition qstyle.cpp:384
virtual void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const
Draws the given pixmap in the specified rectangle, according to the specified alignment,...
Definition qstyle.cpp:612
virtual QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const
Returns the area within the given rectangle in which to draw the provided text according to the speci...
Definition qstyle.cpp:510
virtual int layoutSpacing(QSizePolicy::ControlType control1, QSizePolicy::ControlType control2, Qt::Orientation orientation, const QStyleOption *option=nullptr, const QWidget *widget=nullptr) const =0
@ NStandardPixmap
Definition qstyle.h:794
QStyle()
Constructs a style object.
Definition qstyle.cpp:359
static QRect alignedRect(Qt::LayoutDirection direction, Qt::Alignment alignment, const QSize &size, const QRect &rectangle)
Returns a new rectangle of the specified size that is aligned to the given rectangle according to the...
Definition qstyle.cpp:2174
static int sliderPositionFromValue(int min, int max, int val, int space, bool upsideDown=false)
Converts the given logicalValue to a pixel position.
Definition qstyle.cpp:2223
static QRect visualRect(Qt::LayoutDirection direction, const QRect &boundingRect, const QRect &logicalRect)
Returns the given logicalRectangle converted to screen coordinates based on the specified direction.
Definition qstyle.cpp:2144
int combinedLayoutSpacing(QSizePolicy::ControlTypes controls1, QSizePolicy::ControlTypes controls2, Qt::Orientation orientation, QStyleOption *option=nullptr, QWidget *widget=nullptr) const
Definition qstyle.cpp:2372
static int sliderValueFromPosition(int min, int max, int pos, int space, bool upsideDown=false)
Converts the given pixel position to a logical value.
Definition qstyle.cpp:2268
QString name() const
Returns the name of the style.
Definition qstyle.cpp:396
virtual void drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal, bool enabled, const QString &text, QPalette::ColorRole textRole=QPalette::NoRole) const
Draws the given text in the specified rectangle using the provided painter and palette.
Definition qstyle.cpp:574
virtual void unpolish(QWidget *widget)
Uninitialize the given {widget}'s appearance.
Definition qstyle.cpp:455
const QStyle * proxy() const
Definition qstyle.cpp:2401
static QPoint visualPos(Qt::LayoutDirection direction, const QRect &boundingRect, const QPoint &logicalPos)
Returns the given logicalPosition converted to screen coordinates based on the specified direction.
Definition qstyle.cpp:2163
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
#define this
Definition dialogs.cpp:9
QOpenGLWidget * widget
[1]
QString text
qreal spacing
rect
[4]
uint alignment
direction
Combined button and popup list for selecting options.
@ AlignRight
Definition qnamespace.h:145
@ AlignBottom
Definition qnamespace.h:153
@ AlignVCenter
Definition qnamespace.h:154
@ AlignHCenter
Definition qnamespace.h:147
@ AlignLeft
Definition qnamespace.h:143
LayoutDirection
@ LeftToRight
Orientation
Definition qnamespace.h:97
@ gray
Definition qnamespace.h:32
@ white
Definition qnamespace.h:30
@ black
Definition qnamespace.h:29
@ Dense5Pattern
Q_DECL_CONST_FUNCTION QT_POPCOUNT_CONSTEXPR uint qPopulationCount(quint32 v) noexcept
#define Q_STATIC_ASSERT_X(Condition, Message)
Definition qassert.h:108
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLsizei range
GLuint name
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLenum array
GLenum GLenum GLsizei void GLsizei void void * span
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLuint GLenum option
GLenum GLenum GLenum GLenum GLenum scale
static const QRectF boundingRect(const QPointF *points, int pointCount)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
Int aligned(Int v, Int byteAlign)
static int unpackControlTypes(QSizePolicy::ControlTypes controls, QSizePolicy::ControlType *array)
Definition qstyle.cpp:25
static QT_BEGIN_NAMESPACE const int MaxBits
Definition qstyle.cpp:23
unsigned int uint
Definition qtypes.h:29
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92
widget render & pixmap
QPainter painter(this)
[7]