-
Notifications
You must be signed in to change notification settings - Fork 2
/
tipcard.cpp
209 lines (179 loc) · 6.28 KB
/
tipcard.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "tipcard.h"
TipCard::TipCard(QWidget *parent, NotificationEntry *noti)
: ThreeDimenButton(parent), noti(noti)
{
setBgColor(Qt::yellow);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// 初始化控件
title_label = new QLabel(noti->title, this);
content_label = new QLabel(noti->content, this);
close_button = new InteractiveButtonBase(QIcon(":/icon/hide_right"), this);
operator1_button = nullptr;
operator2_button = nullptr;
operator3_button = nullptr;
// 初始化布局
QHBoxLayout* margin_hlayout = new QHBoxLayout(this);
{
margin_hlayout->setSpacing(0);
QVBoxLayout* main_vlayout = new QVBoxLayout;
{
main_vlayout->setSpacing(0);
// 添加标题
QHBoxLayout* title_hlayout = new QHBoxLayout;
{
title_hlayout->setSpacing(0);
title_hlayout->addWidget(title_label);
title_hlayout->addWidget(close_button);
}
// 添加内容
main_vlayout->addSpacing(aop_h*2);
main_vlayout->addLayout(title_hlayout);
main_vlayout->addWidget(content_label);
// 添加按钮
btn_layout = new QHBoxLayout;
main_vlayout->addLayout(btn_layout);
main_vlayout->addSpacing(aop_h*2);
}
margin_hlayout->addSpacing(aop_w*2);
margin_hlayout->addLayout(main_vlayout);
margin_hlayout->addSpacing(aop_w*2);
}
setLayout(margin_hlayout);
// 添加按钮1
if (!noti->btn1.isEmpty())
{
btn_layout->addWidget(operator1_button = new InteractiveButtonBase(noti->btn1, this));
connect(operator1_button, &InteractiveButtonBase::clicked, [=]{
emit noti->signalBtnClicked(1);
emit signalButton1Clicked(noti);
});
this->setFixedSize(width(), height() + operator1_button->height());
}
// 添加按钮2
if (!noti->btn2.isEmpty())
{
btn_layout->addWidget(operator2_button = new InteractiveButtonBase(noti->btn2, this));
connect(operator2_button, &InteractiveButtonBase::clicked, [=]{
emit noti->signalBtnClicked(2);
emit signalButton2Clicked(noti);
});
}
// 添加按钮3
if (!noti->btn3.isEmpty())
{
btn_layout->addWidget(operator3_button = new InteractiveButtonBase(noti->btn3, this));
connect(operator3_button, &InteractiveButtonBase::clicked, [=]{
emit noti->signalBtnClicked(3);
emit signalButton3Clicked(noti);
});
}
// 事件
connect(close_button, SIGNAL(clicked(bool)), this, SLOT(slotClosed()));
connect(this, &ThreeDimenButton::clicked, [=]{
emit noti->signalCardClicked();
emit signalCardClicked(noti);
});
// 样式
QFont bold_font = title_label->font();
bold_font.setBold(true);
bold_font.setPointSize(bold_font.pointSize() * 1.2);
title_label->setFont(bold_font);
// 高度
title_label->adjustSize();
content_label->adjustSize();
// title_label->setFixedHeight(getWidgetHeight(title_label));
content_label->setFixedWidth(parentWidget()->width()-aop_w*4-TIP_CARD_CONTENT_MARGIN*2);
content_label->setFixedHeight(getWidgetHeight(content_label));
int height = title_label->height() + content_label->height() + aop_h*4 + TIP_CARD_CONTENT_MARGIN*2;
if (operator1_button != nullptr)
height += operator1_button->height();
setMinimumSize(100, max(50, height));
// 控件大小
close_button->setFixedSize(title_label->height(), title_label->height());
if (parentWidget() != nullptr)
setFixedWidth(parentWidget()->width());
content_label->setWordWrap(true);
// 初始化变量
is_closing = false;
has_leaved = false;
// 定时器
close_timer = new QTimer(this);
close_timer->setSingleShot(true);
connect(close_timer, SIGNAL(timeout()), this, SLOT(slotClosed()));
}
void TipCard::slotClosed()
{
if (is_closing) return ;
emit signalClosed(this);
is_closing = true;
}
void TipCard::startWaitingLeave()
{
close_timer->start(has_leaved ? 1000 : (noti->time>0?noti->time:5000));
}
void TipCard::pauseWaitingLeave()
{
close_timer->stop();
}
void TipCard::setBgColor(QColor c)
{
ThreeDimenButton::setBgColor(c);
}
void TipCard::setFontColor(QColor c)
{
QString color_qss = "color:" + colorToCss(c) + ";";
title_label->setStyleSheet("padding-left:5px;\n" + color_qss);
content_label->setStyleSheet(color_qss);
}
void TipCard::setBtnColor(QColor c)
{
if (operator1_button != nullptr)
operator1_button->setTextColor(c);
if (operator2_button != nullptr)
operator2_button->setTextColor(c);
if (operator3_button != nullptr)
operator3_button->setTextColor(c);
}
void TipCard::enterEvent(QEvent *event)
{
ThreeDimenButton::enterEvent(event);
if (is_closing) return ;
close_timer->stop();
}
void TipCard::leaveEvent(QEvent *event)
{
ThreeDimenButton::leaveEvent(event);
has_leaved = true;
}
QString TipCard::colorToCss(QColor c)
{
if (c.alpha() == 255)
return QString("rgb(%1, %2, %3)").arg(c.red()).arg(c.green()).arg(c.blue());
else
return QString("rgb(%1, %2, %3, %4)").arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
}
QString TipCard::getXml(QString str, QString tag)
{
int pos = str.indexOf("<"+tag+">");
if (pos == -1) return str;
int pos2 = str.indexOf("</"+tag+">");
if (pos2 == -1) return str.right(str.length()-pos-tag.length()-2);
return str.mid(pos+tag.length()+2, pos2-tag.length()-2);
}
template<typename T>
int TipCard::getWidgetHeight(T *w)
{
QStringList strs = w->text().split("\n"); // 分成多行
int w_width = w->width(); // 控件宽度(用以拆分长字符串为多行)
int height = 0;
QFontMetrics fm = w->fontMetrics();
double mPixelPerCentimer = 1.0;
int linesCount = 0; // 放在控件上的文字总行数(非换行数)
foreach (QString str, strs)
{
double tempWidth = fm.horizontalAdvance(str) / mPixelPerCentimer; // 字数转行数
linesCount += (tempWidth+w_width-1) / w_width;
}
height = fm.lineSpacing() * linesCount; // 总高度 = 单行高度 × 行数
return height;
}