diff --git a/kate/katemainwindow.h b/kate/katemainwindow.h --- a/kate/katemainwindow.h +++ b/kate/katemainwindow.h @@ -584,6 +584,7 @@ protected: bool event(QEvent *e) override; void mousePressEvent(QMouseEvent *e) override; + void closeEvent(QCloseEvent *e) override; }; #endif diff --git a/kate/katemainwindow.cpp b/kate/katemainwindow.cpp --- a/kate/katemainwindow.cpp +++ b/kate/katemainwindow.cpp @@ -1191,6 +1191,55 @@ } } +void KateMainWindow::closeEvent(QCloseEvent *e) +{ + // Find out if Kate is closed directly by the user or + // by the session manager because the session is closed + const bool closedByUser = !qApp->isSavingSession(); + const bool multipleDocumentsOpen = KateApp::self()->documentManager()->documentList().count() > 1; + const bool isLastWindow = KateApp::self()->mainWindowsCount() == 1; + const bool askConfirmation = closedByUser && multipleDocumentsOpen && isLastWindow; + + if (askConfirmation) { + QDialog *dialog = new QDialog(this, Qt::Dialog); + dialog->setWindowTitle(i18nc("@title:window", "Confirmation")); + dialog->setModal(true); + QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Yes | QDialogButtonBox::No); + KGuiItem::assign(buttons->button(QDialogButtonBox::Yes), KGuiItem(i18nc("@action:button 'Quit Kate' button", "&Quit %1", QGuiApplication::applicationDisplayName()), QIcon::fromTheme(QStringLiteral("application-exit")))); + KGuiItem::assign(buttons->button(QDialogButtonBox::No), KGuiItem(i18n("C&lose Current Tab"), QIcon::fromTheme(QStringLiteral("tab-close")))); + buttons->button(QDialogButtonBox::Yes)->setDefault(true); + + bool doNotAskAgainCheckboxResult = false; + + auto view = viewManager()->activeView(); + const auto result = KMessageBox::createKMessageBox(dialog, + buttons, + QMessageBox::Warning, + i18n("You have multiple documents open, are you sure you want to quit?"), + QStringList(), + i18n("Do not ask again"), + &doNotAskAgainCheckboxResult, + KMessageBox::Notify); + + if (doNotAskAgainCheckboxResult) { + readOptions(); + } + + switch (result) { + case QDialogButtonBox::Yes: + break; + case QDialogButtonBox::No: + KateApp::self()->documentManager()->closeDocument(view->document()); + Q_FALLTHROUGH(); + default: + e->ignore(); + return; + } + saveOptions(); + } + KXmlGuiWindow::closeEvent(e); +} + void KateMainWindow::slotFocusPrevTab() { if (m_viewManager->activeViewSpace()) {