11 #include <QSizePolicy>
22 CLoadIndicator::CLoadIndicator(
int width,
int height, QWidget *parent) : QWidget(parent)
24 this->setObjectName(
"CLoadIndicator");
25 this->resize(width, height);
26 this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
27 this->setFocusPolicy(Qt::NoFocus);
28 this->setAutoFillBackground(
true);
29 this->setStyleSheet(
"background-color: transparent;");
36 m_displayedWhenStopped = state;
44 using namespace std::chrono_literals;
47 this->setEnabled(
true);
49 QPointer<CLoadIndicator> myself(
this);
50 if (m_timerId == -1) { m_timerId = startTimer(m_delayMs); }
51 if (processEvents &&
sGui)
60 const int stopId = m_currentId++;
64 if (!myself) {
return; }
71 m_pendingIds.push_back(stopId);
79 m_pendingIds.removeOne(indicatorId);
81 if (!m_pendingIds.isEmpty()) {
return; }
86 SWIFT_AUDIT_X(CThreadUtils::isInThisThread(
this), Q_FUNC_INFO,
"Try to kill timer from another thread");
87 this->killTimer(m_timerId);
91 this->setEnabled(
false);
100 SWIFT_AUDIT_X(CThreadUtils::isInThisThread(
this), Q_FUNC_INFO,
"Try to kill timer from another thread");
101 this->killTimer(m_timerId);
103 m_timerId = this->startTimer(m_delayMs);
119 m_angle = (m_angle + 30) % 360;
132 if (this->parentWidget()) {
return parentWidget()->isVisible(); }
138 if (!m_displayedWhenStopped && !
isAnimated()) {
return; }
139 if (!this->isVisible() || !this->isEnabled()) {
return; }
142 int width = qMin(this->width(), this->height());
143 painter.setRenderHint(QPainter::Antialiasing);
148 int outerRadius = qRound((width - 1) * 0.5);
149 int innerRadius = qRound((width - 1) * 0.5 * 0.38);
151 int capsuleHeight = outerRadius - innerRadius;
152 int capsuleWidth = (width > 32) ? qRound(capsuleHeight * .23) : qRound(capsuleHeight * .35);
153 int capsuleRadius = capsuleWidth / 2;
155 for (
int i = 0; i < 12; i++)
157 QColor
color = m_color;
158 color.setAlphaF(qRound(1.0f - (i / 12.0f)));
159 painter.setPen(Qt::NoPen);
160 painter.setBrush(
color);
162 painter.translate(rect().center());
163 painter.rotate(m_angle - qRound(i * 30.0f));
164 painter.drawRoundedRect(-qRound(capsuleWidth * 0.5), -(innerRadius + capsuleHeight), capsuleWidth,
165 capsuleHeight, capsuleRadius, capsuleRadius);
172 const int w = this->width();
173 const int h = this->height();
174 const int x = middle.x() - w / 2;
175 const int y = middle.y() - h / 2;
176 this->setGeometry(x, y, w, h);
181 Q_ASSERT_X(usingWidget, Q_FUNC_INFO,
"need widget");
211 const QPoint middle =
m_usingWidget->visibleRegion().boundingRect().center();
void processEventsToRefreshGui() const
Allow the GUI to refresh by processing events, call the event loop.
QWidget * m_usingWidget
widget which uses load indicator
void centerLoadIndicator()
Center load indicator.
bool m_loadInProgress
flag indicating loading
CLoadIndicator * m_loadIndicator
indicator itself
void showLoading(std::chrono::milliseconds timeout=std::chrono::milliseconds(0), bool processEvents=true)
Show load indicator.
bool isLoadInProgress() const
Loading in progress?
virtual void indicatorTimedOut()
Indicator timed out.
void hideLoading()
Hide load indicator.
CLoadIndicatorEnabled(QWidget *usingWidget)
Ctor.
int m_indicatorId
last indicator id returned
bool isShowingLoadIndicator() const
Showing load indicator?
The QProgressIndicator class lets an application display a progress indicator to show that a lengthy ...
void centerLoadIndicator(const QPoint &middle)
Center this load indicator.
bool isAnimated() const
Returns a Boolean value indicating whether the component is currently animated.
void setColor(const QColor &color)
Sets the color of the components to the given color.
virtual int heightForWidth(int w) const
bool isDisplayedWhenStopped() const
Returns a Boolean value indicating whether the receiver shows itself even when it is not animating.
const QColor & color() const
Returns the color of the component.
int startAnimation(std::chrono::milliseconds timeout=std::chrono::milliseconds(0), bool processEvents=false)
Starts the spin animation.
bool isParentVisible() const
Is parent widget visible?
void paint(QPainter &painter) const
Paint to another painter.
virtual void paintEvent(QPaintEvent *event)
void timedOut()
Timed out.
virtual void timerEvent(QTimerEvent *event)
void stopAnimation(int indicatorId=-1)
Stops the spin animation.
virtual QSize sizeHint() const
void setDisplayedWhenStopped(bool state)
Sets whether the component hides itself when it is not animating.
void setAnimationDelay(int delay)
Sets the delay between animation steps. Setting the delay to a value larger than 40 slows the animati...
SWIFT_GUI_EXPORT swift::gui::CGuiApplication * sGui
Single instance of GUI application object.
Free functions in swift::misc.
auto singleShot(int msec, QObject *target, F &&task)
Starts a single-shot timer which will call a task in the thread of the given object when it times out...
#define SWIFT_AUDIT_X(COND, WHERE, WHAT)
A weaker kind of verify.