1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2021 S. MANKOWSKI stephane@mankowski.fr
3  * SPDX-FileCopyrightText: 2021 G. DE BURE support@mankowski.fr
4  * SPDX-License-Identifier: GPL-3.0-or-later
5  ***************************************************************************/
6 /** @file
7  * This file is a plugin for undoredo management.
8  *
9  * @author Stephane MANKOWSKI / Guillaume DE BURE
10  */
11 #include "skgundoredoplugindockwidget.h"
12 
13 #include <qheaderview.h>
14 
15 #include "skgdocument.h"
16 #include "skgmainpanel.h"
17 #include "skgobjectmodelbase.h"
18 #include "skgtraces.h"
19 
SKGUndoRedoPluginDockWidget(QWidget * iParent,SKGDocument * iDocument)20 SKGUndoRedoPluginDockWidget::SKGUndoRedoPluginDockWidget(QWidget* iParent, SKGDocument* iDocument)
21     : SKGWidget(iParent, iDocument)
22 {
23     SKGTRACEINFUNC(1)
24     if (iDocument == nullptr) {
25         return;
26     }
27 
28     ui.setupUi(this);
29 
30     QPalette newPalette = QApplication::palette();
31     newPalette.setColor(QPalette::Base, Qt::transparent);
32     ui.kTransactionList->setPalette(newPalette);
33 
34     auto modelview = new SKGObjectModelBase(getDocument(), QStringLiteral("doctransaction"), QStringLiteral("1=1 ORDER BY d_date DESC, id DESC"), this);
35     ui.kTransactionList->setModel(modelview);
36     ui.kTransactionList->header()->hide();
37 
38     QAction* act = SKGMainPanel::getMainPanel()->getGlobalAction(QStringLiteral("edit_clear_history"));
39     if (act != nullptr) {
40         ui.kClearHistoryBtn->setIcon(act->icon());
41         connect(ui.kClearHistoryBtn, &QPushButton::clicked, act, &QAction::trigger);
42     }
43 
44     ui.kTransactionList->setDefaultSaveParameters(getDocument(), QStringLiteral("SKG_DEFAULT_UNDOREDO"));
45 
46     connect(ui.kTransactionList, &SKGTableView::doubleClicked, this, &SKGUndoRedoPluginDockWidget::onUndoRedo);
47     connect(ui.kTransactionList, &SKGTableView::selectionChangedDelayed, this, &SKGUndoRedoPluginDockWidget::selectionChanged);
48     connect(getDocument(), &SKGDocument::transactionSuccessfullyEnded, ui.kTransactionList, &SKGTableView::resizeColumnsToContentsDelayed, Qt::QueuedConnection);
49 
50     ui.kTransactionList->setTextResizable(false);
51 }
52 
~SKGUndoRedoPluginDockWidget()53 SKGUndoRedoPluginDockWidget::~SKGUndoRedoPluginDockWidget()
54 {
55     SKGTRACEINFUNC(1)
56 }
57 
mainWidget()58 QWidget* SKGUndoRedoPluginDockWidget::mainWidget()
59 {
60     return ui.kTransactionList;
61 }
62 
onClearHistory()63 void SKGUndoRedoPluginDockWidget::onClearHistory()
64 {
65     SKGTRACEINFUNC(1)
66     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
67     SKGError err = getDocument()->removeAllTransactions();
68     QApplication::restoreOverrideCursor();
69 
70     // status bar
71     IFOKDO(err, SKGError(0, i18nc("Message for successful user action", "Clear history successfully done.")))
72     else {
73         err.addError(ERR_FAIL, i18nc("Error message", "Clear history failed"));
74     }
75 
76     // Display error
77     SKGMainPanel::displayErrorMessage(err);
78 }
79 
onUndoRedo(const QModelIndex & index)80 void SKGUndoRedoPluginDockWidget::onUndoRedo(const QModelIndex& index)
81 {
82     SKGTRACEINFUNC(1)
83     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
84 
85     // Get Selection
86     SKGError err;
87     SKGDocument::UndoRedoMode mode = SKGDocument::UNDO;
88     auto* model = qobject_cast<SKGObjectModelBase*>(ui.kTransactionList->model());
89     if (model != nullptr) {
90         SKGObjectBase obj = model->getObject(index);
91         int id = obj.getID();
92         int lastExecuted = -1;
93         mode = (obj.getAttribute(QStringLiteral("t_mode")) == QStringLiteral("U") ? SKGDocument::UNDO : SKGDocument::REDO);
94         do {
95             lastExecuted = getDocument()->getTransactionToProcess(mode);
96             err = getDocument()->undoRedoTransaction(mode);
97         } while (!err && lastExecuted != id);
98     }
99     QApplication::restoreOverrideCursor();
100 
101     // status bar
102     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.")))
103     else {
104         err.addError(ERR_FAIL, mode == SKGDocument::UNDO ? i18nc("Error message", "Undo failed") : i18nc("Error message", "Redo failed"));
105     }
106 
107     // Display error
108     SKGMainPanel::displayErrorMessage(err);
109 }
110