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
qquickstyle.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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 "qquickstyle.h"
5#include "qquickstyle_p.h"
6#include "qquickstyleoption.h"
7
8#include <QtGui/qpainter.h>
9#include <QtGui/qbitmap.h>
10#include <QtGui/qpixmapcache.h>
11#include <QtGui/qpa/qplatformtheme.h>
12
13#include <QtGui/private/qguiapplication_p.h>
14
15#ifndef QT_NO_DEBUG
16# include <QtCore/qdebug.h>
17#endif
18
19#include <limits.h>
20#include <algorithm>
21
23
24namespace QQC2 {
25
31{
32 Q_D(QStyle);
33 d->proxyStyle = this;
34}
35
42 : QObject(dd)
43{
44 Q_D(QStyle);
45 d->proxyStyle = this;
46}
47
54
74 const QString &text) const
75{
77 int x, y, w, h;
78 rect.getRect(&x, &y, &w, &h);
79 if (!text.isEmpty()) {
80 result = metrics.boundingRect(x, y, w, h, alignment, text);
82 result.setWidth(result.width()+1);
83 result.setHeight(result.height()+1);
84 }
85 } else {
86 result = QRect(x, y, w, h);
87 }
88 return result;
89}
90
98{
100 int x, y, w, h;
101 rect.getRect(&x, &y, &w, &h);
102
103 const int pixmapWidth = pixmap.width()/pixmap.devicePixelRatio();
104 const int pixmapHeight = pixmap.height()/pixmap.devicePixelRatio();
105
107 y += h/2 - pixmapHeight/2;
109 y += h - pixmapHeight;
111 x += w - pixmapWidth;
113 x += w/2 - pixmapWidth/2;
115 x += w - pixmapWidth;
116 result = QRect(x, y, pixmapWidth, pixmapHeight);
117 return result;
118}
119
137 bool enabled, const QString& text, QPalette::ColorRole textRole) const
138{
139 if (text.isEmpty())
140 return;
141 QPen savedPen;
142 if (textRole != QPalette::NoRole) {
143 savedPen = painter->pen();
144 painter->setPen(QPen(pal.brush(textRole), savedPen.widthF()));
145 }
146 if (!enabled) {
148 QRect br;
151 return;
152 } else if (proxy()->styleHint(SH_EtchDisabledText)) {
153 QPen pen = painter->pen();
154 painter->setPen(pal.light().color());
155 painter->drawText(rect.adjusted(1, 1, 1, 1), alignment, text);
156 painter->setPen(pen);
157 }
158 }
160 if (textRole != QPalette::NoRole)
161 painter->setPen(savedPen);
162}
163
175 const QPixmap &pixmap) const
176{
177 qreal scale = pixmap.devicePixelRatio();
179 QRect inter = aligned.intersected(rect);
180
181 painter->drawPixmap(inter.x(), inter.y(), pixmap, inter.x() - aligned.x(), inter.y() - aligned.y(), inter.width() * scale, inter.height() *scale);
182}
183
198{
200 return logicalRect;
201 QRect rect = logicalRect;
202 rect.translate(2 * (boundingRect.right() - logicalRect.right()) +
203 logicalRect.width() - boundingRect.width(), 0);
204 return rect;
205}
206
217{
219 return logicalPos;
220 return QPoint(boundingRect.right() - logicalPos.x(), logicalPos.y());
221}
222
228{
230 int x = rectangle.x();
231 int y = rectangle.y();
232 int w = size.width();
233 int h = size.height();
235 y += rectangle.size().height()/2 - h/2;
237 y += rectangle.size().height() - h;
239 x += rectangle.size().width() - w;
241 x += rectangle.size().width()/2 - w/2;
242 return QRect(x, y, w, h);
243}
244
260
276int QStyle::sliderPositionFromValue(int min, int max, int logicalValue, int span, bool upsideDown)
277{
278 if (span <= 0 || logicalValue < min || max <= min)
279 return 0;
280 if (logicalValue > max)
281 return upsideDown ? span : min;
282
283 uint range = max - min;
284 uint p = upsideDown ? max - logicalValue : logicalValue - min;
285
286 if (range > (uint)INT_MAX/4096) {
287 double dpos = (double(p))/(double(range)/span);
288 return int(dpos);
289 } else if (range > (uint)span) {
290 return (2 * p * span + range) / (2*range);
291 } else {
292 uint div = span / range;
293 uint mod = span % range;
294 return p * div + (2 * p * mod + range) / (2 * range);
295 }
296 // equiv. to (p * span) / range + 0.5
297 // no overflow because of this implicit assumption:
298 // span <= 4096
299}
300
319int QStyle::sliderValueFromPosition(int min, int max, int pos, int span, bool upsideDown)
320{
321 if (span <= 0 || pos <= 0)
322 return upsideDown ? max : min;
323 if (pos >= span)
324 return upsideDown ? min : max;
325
326 uint range = max - min;
327
328 if ((uint)span > range) {
329 int tmp = (2 * pos * range + span) / (2 * span);
330 return upsideDown ? max - tmp : tmp + min;
331 } else {
332 uint div = range / span;
333 uint mod = range % span;
334 int tmp = pos * div + (2 * pos * mod + span) / (2 * span);
335 return upsideDown ? max - tmp : tmp + min;
336 }
337 // equiv. to min + (pos*range)/span + 0.5
338 // no overflow because of this implicit assumption:
339 // pos <= span < sqrt(INT_MAX+0.0625)+0.25 ~ sqrt(INT_MAX)
340}
341
354{
355 QColor background = QColor(0xd4, 0xd0, 0xc8); // win 2000 grey
356
357 QColor light(background.lighter());
358 QColor dark(background.darker());
359 QColor mid(Qt::gray);
360 QPalette palette(Qt::black, background, light, dark, mid, Qt::black, Qt::white);
364 palette.setBrush(QPalette::Disabled, QPalette::Base, background);
365 return palette;
366}
367
368//Windows and KDE allow menus to cover the taskbar, while GNOME and macOS don't
370{
372 return theme && theme->themeHint(QPlatformTheme::UseFullScreenForPopupMenu).toBool();
373}
374
375} // namespace QQC2
376
378
379#include "moc_qquickstyle.cpp"
\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
static bool useFullScreenForPopup()
const QStyle * proxy() const
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...
virtual QPalette standardPalette() const
Returns the style's standard palette.
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...
virtual ~QStyle()
Destroys the style object.
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,...
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...
QStyle()
Constructs a style object.
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...
static int sliderPositionFromValue(int min, int max, int val, int space, bool upsideDown=false)
Converts the given logicalValue to a pixel position.
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.
static int sliderValueFromPosition(int min, int max, int pos, int space, bool upsideDown=false)
Converts the given pixel position to a logical value.
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt=nullptr, QStyleHintReturn *returnData=nullptr) const =0
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.
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.
\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: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
QString text
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
@ gray
Definition qnamespace.h:33
@ white
Definition qnamespace.h:31
@ black
Definition qnamespace.h:30
@ Dense5Pattern
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLsizei range
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLenum GLenum GLsizei void GLsizei void void * span
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLenum GLenum GLenum GLenum GLenum scale
static const QRectF boundingRect(const QPointF *points, int pointCount)
Int aligned(Int v, Int byteAlign)
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
widget render & pixmap
QPainter painter(this)
[7]