Rolling Message
This commit is contained in:
92
src/components/RollingMessageWidget.cpp
Normal file
92
src/components/RollingMessageWidget.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
//
|
||||
// Created by Krad on 2021/12/16.
|
||||
//
|
||||
|
||||
#include "RollingMessageWidget.h"
|
||||
#include <QLabel>
|
||||
#include <QThread>
|
||||
|
||||
RollingMessageWidget::RollingMessageWidget(QWidget *parent) :QWidget(parent){
|
||||
this->setFixedHeight(36);
|
||||
this->setFixedWidth(500);
|
||||
label_current = new QLabel(this);
|
||||
label_next = new QLabel(this);
|
||||
QPoint p1 = this->mapToGlobal({5,2});
|
||||
label_current->setFixedSize(400, 30);
|
||||
label_current->show();
|
||||
label_current->move(p1);
|
||||
label_current->setText(tr("Message of warn!"));
|
||||
QPoint p2 = this->mapToGlobal({5,-30});
|
||||
label_next->setFixedSize(400, 30);
|
||||
label_next->show();
|
||||
label_next->move(p2);
|
||||
label_next->setText(tr("Message2 of warn!"));
|
||||
timerId = startTimer(100);
|
||||
}
|
||||
|
||||
void RollingMessageWidget::timerEvent(QTimerEvent *e) {
|
||||
if (!this->isVisible()) return;
|
||||
if (!needRolling) return;
|
||||
if (waitStep == 50){
|
||||
QPoint p1 = label_current->geometry().topLeft();
|
||||
p1.setY(p1.y()+1);
|
||||
label_current->move(p1);
|
||||
QPoint p2 = label_next->geometry().topLeft();
|
||||
p2.setY(p2.y()+1);
|
||||
label_next->move(p2);
|
||||
|
||||
movingStep++;
|
||||
if (movingStep == 32)
|
||||
{
|
||||
movingStep = 0;
|
||||
waitStep = 0;
|
||||
std::swap(label_next,label_current);
|
||||
QPoint p3 = this->mapToGlobal({5,-30});
|
||||
p3.setX(label_next->geometry().x());
|
||||
label_next->move(p3);
|
||||
int current_index = messages.indexOf(label_current->text());
|
||||
int new_index = (current_index+1)<messages.length()?(current_index+1):0;
|
||||
//find next visible message
|
||||
for (int i = new_index; ; ++i) {
|
||||
if (i == current_index)
|
||||
{
|
||||
needRolling = false;
|
||||
break;
|
||||
}
|
||||
if (visibleValues[i])
|
||||
{
|
||||
label_next->setText(messages[i]);
|
||||
break;
|
||||
}
|
||||
if (i>=messages.length())
|
||||
{
|
||||
i=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else{
|
||||
waitStep++;
|
||||
}
|
||||
}
|
||||
|
||||
void RollingMessageWidget::setMessageList(const QStringList &message, QList<bool> visible) {
|
||||
messages = message;
|
||||
visibleValues = visible;
|
||||
int first_index = 0;
|
||||
for (int i = 0; i < messages.length(); ++i) {
|
||||
if (visibleValues[i])
|
||||
{
|
||||
label_current->setText(messages[i]);
|
||||
first_index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int j = first_index+1; j < messages.length(); ++j) {
|
||||
if (visibleValues[j])
|
||||
{
|
||||
label_next->setText(messages[j]);
|
||||
needRolling = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/components/RollingMessageWidget.h
Normal file
36
src/components/RollingMessageWidget.h
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by Krad on 2021/12/16.
|
||||
//
|
||||
|
||||
#ifndef GUI_ROLLINGMESSAGEWIDGET_H
|
||||
#define GUI_ROLLINGMESSAGEWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
class QThread;
|
||||
class QLabel;
|
||||
class QTimerEvent;
|
||||
class RollingMessageWidget:public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit RollingMessageWidget(QWidget *parent = nullptr);
|
||||
void setMessageList(const QStringList& message,const QList<bool> visible);
|
||||
void setMessageVisibility(int index, bool visible)
|
||||
{
|
||||
visibleValues[index] = visible;
|
||||
}
|
||||
protected:
|
||||
void timerEvent(QTimerEvent* event) override ;
|
||||
|
||||
private:
|
||||
int timerId = -1;
|
||||
int waitStep = 0;
|
||||
int movingStep = 0;
|
||||
bool needRolling = false;
|
||||
QLabel* label_current = nullptr;
|
||||
QLabel* label_next = nullptr;
|
||||
QStringList messages;
|
||||
QList<bool> visibleValues;
|
||||
};
|
||||
|
||||
|
||||
#endif //GUI_ROLLINGMESSAGEWIDGET_H
|
||||
Reference in New Issue
Block a user