垂直時間軸控件,主要用來描述企業發展歷程大事件,或者軟件版本迭代歷史等,經過時間節點和事件描述來直觀的展現發展的過程,通常在web網頁或者app中常常看到此類控件,尤爲是公司的官網關於公司部分,着重在一些獨角獸公司或者正處於蓬勃發展的公司,用來展現本身多牛逼,發展多麼迅猛等。 垂直時間軸控件主要存儲的數據包含兩個,一個是時間節點,一個是事件描述,爲了後期的拓展性,採用結構體來存放這個數據,好比後期還可能增長該事件是否屬於大事件標記,是的話則繪製的時候突出顯示好比加大字號加粗,本控件的主要難點在於自動計算和排列來繪製時間和事件描述,默認採用對等分的機制來處理繪製,還有部分時間軸控件是左側時間右側事件描述,這個能夠在源碼基礎上自行更改或者增長樣式,爲了可以展現全部的事件,本控件主體是繼承自滾動條區域控件,超太高度自動產生滾動條。linux
#ifndef TIMEAXIS_H #define TIMEAXIS_H /** * 垂直時間軸控件 做者:雨田哥(QQ:3246214072) 整理:feiyangqingyun(QQ:517216493) 2019-10-07 * 1:可設置節點邊距 * 2:可設置節點高度 * 3:可設置信息邊框邊距 * 4:可設置信息所佔高度 * 5:可設置基準顏色/線條顏色 * 6:可設置標題/信息集合 * 7:自動產生滾動條 * 8:支持字符串形式設置數據 */ #include <QScrollArea> class TimeAxisWidget; #ifdef quc #if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) #include <QtDesigner/QDesignerExportWidget> #else #include <QtUiPlugin/QDesignerExportWidget> #endif class QDESIGNER_WIDGET_EXPORT TimeAxis : public QScrollArea #else class TimeAxis : public QScrollArea #endif { Q_OBJECT Q_PROPERTY(int itemMargin READ getItemMargin WRITE setItemMargin) Q_PROPERTY(int itemHeight READ getItemHeight WRITE setItemHeight) Q_PROPERTY(int infoPadding READ getInfoPadding WRITE setInfoPadding) Q_PROPERTY(int infoHeight READ getInfoHeight WRITE setInfoHeight) Q_PROPERTY(QColor baseColor READ getBaseColor WRITE setBaseColor) Q_PROPERTY(QColor lineColor READ getLineColor WRITE setLineColor) Q_PROPERTY(QString title READ getTitle WRITE setTitle) Q_PROPERTY(QString infos READ getInfos WRITE setInfos) public: explicit TimeAxis(QWidget *parent = 0); private: int itemMargin; //節點邊距 int itemHeight; //節點高度 int infoPadding; //信息邊距 int infoHeight; //信息高度 QColor baseColor; //基準顏色 QColor lineColor; //線條顏色 QString title; //標題 QString infos; //信息集合 //時間軸主控件 TimeAxisWidget *timeAxisWidget; public: int getItemMargin() const; int getItemHeight() const; int getInfoPadding() const; int getInfoHeight() const; QColor getBaseColor() const; QColor getLineColor() const; QString getTitle() const; QString getInfos() const; QSize sizeHint() const; QSize minimumSizeHint() const; TimeAxisWidget *getWidget(); public Q_SLOTS: //設置節點邊距+節點高度 void setItemMargin(int itemMargin); void setItemHeight(int itemHeight); //設置信息邊距+信息高度 void setInfoPadding(int infoPadding); void setInfoHeight(int infoHeight); //設置基準顏色+線條顏色 void setBaseColor(const QColor &baseColor); void setLineColor(const QColor &lineColor); //設置標題+信息集合 void setTitle(const QString &title); void setInfos(const QString &infos); }; class TimeAxisWidget : public QWidget { Q_OBJECT public: //能夠自行拓展其餘信息 struct TimeAxisInfo { QString time; //時間 QString info; //信息 }; explicit TimeAxisWidget(QWidget *parent = 0); protected: void paintEvent(QPaintEvent *); void drawTitle(QPainter *painter); void drawLine(QPainter *painter); void drawInfo(QPainter *painter); void drawInfoRight(QPainter *painter, const QRectF &infoRect, int infoHeight); void drawInfoLeft(QPainter *painter, const QRectF &infoRect, int infoHeight); private: int itemMargin; //節點邊距 int itemHeight; //節點高度 int infoPadding; //信息邊距 int infoHeight; //信息高度 QColor baseColor; //基準顏色 QColor lineColor; //線條顏色 QString title; //標題 QString infos; //信息集合 //信息集合結構體 QList<TimeAxisInfo> itemInfos; public: int getItemMargin() const; int getItemHeight() const; int getInfoPadding() const; int getInfoHeight() const; QColor getBaseColor() const; QColor getLineColor() const; QString getTitle() const; QString getInfos() const; QSize sizeHint() const; QSize minimumSizeHint() const; public Q_SLOTS: //設置節點邊距+節點高度 void setItemMargin(int itemMargin); void setItemHeight(int itemHeight); //設置信息邊距+信息高度 void setInfoPadding(int infoPadding); void setInfoHeight(int infoHeight); //設置基準顏色+線條顏色 void setBaseColor(const QColor &baseColor); void setLineColor(const QColor &lineColor); //設置標題+信息集合 void setTitle(const QString &title); void setInfos(const QString &infos); //設置信息集合,結構體方式 void setItemInfos(const QList<TimeAxisInfo> &itemInfos); }; #endif // TIMEAXIS_H
void TimeAxisWidget::drawTitle(QPainter *painter) { painter->save(); QFont font; font.setBold(true); font.setPointSize(16); painter->setFont(font); painter->setPen(baseColor); painter->drawText(itemMargin, itemMargin, width() - 2 * itemMargin, 40, Qt::AlignCenter, title); painter->restore(); } void TimeAxisWidget::drawLine(QPainter *painter) { painter->save(); painter->setPen(QPen(lineColor, 6)); int startY = itemMargin + 50; int endY = startY + itemInfos.size() * itemHeight; painter->drawLine(width() / 2.0, startY, width() / 2.0, endY); painter->restore(); //設置下固定高度 this->setFixedHeight(endY + itemMargin); } void TimeAxisWidget::drawInfo(QPainter *painter) { painter->save(); painter->setPen(Qt::NoPen); QFont font; font.setPointSize(12); painter->setFont(font); int startY = itemMargin + 50; int centerX = this->width() / 2.0; int spacer = itemMargin + 10; //追個繪製時間軸信息集合,偶數行左側繪製時間右側繪製信息 for (int i = 0; i < itemInfos.size(); i++) { painter->setBrush(Qt::white); painter->setPen(QPen(baseColor, 2)); if (i % 2 == 0) { //繪製時間 QRectF textRect(0, startY, centerX - spacer, itemHeight); painter->drawText(textRect, Qt::AlignRight | Qt::AlignVCenter, itemInfos.at(i).time); //繪製信息邊框 QRectF infoRect(centerX + spacer, textRect.center().y() - infoHeight / 2.0, centerX - spacer - itemMargin - infoHeight / 2.0, infoHeight); drawInfoRight(painter, infoRect, infoHeight); //繪製信息背景 painter->setBrush(baseColor); drawInfoRight(painter, infoRect.adjusted(infoPadding, infoPadding, 0, -infoPadding), infoHeight - infoPadding * 2); //繪製信息文字 painter->setPen(Qt::white); painter->drawText(infoRect.adjusted(infoPadding, infoPadding, 0, -infoPadding), Qt::AlignCenter, itemInfos.at(i).info); } else { //繪製時間 QRectF textRect(centerX + spacer, startY, centerX - spacer, itemHeight); painter->drawText(centerX + spacer, startY, centerX - spacer, itemHeight, Qt::AlignLeft | Qt::AlignVCenter, itemInfos.at(i).time); //繪製信息邊框 QRectF infoRect(itemMargin + infoHeight / 2.0, textRect.center().y() - infoHeight / 2.0, centerX - spacer - itemMargin - infoHeight / 2.0, infoHeight); drawInfoLeft(painter, infoRect, infoHeight); //繪製信息背景 painter->setBrush(baseColor); drawInfoLeft(painter, infoRect.adjusted(0, infoPadding, -infoPadding, -infoPadding), infoHeight - infoPadding * 2); //繪製信息文字 painter->setPen(Qt::white); painter->drawText(infoRect.adjusted(0, infoPadding, -infoPadding, -infoPadding), Qt::AlignCenter, itemInfos.at(i).info); } //繪製垂直線對應的圓 painter->setPen(Qt::NoPen); painter->setBrush(baseColor); painter->drawEllipse(centerX - 8, startY + itemHeight / 2.0 - 8, 16, 16); painter->setBrush(Qt::white); painter->drawEllipse(centerX - 4, startY + itemHeight / 2.0 - 4, 8, 8); //Y軸往下移一個高度 startY += itemHeight; } painter->restore(); }