213 lines
6.0 KiB
C++
213 lines
6.0 KiB
C++
#include "TimeSlider.h"
|
|
#include <QDebug>
|
|
#include <QStyleOption>
|
|
#include <QTime>
|
|
|
|
#define MARGIN_LEFT 15 // 左边距
|
|
#define MARGIN_RIGHT 15 // 又边距
|
|
#define LONG_SCALE_LENGTH 10 // 长刻度的长度
|
|
#define SHORT_SCALE_LENGTH 6 // 段刻度的长度
|
|
#define SMALL_SCALE_COUNT 5 // 下刻度的数量
|
|
#define TIME_BAR_HEIGHT 20 // 时间进度条高度
|
|
|
|
TimeSlider::TimeSlider(QWidget* parent)
|
|
: QWidget(parent)
|
|
{
|
|
setMinimumSize(QSize(100, 90));
|
|
}
|
|
|
|
/**
|
|
* @brief 重写绘制函数
|
|
* @param event
|
|
*/
|
|
void TimeSlider::paintEvent(QPaintEvent* event)
|
|
{
|
|
QPainter painter(this);
|
|
// 有焦点绘制绿色边框
|
|
if (hasFocus()) {
|
|
borderColor = QColor(15, 216, 82, opacity * 255);
|
|
} else {
|
|
borderColor = QColor(0, 0, 0, opacity * 255);
|
|
}
|
|
painter.setPen(QPen(borderColor, borderWidth));
|
|
// 绘制背景颜色
|
|
painter.setBrush(QColor(50, 50, 50, opacity * 255));
|
|
painter.drawRect(rect().adjusted(borderWidth / 2, borderWidth / 2, -borderWidth, -borderWidth));
|
|
// 绘制时间条
|
|
painter.setPen(QPen(QColor { 255, 255, 255, opacity * 255 }, 1));
|
|
painter.drawRect(MARGIN_LEFT, height() / 2, width() - MARGIN_LEFT - MARGIN_RIGHT, TIME_BAR_HEIGHT);
|
|
// 绘制时间刻度
|
|
drawTimeScale(painter);
|
|
// 绘制当前时间
|
|
if (curScaleVisible) {
|
|
drawCurrentTime(painter);
|
|
}
|
|
// 绘制具体的时间段信息
|
|
drawTimeSegments(painter);
|
|
QWidget::paintEvent(event);
|
|
}
|
|
|
|
/**
|
|
* @brief 绘制当前的时间
|
|
* @param painter
|
|
*/
|
|
void TimeSlider::drawCurrentTime(QPainter& painter)
|
|
{
|
|
float x = MARGIN_LEFT + (current - min) * (width() - MARGIN_LEFT - MARGIN_LEFT) / (max - min);
|
|
QString text = secToString(current, "hh:mm:ss");
|
|
QFont font;
|
|
font.setFamily("Microsoft YaHei");
|
|
font.setPointSize(9);
|
|
painter.setFont(font);
|
|
QFontMetrics fm(painter.font());
|
|
QRect textRect = fm.boundingRect(text);
|
|
// 当时间接近24:00:00时,文字超出显示范围则把文字定在右上角
|
|
if (x + textRect.width() >= rect().right()) {
|
|
textRect.moveTopRight(QPoint(rect().right(), borderWidth));
|
|
} else {
|
|
textRect.moveTopLeft(QPoint(x, borderWidth));
|
|
}
|
|
if (x >= width() - MARGIN_LEFT) {
|
|
x = width() - MARGIN_LEFT;
|
|
}
|
|
// 设置文字颜色位黄色以及背景为黑色
|
|
painter.setPen(QColor(255, 248, 33, opacity * 255));
|
|
QBrush brush(QColor(0, 0, 0, opacity * 255));
|
|
painter.fillRect(textRect, brush);
|
|
painter.drawText(textRect, text);
|
|
painter.drawLine(x, borderWidth, x, height() - borderWidth);
|
|
}
|
|
|
|
/**
|
|
* @brief 绘制时间刻度
|
|
* @param paint
|
|
*/
|
|
void TimeSlider::drawTimeScale(QPainter& painter)
|
|
{
|
|
int scale = min;
|
|
// 绘制刻度线
|
|
while (scale % 3600 != 0) {
|
|
scale++;
|
|
}
|
|
QFont font;
|
|
font.setFamily("Microsoft YaHei");
|
|
font.setPointSize(9);
|
|
painter.setFont(font);
|
|
// 绘制
|
|
while (scale <= max) {
|
|
float x = MARGIN_LEFT + (scale - min) * (width() - MARGIN_LEFT - MARGIN_RIGHT) / (max - min);
|
|
// 整点绘制大刻度和文字
|
|
if (scale % 3600 == 0) {
|
|
// 绘制大刻度
|
|
painter.drawLine(x, height() / 2 - LONG_SCALE_LENGTH, x, height() / 2);
|
|
QString text = secToString(scale, "hh");
|
|
QFontMetrics fm(painter.font());
|
|
QRect textRect = fm.boundingRect(text);
|
|
textRect.moveCenter(QPoint(x, height() / 2 - LONG_SCALE_LENGTH - textRect.height() / 2));
|
|
painter.drawText(textRect, text);
|
|
}
|
|
// 绘制小刻度
|
|
else {
|
|
painter.drawLine(x, height() / 2 - SHORT_SCALE_LENGTH, x, height() / 2);
|
|
}
|
|
scale += 3600 / SMALL_SCALE_COUNT;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 绘制具体时间段的信息
|
|
* @param painter
|
|
*/
|
|
void TimeSlider::drawTimeSegments(QPainter& painter)
|
|
{
|
|
painter.setPen(Qt::NoPen);
|
|
for (const auto& segment : segments) {
|
|
int endTime = segment.startTime + segment.duration;
|
|
float startX = MARGIN_LEFT + (segment.startTime - min) * (width() - MARGIN_LEFT - MARGIN_LEFT) / (max - min);
|
|
float endX = MARGIN_LEFT + (endTime - min) * (width() - MARGIN_LEFT - MARGIN_LEFT) / (max - min);
|
|
if (endX >= width() - MARGIN_LEFT) {
|
|
endX = width() - MARGIN_LEFT;
|
|
}
|
|
painter.setBrush(QColor(0, 255, 255, opacity * 255));
|
|
painter.drawRect(startX, height() / 2, endX - startX, TIME_BAR_HEIGHT);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief 设置是否显示当前时间刻度
|
|
* @param visible
|
|
*/
|
|
void TimeSlider::setCurScaleVisble(bool visible)
|
|
{
|
|
curScaleVisible = visible;
|
|
}
|
|
|
|
/**
|
|
* @brief 设置当前的时间分片
|
|
* @param timeSegments
|
|
*/
|
|
void TimeSlider::setTimeSegments(QList<TimeSegment>& segments)
|
|
{
|
|
this->segments = segments;
|
|
}
|
|
|
|
/**
|
|
* @brief 设置当前时间,单位秒数
|
|
* @param current
|
|
*/
|
|
void TimeSlider::setCurrent(int current)
|
|
{
|
|
this->current = current;
|
|
update();
|
|
}
|
|
|
|
/**
|
|
* @brief 将当前设置为下一段
|
|
*/
|
|
void TimeSlider::nextSegment()
|
|
{
|
|
// for (auto it = segments.begin(); it != segments.end(); it++) {
|
|
// if (it->startTime > current) {
|
|
// current = it->startTime;
|
|
// repaint();
|
|
// break;
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* @brief 将当前设置为下一段
|
|
*/
|
|
void TimeSlider::preSegment()
|
|
{
|
|
// for(auto it = segments.rbegin(); it != segments.rend(); it++){
|
|
// if(it->startTime < current){
|
|
// current = it->startTime;
|
|
// repaint();
|
|
// break;
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* @brief 设置背景色
|
|
* @param color
|
|
*/
|
|
void TimeSlider::setBackgroundColor(QColor color)
|
|
{
|
|
backgroundColor = color;
|
|
}
|
|
|
|
/**
|
|
* @brief 设置透明度
|
|
* @param opa
|
|
*/
|
|
void TimeSlider::setOpacity(float opa)
|
|
{
|
|
if (opa < 0) {
|
|
opa = 0;
|
|
} else if (opa > 1) {
|
|
opa = 1;
|
|
}
|
|
opacity = opa;
|
|
} |