1 // SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
2 // SPDX-FileCopyrightText: 2021 Nheko Contributors
3 //
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 
6 #include <QPainter>
7 #include <QVBoxLayout>
8 
9 #include "OverlayModal.h"
10 
OverlayModal(QWidget * parent)11 OverlayModal::OverlayModal(QWidget *parent)
12   : OverlayWidget(parent)
13   , color_{QColor(30, 30, 30, 170)}
14 {
15     layout_ = new QVBoxLayout(this);
16     layout_->setSpacing(0);
17     layout_->setContentsMargins(10, 40, 10, 20);
18     setContentAlignment(Qt::AlignCenter);
19 }
20 
21 void
setWidget(QWidget * widget)22 OverlayModal::setWidget(QWidget *widget)
23 {
24     // Delete the previous widget
25     if (layout_->count() > 0) {
26         QLayoutItem *item;
27         while ((item = layout_->takeAt(0)) != nullptr) {
28             delete item->widget();
29             delete item;
30         }
31     }
32 
33     layout_->addWidget(widget);
34     content_ = widget;
35     content_->setFocus();
36 }
37 
38 void
paintEvent(QPaintEvent * event)39 OverlayModal::paintEvent(QPaintEvent *event)
40 {
41     Q_UNUSED(event);
42 
43     QPainter painter(this);
44     painter.fillRect(rect(), color_);
45 }
46 
47 void
mousePressEvent(QMouseEvent * e)48 OverlayModal::mousePressEvent(QMouseEvent *e)
49 {
50     if (isDismissible_ && content_ && !content_->geometry().contains(e->pos()))
51         hide();
52 }
53 
54 void
keyPressEvent(QKeyEvent * event)55 OverlayModal::keyPressEvent(QKeyEvent *event)
56 {
57     if (event->key() == Qt::Key_Escape) {
58         event->accept();
59         hide();
60     }
61 }
62