Skip to content

Commit

Permalink
Also animate busy progressbars without QWidget.
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujan committed Feb 2, 2023
1 parent 359d1ff commit fa386cc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 102 deletions.
1 change: 1 addition & 0 deletions Kvantum/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ V1.0.8
* Limited the extra vertical margins of menu-item icons to 1px.
* Smaller text-icon spacing for editable comboboxes that are drawn as lineedits.
* Added vertical text margins of lineedits to their heights.
* Also animate busy progressbars without `QWidget`.

V1.0.7
---------
Expand Down
2 changes: 1 addition & 1 deletion Kvantum/NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Latest version:

29 Jan 2023, V1.0.8
3 Feb 2023, V1.0.8

See "ChangeLog" for changes.
74 changes: 24 additions & 50 deletions Kvantum/style/Kvantum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ static inline QString getName(const QColor &col)

Style::Style(bool useDark) : QCommonStyle()
{
progressTimer_ = new QTimer(this);
opacityTimer_ = opacityTimerOut_ = nullptr;
animationOpacity_ = animationOpacityOut_ = 100;
animationStartState_ = animationStartStateOut_ = "normal";
Expand Down Expand Up @@ -315,8 +314,6 @@ Style::Style(bool useDark) : QCommonStyle()
ticklessSliderHandleSize_ = -1;
isKisSlider_ = false;

connect(progressTimer_, &QTimer::timeout, this, &Style::advanceProgressbar);

if (tspec_.animate_states)
{
opacityTimer_ = new QTimer(this);
Expand Down Expand Up @@ -428,12 +425,6 @@ Style::~Style()

/* all the following timers have "this" as their parent
but are explicitly deleted here only to be listed */
if (progressTimer_)
{
progressTimer_->stop();
delete progressTimer_;
progressTimer_ = nullptr;
}
if (opacityTimer_)
{
opacityTimer_->stop();
Expand Down Expand Up @@ -831,37 +822,6 @@ void Style::setupThemeDeps()
settings_ = defaultSettings_;
}

void Style::advanceProgressbar()
{
QMap<QWidget*,int>::iterator it;
for (it = progressbars_.begin(); it != progressbars_.end(); ++it)
{
auto widget = it.key();
if (widget && widget->isVisible())
{
if (it.value() > INT_MAX - 2)
it.value() = 0;
else
it.value() += 2;
widget->update();
}
}
}

void Style::forgetProgressBar(QObject *o)
{
if (auto w = qobject_cast<QWidget*>(o))
{
if (progressbars_.contains(w))
{
disconnect(w, &QObject::destroyed, this, &Style::forgetProgressBar);
progressbars_.remove(w);
if (progressbars_.size() == 0)
progressTimer_->stop();
}
}
}

void Style::forgetPopupOrigin(QObject *o)
{
if (auto w = qobject_cast<QWidget*>(o))
Expand Down Expand Up @@ -8030,6 +7990,16 @@ void Style::drawControl(QStyle::ControlElement element,
bool thin = false;
if (opt->maximum != 0 || opt->minimum != 0)
{
if (QObject *styleObject = option->styleObject)
{ // could only have ProgressbarAnimation
if (Animation *animation = animations_.take(styleObject))
{
animation->stop();
delete animation;
animation = nullptr;
}
}

int length = isVertical ? h : w;
int empty = length
- sliderPositionFromValue(opt->minimum,
Expand Down Expand Up @@ -8111,32 +8081,38 @@ void Style::drawControl(QStyle::ControlElement element,
if (thin)
painter->restore();
}
else if (widget)
else if (QObject *styleObject = option->styleObject)
{ // busy progressbar
if (!r.isValid()) return;
QWidget *wi = const_cast<QWidget*>(widget);
int animcount = progressbars_[wi];
ProgressbarAnimation *anim = qobject_cast<ProgressbarAnimation *>(animations_.value(styleObject));
if (!anim)
{
anim = new ProgressbarAnimation(styleObject);
startAnimation(anim);
return;
}
int animPixels = anim->pixels();
int pm = qMin(qMax(pixelMetric(PM_ProgressBarChunkWidth),fspec.left+fspec.right),r.width()/2-2);
if (pm > h
&& h >= fspec.left+fspec.right
&& h >= QFontMetrics(f).height())
{ // make it compact with ordinary progressbars
pm = h;
}
QRect R = r.adjusted(animcount,0,0,0);
QRect R = r.adjusted(animPixels,0,0,0);
if (isVertical)
{
if (inverted)
R.setX(r.x()+r.width()-(animcount % r.width()));
R.setX(r.x()+r.width()-(animPixels % r.width()));
else
R.setX(r.x()+(animcount % r.width()));
R.setX(r.x()+(animPixels % r.width()));
}
else
{
if (inverted)
R.setX(r.x()+r.width()-(animcount % r.width()));
R.setX(r.x()+r.width()-(animPixels % r.width()));
else
R.setX(r.x()+(animcount % r.width()));
R.setX(r.x()+(animPixels % r.width()));
}
int W = !isRounded ? pm : !isVertical ? qMax(h,pm) : qMax(w,pm);
if (W <= 0 || W > r.width()) return;
Expand Down Expand Up @@ -8220,8 +8196,6 @@ void Style::drawControl(QStyle::ControlElement element,
renderInterior(painter,R,fspec,ispec,ispec.element+"-"+status,true);
}
}
else
QCommonStyle::drawControl(element,option,painter,widget);
}

break;
Expand Down
11 changes: 2 additions & 9 deletions Kvantum/style/Kvantum.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2014-2019 <tsujan2000@gmail.com>
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2014-2023 <tsujan2000@gmail.com>
*
* Kvantum is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -432,10 +432,6 @@ class Style : public QCommonStyle {
}

private slots:
/* For busy progress bars. */
void advanceProgressbar();
void forgetProgressBar(QObject *o);

void forgetPopupOrigin(QObject *o);

void forgetMovedMenu(QObject *o);
Expand All @@ -457,17 +453,14 @@ class Style : public QCommonStyle {

QString xdg_config_home;

QTimer *progressTimer_, *opacityTimer_, *opacityTimerOut_;
QTimer *opacityTimer_, *opacityTimerOut_;
mutable int animationOpacity_, animationOpacityOut_; // A value >= 100 stops state change animation.
/* The start state for state change animation */
mutable QString animationStartState_, animationStartStateOut_;
/* The widget whose state change is animated */
QPointer<QWidget> animatedWidget_, animatedWidgetOut_;
QHash<QWidget*, QPointer<QWidget>> popupOrigins_;

/* List of busy progress bars */
QMap<QWidget*,int> progressbars_;

/* List of menus that are moved because of their shadows. */
QSet<const QWidget*> movedMenus_;

Expand Down
23 changes: 22 additions & 1 deletion Kvantum/style/animation/animation.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Adapted from Qt

/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2016-2021 <tsujan2000@gmail.com>
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2016-2023 <tsujan2000@gmail.com>
*
* Kvantum is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -118,6 +118,27 @@ void Animation::updateCurrentTime(int)

/*************************/

ProgressbarAnimation::ProgressbarAnimation(QObject *target) : Animation(target),
pixels_(0)
{
setFrameRate(TwentyFps);
}

int ProgressbarAnimation::pixels() const {
return pixels_;
}

void ProgressbarAnimation::updateTarget()
{
if (pixels_ > INT_MAX - 2)
pixels_ = 0;
else
pixels_ += 2;
Animation::updateTarget();
}

/*************************/

NumberAnimation::NumberAnimation(QObject *target) :
Animation(target),
start_(0.0),
Expand Down
28 changes: 25 additions & 3 deletions Kvantum/style/animation/animation.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Adapted from Qt

/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2016-2021 <tsujan2000@gmail.com>
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2016-2023 <tsujan2000@gmail.com>
*
* Kvantum is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -49,13 +49,14 @@ class Animation : public QAbstractAnimation
enum FrameRate {
DefaultFps,
SixtyFps,
ThirtyFps
ThirtyFps,
TwentyFps
};

FrameRate frameRate() const;
void setFrameRate(FrameRate fps);

void updateTarget();
virtual void updateTarget();

public Q_SLOTS:
void start();
Expand All @@ -72,6 +73,25 @@ public Q_SLOTS:
int skip_;
};

/*************************/

class ProgressbarAnimation : public Animation
{
Q_OBJECT

public:
ProgressbarAnimation(QObject *target);

void updateTarget() override;

int pixels() const;

private:
int pixels_;
};

/*************************/

class NumberAnimation : public Animation
{
Q_OBJECT
Expand All @@ -96,6 +116,8 @@ class NumberAnimation : public Animation
mutable qreal prev_;
};

/*************************/

class ScrollbarAnimation : public NumberAnimation
{
Q_OBJECT
Expand Down
44 changes: 6 additions & 38 deletions Kvantum/style/eventFiltering.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2014-2022 <tsujan2000@gmail.com>
* Copyright (C) Pedram Pourang (aka Tsu Jan) 2014-2023 <tsujan2000@gmail.com>
*
* Kvantum is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down Expand Up @@ -220,22 +220,8 @@ bool Style::eventFilter(QObject *o, QEvent *e)
{
if (w->inherits("KisAbstractSliderSpinBox") || w->inherits("Digikam::DAbstractSliderSpinBox"))
isKisSlider_ = true;
else if (QProgressBar *pb = qobject_cast<QProgressBar*>(o))
{
if (pb->maximum() == 0 && pb->minimum() == 0)
{ // add the busy progress bar to the list
if (!progressbars_.contains(w))
{
progressbars_.insert(w, 0);
connect(w, &QObject::destroyed, this, &Style::forgetProgressBar);
if (!progressTimer_->isActive())
progressTimer_->start(50);
}
}
else
forgetProgressBar(o);
else if (qobject_cast<QProgressBar*>(o))
isKisSlider_ = false;
}
else if (w->isWindow()
&& w->testAttribute(Qt::WA_StyledBackground)
&& w->testAttribute(Qt::WA_TranslucentBackground)
Expand Down Expand Up @@ -960,20 +946,7 @@ bool Style::eventFilter(QObject *o, QEvent *e)
connect(w, &QObject::destroyed, this, &Style::forgetPopupOrigin);
}

if (QProgressBar *pb = qobject_cast<QProgressBar*>(o))
{
if (pb->maximum() == 0 && pb->minimum() == 0)
{
if (!progressbars_.contains(w))
{
progressbars_.insert(w, 0);
connect(w, &QObject::destroyed, this, &Style::forgetProgressBar);
}
if (!progressTimer_->isActive())
progressTimer_->start(50);
}
}
else if (QMenu *menu = qobject_cast<QMenu*>(o))
if (QMenu *menu = qobject_cast<QMenu*>(o))
{
//if (isLibreoffice_) break;
if (w->testAttribute(Qt::WA_X11NetWmWindowTypeMenu)) break; // detached menu
Expand Down Expand Up @@ -1497,14 +1470,9 @@ bool Style::eventFilter(QObject *o, QEvent *e)
/* remove the widget from some lists when it becomes hidden */
if (qobject_cast<QMenu*>(w))
forgetMovedMenu(o);
if (!progressbars_.isEmpty() && qobject_cast<QProgressBar*>(o))
{
forgetProgressBar(o);
break;
}
else if (tspec_.animate_states
&& (w->windowType() == Qt::Popup
|| w->windowType() == Qt::ToolTip))
if (tspec_.animate_states
&& (w->windowType() == Qt::Popup
|| w->windowType() == Qt::ToolTip))
{
forgetPopupOrigin(o);
break; // popups aren't animated (see below)
Expand Down

0 comments on commit fa386cc

Please sign in to comment.