-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPopup.cpp
More file actions
117 lines (91 loc) · 3.36 KB
/
Popup.cpp
File metadata and controls
117 lines (91 loc) · 3.36 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
#include "Popup.h"
Popup::Popup(QWidget *parent) : RoundedBox(parent) {
// Items List
_list = new QListView(this);
// Scrollbar
vScroll = new ScrollBar(Qt::Vertical);
// List Properties
_list->setAutoScroll(true);
_list->setViewMode(QListView::ListMode);
_list->setResizeMode(QListView::Adjust);
_list->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
_list->setUniformItemSizes(true);
_list->setFrameShape(QFrame::NoFrame);
_list->setEditTriggers(QAbstractItemView::NoEditTriggers);
_list->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
_list->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
_list->setVerticalScrollBar(vScroll);
_list->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_list->setStyleSheet(
"QListView::item { margin: 0px; padding: 0px; } "
"QListView { background: transparent; margin: 4px; padding: 0px; border:none; }"
);
// Layout of Popup
layout = new QVBoxLayout(this);
layout->setContentsMargins(0,0,0,0);
layout->addSpacing(0);
layout->addWidget(_list, 0, Qt::AlignVCenter);
// Opacity Effect
smooth_opacity = new SmoothOpacity(this);
setGraphicsEffect(smooth_opacity);
// Opacity Effect Animation
animation = new QPropertyAnimation(smooth_opacity, "opacity", this);
animation->setEasingCurve(QEasingCurve::InOutQuad);
animation->setDuration(300);
hide();
}
void Popup::setPopupWidth(int width) {
explicitWidth = width;
updatePopup();
}
void Popup::updatePopup() {
if (!_list || !_list->model()) return;
int itemsHeight = _list->sizeHintForRow(0);
int totalItems = _list->model()->rowCount();
int visibleItems = std::min(totalItems, maxVisibleItems);
if (totalItems <= 0) return;
int height = (visibleItems * itemsHeight) + (_list->spacing()) * (visibleItems - 1) + 8;
_list->setVerticalScrollBarPolicy(totalItems > visibleItems ? Qt::ScrollBarAsNeeded : Qt::ScrollBarAlwaysOff);
_list->setFixedHeight(height);
int widthToUse = (explicitWidth > 0) ? explicitWidth
: (parentWidget() ? parentWidget()->width() : 150);
setFixedSize(widthToUse, height);
}
void Popup::setSelectionMode(QAbstractItemView::SelectionMode mode) {
if (_list) _list->setSelectionMode(mode);
}
void Popup::setModel(QAbstractItemModel *model) {
if (_list) _list->setModel(model);
}
void Popup::setItemDelegate(QAbstractItemDelegate *delegate) {
if (_list) _list->setItemDelegate(delegate);
}
void Popup::fadeIn() {
if (!animation) return;
animation->stop();
disconnect(animation, &QPropertyAnimation::finished, nullptr, nullptr);
animation->setStartValue(0.0);
animation->setEndValue(1.0);
show();
animation->start();
}
void Popup::fadeOut() {
if (!animation) return;
animation->stop();
disconnect(animation, &QPropertyAnimation::finished, nullptr, nullptr);
animation->setStartValue(1.0);
animation->setEndValue(0.0);
connect(animation, &QPropertyAnimation::finished, this, [this]() { hide(); });
animation->start();
}
void Popup::setMaxVisibleItems(int items) {
maxVisibleItems = items;
}
void Popup::setDarkMode(bool value) {
if (vScroll)
vScroll->setDarkMode(value);
RoundedBox::setDarkMode(value);
}
QListView* Popup::list() const {
return _list;
}