diff --git a/src/components/RollingMessageWidget.cpp b/src/components/RollingMessageWidget.cpp index 3f96d82..01af7b7 100644 --- a/src/components/RollingMessageWidget.cpp +++ b/src/components/RollingMessageWidget.cpp @@ -3,61 +3,67 @@ // #include "RollingMessageWidget.h" + #include #include #include #include +namespace { + const int WAIT_TIMES = 60; + const int LABEL_MARGIN_TOP = 2; + const int LABEL_MARGIN_LEFT = 5; + const int LABEL_FONT_WIDTH = 16; + const int MOVING_STEP = 2; +} -const int WAIT_TIMES = 60; - -RollingMessageWidget::RollingMessageWidget(QWidget *parent) :QWidget(parent){ +RollingMessageWidget::RollingMessageWidget(QWidget *parent) +: QWidget(parent) +, mCurrentLabel(new QLabel(this)) +, mNextLabel(new QLabel(this)) +{ this->setFixedHeight(36); -// this->setMinimumWidth(1000); - this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred); - label_current = new QLabel(this); - label_next = new QLabel(this); +} +RollingMessageWidget::~RollingMessageWidget() { + if (mTimerId >= 0) killTimer(mTimerId); } void RollingMessageWidget::timerEvent(QTimerEvent *e) { if (!this->isVisible()) return; //滚动 - if (rolling) { + if (mRolling) { //end rolling, reset next - if (rollStep == ROLL_DISTANCE){ - waitStep = 0; - rolling = false; - QPoint p3 = {ROLL_DISTANCE+5,2}; -// p3.setY(label_next->geometry().y()); - label_current->move(p3); -// printf("move last to 505,2\n"); - std::swap(label_current,label_next); + if (mRolledDistance == TOTAL_ROLL_DISTANCE){ + mWaitStep = 0; + mRolling = false; + QPoint p3 = {TOTAL_ROLL_DISTANCE + LABEL_MARGIN_LEFT, LABEL_MARGIN_TOP}; + mCurrentLabel->move(p3); + std::swap(mCurrentLabel, mNextLabel); return; } //rolling - rollStep+=2; - QPoint p1 = {ROLL_DISTANCE+5 - rollStep,2}; - label_next->move(p1); - if (p1.x()-label_current->geometry().x()<=label_current->width()+20) + mRolledDistance += 2; + QPoint p1 = {TOTAL_ROLL_DISTANCE + LABEL_MARGIN_LEFT - mRolledDistance, LABEL_MARGIN_TOP}; + mNextLabel->move(p1); + if (p1.x() - mCurrentLabel->geometry().x() <= mCurrentLabel->width() + 20) { - QPoint p2 = {label_current->geometry().x()-2,2}; - label_current->move(p2); + QPoint p2 = {mCurrentLabel->geometry().x() - MOVING_STEP, LABEL_MARGIN_TOP}; + mCurrentLabel->move(p2); } - return ; } else{ //有等待的高优先级消息时,不停顿 - if (waitStep == WAIT_TIMES || (waitPriorityCount > 0 && waitStep == WAIT_TIMES/3)){ - locker.lock(); - label_next->setText(getNextMessage()); - label_next->setFixedWidth(label_next->text().length()*16); - locker.unlock(); + if (mWaitStep == WAIT_TIMES || (mWaitPriorityCount > 0 && mWaitStep == WAIT_TIMES / 3)){ + mLocker.lock(); + mNextLabel->setText(getNextMessage()); + mNextLabel->setFixedWidth(mNextLabel->text().length() * LABEL_FONT_WIDTH); + mLocker.unlock(); } else{ - waitStep++; + mWaitStep++; } } } @@ -65,57 +71,54 @@ void RollingMessageWidget::timerEvent(QTimerEvent *e) { void RollingMessageWidget::setMessageList(const QStringList &message) { //empty, don't set if (message.isEmpty()) return; - messages.clear(); - needRolling = false; + mMessages.clear(); // set default label - label_current->setText(message[0]); - messages.push_back({0,message[0],0}); + mCurrentLabel->setText(message[0]); + mMessages.push_back({0, message[0], 0}); //add list if (message.length()>0) { - for (int k = 1; k < message.length(); ++k) { - messages.push_front({k, message[k],-1}); + mMessages.push_front({k, message[k], -1}); } - needRolling = true; } } void RollingMessageWidget::updateMessagePriority(int innerIndex, int priority) { //lock thread - locker.lock(); + mLocker.lock(); //高优先级,需要插入 if (priority > 0) { - int idx = -1, insert_idx = -1; - for (int i = 0; i < messages.length(); ++i) { - if (messages[i].innerId == innerIndex) + int idx = -1, insertIdx = -1; + for (int i = 0; i < mMessages.length(); ++i) { + if (mMessages[i].innerId == innerIndex) { - messages[i].priority = priority; + mMessages[i].priority = priority; idx = i; } - else if (insert_idx < 0 && messages[i].priority<1) + else if (insertIdx < 0 && mMessages[i].priority < 1) { - insert_idx = i; + insertIdx = i; } - if (idx>=0 && insert_idx >=0) break; + if (idx>=0 && insertIdx >= 0) break; } - insert_idx = insert_idx==-1?0:insert_idx; + insertIdx = insertIdx == -1 ? 0 : insertIdx; //move - if(insert_idx=0) + if(mMessages.last().priority >= 0) { - qDebug()<<"get next:"<0; - ret = messages.last().msg; - messages.last().priority = 0; + priorityFlag = mMessages.last().priority > 0; + ret = mMessages.last().msg; + mMessages.last().priority = 0; break; } } - if (priorityFlag && waitPriorityCount > 0) waitPriorityCount--; - rollStep = 0; - rolling = true; - waitStep = 0; + if (priorityFlag && mWaitPriorityCount > 0) mWaitPriorityCount--; + mRolledDistance = 0; + mRolling = true; + mWaitStep = 0; return ret; } void RollingMessageWidget::showEvent(QShowEvent *event) { QWidget::showEvent(event); - if (!init) + if (!mInitFlag) { - ROLL_DISTANCE = this->width(); - QPoint p1 = {5,2}; - label_current->setFixedHeight(30); - label_current->show(); - label_current->move(p1); - label_current->setText(tr("Message of warn!")); - label_current->setFixedWidth(label_current->text().length()*16); - QPoint p2 = {ROLL_DISTANCE+5,2}; - label_next->setFixedHeight(30); - label_next->show(); - label_next->move(p2); - label_next->setText(tr("Message2 of warn!")); - label_next->setFixedWidth(label_next->text().length()*16); - - timerId = startTimer(40); - init = true; + TOTAL_ROLL_DISTANCE = this->width(); + QPoint p1 = {LABEL_MARGIN_LEFT,LABEL_MARGIN_TOP}; + mCurrentLabel->setFixedHeight(30); + mCurrentLabel->show(); + mCurrentLabel->move(p1); + mCurrentLabel->setText(tr("Message of warn!")); + mCurrentLabel->setFixedWidth(mCurrentLabel->text().length() * LABEL_FONT_WIDTH); + QPoint p2 = {TOTAL_ROLL_DISTANCE + LABEL_MARGIN_LEFT, LABEL_MARGIN_TOP}; + mNextLabel->setFixedHeight(30); + mNextLabel->show(); + mNextLabel->move(p2); + mNextLabel->setText(tr("Message2 of warn!")); + mNextLabel->setFixedWidth(mNextLabel->text().length() * LABEL_FONT_WIDTH); + mTimerId = startTimer(40); + mInitFlag = true; } } diff --git a/src/components/RollingMessageWidget.h b/src/components/RollingMessageWidget.h index e67d3e5..6b7a4e3 100644 --- a/src/components/RollingMessageWidget.h +++ b/src/components/RollingMessageWidget.h @@ -7,9 +7,11 @@ #include #include + class QThread; class QLabel; class QTimerEvent; + class RollingMessageWidget:public QWidget { struct WarnMessage{ int innerId; @@ -18,13 +20,11 @@ class RollingMessageWidget:public QWidget { * 优先级,默认为0,-1不显示,1代表插入优先显示 */ int priority; - QString toString(){ - return QString("{innerId:%1, msg:%2, priority:%3}").arg(innerId).arg(msg).arg(priority); - } }; Q_OBJECT public: explicit RollingMessageWidget(QWidget *parent = nullptr); + ~RollingMessageWidget() override; void setMessageList(const QStringList& message); void updateMessagePriority(int innerIndex, int priority); @@ -33,18 +33,17 @@ protected: void showEvent(QShowEvent *event) override ; QString getNextMessage(); private: - int timerId = -1; - int waitPriorityCount = 0; - int waitStep = 0; - int rollStep = 0; - bool needRolling = false; - bool rolling = false; - QLabel* label_current = nullptr; - QLabel* label_next = nullptr; - QList messages; - QMutex locker; - bool init = false; - int ROLL_DISTANCE=2000; + int mTimerId = -1; + int mWaitPriorityCount = 0; + int mWaitStep = 0; + int mRolledDistance = 0; + bool mRolling = false; + QLabel* mCurrentLabel; + QLabel* mNextLabel; + QList mMessages; + QMutex mLocker; + bool mInitFlag = false; + int TOTAL_ROLL_DISTANCE = 2000; };