thedesk/platform/messagedialog/messagedialoghelper.cpp

154 lines
5 KiB
C++
Raw Normal View History

2020-07-21 02:44:00 -04:00
/****************************************
*
* INSERT-PROJECT-NAME-HERE - INSERT-GENERIC-NAME-HERE
* Copyright (C) 2020 Victor Tran
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* *************************************/
#include "messagedialoghelper.h"
2022-07-10 08:04:09 -04:00
#include "messagedialog.h"
2020-12-21 02:47:51 -05:00
#include <QApplication>
#include <QDebug>
#include <QGraphicsBlurEffect>
#include <QMainWindow>
2022-07-10 08:04:09 -04:00
#include <QPainter>
#include <QPointer>
#include <QWindow>
2020-12-21 02:47:51 -05:00
#include <tcsdtools.h>
2022-07-10 08:04:09 -04:00
#include <tscrim.h>
2020-12-21 02:47:51 -05:00
#include <tvariantanimation.h>
2020-07-21 02:44:00 -04:00
struct MessageDialogHelperPrivate {
2022-07-10 08:04:09 -04:00
MessageDialog* dlg;
QEventLoop eventLoop;
QPointer<QWindow> parent;
QPointer<QWidget> parentWidget;
QPointer<QWidget> cover2;
2020-12-21 02:47:51 -05:00
2023-09-20 06:29:02 -04:00
const int coverOverscan = 50;
2020-07-21 02:44:00 -04:00
};
2022-07-10 08:04:09 -04:00
MessageDialogHelper::MessageDialogHelper() :
QPlatformMessageDialogHelper() {
2020-07-21 02:44:00 -04:00
d = new MessageDialogHelperPrivate();
d->dlg = new MessageDialog();
2023-09-20 06:29:02 -04:00
connect(d->dlg, &MessageDialog::clicked, this, [this] {
2020-12-21 02:47:51 -05:00
if (d->eventLoop.isRunning()) d->eventLoop.exit();
2022-07-10 08:04:09 -04:00
this->hide();
2020-12-21 02:47:51 -05:00
});
2023-09-20 06:29:02 -04:00
connect(d->dlg, &MessageDialog::checkBoxStateChanged, this, &MessageDialogHelper::checkBoxStateChanged);
2020-07-21 02:44:00 -04:00
connect(d->dlg, &MessageDialog::clicked, this, &MessageDialogHelper::clicked);
}
MessageDialogHelper::~MessageDialogHelper() {
d->dlg->deleteLater();
2020-12-21 02:47:51 -05:00
if (d->cover2) d->cover2->deleteLater();
2020-07-21 02:44:00 -04:00
delete d;
}
void MessageDialogHelper::updateWindowGeometry() {
2020-12-21 02:47:51 -05:00
if (d->parent) {
QRect geometry;
if (d->parentWidget) {
geometry = QRect(-d->coverOverscan, -d->coverOverscan, d->parentWidget->width() + 2 * d->coverOverscan, d->parentWidget->height() + 2 * d->coverOverscan);
} else {
geometry = QRect(-d->coverOverscan, -d->coverOverscan, d->parent->width() + 2 * d->coverOverscan, d->parent->height() + 2 * d->coverOverscan);
}
if (d->cover2) d->cover2->setGeometry(geometry);
d->dlg->updateGeometry();
} else {
d->dlg->setFixedSize(d->dlg->frameGeometry().size());
}
2020-07-21 02:44:00 -04:00
}
void MessageDialogHelper::exec() {
d->dlg->setOptions(this->options());
2020-12-21 02:47:51 -05:00
d->eventLoop.exec();
2020-07-21 02:44:00 -04:00
}
bool MessageDialogHelper::show(Qt::WindowFlags windowFlags, Qt::WindowModality windowModality, QWindow* parent) {
2020-12-21 02:47:51 -05:00
QWidget* widget = nullptr;
if (parent) {
for (QWidget* w : QApplication::allWidgets()) {
if (w->winId() == parent->winId()) widget = w->window();
}
}
QMainWindow* mainWindow = qobject_cast<QMainWindow*>(widget);
if (mainWindow && tCsdTools::csdsInstalled(mainWindow)) widget = mainWindow->centralWidget();
d->parentWidget = widget;
2020-07-21 02:44:00 -04:00
d->dlg->setOptions(this->options());
d->dlg->setWindowFlags(windowFlags);
if (d->parent) {
d->parent->disconnect(this);
}
d->parent = parent;
connect(parent, &QWindow::widthChanged, this, &MessageDialogHelper::updateWindowGeometry);
connect(parent, &QWindow::heightChanged, this, &MessageDialogHelper::updateWindowGeometry);
connect(parent, &QWindow::xChanged, this, &MessageDialogHelper::updateWindowGeometry);
connect(parent, &QWindow::yChanged, this, &MessageDialogHelper::updateWindowGeometry);
2020-12-21 02:47:51 -05:00
if (widget) {
tScrim::scrimForWidget(widget)->show();
d->dlg->setParent(widget);
d->dlg->show();
d->dlg->raise();
2022-07-10 08:04:09 -04:00
// For some reason, Qt doesn't render this properly on tCsd apps
// but it fixes itself if we have a transparent widget on top
2020-12-21 02:47:51 -05:00
d->cover2 = new QWidget();
d->cover2->setAttribute(Qt::WA_TransparentForMouseEvents);
d->cover2->setParent(widget);
d->cover2->show();
d->cover2->raise();
d->dlg->animateIn();
}
2020-07-21 02:44:00 -04:00
updateWindowGeometry();
2020-12-21 02:47:51 -05:00
d->dlg->show();
2020-07-21 02:44:00 -04:00
return true;
}
void MessageDialogHelper::hide() {
2020-12-21 02:47:51 -05:00
if (d->parentWidget) {
d->dlg->animateOut();
tScrim::scrimForWidget(d->parentWidget)->hide();
if (d->cover2) d->cover2->hide();
} else {
d->dlg->hide();
if (d->cover2) d->cover2->hide();
}
2020-07-21 02:44:00 -04:00
}
void MessageDialogHelper::setOptions(const QSharedPointer<QMessageDialogOptions>& options) {
QPlatformMessageDialogHelper::setOptions(options);
d->dlg->setOptions(options);
}
2023-09-20 06:19:51 -04:00
QVariant MessageDialogHelper::styleHint(StyleHint hint) const {
if (hint == DialogIsQtWindow) {
return true;
}
return QPlatformMessageDialogHelper::StyleHint(hint);
}