-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwingcodepopup.cpp
More file actions
80 lines (65 loc) · 2.46 KB
/
wingcodepopup.cpp
File metadata and controls
80 lines (65 loc) · 2.46 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
/****************************************************************************
**
** 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 "wingcodepopup.h"
#include "wingcodeedit.h"
#include <QKeyEvent>
#include <QScrollBar>
#include <QStyleFactory>
#include <QToolTip>
constexpr auto PADDING = 3;
WingCodePopup::WingCodePopup(WingCodeEdit *editor) : QListView() {
// Changes listview properties
setBatchSize(10);
setMovement(Static);
setFlow(TopToBottom);
setLayoutMode(Batched);
setUniformItemSizes(true);
setEditTriggers(NoEditTriggers);
setSelectionMode(SingleSelection);
setCursor(Qt::ArrowCursor);
setSelectionBehavior(SelectRows);
setFocusPolicy(Qt::StrongFocus);
auto fontMetric = QFontMetrics(font());
setIconSize(QSize(fontMetric.height(), fontMetric.height()));
this->setPalette(editor->palette());
setStyleSheet(QStringLiteral("QListView{outline: 0;}"));
setMinimumWidth(200);
setMaximumWidth(600);
connect(editor, &WingCodeEdit::themeChanged, this,
[this, editor] { this->setPalette(editor->palette()); });
}
void WingCodePopup::setModel(QAbstractItemModel *model) {
QListView::setModel(model);
if (selectionModel()) { // Ensure the selection model exists
connect(selectionModel(), &QItemSelectionModel::currentChanged, this,
&WingCodePopup::showTooltip);
}
}
void WingCodePopup::showEvent(QShowEvent *e) {
QListView::showEvent(e);
verticalScrollBar()->setValue(0);
horizontalScrollBar()->setValue(0);
}
void WingCodePopup::showTooltip(const QModelIndex ¤t) {
if (!current.isValid())
return;
QRect rect = visualRect(current); // Get the item's position
QPoint pos =
mapToGlobal(QPoint(rect.right(), rect.top())); // Map to global position
QString tooltipText = model()->data(current, Qt::ToolTipRole).toString();
if (!tooltipText.isEmpty()) {
QToolTip::showText(pos, tooltipText, this);
}
}