Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
2144{
2146 return logicalRect;
2147 QRect rect = logicalRect;
2148 rect.translate(2 * (boundingRect.right() - logicalRect.right()) +
2149 logicalRect.width() - boundingRect.width(), 0);
2150 return rect;
2151}
2152
2163{
2165 return logicalPos;
2166 return QPoint(boundingRect.right() - logicalPos.x(), logicalPos.y());
2167}
2168
2174{
2176 int x = rectangle.x();
2177 int y = rectangle.y();
2178 int w = size.width();
2179 int h = size.height();
2181 y += rectangle.size().height()/2 - h/2;
2183 y += rectangle.size().height() - h;
2185 x += rectangle.size().width() - w;
2187 x += rectangle.size().width()/2 - w/2;
2188 return QRect(x, y, w, h);
2189}
2190
2206
2222int QStyle::sliderPositionFromValue(int min, int max, int logicalValue, int span, bool upsideDown)
2223{
2224 if (span <= 0 || max <= min)
2225 return 0;
2226 if (logicalValue < min)
2227 return upsideDown ? span : 0;
2228 if (logicalValue > max)
2229 return upsideDown ? 0 : span;
2230
2231 const uint range = qint64(max) - min;
2232 const uint p = upsideDown ? qint64(max) - logicalValue : qint64(logicalValue) - min;
2233
2234 if (range > (uint)INT_MAX/4096) {
2235 double dpos = (double(p))/(double(range)/span);
2236 return int(dpos);
2237 } else if (range > (uint)span) {
2238 return (2 * p * span + range) / (2*range);
2239 } else {
2240 uint div = span / range;
2241 uint mod = span % range;
2242 return p * div + (2 * p * mod + range) / (2 * range);
2243 }
2244 // equiv. to (p * span) / range + 0.5
2245 // no overflow because of this implicit assumption:
2246 // span <= 4096
2247}
2248
2267int QStyle::sliderValueFromPosition(int min, int max, int pos, int span, bool upsideDown)
2268{
2269 if (span <= 0 || pos <= 0)
2270 return upsideDown ? max : min;
2271 if (pos >= span)
2272 return upsideDown ? min : max;
2273
2274 const qint64 range = qint64(max) - min;
2275
2276 if ((uint)span > range) {
2277 const int tmp = (2 * range * pos + span) / (qint64(2) * span);
2278 return upsideDown ? max - tmp : tmp + min;
2279 } else {
2280 const qint64 div = range / span;
2281 const qint64 mod = range % span;
2282 const int tmp = pos * div + (2 * mod * pos + span) / (qint64(2) * span);
2283 return upsideDown ? max - tmp : tmp + min;
2284 }
2285 // equiv. to min + (pos*range)/span + 0.5
2286 // no overflow because of this implicit assumption:
2287 // pos <= span < sqrt(INT_MAX+0.0625)+0.25 ~ sqrt(INT_MAX)
2288}
2289
2302{
2303 QColor background = QColor(0xd4, 0xd0, 0xc8); // win 2000 grey
2304
2305 QColor light(background.lighter());
2306 QColor dark(background.darker());
2307 QColor mid(Qt::gray);
2308 QPalette palette(Qt::black, background, light, dark, mid, Qt::black, Qt::white);
2310 palette.setBrush(QPalette::Disabled, QPalette::Text, dark);
2312 palette.setBrush(QPalette::Disabled, QPalette::Base, background);
2313 return palette;
2314}
2315
2371int QStyle::combinedLayoutSpacing(QSizePolicy::ControlTypes controls1,
2372 QSizePolicy::ControlTypes controls2, Qt::Orientation orientation,
2374{
2377 int count1 = unpackControlTypes(controls1, array1);
2378 int count2 = unpackControlTypes(controls2, array2);
2379 int result = -1;
2380
2381 for (int i = 0; i < count1; ++i) {
2382 for (int j = 0; j < count2; ++j) {
2383 int spacing = layoutSpacing(array1[i], array2[j], orientation, option, widget);
2385 }
2386 }
2387 return result;
2388}
2389
2400const QStyle * QStyle::proxy() const
2401{
2402 Q_D(const QStyle);
2403 return d->proxyStyle == this ? this : d->proxyStyle->proxy();
2404}
2405
2406/* \internal
2407
2408 This function sets the base style that style calls will be
2409 redirected to. Note that ownership is not transferred. \a style
2410 must be a valid pointer (not nullptr).
2411*/
2412void QStyle::setProxy(QStyle *style)
2413{
2414 Q_D(QStyle);
2415 Q_ASSERT(style);
2416 d->proxyStyle = style;
2417}
2418
2419//Windows and KDE allow menus to cover the taskbar, while GNOME and macOS don't
2421{
2423 return theme && theme->themeHint(QPlatformTheme::UseFullScreenForPopupMenu).toBool();
2424}
2425
2427
2428#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:103
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:748
const QBrush & light() const
Returns the light brush of the current color group.
Definition qpalette.h:85
@ Disabled
Definition qpalette.h:49
@ ButtonText
Definition qpalette.h:52
@ WindowText
Definition qpalette.h:51
\inmodule QtGui
Definition qpen.h:28
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:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr void getRect(int *x, int *y, int *w, int *h) const
Extracts the position of the rectangle's top-left corner to *x and *y, and its dimensions to *width a...
Definition qrect.h:338
QRect intersected(const QRect &other) const noexcept
Definition qrect.h:415
constexpr int x() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:185
constexpr QSize size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:242
constexpr void translate(int dx, int dy) noexcept
Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position.
Definition qrect.h:245
constexpr int y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:188
\inmodule QtCore
Definition qsize.h:208
constexpr qreal width() const noexcept
Returns the width.
Definition qsize.h:332
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
The QStyleOption class stores the parameters used by QStyle functions.
static bool useFullScreenForPopup()
Definition qstyle.cpp:2420
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:2301
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:2202
@ SH_EtchDisabledText
Definition qstyle.h:585
@ SH_DitherDisabledText
Definition qstyle.h:586
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:796
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:2173
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:2222
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:2143
int combinedLayoutSpacing(QSizePolicy::ControlTypes controls1, QSizePolicy::ControlTypes controls2, Qt::Orientation orientation, QStyleOption *option=nullptr, QWidget *widget=nullptr) const
Definition qstyle.cpp:2371
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:2267
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:2400
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:2162
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:146
@ AlignBottom
Definition qnamespace.h:154
@ AlignVCenter
Definition qnamespace.h:155
@ AlignHCenter
Definition qnamespace.h:148
@ AlignLeft
Definition qnamespace.h:144
LayoutDirection
@ LeftToRight
Orientation
Definition qnamespace.h:98
@ gray
Definition qnamespace.h:33
@ white
Definition qnamespace.h:31
@ black
Definition qnamespace.h:30
@ Dense5Pattern
Q_DECL_CONST_FUNCTION QT_POPCOUNT_CONSTEXPR uint qPopulationCount(quint32 v) noexcept
#define Q_STATIC_ASSERT_X(Condition, Message)
Definition qassert.h:111
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
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:34
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
widget render & pixmap
QPainter painter(this)
[7]