From 7b1b77402d260392f4b73a8ea42adbab2cc51a3d Mon Sep 17 00:00:00 2001 From: deepin-ci-robot Date: Wed, 23 Oct 2024 05:55:21 +0000 Subject: [PATCH] sync: from linuxdeepin/dtkwidget Synchronize source files from linuxdeepin/dtkwidget. Source-pull-request: https://github.com/linuxdeepin/dtkwidget/pull/614 --- include/widgets/dstyle.h | 7 + src/widgets/dbuttonbox.cpp | 45 ++++-- src/widgets/dfloatingmessage.cpp | 31 ++-- src/widgets/dindeterminateprogressbar.cpp | 23 ++- src/widgets/dlabel.cpp | 172 +++++++++------------- src/widgets/dlistview.cpp | 2 +- src/widgets/dmessagemanager.cpp | 84 ++++++++--- src/widgets/dsearchedit.cpp | 79 ++++++---- src/widgets/dstyle.cpp | 54 +++++-- src/widgets/dswitchbutton.cpp | 12 +- 10 files changed, 310 insertions(+), 199 deletions(-) diff --git a/include/widgets/dstyle.h b/include/widgets/dstyle.h index 75573379..f9bc5c05 100644 --- a/include/widgets/dstyle.h +++ b/include/widgets/dstyle.h @@ -19,6 +19,13 @@ QT_BEGIN_NAMESPACE class QTextLayout; QT_END_NAMESPACE +#define ENABLE_ANIMATIONS (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATIONS")) +#define ENABLE_ANIMATION_BUTTONBOX (!qEnvironmentVariableIsSet("DTK_DISABLE_ANIMATION_BUTTONBOX")) +#define ENABLE_ANIMATION_MESSAGE (!qEnvironmentVariableIsSet("DTK_DISABLE_MESSAGE")) +#define ENABLE_ANIMATION_LISTVIEWBOUNCE (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIREBOUNCE")) +#define ENABLE_ANIMATION_SEARCH (!qEnvironmentVariableIsSet("DTK_DISABLE_SEARCH")) +#define ENABLE_ANIMATION_SWITCHBUTTON (!qEnvironmentVariableIsSet("DTK_DISABLE_SWITCHBUTTON")) +#define ENABLE_ANIMATION_PROGRESSBAR (!qEnvironmentVariableIsSet("DTK_DISABLE_PROGRESSBAR")) DWIDGET_BEGIN_NAMESPACE diff --git a/src/widgets/dbuttonbox.cpp b/src/widgets/dbuttonbox.cpp index d90cf735..f14aeac6 100644 --- a/src/widgets/dbuttonbox.cpp +++ b/src/widgets/dbuttonbox.cpp @@ -299,7 +299,10 @@ void DButtonBoxButton::paintEvent(QPaintEvent *e) DStylePainter p(this); DStyleOptionButtonBoxButton option; initStyleOption(&option); - option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color()); + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) + option.palette.setColor(QPalette::HighlightedText, this->palette().highlight().color()); + p.drawControl(DStyle::CE_ButtonBoxButton, option); } @@ -363,10 +366,13 @@ DButtonBoxPrivate::DButtonBoxPrivate(DButtonBox *qq) , m_hoverId(-1) , m_checkedId(-1) , m_pressId(-1) - , m_hoverAnimation(new QVariantAnimation(qq)) - , m_checkMoveAnimation(new QVariantAnimation(qq)) + , m_hoverAnimation(nullptr) + , m_checkMoveAnimation(nullptr) { - + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) { + m_hoverAnimation = new QVariantAnimation(qq); + m_checkMoveAnimation = new QVariantAnimation(qq); + } } void DButtonBoxPrivate::init() @@ -380,14 +386,17 @@ void DButtonBoxPrivate::init() q->connect(group, SIGNAL(buttonPressed(QAbstractButton*)), q, SIGNAL(buttonPressed(QAbstractButton*))); q->connect(group, SIGNAL(buttonReleased(QAbstractButton*)), q, SIGNAL(buttonReleased(QAbstractButton*))); q->connect(group, SIGNAL(buttonToggled(QAbstractButton*, bool)), q, SIGNAL(buttonToggled(QAbstractButton*, bool))); - q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() { - q->update(); - }); - q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() { - q->update(); - }); - m_hoverAnimation->setDuration(HOVER_ANI_DURATION); - m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION); + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) { + q->connect(m_hoverAnimation, &QVariantAnimation::valueChanged, q, [q]() { + q->update(); + }); + q->connect(m_checkMoveAnimation, &QVariantAnimation::valueChanged, q, [q]() { + q->update(); + }); + m_hoverAnimation->setDuration(HOVER_ANI_DURATION); + m_checkMoveAnimation->setDuration(CHECK_ANI_DURATION); + } layout = new QHBoxLayout(q); layout->setContentsMargins(0, 0, 0, 0); @@ -506,7 +515,9 @@ void DButtonBox::setButtonList(const QList &list, bool check d->group->addButton(button); button->setCheckable(checkable); - button->installEventFilter(this); + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) + button->installEventFilter(this); } } @@ -616,6 +627,11 @@ void DButtonBox::paintEvent(QPaintEvent *e) opt.state |= QStyle::State_Active; } + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX) { + p.drawControl(QStyle::CE_PushButtonBevel, opt); + return; + } + bool isDarkType = DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType; int radius = DStyle::pixelMetric(style(), DStyle::PM_FrameRadius); QColor background; @@ -706,6 +722,9 @@ void DButtonBox::paintEvent(QPaintEvent *e) bool DButtonBox::eventFilter(QObject *o, QEvent *e) { + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX) + return QWidget::eventFilter(o, e);; + D_D(DButtonBox); for (int i = 0; i < buttonList().size(); ++i) { if (o == buttonList().at(i)) { diff --git a/src/widgets/dfloatingmessage.cpp b/src/widgets/dfloatingmessage.cpp index c581f289..fd3aaa48 100644 --- a/src/widgets/dfloatingmessage.cpp +++ b/src/widgets/dfloatingmessage.cpp @@ -55,17 +55,23 @@ void DFloatingMessagePrivate::init() iconButton->setIconSize(DSizeModeHelper::element(QSize(20, 20), QSize(30, 30))); hBoxLayout->addWidget(iconButton); - hBoxLayout->addSpacing(10); + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) + hBoxLayout->addSpacing(10); + hBoxLayout->addWidget(labMessage); if (notifyType == DFloatingMessage::MessageType::TransientType) { //临时消息 timer = new QTimer(q); timer->setInterval(4000); timer->setSingleShot(true); - q->connect(timer, &QTimer::timeout, q, [q]() { - q->close(); - Q_EMIT q->messageClosed(); - }); + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) + q->connect(timer, &QTimer::timeout, q, [q]() { + q->close(); + Q_EMIT q->messageClosed(); + }); + else + q->connect(timer, &QTimer::timeout, q, &DFloatingMessage::close); } else { //常驻消息 content = nullptr; closeButton = new DDialogCloseButton(q); @@ -74,12 +80,19 @@ void DFloatingMessagePrivate::init() hBoxLayout->addWidget(closeButton); q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::closeButtonClicked); - q->connect(closeButton, &DIconButton::clicked, q, [q]() { - q->close(); - Q_EMIT q->messageClosed(); - }); + + if(ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) + q->connect(closeButton, &DIconButton::clicked, q, [q]() { + q->close(); + Q_EMIT q->messageClosed(); + }); + else + q->connect(closeButton, &DIconButton::clicked, q, &DFloatingMessage::close); } + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE) + return; + auto effect = new QGraphicsDropShadowEffect(q); effect->setColor(QColor(0, 0, 0, 0.1 * 255)); effect->setBlurRadius(20); diff --git a/src/widgets/dindeterminateprogressbar.cpp b/src/widgets/dindeterminateprogressbar.cpp index e0d04d4d..7835a590 100644 --- a/src/widgets/dindeterminateprogressbar.cpp +++ b/src/widgets/dindeterminateprogressbar.cpp @@ -22,9 +22,14 @@ DIndeterminateProgressbarPrivate::DIndeterminateProgressbarPrivate(DIndeterminat , m_sliderWidget(new QWidget(qq)) , m_timer(new QTimer(qq)) , m_leftToRight(true) - , m_spotWidget(new QWidget(qq)) - , m_animation(new QPropertyAnimation(m_spotWidget, "pos", qq)) + , m_spotWidget(nullptr) + , m_animation(nullptr) { + if (!ENABLE_ANIMATIONS && !ENABLE_ANIMATION_PROGRESSBAR) + return; + + m_spotWidget = new QWidget(qq); + m_animation = new QPropertyAnimation(m_spotWidget, "pos", qq); } DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent) @@ -32,8 +37,11 @@ DIndeterminateProgressbar::DIndeterminateProgressbar(QWidget *parent) , DObject(*new DIndeterminateProgressbarPrivate(this)) { D_D(DIndeterminateProgressbar); - d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height()); - d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0); + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_PROGRESSBAR) { + d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height()); + d->m_spotWidget->move(-SPOT_WIDGET_WIDTH, 0); + } d->m_sliderWidget->setFixedWidth(150); d->m_sliderWidget->move(0, 0); @@ -60,6 +68,10 @@ void DIndeterminateProgressbar::resizeEvent(QResizeEvent *e) { D_D(DIndeterminateProgressbar); d->m_sliderWidget->setFixedHeight(height()); + + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR) + QWidget::resizeEvent(e); + d->m_spotWidget->setFixedSize(SPOT_WIDGET_WIDTH, height()); d->m_animation->setStartValue(QPoint(-SPOT_WIDGET_WIDTH, 0)); @@ -111,6 +123,9 @@ void DIndeterminateProgressbar::paintEvent(QPaintEvent *e) p.setPen(pen); p.drawRoundedRect(d->m_sliderWidget->geometry(), radius, radius); + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_PROGRESSBAR) + return; + if (d->m_sliderWidget->width() < d->m_spotWidget->width() / 2) return; diff --git a/src/widgets/dlabel.cpp b/src/widgets/dlabel.cpp index 8b03aaa5..5b5310f9 100644 --- a/src/widgets/dlabel.cpp +++ b/src/widgets/dlabel.cpp @@ -28,7 +28,8 @@ DWIDGET_BEGIN_NAMESPACE \a parent The argument is sent to the QLabel constructor. */ DLabel::DLabel(QWidget *parent, Qt::WindowFlags f) - : QLabel(parent, f), DObject(*new DLabelPrivate(this)) + : QLabel(parent, f) + , DObject(*new DLabelPrivate(this)) { D_D(DLabel); d->init(); @@ -41,7 +42,8 @@ DLabel::DLabel(QWidget *parent, Qt::WindowFlags f) \a parent Specifying the parent object. */ DLabel::DLabel(const QString &text, QWidget *parent) - : QLabel(text, parent), DObject(*new DLabelPrivate(this)) + : QLabel(text, parent) + , DObject(*new DLabelPrivate(this)) { D_D(DLabel); d->init(); @@ -49,6 +51,7 @@ DLabel::DLabel(const QString &text, QWidget *parent) DLabel::~DLabel() { + } /*! @@ -83,8 +86,7 @@ void DLabel::setForegroundRole(DPalette::ColorType color) void DLabel::setElideMode(Qt::TextElideMode elideMode) { D_D(DLabel); - if (d->elideMode == elideMode) - { + if (d->elideMode == elideMode) { return; } d->elideMode = elideMode; @@ -109,7 +111,8 @@ Qt::TextElideMode DLabel::elideMode() const \a parent Parent control */ DLabel::DLabel(DLabelPrivate &dd, QWidget *parent) - : QLabel(parent), DObject(dd) + : QLabel(parent) + , DObject(dd) { dd.init(); } @@ -123,8 +126,7 @@ void DLabel::initPainter(QPainter *painter) const { D_DC(DLabel); QLabel::initPainter(painter); - if (d->color != DPalette::NoType) - { + if (d->color != DPalette::NoType) { QBrush color = DPaletteHelper::instance()->palette(this).brush(d->color); painter->setPen(QPen(color.color())); } @@ -139,19 +141,17 @@ void DLabel::initPainter(QPainter *painter) const void DLabel::paintEvent(QPaintEvent *event) { Q_UNUSED(event) - QLabelPrivate *d = static_cast(d_ptr.data()); + QLabelPrivate *d = static_cast(d_ptr.data()); QStyle *style = QWidget::style(); QPainter painter(this); drawFrame(&painter); QRect cr = contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); int align = QStyle::visualAlignment(d->isTextLabel ? DLabelPrivate::textDirection(d) - : layoutDirection(), - QFlag(d->align)); + : layoutDirection(), QFlag(d->align)); #if QT_CONFIG(movie) - if (d->movie && !d->movie->currentPixmap().isNull()) - { + if (d->movie && !d->movie->currentPixmap().isNull()) { if (d->scaledcontents) style->drawItemPixmap(&painter, cr, align, d->movie->currentPixmap().scaled(cr.size())); else @@ -159,18 +159,16 @@ void DLabel::paintEvent(QPaintEvent *event) } else #endif - if (d->isTextLabel) - { + if (d->isTextLabel) { QRectF lr = DLabelPrivate::layoutRect(d).toAlignedRect(); QStyleOption opt; opt.initFrom(this); - if (d->control) - { + if (d->control) { #ifndef QT_NO_SHORTCUT const bool underline = (bool)style->styleHint(QStyle::SH_UnderlineShortcut, 0, this, 0); - if (d->shortcutId != 0 && underline != d->shortcutCursor.charFormat().fontUnderline()) - { + if (d->shortcutId != 0 + && underline != d->shortcutCursor.charFormat().fontUnderline()) { QTextCharFormat fmt; fmt.setFontUnderline(underline); d->shortcutCursor.mergeCharFormat(fmt); @@ -181,12 +179,9 @@ void DLabel::paintEvent(QPaintEvent *event) // Adjust the palette context.palette = opt.palette; - if (d_func()->color != DPalette::NoType) - { + if (d_func()->color != DPalette::NoType) { context.palette.setBrush(QPalette::Text, DPaletteHelper::instance()->palette(this).brush(d_func()->color)); - } - else if (foregroundRole() != QPalette::Text && isEnabled()) - { + } else if (foregroundRole() != QPalette::Text && isEnabled()) { context.palette.setColor(QPalette::Text, context.palette.color(foregroundRole())); } @@ -196,13 +191,10 @@ void DLabel::paintEvent(QPaintEvent *event) d->control->setPalette(context.palette); d->control->drawContents(&painter, QRectF(), this); painter.restore(); - } - else - { + } else { int flags = align | (DLabelPrivate::textDirection(d) == Qt::LeftToRight ? Qt::TextForceLeftToRight : Qt::TextForceRightToLeft); - if (d->hasShortcut) - { + if (d->hasShortcut) { flags |= Qt::TextShowMnemonic; if (!style->styleHint(QStyle::SH_UnderlineShortcut, &opt, this)) flags |= Qt::TextHideMnemonic; @@ -210,26 +202,22 @@ void DLabel::paintEvent(QPaintEvent *event) QPalette palette = opt.palette; - if (d_func()->color != DPalette::NoType) - { + if (d_func()->color != DPalette::NoType) { palette.setBrush(foregroundRole(), DPaletteHelper::instance()->palette(this).brush(d_func()->color)); } QString text = d->text; - if (elideMode() != Qt::ElideNone) - { + if (elideMode() != Qt::ElideNone) { const QFontMetrics fm(fontMetrics()); text = fm.elidedText(text, elideMode(), width(), Qt::TextShowMnemonic); } const DToolTip::ToolTipShowMode &toolTipShowMode = DToolTip::toolTipShowMode(this); - if (toolTipShowMode != DToolTip::Default) - { - const bool showToolTip = (toolTipShowMode == DToolTip::AlwaysShow) || ((toolTipShowMode == DToolTip::ShowWhenElided) && (d->text != text)); - if (DToolTip::needUpdateToolTip(this, showToolTip)) - { + if (toolTipShowMode != DToolTip::Default) { + const bool showToolTip = (toolTipShowMode == DToolTip::AlwaysShow) + || ((toolTipShowMode == DToolTip::ShowWhenElided) && (d->text != text)); + if (DToolTip::needUpdateToolTip(this, showToolTip)) { QString toolTip; - if (showToolTip) - { + if (showToolTip) { QTextOption textOption; textOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); textOption.setTextDirection(opt.direction); @@ -242,47 +230,38 @@ void DLabel::paintEvent(QPaintEvent *event) } style->drawItemText(&painter, lr.toRect(), flags, palette, isEnabled(), text, foregroundRole()); } - } - else + } else #ifndef QT_NO_PICTURE - if (d->picture) - { + if (d->picture) { QRect br = d->picture->boundingRect(); int rw = br.width(); int rh = br.height(); - if (d->scaledcontents) - { + if (d->scaledcontents) { painter.save(); painter.translate(cr.x(), cr.y()); - painter.scale((double)cr.width() / rw, (double)cr.height() / rh); + painter.scale((double)cr.width()/rw, (double)cr.height()/rh); painter.drawPicture(-br.x(), -br.y(), *d->picture); painter.restore(); - } - else - { + } else { int xo = 0; int yo = 0; if (align & Qt::AlignVCenter) - yo = (cr.height() - rh) / 2; + yo = (cr.height()-rh)/2; else if (align & Qt::AlignBottom) - yo = cr.height() - rh; + yo = cr.height()-rh; if (align & Qt::AlignRight) - xo = cr.width() - rw; + xo = cr.width()-rw; else if (align & Qt::AlignHCenter) - xo = (cr.width() - rw) / 2; - painter.drawPicture(cr.x() + xo - br.x(), cr.y() + yo - br.y(), *d->picture); + xo = (cr.width()-rw)/2; + painter.drawPicture(cr.x()+xo-br.x(), cr.y()+yo-br.y(), *d->picture); } - } - else + } else #endif - if (d->pixmap && !d->pixmap->isNull()) - { + if (d->pixmap && !d->pixmap->isNull()) { QPixmap pix; - if (d->scaledcontents) - { + if (d->scaledcontents) { QSize scaledSize = cr.size() * devicePixelRatioF(); - if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) - { + if (!d->scaledpixmap || d->scaledpixmap->size() != scaledSize) { #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) if (!d->cachedimage) #endif @@ -291,21 +270,17 @@ void DLabel::paintEvent(QPaintEvent *event) delete d->scaledpixmap; #else #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) - d->cachedimage = QImage(d->pixmap->toImage()); + d->cachedimage = QImage(d->pixmap->toImage()); #endif d->scaledpixmap.reset(); #endif #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) QImage scaledImage = d->cachedimage->scaled(scaledSize, -#else - d->scaledpixmap.reset(); - d->scaledpixmap = d->pixmap->scaled(scaledSize, -#endif Qt::IgnoreAspectRatio, Qt::SmoothTransformation); #else - d->scaledpixmap = d->pixmap->scaled(scaledSize, - Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + d->scaledpixmap = d->pixmap->scaled(scaledSize, + Qt::IgnoreAspectRatio, Qt::SmoothTransformation); #endif #if QT_VERSION < QT_VERSION_CHECK(6, 4, 2) d->scaledpixmap = new QPixmap(QPixmap::fromImage(scaledImage)); @@ -317,8 +292,7 @@ void DLabel::paintEvent(QPaintEvent *event) d->scaledpixmap->setDevicePixelRatio(devicePixelRatioF()); } pix = *d->scaledpixmap; - } - else + } else pix = *d->pixmap; QStyleOption opt; opt.initFrom(this); @@ -335,12 +309,12 @@ DLabelPrivate::DLabelPrivate(DLabel *q) void DLabelPrivate::init() { + } Qt::LayoutDirection DLabelPrivate::textDirection(QLabelPrivate *d) { - if (d->control) - { + if (d->control) { QTextOption opt = d->control->document()->defaultTextOption(); return opt.textDirection(); } @@ -350,18 +324,16 @@ Qt::LayoutDirection DLabelPrivate::textDirection(QLabelPrivate *d) QRectF DLabelPrivate::documentRect(QLabelPrivate *d) { - QLabel *q = qobject_cast(d->q_ptr); + QLabel *q = qobject_cast(d->q_ptr); Q_ASSERT_X(d->isTextLabel, "documentRect", "document rect called for label that is not a text label!"); QRect cr = q->contentsRect(); cr.adjust(d->margin, d->margin, -d->margin, -d->margin); const int align = QStyle::visualAlignment(d->isTextLabel ? textDirection(d) - : q->layoutDirection(), - QFlag(d->align)); + : q->layoutDirection(), QFlag(d->align)); int m = d->indent; if (m < 0 && q->frameWidth()) // no indent, but we do have a frame m = q->fontMetrics().horizontalAdvance(QLatin1Char('x')) / 2 - d->margin; - if (m > 0) - { + if (m > 0) { if (align & Qt::AlignLeft) cr.setLeft(cr.left() + m); if (align & Qt::AlignRight) @@ -384,64 +356,56 @@ QRectF DLabelPrivate::layoutRect(QLabelPrivate *d) qreal rh = d->control->document()->documentLayout()->documentSize().height(); qreal yo = 0; if (d->align & Qt::AlignVCenter) - yo = qMax((cr.height() - rh) / 2, qreal(0)); + yo = qMax((cr.height()-rh)/2, qreal(0)); else if (d->align & Qt::AlignBottom) - yo = qMax(cr.height() - rh, qreal(0)); + yo = qMax(cr.height()-rh, qreal(0)); return QRectF(cr.x(), yo + cr.y(), cr.width(), cr.height()); } void DLabelPrivate::ensureTextLayouted(QLabelPrivate *d) { - if (d->textLayoutDirty) - { - if (d->textDirty) - { - if (d->control) - { + if (d->textLayoutDirty) { + if (d->textDirty) { + if (d->control) { QTextDocument *doc = d->control->document(); - if (d->textDirty) - { -#ifndef QT_NO_TEXTHTMLPARSER -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + if (d->textDirty) { + #ifndef QT_NO_TEXTHTMLPARSER + #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) if (d->textformat == Qt::TextFormat::RichText) -#else + #else if (d->isRichText) -#endif + #endif doc->setHtml(d->text); else doc->setPlainText(d->text); -#else + #else doc->setPlainText(d->text); -#endif + #endif doc->setUndoRedoEnabled(false); -#ifndef QT_NO_SHORTCUT - if (d->hasShortcut) - { + #ifndef QT_NO_SHORTCUT + if (d->hasShortcut) { // Underline the first character that follows an ampersand (and remove the others ampersands) int from = 0; bool found = false; QTextCursor cursor; - while (!(cursor = d->control->document()->find((QLatin1String("&")), from)).isNull()) - { + while (!(cursor = d->control->document()->find((QLatin1String("&")), from)).isNull()) { cursor.deleteChar(); // remove the ampersand cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); from = cursor.position(); - if (!found && cursor.selectedText() != QLatin1String("&")) - { // not a second & + if (!found && cursor.selectedText() != QLatin1String("&")) { //not a second & found = true; d->shortcutCursor = cursor; } } } -#endif + #endif } } d->textDirty = false; } - if (d->control) - { + if (d->control) { QTextDocument *doc = d->control->document(); QTextOption opt = doc->defaultTextOption(); diff --git a/src/widgets/dlistview.cpp b/src/widgets/dlistview.cpp index 8b7bff3d..d1b6ffd6 100644 --- a/src/widgets/dlistview.cpp +++ b/src/widgets/dlistview.cpp @@ -198,7 +198,7 @@ DListView::DListView(QWidget *parent) : DObject(*new DListViewPrivate(this)) { d_func()->init(); - if (!qEnvironmentVariableIsSet("DTK_DISABLE_LISTVIEW_ANIMATION")) { + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) { auto ani = new DBounceAnimation(this); ani->setAnimationTarget(this); ani->setAniMationEnable(true); diff --git a/src/widgets/dmessagemanager.cpp b/src/widgets/dmessagemanager.cpp index f5147241..43830e5c 100644 --- a/src/widgets/dmessagemanager.cpp +++ b/src/widgets/dmessagemanager.cpp @@ -122,11 +122,19 @@ static void sendMessage_helper(DMessageManager *manager, QWidget *par, IconType DMessageManagerPrivate::DMessageManagerPrivate(DMessageManager *qq) : DObjectPrivate(qq) - , m_aniGeometry(new QPropertyAnimation(qq)) - , m_aniOpacity(new QPropertyAnimation(qq)) - , m_aniGroup(new QParallelAnimationGroup(qq)) - , m_label(new ImageLabel) + , m_aniGeometry(nullptr) + , m_aniOpacity(nullptr) + , m_aniGroup(nullptr) + , m_label(nullptr) { + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE) + return; + + m_aniGeometry = new QPropertyAnimation(qq); + m_aniOpacity = new QPropertyAnimation(qq); + m_aniGroup = new QParallelAnimationGroup(qq); + m_label = new ImageLabel; + m_aniGeometry->setPropertyName("geometry"); m_aniGeometry->setDuration(ANIMATION_DURATION); m_aniGeometry->setEasingCurve(QEasingCurve::OutCubic); @@ -187,14 +195,20 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg) layout->setContentsMargins(0, 0, 0, 0); layout->setDirection(QBoxLayout::BottomToTop); } - - if (content->layout()->count() >= 1) { - content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide(); - delete content->layout()->takeAt(content->layout()->count() - 1); + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) { + if (content->layout()->count() >= 1) { + content->layout()->itemAt(content->layout()->count() - 1)->widget()->hide(); + delete content->layout()->takeAt(content->layout()->count() - 1); + } + } else { + content->show(); } static_cast(content->layout())->addWidget(floMsg, 0, Qt::AlignHCenter); + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_MESSAGE) + return; + // 限制通知消息的最大宽度 for (DFloatingMessage *message : content->findChildren(QString(), Qt::FindDirectChildrenOnly)) { message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width()); @@ -214,7 +228,10 @@ void DMessageManager::sendMessage(QWidget *par, DFloatingMessage *floMsg) d->m_label->setParent(par); d->m_label->setAlignment(Qt::AlignCenter); d->m_label->setContentsMargins(MARGIN, 0, MARGIN, 0); - d->m_label->setPixmap(floMsg->grab()); + + if (floMsg && !floMsg->grab().isNull()) + d->m_label->setPixmap(floMsg->grab()); + d->m_label->setScaledContents(true); d->m_label->show(); d->m_aniGeometry->setTargetObject(d->m_label); @@ -287,20 +304,47 @@ bool DMessageManager::setContentMargens(QWidget *par, const QMargins &margins) */ bool DMessageManager::eventFilter(QObject *watched, QEvent *event) { - if (event->type() == QEvent::Resize) { - if (auto content = watched->findChild(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) { + bool isOnlyResizeEvent = ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE ? event->type() == QEvent::Resize : event->type() == QEvent::LayoutRequest || event->type() == QEvent::Resize; - auto par = qobject_cast(watched); + if (isOnlyResizeEvent) { + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) { + if (auto content = watched->findChild(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly)) { - for (DFloatingMessage *message : content->findChildren(QString(), Qt::FindDirectChildrenOnly)) { - message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width()); - message->setMinimumHeight(message->sizeHint().height()); - } + auto par = qobject_cast(watched); - QRect geometry(QPoint(0, 0), content->sizeHint()); - geometry.moveCenter(par->rect().center()); - geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT); - content->setGeometry(geometry); + for (DFloatingMessage *message : content->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width()); + message->setMinimumHeight(message->sizeHint().height()); + } + + QRect geometry(QPoint(0, 0), content->sizeHint()); + geometry.moveCenter(par->rect().center()); + geometry.moveBottom(par->rect().bottom() - MESSGAE_HEIGHT); + content->setGeometry(geometry); + } + } else { + if (QWidget *widget = qobject_cast(watched)) { + QWidget *content; + + if (widget->objectName() == D_MESSAGE_MANAGER_CONTENT) { + content = widget; + } else { + content = widget->findChild(D_MESSAGE_MANAGER_CONTENT, Qt::FindDirectChildrenOnly); + } + + QWidget *par = content->parentWidget(); + + // 限制通知消息的最大宽度 + for (DFloatingMessage *message : content->findChildren(QString(), Qt::FindDirectChildrenOnly)) { + message->setMaximumWidth(par->rect().marginsRemoved(content->contentsMargins()).width()); + message->setMinimumHeight(message->sizeHint().height()); + } + + QRect geometry(QPoint(0, 0), content->sizeHint()); + geometry.moveCenter(par->rect().center()); + geometry.moveBottom(par->rect().bottom()); + content->setGeometry(geometry); + } } } else if (event->type() == QEvent::ChildRemoved) { // 如果是通知消息被删除的事件 diff --git a/src/widgets/dsearchedit.cpp b/src/widgets/dsearchedit.cpp index 35acfdcd..734c14de 100644 --- a/src/widgets/dsearchedit.cpp +++ b/src/widgets/dsearchedit.cpp @@ -273,11 +273,14 @@ DSearchEditPrivate::DSearchEditPrivate(DSearchEdit *q) , action(nullptr) , iconWidget(nullptr) , label(nullptr) - , animation(new QPropertyAnimation) + , animation(nullptr) { - animation->setPropertyName("pos"); - animation->setEasingCurve(QEasingCurve::OutCubic); - animation->setDuration(ANI_DURATION); + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) { + animation = new QPropertyAnimation; + animation->setPropertyName("pos"); + animation->setEasingCurve(QEasingCurve::OutCubic); + animation->setDuration(ANI_DURATION); + } } DSearchEditPrivate::~DSearchEditPrivate() @@ -384,31 +387,58 @@ void DSearchEditPrivate::_q_toEditMode(bool focus) { D_Q(DSearchEdit); - if (animation->state() == QPropertyAnimation::Running) - return; + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SEARCH) { + if (animation->state() == QPropertyAnimation::Running) + return; - auto textMargins = q->lineEdit()->textMargins(); - QMargins marginsInAnimation(HIDE_CURSOR_MARGIN, 0, 0, 0); + auto textMargins = q->lineEdit()->textMargins(); + QMargins marginsInAnimation(HIDE_CURSOR_MARGIN, 0, 0, 0); + + if (!animation->parent()) + animation->setParent(iconWidget); + + animation->setTargetObject(iconWidget); + animation->setStartValue(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y())); + animation->setEndValue(QPoint(10, iconWidget->pos().y())); + + q->connect(animation, &QPropertyAnimation::finished, q, [q, this, textMargins]() { + q->lineEdit()->setTextMargins(textMargins); + if (animation->direction() == QPropertyAnimation::Direction::Forward) { + iconWidget->setVisible(false); + action->setVisible(true); + lineEdit->setPlaceholderText(placeholderText); + } else { + iconWidget->setVisible(true); + lineEdit->setPlaceholderText(QString()); + iconWidget->move(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y())); + } + }); - if (!animation->parent()) - animation->setParent(iconWidget); + if (!q->lineEdit()->text().isEmpty()) + return; - animation->setTargetObject(iconWidget); - animation->setStartValue(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y())); - animation->setEndValue(QPoint(0, iconWidget->pos().y())); + if (focus) { + animation->setDirection(QPropertyAnimation::Direction::Forward); + } else { + action->setVisible(false); + animation->setDirection(QPropertyAnimation::Direction::Backward); + } - q->connect(animation, &QPropertyAnimation::finished, q, [q, this, textMargins]() { - q->lineEdit()->setTextMargins(textMargins); - if (animation->direction() == QPropertyAnimation::Direction::Forward) { + iconWidget->setVisible(true); + q->lineEdit()->setTextMargins(marginsInAnimation); + animation->start(); + } else { + if (focus || !q->lineEdit()->text().isEmpty()) { action->setVisible(true); iconWidget->setVisible(false); lineEdit->setPlaceholderText(placeholderText); } else { + action->setVisible(false); iconWidget->setVisible(true); lineEdit->setPlaceholderText(QString()); - iconWidget->move(QPoint(q->lineEdit()->geometry().center().x() - iconWidget->width() / 2, iconWidget->pos().y())); } - }); + + } #ifdef ENABLE_AI //Focus disappears, clear voice check @@ -418,19 +448,6 @@ void DSearchEditPrivate::_q_toEditMode(bool focus) } #endif - if (!q->lineEdit()->text().isEmpty()) - return; - - if (focus) { - animation->setDirection(QPropertyAnimation::Direction::Forward); - } else { - action->setVisible(false); - animation->setDirection(QPropertyAnimation::Direction::Backward); - } - - iconWidget->setVisible(true); - q->lineEdit()->setTextMargins(marginsInAnimation); - animation->start(); } void DSearchEditPrivate::_q_onVoiceActionTrigger(bool checked) diff --git a/src/widgets/dstyle.cpp b/src/widgets/dstyle.cpp index d1fd1160..349e5336 100644 --- a/src/widgets/dstyle.cpp +++ b/src/widgets/dstyle.cpp @@ -1339,18 +1339,27 @@ void DStyle::drawPrimitive(const QStyle *style, DStyle::PrimitiveElement pe, con //先绘画阴影 DDrawUtils::drawShadow(p, btn->rect + shadow_margin, frameRadius, frameRadius, QColor(0, 0, 0, 0.25 * 255), shadowRadius, QPoint(offsetX, offsetY)); //再绘画上面的待显示区域 + + if(ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) { + p->setPen(Qt::NoPen); + } else { + p->setPen(QPen(btn->dpalette.frameShadowBorder(), 1)); + } + p->setPen(Qt::NoPen); p->setBrush(btn->noBackground ? Qt::NoBrush : p->background()); p->drawRoundedRect(opt->rect, frameRadius, frameRadius); - p->setBrush(Qt::NoBrush); - QPen pen; - pen.setWidth(1); - pen.setColor(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType - ? QColor(255, 255, 255, int(0.1 * 255)) - : QColor(0, 0, 0, int(0.12 * 255))); - p->setPen(pen); - p->drawRoundedRect(opt->rect, frameRadius, frameRadius); + if(ENABLE_ANIMATIONS && ENABLE_ANIMATION_MESSAGE) { + p->setBrush(Qt::NoBrush); + QPen pen; + pen.setWidth(1); + pen.setColor(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType + ? QColor(255, 255, 255, int(0.1 * 255)) + : QColor(0, 0, 0, int(0.12 * 255))); + p->setPen(pen); + p->drawRoundedRect(opt->rect, frameRadius, frameRadius); + } } break; } @@ -1453,6 +1462,12 @@ void DStyle::drawControl(const QStyle *style, DStyle::ControlElement ce, const Q option.dpalette = btn->dpalette; option.rect = dstyle.subElementRect(SE_SwitchButtonGroove, opt, w); + if(!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_SWITCHBUTTON) { + dstyle.drawPrimitive(PE_SwitchButtonGroove, &option, p, w); + option.rect = dstyle.subElementRect(SE_SwitchButtonHandle, opt, w); + dstyle.drawPrimitive(PE_SwitchButtonHandle, &option, p, w); + } + if (btn->state & State_HasFocus) { QStyleOptionFocusRect fropt; fropt.QStyleOption::operator=(*btn); @@ -1493,6 +1508,10 @@ void DStyle::drawControl(const QStyle *style, DStyle::ControlElement ce, const Q case CE_ButtonBoxButton: { if (const DStyleOptionButton *btn = qstyleoption_cast(opt)) { DStyleHelper dstyle(style); + + if (!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_BUTTONBOX) + dstyle.drawControl(CE_ButtonBoxButtonBevel, btn, p, w); + DStyleOptionButton subopt = *btn; if (btn->features & DStyleOptionButton::HasDciIcon) subopt.dciIcon = btn->dciIcon; @@ -1503,7 +1522,10 @@ void DStyle::drawControl(const QStyle *style, DStyle::ControlElement ce, const Q DStyleOptionButtonBoxButton fropt; fropt = *boxbtn; fropt.rect = dstyle.subElementRect(SE_ButtonBoxButtonFocusRect, btn, w); - fropt.position = DStyleOptionButtonBoxButton::OnlyOne; + + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) + fropt.position = DStyleOptionButtonBoxButton::OnlyOne; + style->drawPrimitive(PE_FrameFocusRect, &fropt, p, w); } } @@ -1578,12 +1600,14 @@ void DStyle::drawControl(const QStyle *style, DStyle::ControlElement ce, const Q break; } case CE_ButtonBoxButtonLabel: { - if (opt->state & StateFlag::State_MouseOver) { - constexpr qreal hoverScale = 1.2; // hover 时放大1.2倍 - p->scale(hoverScale, hoverScale); - p->setRenderHint(QPainter::SmoothPixmapTransform); - p->translate((1 - hoverScale) * (opt->rect.x() + opt->rect.width() / 2) / hoverScale - , (1 - hoverScale) * (opt->rect.y() + opt->rect.height() / 2) / hoverScale); + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_BUTTONBOX) { + if (opt->state & StateFlag::State_MouseOver) { + constexpr qreal hoverScale = 1.2; // hover 时放大1.2倍 + p->scale(hoverScale, hoverScale); + p->setRenderHint(QPainter::SmoothPixmapTransform); + p->translate((1 - hoverScale) * (opt->rect.x() + opt->rect.width() / 2) / hoverScale + , (1 - hoverScale) * (opt->rect.y() + opt->rect.height() / 2) / hoverScale); + } } style->drawControl(CE_PushButtonLabel, opt, p, w); diff --git a/src/widgets/dswitchbutton.cpp b/src/widgets/dswitchbutton.cpp index 8935c0c0..b0625ac1 100644 --- a/src/widgets/dswitchbutton.cpp +++ b/src/widgets/dswitchbutton.cpp @@ -60,8 +60,10 @@ void DSwitchButton::paintEvent(QPaintEvent *e) initStyleOption(&opt); painter.drawControl(DStyle::CE_SwitchButton, opt); - painter.setRenderHint(QPainter::SmoothPixmapTransform); - painter.drawImage(rect().adjusted(4, -8, -4, 8), d->player.currentImage()); // 为了显示按钮的阴影所留的空白 + if (ENABLE_ANIMATIONS && ENABLE_ANIMATION_SWITCHBUTTON) { + painter.setRenderHint(QPainter::SmoothPixmapTransform); + painter.drawImage(rect().adjusted(4, -8, -4, 8), d->player.currentImage()); // 为了显示按钮的阴影所留的空白 + } } /*! @@ -128,6 +130,12 @@ void DSwitchButtonPrivate::init() q->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); q->setCheckable(true); + + if(!ENABLE_ANIMATIONS || !ENABLE_ANIMATION_SWITCHBUTTON) { + q->connect(q, &DSwitchButton::toggled, q, &DSwitchButton::checkedChanged); + return; + } + auto initPlayer= [this, q]() { DDciIcon icon = !checked ? DDciIcon::fromTheme("switch_on") : DDciIcon::fromTheme("switch_off"); player.setIcon(icon);