Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
810 changes: 810 additions & 0 deletions QelProgress/QelProgress.cpp

Large diffs are not rendered by default.

171 changes: 171 additions & 0 deletions QelProgress/QelProgress.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#ifndef QELPROGRESS_H
#define QELPROGRESS_H

#include <functional>

#include <QElapsedTimer>
#include <QString>
#include <QVector>
#include <QWidget>

class QHideEvent;
class QPaintEvent;
class QPainter;
class QResizeEvent;
class QShowEvent;
class QTimer;

namespace qel {

class QelProgress : public QWidget
{
Q_OBJECT

public:
enum class Type {
Line,
Circle,
Dashboard
};

enum class Status {
Normal,
Success,
Exception,
Warning
};

enum class StrokeLinecap {
Butt,
Round,
Square
};

struct ProgressColor {
QString color;
double percentage = 100.0;
};

using ColorFunction = std::function<QString(double percentage)>;
using FormatFunction = std::function<QString(double percentage)>;
using ContentRenderer =
std::function<QWidget *(double percentage, QWidget *parent)>;

explicit QelProgress(QWidget *parent = nullptr);
~QelProgress() override;

void setType(Type type);
Type type() const;

void setPercentage(double percentage);
double percentage() const;

void setStatus(Status status);
Status status() const;

void setIndeterminate(bool indeterminate);
bool isIndeterminate() const;

void setDuration(double seconds);
double duration() const;

void setStrokeWidth(int width);
int strokeWidth() const;

void setStrokeLinecap(StrokeLinecap linecap);
StrokeLinecap strokeLinecap() const;

void setTextInside(bool textInside);
bool textInside() const;

void setWidth(int width);
int progressWidth() const;

void setShowText(bool show);
bool showText() const;

void setColor(const QString &color);
QString color() const;

void setColors(const QVector<ProgressColor> &colors);
QVector<ProgressColor> colors() const;

void setColorFunction(const ColorFunction &function);
bool hasColorFunction() const;

void clearColor();

void setStriped(bool striped);
bool isStriped() const;

void setStripedFlow(bool flow);
bool stripedFlow() const;

void setFormat(const FormatFunction &format);
void clearFormat();
QString formattedText() const;

// Maps the Element Plus default scoped slot.
void setContentRenderer(const ContentRenderer &renderer);
void clearContentRenderer();
bool hasContentRenderer() const;

QSize sizeHint() const override;
QSize minimumSizeHint() const override;

protected:
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;

private:
void paintLine(QPainter &painter);
void paintCircle(QPainter &painter, bool dashboard);
void paintDefaultText(QPainter &painter, const QRectF &rect);

void updateAnimationState();
void updateContentWidget();
void updateContentGeometry();

QRectF lineTrackRect() const;
QRectF circularRect() const;
QString currentColor() const;
QString statusColor() const;
Qt::PenCapStyle qtCapStyle() const;
double animationPhase() const;
double visualPercentage() const;

Type type_ = Type::Line;
double percentage_ = 0.0;
Status status_ = Status::Normal;
bool indeterminate_ = false;
double duration_ = 3.0;
int strokeWidth_ = 6;
StrokeLinecap strokeLinecap_ = StrokeLinecap::Round;
bool textInside_ = false;
int width_ = 126;
bool showText_ = true;

QString color_;
QVector<ProgressColor> colors_;
ColorFunction colorFunction_;

bool striped_ = false;
bool stripedFlow_ = false;

FormatFunction format_;
ContentRenderer contentRenderer_;
QWidget *contentWidget_ = nullptr;

QTimer *animationTimer_ = nullptr;
QElapsedTimer animationClock_;

double transitionStartPercentage_ = 0.0;
bool transitionActive_ = false;
QElapsedTimer transitionClock_;
};

} // namespace qel

#endif // QELPROGRESS_H
12 changes: 12 additions & 0 deletions QelProgress/QelProgress.pri
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
isEmpty(QEL_PROGRESS_PRI_INCLUDED) {
QEL_PROGRESS_PRI_INCLUDED = 1

SOURCES += \
$$PWD/QelProgress.cpp

HEADERS += \
$$PWD/QelProgress.h

include($$PWD/../QelTheme/QelTheme.pri)
include($$PWD/../QelIcon/QelIcon.pri)
}
21 changes: 21 additions & 0 deletions QelProgress/QelProgress.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QelProgress
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS
CONFIG += c++17

SOURCES += \
main.cpp

HEADERS += \
QelProgressTester.h

include($$PWD/QelProgress.pri)
include($$PWD/../QelButton/QelButton.pri)

DISTFILES += \
QelProgress.pri
Loading