forked from ic005k/OCAuxiliaryTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltip.cpp
203 lines (165 loc) · 4.99 KB
/
tooltip.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
#include "tooltip.h"
#include <QAbstractTextDocumentLayout>
#include <QDebug>
#include <QEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QVBoxLayout>
#include "mainwindow.h"
extern MainWindow* mw_one;
extern bool zh_cn;
Tooltip::Tooltip(QWidget* parent) : QDialog(parent) {
setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
delay = 600;
this->setAutoFillBackground(true);
QPalette palette = this->palette();
// palette.setColor(QPalette::Base, QColor(255, 255, 225, 255));
// palette.setColor(QPalette::Window, QColor(255, 255, 225, 255));
palette.setColor(QPalette::Base, QColor(236, 236, 236, 255));
palette.setColor(QPalette::Window, QColor(236, 236, 236, 255));
palette.setColor(QPalette::Text, QColor(0, 0, 0, 255));
this->setPalette(palette);
edit = new QTextEdit(this);
edit->setWordWrapMode(QTextOption::WordWrap);
edit->setReadOnly(true);
edit->setFrameStyle(QFrame::NoFrame);
QHBoxLayout* mLayout = new QHBoxLayout(this);
mLayout->setContentsMargins(0, 0, 0, 0);
lblTitle = new QLabel();
QFont font;
font.setBold(true);
lblTitle->setFont(font);
lblTitle->setFixedHeight(40);
lblTitle->setFixedWidth(40);
lblTitle->setText("");
lblTitle->setStyleSheet(
"QLabel{"
"border-image:url(:/icon/tip.png) 4 4 4 4 stretch stretch;"
"}");
mLayout->addWidget(lblTitle);
mLayout->addWidget(edit);
thisWidth = 500;
thisHeight = 250;
#ifdef Q_OS_WIN32
thisWidth = 600;
thisHeight = 300;
#endif
this->resize(thisWidth, thisHeight);
this->installEventFilter(this);
}
Tooltip::~Tooltip() {}
void Tooltip::setMyText(QString strHead, const QString& text) {
QString str;
QStringList strList;
if (text.contains("----")) {
strList = text.split("----");
if (zh_cn) {
str = strList.at(1);
} else
str = strList.at(0);
} else
str = text;
this->setWindowTitle(strHead);
edit->setText(str.trimmed());
// 文本高度
QTextDocument* document = edit->document(); // new QTextDocument(edit);
document->setTextWidth(thisWidth);
QTextOption op;
op.setWrapMode(QTextOption::WordWrap);
document->setDefaultTextOption(op);
document->adjustSize();
QAbstractTextDocumentLayout* layout = document->documentLayout();
int newHeight = layout->documentSize().height();
if (newHeight < thisHeight)
currentHeight = newHeight;
else
currentHeight = thisHeight;
if (currentHeight < 40) currentHeight = 40;
edit->setFixedHeight(currentHeight * 1.05);
this->setFixedHeight(currentHeight * 1.05);
}
bool Tooltip::eventFilter(QObject* obj, QEvent* e) {
if (obj == this) {
if (QEvent::WindowDeactivate == e->type()) {
if (popClose) return QWidget::eventFilter(obj, e);
QPropertyAnimation* animation =
new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(delay);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, &QPropertyAnimation::finished, [=]() {
end = true;
close();
});
e->accept();
return true;
}
}
return QWidget::eventFilter(obj, e);
}
void Tooltip::popup(QPoint pos, QString strHead, const QString& text) {
if (text.trimmed().length() == 0) return;
this->installEventFilter(this);
this->setMyText(strHead, text);
int newX;
if (pos.x() + thisWidth > mw_one->getMainWidth())
newX = pos.x() - thisWidth;
else
newX = pos.x();
pos.setY(pos.y() - this->height());
pos.setX(newX - 10);
this->move(pos);
QPropertyAnimation* animation = new QPropertyAnimation(this, "windowOpacity");
animation->setDuration(delay);
animation->setStartValue(0);
animation->setEndValue(1);
animation->start();
show();
end = false;
popClose = false;
int t1 = text.length() * 35;
if (t1 < 5000) t1 = 5000;
QElapsedTimer t0;
t0.start();
while (t0.elapsed() < t1 && !end) {
QCoreApplication::processEvents();
}
if (end) return;
// QEvent event(QEvent::WindowDeactivate);
// QApplication::sendEvent(this, &event);
popClose = true;
QPropertyAnimation* animation1 =
new QPropertyAnimation(this, "windowOpacity");
animation1->setDuration(delay);
animation1->setStartValue(1);
animation1->setEndValue(0);
connect(animation1, &QPropertyAnimation::finished, [=]() {
end = true;
close();
});
animation1->start();
}
void Tooltip::paintEvent(QPaintEvent* event) {
return;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(QBrush(QColor(0xff, 0xff, 0xe1)));
// painter.setBrush(QBrush(Qt::lightGray));
painter.setPen(Qt::black);
QRect rect = this->rect();
rect.setWidth(rect.width() - 1);
rect.setHeight(rect.height() - 1);
painter.drawRoundedRect(rect, 15, 15);
QWidget::paintEvent(event);
}
void Tooltip::closeEvent(QCloseEvent* event) {
Q_UNUSED(event);
end = true;
}
void Tooltip::endClose() {
end = true;
this->close();
}
void Tooltip::thisShow() { this->show(); }