-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDialog.cpp
More file actions
101 lines (82 loc) · 2.75 KB
/
Dialog.cpp
File metadata and controls
101 lines (82 loc) · 2.75 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
#include "Dialog.h"
Overlay::Overlay(QWidget *parent) : QWidget(parent) {
setWindowFlags(Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_TransparentForMouseEvents, true);
setMouseTracking(false);
}
void Overlay::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
// Colors
QColor BG = QColor(0,0,0,80);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing);
painter.setBrush(BG);
painter.setPen(Qt::NoPen);
QPainterPath path;
path.addRoundedRect(rect().adjusted(1.5, 1.5, -1.5, -1.5), 6, 6);
painter.drawPath(path);
}
Dialog::Dialog(QWidget *centralWidget, QWidget *parent, bool hasCloseButton) : SubWindow(centralWidget->size(), parent, hasCloseButton, false), contentWidget(centralWidget) {
setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);
setWindowModality(Qt::WindowModal);
setFocusPolicy(Qt::StrongFocus);
if (parent) {
overlay = new Overlay(parent);
overlay->setGeometry(parent->rect());
overlay->hide();
parent->installEventFilter(this);
}
}
void Dialog::setDarkMode(bool value) {
if (isDarkMode == value)
return;
isDarkMode = value;
SubWindow::setDarkMode(isDarkMode);
}
void Dialog::centerInParent() {
if (parentWidget()) {
QScreen *screen = QApplication::screenAt(QCursor::pos());
if (!screen)
screen = QApplication::primaryScreen();
QRect parentRect = parentWidget()->geometry();
int x = parentRect.x() + (parentRect.width() - width()) / 2;
int y = parentRect.y() + (parentRect.height() - height()) / 2;
move(x, y);
}
}
void Dialog::showEvent(QShowEvent *event) {
if (!setupDone) {
setup();
setupDone = true;
}
if (overlay && parentWidget()) {
overlay->setGeometry(parentWidget()->rect());
overlay->show();
overlay->raise();
}
centerInParent();
this->raise();
SubWindow::showEvent(event);
}
void Dialog::resizeEvent(QResizeEvent *event) {
if (overlay && parentWidget()) overlay->setGeometry(parentWidget()->rect());
SubWindow::resizeEvent(event);
}
void Dialog::closeEvent(QCloseEvent *event) {
if (overlay) overlay->hide();
SubWindow::closeEvent(event);
}
bool Dialog::eventFilter(QObject *obj, QEvent *event) {
if (obj == parentWidget() && overlay)
if (event->type() == QEvent::Resize || event->type() == QEvent::Move)
overlay->setGeometry(parentWidget()->rect());
return SubWindow::eventFilter(obj, event);
}
void Dialog::setup() {
auto* layout = new QVBoxLayout(this->contentArea());
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
if (contentWidget)
layout->addWidget(contentWidget, 0, Qt::AlignCenter);
}