File indexing completed on 2024-05-12 07:41:25

0001 /*
0002     File                 : HistoryDialog.cpp
0003     Project              : LabPlot
0004     Description          : history dialog
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2012-2019 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "HistoryDialog.h"
0011 #include "backend/core/Settings.h"
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 
0016 #include <KWindowConfig>
0017 #include <kcoreaddons_version.h>
0018 
0019 #include <QDialogButtonBox>
0020 #include <QPushButton>
0021 #include <QUndoStack>
0022 #include <QUndoView>
0023 #include <QVBoxLayout>
0024 #include <QWindow>
0025 
0026 /*!
0027     \class HistoryDialog
0028     \brief Display the content of project's undo stack.
0029 
0030     \ingroup kdefrontend
0031  */
0032 HistoryDialog::HistoryDialog(QWidget* parent, QUndoStack* stack, const QString& emptyLabel)
0033     : QDialog(parent)
0034     , m_undoStack(stack) {
0035     auto* undoView = new QUndoView(stack, this);
0036     undoView->setCleanIcon(QIcon::fromTheme(QLatin1String("edit-clear-history")));
0037     undoView->setEmptyLabel(emptyLabel);
0038     undoView->setMinimumWidth(350);
0039     undoView->setWhatsThis(
0040         i18n("List of all performed steps/actions.\n"
0041              "Select an item in the list to navigate to the corresponding step."));
0042 
0043     setWindowIcon(QIcon::fromTheme(QLatin1String("view-history")));
0044     setWindowTitle(i18nc("@title:window", "Undo/Redo History"));
0045     setAttribute(Qt::WA_DeleteOnClose);
0046     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0047     m_okButton = btnBox->button(QDialogButtonBox::Ok);
0048 
0049     connect(btnBox->button(QDialogButtonBox::Cancel), &QPushButton::clicked, this, &HistoryDialog::close);
0050     connect(btnBox, &QDialogButtonBox::accepted, this, &HistoryDialog::accept);
0051     connect(btnBox, &QDialogButtonBox::rejected, this, &HistoryDialog::reject);
0052 
0053     if (stack->count()) {
0054         m_clearUndoStackButton = new QPushButton;
0055         btnBox->addButton(m_clearUndoStackButton, QDialogButtonBox::ActionRole);
0056         m_clearUndoStackButton->setText(i18n("&Clear"));
0057         m_clearUndoStackButton->setToolTip(i18n("Clears the undo history. Commands are not undone or redone; the state of the project remains unchanged."));
0058         m_clearUndoStackButton->setIcon(QIcon::fromTheme(QLatin1String("edit-clear")));
0059         connect(m_clearUndoStackButton, &QPushButton::clicked, this, &HistoryDialog::clearUndoStack);
0060     }
0061 
0062     auto* line = new QFrame;
0063     line->setFrameShape(QFrame::HLine);
0064     line->setFrameShadow(QFrame::Sunken);
0065 
0066     auto* layout = new QVBoxLayout;
0067 
0068     layout->addWidget(undoView);
0069     layout->addWidget(line);
0070     layout->addWidget(btnBox);
0071 
0072     setLayout(layout);
0073 
0074     // restore saved settings if available
0075     create(); // ensure there's a window created
0076     KConfigGroup conf = Settings::group(QStringLiteral("HistoryDialog"));
0077     if (conf.exists()) {
0078         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0079         resize(windowHandle()->size()); // workaround for QTBUG-40584
0080     } else
0081         resize(QSize(500, 300).expandedTo(minimumSize()));
0082 }
0083 
0084 HistoryDialog::~HistoryDialog() {
0085     // save dialog size
0086     KConfigGroup conf = Settings::group(QStringLiteral("HistoryDialog"));
0087     KWindowConfig::saveWindowSize(windowHandle(), conf);
0088 }
0089 
0090 void HistoryDialog::clearUndoStack() {
0091 #if KCOREADDONS_VERSION >= QT_VERSION_CHECK(5, 100, 0)
0092     if (KMessageBox::questionTwoActions(this,
0093                                         i18n("Do you really want to clear the undo history?"),
0094                                         i18n("Clear History"),
0095                                         KStandardGuiItem::clear(),
0096                                         KStandardGuiItem::cancel())
0097         == KMessageBox::PrimaryAction)
0098 #else
0099     if (KMessageBox::questionYesNo(this, i18n("Do you really want to clear the undo history?"), i18n("Clear History")) == KMessageBox::Yes)
0100 #endif
0101         m_undoStack->clear();
0102 }