diff options
author | Wladimir Leuschner <wladimir.leuschner@qt.io> | 2023-06-21 14:15:55 +0300 |
---|---|---|
committer | Wladimir Leuschner <wladimir.leuschner@qt.io> | 2023-10-27 16:32:56 +0000 |
commit | 6723515e5b641e228b5b729472ef10d8f19935ae (patch) | |
tree | 9044cd1b4abdfca2c07d334b50a712f8db8a8d65 | |
parent | c0b5c1567b53df0252119495d6ccdf8a5f2eadcf (diff) |
Introduce progressbar in QWindows11Style
Introduces Windows 11 styled progressbar to QWindows11Style drawn by QPainter.
The progress is indicated by a thicker line in accent color on a grey track.
Animation for indetermined progress is changed to follow the animation of Windows 11
progress bars.
Change-Id: Icac79fdbe40b82b10e1fd0e59b389890a7de6683
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
-rw-r--r-- | src/plugins/styles/modernwindows/qwindows11style.cpp | 76 | ||||
-rw-r--r-- | src/plugins/styles/modernwindows/qwindows11style_p.h | 1 |
2 files changed, 76 insertions, 1 deletions
diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index 8477814444..2bb9a4eed8 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -10,6 +10,8 @@ #include <QGraphicsDropShadowEffect> #include "qdrawutil.h" +#include <chrono> + QT_BEGIN_NAMESPACE const static int topLevelRoundingRadius = 8; //Radius for toplevel items like popups for round corners @@ -718,6 +720,80 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op } } break; + case QStyle::CE_ProgressBarGroove:{ + if (const QStyleOptionProgressBar* progbaropt = qstyleoption_cast<const QStyleOptionProgressBar*>(option)) { + const QProgressBar* bar = qobject_cast<const QProgressBar*>(widget); + QRectF rect = subElementRect(SE_ProgressBarContents, progbaropt, widget); + QPointF center = rect.center(); + if (bar->orientation() & Qt::Horizontal) { + rect.setHeight(1); + rect.moveTop(center.y()); + } else { + rect.setWidth(1); + rect.moveLeft(center.x()); + } + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::gray); + painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + } + break; + } + case QStyle::CE_ProgressBarContents: + if (const QStyleOptionProgressBar* progbaropt = qstyleoption_cast<const QStyleOptionProgressBar*>(option)) { + const QProgressBar* bar = qobject_cast<const QProgressBar*>(widget); + QRectF rect = subElementRect(SE_ProgressBarContents, progbaropt, widget); + QRectF originalRect = rect; + QPointF center = rect.center(); + bool isIndeterminate = progbaropt->maximum == 0 && progbaropt->minimum == 0; + float fillPercentage = 0; + + if (!isIndeterminate) { + fillPercentage = ((float(progbaropt->progress) - float(progbaropt->minimum)) / (float(progbaropt->maximum) - float(progbaropt->minimum))); + if (bar->orientation() == Qt::Horizontal) { + rect.setHeight(4); + rect.moveTop(center.y() - 1.5); + rect.setWidth(rect.width() * fillPercentage); + } else { + float oldHeight = rect.height(); + rect.setWidth(4); + rect.moveLeft(center.x() - 1.5); + rect.moveTop(oldHeight * (1.0f - fillPercentage)); + rect.setHeight(oldHeight * fillPercentage); + } + } else { + auto elapsedTime = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()); + fillPercentage = (elapsedTime.time_since_epoch().count() % 5000)/(5000.0f*0.75); + if (bar->orientation() == Qt::Horizontal) { + float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.width(), float(rect.width())); + float barEnd = qMin(fillPercentage * rect.width(), float(rect.width())); + rect = QRect(QPoint(rect.left() + barBegin, rect.top()), QPoint(rect.left() + barEnd, rect.bottom())); + rect.setHeight(4); + rect.moveTop(center.y() - 1.5); + } else { + float barBegin = qMin(qMax(fillPercentage-0.25,0.0) * rect.height(), float(rect.height())); + float barEnd = qMin(fillPercentage * rect.height(), float(rect.height())); + rect = QRect(QPoint(rect.left(), rect.bottom() - barEnd), QPoint(rect.right(), rect.bottom() - barBegin)); + rect.setWidth(4); + rect.moveLeft(center.x() - 1.5); + } + const_cast<QWidget*>(widget)->update(); + } + if (progbaropt->invertedAppearance && bar->orientation() == Qt::Horizontal) + rect.moveLeft(originalRect.width() * (1.0 - fillPercentage)); + else if (progbaropt->invertedAppearance && bar->orientation() == Qt::Vertical) + rect.moveBottom(originalRect.height() * fillPercentage); + painter->setPen(Qt::NoPen); + painter->setBrush(progbaropt->palette.accent()); + painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + } + break; + case QStyle::CE_ProgressBarLabel: + if (const QStyleOptionProgressBar* progbaropt = qstyleoption_cast<const QStyleOptionProgressBar*>(option)) { + QRect rect = subElementRect(SE_ProgressBarLabel, progbaropt, widget); + painter->setPen(progbaropt->palette.text().color()); + painter->drawText(rect, progbaropt->text,Qt::AlignVCenter|Qt::AlignLeft); + } + break; case CE_PushButtonLabel: if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(option)) { QRect textRect = btn->rect; diff --git a/src/plugins/styles/modernwindows/qwindows11style_p.h b/src/plugins/styles/modernwindows/qwindows11style_p.h index 95990958db..3b4a4da410 100644 --- a/src/plugins/styles/modernwindows/qwindows11style_p.h +++ b/src/plugins/styles/modernwindows/qwindows11style_p.h @@ -29,7 +29,6 @@ class QWindows11Style : public QWindowsVistaStyle public: QWindows11Style(); ~QWindows11Style() override; - void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const override; void drawPrimitive(PrimitiveElement element, const QStyleOption *option, |