Skip to content

Commit

Permalink
chore: 更新滚动条样式,适配动画
Browse files Browse the repository at this point in the history
更新滚动条样式,适配动画

Log:
  • Loading branch information
Whale107 committed Aug 1, 2024
1 parent cc748a7 commit 7cd7a77
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
107 changes: 105 additions & 2 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <QTableView>
#include <QStyledItemDelegate>
#include <QVariantAnimation>
#include <QProgressBar>
#include <QTimer>
#include <DSpinBox>
#include <DTreeView>
#include <DIconButton>
Expand Down Expand Up @@ -1422,7 +1424,17 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
frameRadius = qMin(height / 2, 4);
}
p->setBrush(getColor(opt, DPalette::ObviousBackground, w));
p->setPen(Qt::NoPen);
p->drawRoundedRect(opt->rect, frameRadius, frameRadius);

QPen pen;
pen.setColor(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? QColor(255, 255, 255, 0.15 * 255)
: QColor(0, 0, 0, 0.15 * 255));
pen.setWidth(1);
p->setPen(pen);
p->setBrush(Qt::NoBrush);
p->drawRoundedRect(opt->rect.marginsRemoved(QMargins(1, 1, 1, 1)), frameRadius, frameRadius);
}
return;
}
Expand Down Expand Up @@ -1462,15 +1474,33 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
linear.setColorAt(0, startColor);
linear.setColorAt(1, endColor);
linear.setSpread(QGradient::PadSpread);
p->setBrush(QBrush(linear));
p->setBrush(startColor);

if (progBar->textVisible) {
QPainterPath pathRect;
pathRect.addRect(rect);
QPainterPath pathRoundRect;
pathRoundRect.addRoundedRect(opt->rect, frameRadius, frameRadius);
QPainterPath inter = pathRoundRect.intersected(pathRect);

QPainterPath clipPath;
clipPath.addRoundedRect(rect, frameRadius, frameRadius);
p->setClipPath(clipPath);
p->setClipping(true);
p->drawPath(inter);
p->setClipping(false);

QPen pen;
pen.setColor(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? QColor(255, 255, 255, 0.3 * 255)
: QColor(0, 0, 0, 0.3 * 255));
pen.setWidth(1);
p->setPen(pen);
p->setBrush(Qt::NoBrush);
p->setClipping(true);
p->drawRoundedRect(rect.marginsRemoved(QMargins(1, 1, 1, 1)), frameRadius, frameRadius);
p->setClipping(false);

} else {
//进度条高度 <= 8px && 进度条宽度 <= 8px && value有效
if (rect.height() <= ProgressBar_MinimumStyleHeight &&
Expand All @@ -1496,8 +1526,70 @@ void ChameleonStyle::drawControl(QStyle::ControlElement element, const QStyleOpt
path2.arcTo(endRect, 90, -180);
p->drawPath(path2);
}
} else
} else {
QPainterPath clipPath;
clipPath.addRoundedRect(opt->rect, frameRadius, frameRadius);
p->setClipPath(clipPath);
p->setClipping(true);
p->drawRoundedRect(rect, frameRadius, frameRadius);
p->setClipping(false);

QPen pen;
pen.setColor(QColor(0, 0, 0, 0.3 * 255));
pen.setWidth(1);
p->setPen(pen);
p->setBrush(Qt::NoBrush);
p->setClipping(true);
p->drawRoundedRect(rect.marginsRemoved(QMargins(1, 1, 1, 1)), frameRadius, frameRadius);
p->setClipping(false);
}
}

// 进度条光斑
int spotWidth = 200;
if (rect.width() >= spotWidth) {
p->setPen(Qt::NoPen);

QObject *progressbar = opt->styleObject;
if (!progressbar)
progressbar = dynamic_cast<QObject *>(p->device());

if (m_progressAni.isEmpty() || !m_progressAni.value(progressbar))
return;

QColor shadowColor(0, 0, 0, int(0.15 * 255));
QColor spotColor(255, 255, 255, int(0.5 * 255));
QColor highLightColor(getColor(opt, DPalette::Highlight));

QPointF pointStart(m_progressAni.value(progressbar)->currentValue().toRect().left(), m_progressAni.value(progressbar)->currentValue().toRect().center().y());
QPointF pointEnd(m_progressAni.value(progressbar)->currentValue().toRect().right(), m_progressAni.value(progressbar)->currentValue().toRect().center().y());
QLinearGradient linear(pointStart, pointEnd);
linear.setColorAt(0, highLightColor);
linear.setColorAt(0.35, shadowColor);
linear.setColorAt(0.5, spotColor);
linear.setColorAt(0.65, shadowColor);
linear.setColorAt(1, highLightColor);
linear.setSpread(QGradient::PadSpread);
linear.setInterpolationMode(QLinearGradient::InterpolationMode::ColorInterpolation);
p->setBrush(linear);

QPainterPath clipPath;
clipPath.addRoundedRect(rect, frameRadius, frameRadius);
p->setClipping(true);
p->drawRect(m_progressAni.value(progressbar)->currentValue().toRect().marginsRemoved(QMargins(1, 1, 1, 1)));
p->setClipping(false);

if (m_progressAni.value(progressbar)->state() == QVariantAnimation::Running)
return;

QRect startRect(rect.x() - spotWidth, rect.y(), spotWidth, rect.height());
m_progressAni.value(progressbar)->setStartValue(startRect);
QRect endRect = startRect;
endRect.moveRight(rect.width() + spotWidth);
m_progressAni.value(progressbar)->setEndValue(endRect);
m_progressAni.value(progressbar)->setDuration(2500);
m_progressAni.value(progressbar)->setEasingCurve(QEasingCurve::InQuad);
m_progressAni.value(progressbar)->start();
}
}
return;
Expand Down Expand Up @@ -4357,6 +4449,17 @@ void ChameleonStyle::resetAttribute(QWidget *w, bool polish)
scrollbar->setProperty("_d_dtk_scrollbar_visible", true);
scrollbar->setAttribute(Qt::WA_OpaquePaintEvent, !polish);
}

if (auto progressBar = qobject_cast<QProgressBar *>(w)) {
m_progressAni.insert(progressBar, new QVariantAnimation(progressBar));
connect(m_progressAni.value(progressBar), &QVariantAnimation::valueChanged, progressBar, [progressBar](){
progressBar->update();
});

connect(progressBar, &QObject::destroyed, this, [this]() {
m_progressAni.clear();
});
}
}

static void updateWeekendTextFormat(QCalendarWidget *calendar, QColor)
Expand Down
1 change: 1 addition & 0 deletions styleplugins/chameleon/chameleonstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class ChameleonStyle : public DStyle

private:
mutable QHash<const QObject*, dstyle::DStyleAnimation*> animations;
mutable QHash<const QObject*, QVariantAnimation*> m_progressAni;
};
} // namespace chameleon

Expand Down

0 comments on commit 7cd7a77

Please sign in to comment.