-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtips.cpp
192 lines (178 loc) · 6.84 KB
/
tips.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "tips.h"
#include "ui_tips.h"
QList<Tips*> Tips::allTipsObject;
int Tips::currentAnimationCount = 0;
int Tips::Count = 0;
bool Tips::isFirst = true;
Tips::Tips(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Tips)
, animation(nullptr)
, timerId(0)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_TranslucentBackground);
// 设置默认样式
setAchieveProgressStyle();
}
void Tips::StartTips(const QString &url, const QString &achievementTitleText, const QString &achievementText, bool isAchievementStyle)
{
// 检查是否已经存在相同的 Tips 对象
bool found = false;
for (Tips* tip : allTipsObject) {
if (tip->url == url && tip->ui->achievementTitleLabel->text() == achievementTitleText && tip->ui->achievementLabel->text() == achievementText) {
found = true;
break;
}
}
// 先把对象保存起来
this->url = url;
ui->achievementTitleLabel->setText(achievementTitleText);
ui->achievementTitleLabel->setFont(QFont("Arial",16));
ui->achievementLabel->setText(achievementText);
ui->achievementLabel->setFont(QFont("Arial",14));
QPixmap pixmap(url);
QPixmap scaledPixmap = pixmap.scaled(45, 45, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->iconLabel->setPixmap(scaledPixmap);
// 设置解锁新进度样式
if(isAchievementStyle) setUnlockNewProgressStyle();
if (!found) {
Count++;
allTipsObject.append(this);
// qDebug() << "currentAnimationCount:" << currentAnimationCount;
// 启动 Tips 对象
if(currentAnimationCount == 0)
{// 先启动第一个
Tips* firstTip = allTipsObject.at(0);
firstTip->startAnimation(0);
currentAnimationCount++;
}
else if(currentAnimationCount == 1)
{// 再启动第二个
Tips* secondTip = allTipsObject.at(1);
secondTip->startAnimation(1);
currentAnimationCount++;
}
}
}
void Tips::timerEvent(QTimerEvent *event)
{
if(event->timerId() == timerId){
// 创建动画效果
QPropertyAnimation* animation = new QPropertyAnimation(this, "pos");
animation->setDuration(3000); // 动画持续时间
animation->setStartValue(this->pos());
animation->setEndValue(QPoint(this->pos().x() + 450, this->pos().y())); // 向右移动450个像素
connect(animation, &QPropertyAnimation::finished, this, &QDialog::close); // 动画结束后关闭对话框
animation->start();
killTimer(timerId);
timerId = 0;
connect(animation, &QPropertyAnimation::finished, []{
// 检查是否有等待的 Tips 对象
if (!allTipsObject.isEmpty()) {
// 动画结束后先弹出第一个对象
allTipsObject.takeFirst();
Count--;
// 找到并执行下一个 Tips 对象
if (!allTipsObject.isEmpty()) {
Tips* nextTip = allTipsObject.at(0);
// 如果是上一对象是在y = 0位置
if(isFirst)
{
nextTip->startAnimation(1);
isFirst = false;
}
else if(!isFirst)
{
nextTip->startAnimation(0);
isFirst = true;
}
}
else{
Count = 0;
currentAnimationCount = 0;
}
}
else{
Count = 0;
currentAnimationCount = 0;
}
});
}
}
void Tips::startAnimation(int order)
{
this->show();
// 设置对话框位置
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
int dialogWidth = this->width();
int screenWidth = screenGeometry.width();
if(order == 0)
{
this->move(screenWidth - dialogWidth, 20);
}
else
{
this->move(screenWidth - dialogWidth, 95);
}
// 创建定时器,延迟数秒后开始动画
timerId = startTimer(5000);
}
// 达成进度tip样式
void Tips::setAchieveProgressStyle()
{
ui->iconLabel->setStyleSheet("border:0;"
"color:white;"
"background-color: rgb(33, 33, 33);"
"border-radius:0;"
);
ui->achievementTitleLabel->setStyleSheet("border:0;"
"color:rgb(230, 240, 143);"
"background-color: rgb(33, 33, 33);"
"border-radius:0;"
);
ui->achievementLabel->setStyleSheet("border:0;"
"color:white;"
"background-color: rgb(33, 33, 33);"
"border-radius:0;"
);
ui->widget->setStyleSheet("border:0;"
"background-color: rgb(33, 33, 33);"
"border: 6px solid rgb(84, 84, 84);"
"border-top-left-radius:10px;"
"border-top-right-radius:10px;"
"border-bottom-right-radius:10px;"
"border-bottom-left-radius:10px;"
);
setStyleSheet("background-color: rgba(0, 0, 0,0);");
}
// 解锁新进度tip样式
void Tips::setUnlockNewProgressStyle()
{
ui->iconLabel->setStyleSheet("border:0;"
"color:white;"
"border-radius:0;"
);
ui->achievementTitleLabel->setStyleSheet("border:0;"
"border-radius:0;"
"color: rgb(85, 85, 255);"
);
ui->achievementLabel->setStyleSheet("border:0;"
"border-radius:0;"
"color: rgb(0, 0, 0);"
);
ui->widget->setStyleSheet("border:0;"
"background-color: rgb(160, 160, 160);"
"border: 6px solid rgb(121, 121, 121);"
"border-top-left-radius:10px;"
"border-top-right-radius:10px;"
"border-bottom-right-radius:10px;"
"border-bottom-left-radius:10px;"
);
}
Tips::~Tips()
{
delete ui;
}