File indexing completed on 2024-06-16 04:47:18

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file is a plugin for undoredo management.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgundoredoplugindockwidget.h"
0012 
0013 #include <qheaderview.h>
0014 
0015 #include "skgdocument.h"
0016 #include "skgmainpanel.h"
0017 #include "skgobjectmodelbase.h"
0018 #include "skgtraces.h"
0019 
0020 SKGUndoRedoPluginDockWidget::SKGUndoRedoPluginDockWidget(QWidget* iParent, SKGDocument* iDocument)
0021     : SKGWidget(iParent, iDocument)
0022 {
0023     SKGTRACEINFUNC(1)
0024     if (iDocument == nullptr) {
0025         return;
0026     }
0027 
0028     ui.setupUi(this);
0029 
0030     QPalette newPalette = QApplication::palette();
0031     newPalette.setColor(QPalette::Base, Qt::transparent);
0032     ui.kTransactionList->setPalette(newPalette);
0033 
0034     auto modelview = new SKGObjectModelBase(getDocument(), QStringLiteral("doctransaction"), QStringLiteral("1=1 ORDER BY d_date DESC, id DESC"), this);
0035     ui.kTransactionList->setModel(modelview);
0036     ui.kTransactionList->header()->hide();
0037 
0038     QAction* act = SKGMainPanel::getMainPanel()->getGlobalAction(QStringLiteral("edit_clear_history"));
0039     if (act != nullptr) {
0040         ui.kClearHistoryBtn->setIcon(act->icon());
0041         connect(ui.kClearHistoryBtn, &QPushButton::clicked, act, &QAction::trigger);
0042     }
0043 
0044     ui.kTransactionList->setDefaultSaveParameters(getDocument(), QStringLiteral("SKG_DEFAULT_UNDOREDO"));
0045 
0046     connect(ui.kTransactionList, &SKGTableView::doubleClicked, this, &SKGUndoRedoPluginDockWidget::onUndoRedo);
0047     connect(ui.kTransactionList, &SKGTableView::selectionChangedDelayed, this, &SKGUndoRedoPluginDockWidget::selectionChanged);
0048     connect(getDocument(), &SKGDocument::transactionSuccessfullyEnded, ui.kTransactionList, &SKGTableView::resizeColumnsToContentsDelayed, Qt::QueuedConnection);
0049 
0050     ui.kTransactionList->setTextResizable(false);
0051 }
0052 
0053 SKGUndoRedoPluginDockWidget::~SKGUndoRedoPluginDockWidget()
0054 {
0055     SKGTRACEINFUNC(1)
0056 }
0057 
0058 QWidget* SKGUndoRedoPluginDockWidget::mainWidget()
0059 {
0060     return ui.kTransactionList;
0061 }
0062 
0063 void SKGUndoRedoPluginDockWidget::onClearHistory()
0064 {
0065     SKGTRACEINFUNC(1)
0066     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
0067     SKGError err = getDocument()->removeAllTransactions();
0068     QApplication::restoreOverrideCursor();
0069 
0070     // status bar
0071     IFOKDO(err, SKGError(0, i18nc("Message for successful user action", "Clear history successfully done.")))
0072     else {
0073         err.addError(ERR_FAIL, i18nc("Error message", "Clear history failed"));
0074     }
0075 
0076     // Display error
0077     SKGMainPanel::displayErrorMessage(err);
0078 }
0079 
0080 void SKGUndoRedoPluginDockWidget::onUndoRedo(const QModelIndex& index)
0081 {
0082     SKGTRACEINFUNC(1)
0083     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
0084 
0085     // Get Selection
0086     SKGError err;
0087     SKGDocument::UndoRedoMode mode = SKGDocument::UNDO;
0088     auto* model = qobject_cast<SKGObjectModelBase*>(ui.kTransactionList->model());
0089     if (model != nullptr) {
0090         SKGObjectBase obj = model->getObject(index);
0091         int id = obj.getID();
0092         int lastExecuted = -1;
0093         mode = (obj.getAttribute(QStringLiteral("t_mode")) == QStringLiteral("U") ? SKGDocument::UNDO : SKGDocument::REDO);
0094         do {
0095             lastExecuted = getDocument()->getTransactionToProcess(mode);
0096             err = getDocument()->undoRedoTransaction(mode);
0097         } while (!err && lastExecuted != id);
0098     }
0099     QApplication::restoreOverrideCursor();
0100 
0101     // status bar
0102     IFOKDO(err, SKGError(0, mode == SKGDocument::UNDO ? i18nc("Message for successful user action", "Undo successfully done.") : i18nc("Message for successful user action", "Redo successfully done.")))
0103     else {
0104         err.addError(ERR_FAIL, mode == SKGDocument::UNDO ? i18nc("Error message", "Undo failed") : i18nc("Error message", "Redo failed"));
0105     }
0106 
0107     // Display error
0108     SKGMainPanel::displayErrorMessage(err);
0109 }