File indexing completed on 2025-05-04 05:17:22

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "appmainwindow.h"
0008 #include "libkommitwidgets_appdebug.h"
0009 
0010 #include <QCloseEvent>
0011 #include <QEventLoop>
0012 
0013 AppMainWindow::AppMainWindow(QWidget *parent, Qt::WindowFlags f)
0014 #ifdef LIBKOMMIT_WIDGET_USE_KF
0015     : KXmlGuiWindow(parent, f)
0016 #else
0017     : QMainWindow{parent}
0018 #endif
0019 {
0020 }
0021 
0022 int AppMainWindow::exec()
0023 {
0024     QEventLoop eventLoop;
0025     mLoop = &eventLoop;
0026     showModal();
0027     (void)eventLoop.exec(QEventLoop::DialogExec);
0028     mLoop = nullptr;
0029     qCDebug(KOMMIT_WIDGETS_LOG()) << "returnCode=" << mReturnCode;
0030     return mReturnCode;
0031 }
0032 
0033 void AppMainWindow::showModal()
0034 {
0035     mIsModal = true;
0036     setWindowModality(Qt::ApplicationModal);
0037     setAttribute(Qt::WA_DeleteOnClose, true);
0038     show();
0039 }
0040 
0041 void AppMainWindow::accept()
0042 {
0043     mReturnCode = Accepted;
0044 }
0045 
0046 void AppMainWindow::reject()
0047 {
0048     mReturnCode = Rejected;
0049 }
0050 
0051 void AppMainWindow::setVisible(bool visible)
0052 {
0053     if (!visible && mLoop)
0054         mLoop->exit();
0055     KXmlGuiWindow::setVisible(visible);
0056 }
0057 
0058 // void AppMainWindow::closeDialog(int resultCode)
0059 //{
0060 //     _returnCode = resultCode;
0061 //     if (_loop && _loop->isRunning())
0062 //         _loop->quit();
0063 // }
0064 
0065 // void AppMainWindow::closeEvent(QCloseEvent *event)
0066 //{
0067 //     qCDebug(KOMMIT_LOG) << Q_FUNC_INFO;
0068 //     Q_UNUSED(event)
0069 //     if (_loop && _loop->isRunning())
0070 //         _loop->quit();
0071 //     KXmlGuiWindow::closeEvent(event);
0072 // }
0073 
0074 void AppMainWindow::keyPressEvent(QKeyEvent *event)
0075 {
0076     if (event->key() == Qt::Key_Escape && mIsModal)
0077         close();
0078 
0079     KXmlGuiWindow::keyPressEvent(event);
0080 }
0081 
0082 #include "moc_appmainwindow.cpp"