1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update customprogressbar

This commit is contained in:
tianduanrui 2017-10-20 23:29:42 +08:00
parent 04a5cdb9ed
commit f29917a08a
5 changed files with 132 additions and 25 deletions

View File

@ -1,6 +1,7 @@
#include "qqtexquisiteform.h"
#include "ui_qqtexquisiteform.h"
#include <QMouseEvent>
#include <QKeyEvent>
#include <qqtcore.h>
@ -10,23 +11,32 @@ QQtExquisiteForm::QQtExquisiteForm(QWidget *parent) :
{
ui->setupUi(this);
ui->hs0->setRange(0, 100);
ui->w0->setMinValue(0);
ui->w0->setMaxValue(80);
ui->w2->setRange(0, 80);
//ui->w2->setCircleType(QQtCustomProgressBar::CircleType_Image);
//ui->w2->setCircleImage("./xxtest.png");
ui->w2->setCircleColor(QColor(20, 40, 100));
connect(ui->hs0, SIGNAL(valueChanged(int)), ui->w0, SLOT(setValue(int)));
connect(ui->hs0, SIGNAL(valueChanged(int)), ui->w2, SLOT(setValue(int)));
connect(ui->hs0, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
m_timer = new QTimer(this);
m_timer->setSingleShot(false);
m_t1 = new QTimer(this);
m_t1->setSingleShot(false);
m_timer_down = new QTimer(this);
m_timer_down->setSingleShot(false);
connect(m_t1, SIGNAL(timeout()), this, SLOT(setValueDown()));
connect(m_timer_down, SIGNAL(timeout()), this, SLOT(setValueDown()));
connect(m_timer, SIGNAL(timeout()), this, SLOT(setValue()));
value = 0;
m_t1->start(10);
curmaxValue = 80;
m_timer_down->start(10);
ui->hs0->installEventFilter(this);
}
QQtExquisiteForm::~QQtExquisiteForm()
@ -34,15 +44,14 @@ QQtExquisiteForm::~QQtExquisiteForm()
delete ui;
}
void QQtExquisiteForm::setValue(int value)
{
this->value = value;
}
void QQtExquisiteForm::setValue()
{
if(value < 80)
if(value>curmaxValue)
return;
if(value < curmaxValue)
value++;
ui->hs0->setValue(value);
}
@ -58,7 +67,7 @@ void QQtExquisiteForm::keyPressEvent(QKeyEvent *event)
{
//pline() << hex << event->key();
if(event->key() == Qt::Key_Up) {
m_t1->stop();
m_timer_down->stop();
m_timer->start(10);
event->accept();
}
@ -70,9 +79,41 @@ void QQtExquisiteForm::keyPressEvent(QKeyEvent *event)
void QQtExquisiteForm::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Up) {
m_t1->start(10);
m_timer_down->start(10);
m_timer->stop();
event->accept();
}
QDialog::keyReleaseEvent(event);
}
bool QQtExquisiteForm::eventFilter(QObject *watched, QEvent *event)
{
if(watched == ui->hs0){
if(event->type() == QEvent::MouseButtonPress) {
QMouseEvent* e = (QMouseEvent*)event;
//pline() << hex << e->button();
if(e->button() == Qt::LeftButton) {
curmaxValue = ui->hs0->minimum() + (ui->hs0->maximum()-ui->hs0->minimum()) * e->x() / ui->hs0->width();
m_timer_down->stop();
m_timer->start(10);
// not necessary because of the bug later.
event->accept();
// bug:in theory this return is not necessary, after accept, parent won't handle this e continue;
// but, it handled. handled once, but child has no action any.
// then, use return ,don't goto parent, well to use.
return true;
}
}
if(event->type() == QEvent::MouseButtonRelease) {
QMouseEvent* e = (QMouseEvent*)event;
//pline() << hex << e->button();
if(e->button() == Qt::LeftButton) {
curmaxValue = 80;
m_timer->stop();
m_timer_down->start(10);
event->accept();
}
}
}
return QDialog::eventFilter(watched, event);
}

View File

@ -17,19 +17,23 @@ public:
~QQtExquisiteForm();
private slots:
void setValue(int);
void setValue();
void setValueDown();
private:
Ui::QQtExquisiteForm *ui;
QTimer* m_timer ;
QTimer* m_t1 ;
QTimer* m_timer_down ;
int value;
int curmaxValue;
// QWidget interface
protected:
virtual void keyPressEvent(QKeyEvent *event) override;
virtual void keyReleaseEvent(QKeyEvent *event) override;
// QObject interface
public:
virtual bool eventFilter(QObject *watched, QEvent *event) override;
};
#endif // QQTEXQUISITEFORM_H

View File

@ -28,7 +28,7 @@
<rect>
<x>250</x>
<y>30</y>
<width>181</width>
<width>201</width>
<height>121</height>
</rect>
</property>

View File

@ -25,13 +25,13 @@ QQtCustomProgressBar::QQtCustomProgressBar(QWidget *parent) : QWidget(parent)
freeColor = QColor(100, 100, 100);
circleColor = QColor(70, 70, 70);
textColor = QColor(250, 250, 250);
textFont = font();
percentStyle = PercentStyle_Arc;
circleType = CircleType_Color;
waterDensity = 1;
waterHeight = 1;
setFont(QFont("Arial", 7));
}
QQtCustomProgressBar::~QQtCustomProgressBar()
@ -42,15 +42,25 @@ void QQtCustomProgressBar::paintEvent(QPaintEvent *)
{
int width = this->width();
int height = this->height();
/*显示实长*/
int side = qMin(width, height);
/*绘制准备工作,启用反锯齿,平移坐标轴中心,等比例缩放*/
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
/*平移坐标系*/
painter.translate(width / 2, height / 2);
/*更改刻度 设置的是放大的倍数 */
/*有利于在绘制的时候,统一绘制数据*/
/*矢量放大,不失真*/
painter.scale(side / 200.0, side / 200.0);
/*绘制中心圆*/
/*
*
* QPainter绘制区域大小 *
* 200 * ( side / 200.0 )
*/
drawCircle(&painter, 99);
/*根据样式绘制进度*/
@ -80,7 +90,21 @@ void QQtCustomProgressBar::drawCircle(QPainter *painter, int radius)
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(circleColor);
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
if(circleType == CircleType_Color) {
painter->drawEllipse(-radius, -radius, radius * 2, radius * 2);
} else {
QPixmap pix;
pix = QPixmap(circleImage);
/*自动等比例平滑缩放居中显示*/
int targetWidth = pix.width();
int targetHeight = pix.height();
pix = pix.scaled(targetWidth, targetHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter->drawPixmap(-radius, -radius, radius * 2, radius * 2, pix);
}
painter->restore();
}
@ -230,17 +254,22 @@ void QQtCustomProgressBar::drawWave(QPainter *painter, int radius)
void QQtCustomProgressBar::drawText(QPainter *painter, int radius)
{
painter->save();
painter->setPen(textColor);
painter->setFont(QFont("Arial", 50));
QRectF textRect(-radius, -radius, radius * 2, radius * 2);
QString strValue = QString("%1").arg(value);
if (showPercent) {
strValue = QString("%1%").arg((double)value / (maxValue - minValue) * 100);
}
painter->save();
painter->setPen(textColor);
painter->setFont(textFont);
QFontMetricsF fm(textFont);
QSizeF textSize = fm.size(Qt::TextSingleLine, strValue);
//QRectF textRect(-radius, -radius, radius * 2, radius * 2);
QRectF textRect(-textSize.width()/2, 40, textSize.width(), textSize.height());
//painter->drawText(-w / 2, 42, strValue);
painter->drawText(textRect, Qt::AlignCenter, strValue);
painter->restore();
@ -316,6 +345,11 @@ QQtCustomProgressBar::PercentStyle QQtCustomProgressBar::getPercentStyle() const
return this->percentStyle;
}
QQtCustomProgressBar::CircleType QQtCustomProgressBar::getCircleType() const
{
return this->circleType;
}
QSize QQtCustomProgressBar::sizeHint() const
{
return QSize(200, 200);
@ -438,6 +472,14 @@ void QQtCustomProgressBar::setCircleColor(const QColor &circleColor)
}
}
void QQtCustomProgressBar::setCircleImage(const QString &circleImage)
{
if (this->circleImage != circleImage) {
this->circleImage = circleImage;
update();
}
}
void QQtCustomProgressBar::setTextColor(const QColor &textColor)
{
if (this->textColor != textColor) {
@ -462,6 +504,14 @@ void QQtCustomProgressBar::setPercentStyle(QQtCustomProgressBar::PercentStyle pe
}
}
void QQtCustomProgressBar::setCircleType(QQtCustomProgressBar::CircleType circleType)
{
if (this->circleType != circleType) {
this->circleType = circleType;
update();
}
}
void QQtCustomProgressBar::setWaterDensity(int value)
{
if(value < 0)

View File

@ -57,6 +57,11 @@ class QQTSHARED_EXPORT QQtCustomProgressBar : public QWidget
Q_PROPERTY(QColor textColor READ getTextColor WRITE setTextColor)
public:
enum CircleType {
CircleType_Color = 0, /*使用颜色*/
CircleType_Image = 1, /*使用图片*/
};
enum PercentStyle {
PercentStyle_Arc = 0, /*圆弧风格*/
PercentStyle_Polo = 1, /*水池风格*/
@ -91,12 +96,15 @@ private:
QColor usedColor; /*已使用百分比颜色*/
QColor freeColor; /*未使用百分比颜色*/
QColor circleColor; /*圆颜色*/
QString circleImage; /*圆图片*/
QColor textColor; /*文字颜色*/
QFont textFont; /*文字字体*/
PercentStyle percentStyle; /*进度样式风格*/
CircleType circleType; /*圆的样式类型*/
int waterDensity,waterHeight;
int waterDensity;
int waterHeight;
public:
int getMinValue() const;
@ -117,6 +125,7 @@ public:
QColor getTextColor() const;
PercentStyle getPercentStyle() const;
CircleType getCircleType() const;
virtual QSize sizeHint() const;
virtual QSize minimumSizeHint() const;
@ -152,6 +161,8 @@ public Q_SLOTS:
void setFreeColor(const QColor &freeColor);
/*设置圆颜色*/
void setCircleColor(const QColor &circleColor);
/*设置圆背景图*/
void setCircleImage(const QString &circleImage);
/*设置文本颜色*/
void setTextColor(const QColor &textColor);
/*设置字体*/
@ -159,6 +170,7 @@ public Q_SLOTS:
/*设置进度样式风格*/
void setPercentStyle(PercentStyle percentStyle);
void setCircleType(CircleType circleType);
/*设置水波密度 0-10*/
void setWaterDensity(int value);