140 lines
4.2 KiB
C++
140 lines
4.2 KiB
C++
#include <QApplication>
|
|
#include <QPushButton>
|
|
#include <QPropertyAnimation>
|
|
#include <QMouseEvent>
|
|
#include <globalStruct.h>
|
|
#include <qpainter.h>
|
|
#include <qlayout.h>
|
|
#include <qlabel.h>
|
|
#include <QPainter>
|
|
#include <QLinearGradient>
|
|
#include <globalStruct.h>
|
|
#include <qprogressbar.h>
|
|
#include <qstyle.h>
|
|
#include <qstyleoption.h>
|
|
class fancyProcessBar : public QProgressBar {
|
|
public:
|
|
fancyProcessBar(QWidget* parent = nullptr) : QProgressBar(parent) {}
|
|
|
|
protected:
|
|
void paintEvent(QPaintEvent* event) override {
|
|
Q_UNUSED(event);
|
|
|
|
QPainter painter(this);
|
|
QStyleOptionProgressBar option;
|
|
initStyleOption(&option);
|
|
|
|
// 自定义进度条外观
|
|
option.textVisible = false; // 隐藏进度文本
|
|
option.progress = value(); // 设置当前进度
|
|
option.maximum = maximum(); // 设置最大值
|
|
|
|
// 绘制进度条
|
|
style()->drawControl(QStyle::CE_ProgressBar, &option, &painter, this);
|
|
}
|
|
};
|
|
|
|
class FrequencyBar : public QWidget
|
|
{
|
|
|
|
public:
|
|
FrequencyBar(QString titleIn)
|
|
{
|
|
title = titleIn;
|
|
setMinimumSize(100, 60);
|
|
}
|
|
QSize sizeHint() const override
|
|
{
|
|
return QSize(100, 60);
|
|
}
|
|
void addData(int value)
|
|
{
|
|
maxValue = getMax(value, maxValue);
|
|
minValue = getMin(value, minValue);
|
|
currentValue = value;
|
|
if((maxValue - minValue) ==0)
|
|
setProgress(1);
|
|
else if (value == 0)
|
|
setProgress(0);
|
|
else
|
|
{
|
|
double v = (double)((double)(value - minValue) / (double)(maxValue - minValue));
|
|
setProgress(v);
|
|
}
|
|
|
|
}
|
|
void setProgress(double newProgress)
|
|
{
|
|
if (newProgress < 0.0) {
|
|
progress = 0.0;
|
|
}
|
|
else if (newProgress > 1.0) {
|
|
progress = 1.0;
|
|
}
|
|
else {
|
|
progress = newProgress;
|
|
}
|
|
update(); // 重新绘制控件
|
|
}
|
|
protected:
|
|
void paintEvent(QPaintEvent* event) override
|
|
{
|
|
Q_UNUSED(event);
|
|
|
|
QPainter painter(this);
|
|
QRect rect = this->rect();
|
|
|
|
// Draw background
|
|
//painter.fillRect(rect, Qt::darkGray);
|
|
|
|
// Draw frequency text
|
|
painter.setPen(Qt::white);
|
|
|
|
if (maxValue == INT_MIN)
|
|
painter.drawText(rect.adjusted(10, 10, -10, 10), Qt::AlignLeft | Qt::AlignTop, "0ms");
|
|
else
|
|
painter.drawText(rect.adjusted(10, 10, -10, 10), Qt::AlignLeft | Qt::AlignTop, QString::number(minValue) + "ms");
|
|
painter.drawText(rect.adjusted(10, 10, -10, 10), Qt::AlignHCenter | Qt::AlignTop, QString::number(currentValue) + "ms");
|
|
if (maxValue == INT_MIN)
|
|
painter.drawText(rect.adjusted(10, 10, -10, 10), Qt::AlignRight | Qt::AlignTop, "0ms");
|
|
else
|
|
painter.drawText(rect.adjusted(10, 10, -10, 10), Qt::AlignRight | Qt::AlignTop, QString::number(maxValue) + "ms");
|
|
|
|
|
|
painter.drawText(rect.adjusted(10, 30, -10, -10), Qt::AlignLeft | Qt::AlignTop, "Min");
|
|
painter.drawText(rect.adjusted(10, 30, -10, -10), Qt::AlignHCenter | Qt::AlignTop, title);
|
|
painter.drawText(rect.adjusted(10, 30, -10, -10), Qt::AlignRight | Qt::AlignTop, "Max");
|
|
|
|
int barWidth = rect.width() - 20;
|
|
int barHeight = 5;
|
|
QRect barRect(10, rect.height() / 2 - barHeight / 2-5, barWidth, barHeight);
|
|
|
|
// Draw progress in the bar
|
|
int progressWidth = static_cast<int>(barRect.width() * progress);
|
|
QRect progressRect(barRect.left(), barRect.top(), progressWidth, barHeight);
|
|
|
|
QLinearGradient gradient(progressRect.left(), progressRect.top(), progressRect.right(), progressRect.bottom());
|
|
gradient.setColorAt(0.0, QColor("#F4A582"));
|
|
gradient.setColorAt(1.0, QColor("#92C5DE"));
|
|
|
|
painter.fillRect(progressRect, gradient);
|
|
|
|
// Draw ticks
|
|
int numTicks = 50;
|
|
painter.setPen(Qt::white);
|
|
for (int i = 0; i <= numTicks; ++i) {
|
|
int x = barRect.left() + i * (barRect.width() / numTicks);
|
|
painter.drawLine(x, barRect.top(), x, barRect.bottom());
|
|
}
|
|
init = true;
|
|
}
|
|
|
|
private:
|
|
bool init = false;
|
|
QString title;
|
|
int maxValue = INT_MIN, minValue = INT_MAX, currentValue = 0;
|
|
int frequency =50;
|
|
double progress;
|
|
};
|
|
#pragma once
|