-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwingsignaturetooltip.cpp
More file actions
155 lines (133 loc) · 4.64 KB
/
wingsignaturetooltip.cpp
File metadata and controls
155 lines (133 loc) · 4.64 KB
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
/****************************************************************************
**
** Copyright (C) 2025-2028 WingSummer
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License version 3
** along with this program. If not, see <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "wingsignaturetooltip.h"
#include <QApplication>
#include <QDesktopServices>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QRegularExpression>
#include <QScreen>
#include <QUrl>
WingSignatureTooltip::WingSignatureTooltip(QWidget *parent)
: QWidget(parent, Qt::ToolTip), m_labelSignature(new QLabel(this)),
m_textDoc(new QTextBrowser(this)), m_countLabel(new QLabel(this)),
m_currentIndex(0), m_sigHelpType(SignatureHelpType::MarkDown) {
setAttribute(Qt::WA_ShowWithoutActivating);
setWindowFlags(windowFlags() | Qt::FramelessWindowHint |
Qt::WindowStaysOnTopHint);
setFocusPolicy(Qt::NoFocus);
setContentsMargins(6, 6, 6, 6);
m_labelSignature->setTextFormat(Qt::RichText);
m_labelSignature->setWordWrap(true);
m_textDoc->setReadOnly(true);
m_textDoc->setOpenExternalLinks(true);
m_textDoc->setFocusPolicy(Qt::NoFocus);
m_textDoc->setMaximumHeight(100);
m_textDoc->setUndoRedoEnabled(false);
m_textDoc->setWordWrapMode(QTextOption::WordWrap);
QHBoxLayout *topRow = new QHBoxLayout;
topRow->addWidget(m_labelSignature);
topRow->addStretch();
topRow->addWidget(m_countLabel);
QVBoxLayout *main = new QVBoxLayout(this);
main->setContentsMargins(6, 6, 6, 6);
main->addLayout(topRow);
main->addWidget(m_textDoc);
setLayout(main);
setStyleSheet(
QStringLiteral("QTextBrowser{background:transparent;border:none;}"));
}
void WingSignatureTooltip::showSignatures(const QPoint &globalPos,
const QList<Signature> &sigs,
qsizetype index) {
if (sigs.isEmpty()) {
hideTooltip();
return;
}
m_signatures = sigs;
m_currentIndex = qBound(0, index, m_signatures.count() - 1);
updateDisplay();
QRect screenRect = QApplication::screenAt(globalPos)->availableGeometry();
adjustSize();
QPoint pos = globalPos;
pos.setY(globalPos.y() + 18);
if (pos.y() + height() > screenRect.bottom()) {
pos.setY(globalPos.y() - height() - 4);
}
if (pos.x() + width() > screenRect.right()) {
pos.setX(screenRect.right() - width() - 4);
}
move(pos);
show();
}
void WingSignatureTooltip::hideTooltip() {
hide();
m_labelSignature->clear();
m_textDoc->clear();
}
bool WingSignatureTooltip::isDocHelpVisible() const { return m_textDoc; }
void WingSignatureTooltip::nextSignature() {
if (m_signatures.isEmpty())
return;
m_currentIndex = (m_currentIndex + 1) % m_signatures.count();
updateDisplay();
emit signatureChanged(m_currentIndex);
}
void WingSignatureTooltip::prevSignature() {
if (m_signatures.isEmpty())
return;
m_currentIndex =
(m_currentIndex - 1 + m_signatures.count()) % m_signatures.count();
updateDisplay();
emit signatureChanged(m_currentIndex);
}
void WingSignatureTooltip::setHelpDocVisible(bool visible) {
m_textDoc->setVisible(visible);
}
void WingSignatureTooltip::wheelEvent(QWheelEvent *ev) {
if (ev->angleDelta().y() > 0)
prevSignature();
else
nextSignature();
ev->accept();
}
void WingSignatureTooltip::updateDisplay() {
if (m_signatures.isEmpty())
return;
const Signature &s = m_signatures.at(m_currentIndex);
m_labelSignature->setText(s.label);
if (s.doc.isEmpty()) {
m_textDoc->setVisible(false);
} else {
m_textDoc->clear();
switch (m_sigHelpType) {
case SignatureHelpType::PlainText:
m_textDoc->setPlainText(s.doc);
break;
case SignatureHelpType::MarkDown:
m_textDoc->setMarkdown(s.doc);
break;
case SignatureHelpType::Html:
m_textDoc->setHtml(s.doc);
break;
}
m_textDoc->setVisible(true);
}
m_countLabel->setText(QStringLiteral("▴ %1 / %2 ▾")
.arg(m_currentIndex + 1)
.arg(m_signatures.count()));
m_countLabel->show();
adjustSize();
}