这个控件源自于一个音乐播放器,在写该音乐播放器的时候,须要将音频的数据转换成对应的频谱显示,采用的fmod第三方库来处理(fmod声音系统是为游戏开发者准备的革命性音频引擎,很是强大和牛逼),fmod负责拿到音频数据对应的采样频谱数据,而后传给这个控件进行绘制便可,本控件主须要专一于绘制便可,这样fmod对应封装的类专一于音频采集等处理,实现了隔离,修改和增长功能比较方便,声音波形图控件除了能够设置采样的深度之外,还支持三种数据样式展现,线条样式、柱状样式、平滑样式。为了能够直接定位到某一位置直接跳转到音频位置,还增长了绘制数线条定位线。linux
#ifndef WAVEDATA_H #define WAVEDATA_H /** * 音量采样值波形控件 做者:feiyangqingyun(QQ:517216493) 2017-9-10 * 1:可设置采样深度 * 2:可设置当前位置线条宽度/线条颜色 * 3:可设置前景色/背景色 * 4:可设置数据展现样式,线条样式/柱状样式/平滑样式 */ #include <QWidget> #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT WaveData : public QWidget #else class WaveData : public QWidget #endif { Q_OBJECT Q_ENUMS(WaveStyle) Q_PROPERTY(double deep READ getDeep WRITE setDeep) Q_PROPERTY(bool showLine READ getShowLine WRITE setShowLine) Q_PROPERTY(int lineWidth READ getLineWidth WRITE setLineWidth) Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor) Q_PROPERTY(QColor foreground READ getForeground WRITE setForeground) Q_PROPERTY(QColor background READ getBackground WRITE setBackground) Q_PROPERTY(WaveStyle waveStyle READ getWaveStyle WRITE setWaveStyle) public: enum WaveStyle { WaveStyle_Line = 0, //线条样式 WaveStyle_Smooth = 1, //平滑样式 WaveStyle_Bar = 2 //柱状样式 }; explicit WaveData(QWidget *parent = 0); protected: void mousePressEvent(QMouseEvent *); void paintEvent(QPaintEvent *); void drawBg(QPainter *painter); void drawData(QPainter *painter); void drawLine(QPainter *painter); private: double deep; //采集深度 bool showLine; //显示线条 int lineWidth; //线条宽度 QColor lineColor; //线条颜色 QColor foreground; //前景色 QColor background; //背景色 WaveStyle waveStyle; //数据样式 int length; //采样点长度 int position; //当前位置 QVector<float> data; //采样点数据 public: double getDeep() const; bool getShowLine() const; int getLineWidth() const; QColor getLineColor() const; QColor getForeground() const; QColor getBackground() const; WaveStyle getWaveStyle() const; QSize sizeHint() const; QSize minimumSizeHint() const; public slots: //设置深度 void setDeep(double deep); //设置是否显示线条 void setShowLine(bool showLine); //设置线条宽度 void setLineWidth(int lineWidth); //设置线条颜色 void setLineColor(const QColor &lineColor); //设置前景色 void setForeground(const QColor &foreground); //设置背景色 void setBackground(const QColor &background); //设置数据样式 void setWaveStyle(const WaveStyle &waveStyle); //设置总长度 void setLength(int length); //设置当前位置 void setPosition(int position); //设置当前数据 void setData(const QVector<float> &data); //清空数据 void clearData(); signals: void positionChanged(int position); }; #endif // WAVEDATA_H
void WaveData::paintEvent(QPaintEvent *) { //绘制准备工做,启用反锯齿 QPainter painter(this); painter.setRenderHints(QPainter::Antialiasing); //绘制背景 drawBg(&painter); //绘制数据 drawData(&painter); //绘制当前位置线条 drawLine(&painter); } void WaveData::drawBg(QPainter *painter) { painter->save(); painter->fillRect(this->rect(), background); painter->restore(); } void WaveData::drawData(QPainter *painter) { if (data.count() == 0) { return; } painter->save(); //获取最大值最小值 float max = data.at(0); float min = data.at(0); int count = data.count(); for (int i = 1; i < count; i++) { if (max < data.at(i)) { max = data.at(i); } if (min > data.at(i)) { min = data.at(i); } } //转化成当前屏幕的内的坐标大小 max += deep; min -= deep; //自动转换数据到屏幕坐标位置 QVector<QPointF> points; for (int i = 0; i < count; i++) { double x = i * width() / count; double y = height() - (((data.at(i) - min) / (max - min)) * height()); points.append(QPointF(x, y)); } //绘制不一样的风格 if (waveStyle == WaveStyle_Line) { painter->setPen(foreground); for (int i = 0; i < count - 1; i++) { painter->drawLine(points.at(i), points.at(i + 1)); } } else if (waveStyle == WaveStyle_Smooth) { painter->setPen(foreground); QPainterPath path = SmoothCurveCreator::createSmoothCurve(points); painter->drawPath(path); } else if (waveStyle == WaveStyle_Bar) { double penWidth = width() / (count * 1.6); QPen pen; pen.setColor(foreground); pen.setWidthF(penWidth); pen.setCapStyle(Qt::RoundCap); painter->setPen(pen); for (int i = 0; i < count; i++) { QPointF point = points.at(i); double x = point.x() + penWidth / 1.2; painter->drawLine(QPointF(x, point.y()), QPointF(x, height())); } } painter->restore(); } void WaveData::drawLine(QPainter *painter) { if (!showLine || position > length || data.count() <= 0) { return; } painter->save(); QPen pen; pen.setWidth(lineWidth); pen.setColor(lineColor); painter->setPen(pen); //计算当前位置对应的坐标 int x = ((double)position / length) * width(); painter->drawLine(x, 0, x, height()); painter->restore(); }