File indexing completed on 2024-06-16 04:47:32
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 * A skrooge plugin to track transactions. 0008 * 0009 * @author Stephane MANKOWSKI 0010 */ 0011 #include "skgtrackerpluginwidget.h" 0012 0013 #include <qdom.h> 0014 #include <qevent.h> 0015 0016 #include "skgdocumentbank.h" 0017 #include "skgmainpanel.h" 0018 #include "skgobjectmodel.h" 0019 #include "skgtraces.h" 0020 #include "skgtrackerobject.h" 0021 #include "skgtransactionmng.h" 0022 0023 SKGTrackerPluginWidget::SKGTrackerPluginWidget(QWidget* iParent, SKGDocument* iDocument) 0024 : SKGTabPage(iParent, iDocument) 0025 { 0026 SKGTRACEINFUNC(1) 0027 if (iDocument == nullptr) { 0028 return; 0029 } 0030 0031 ui.setupUi(this); 0032 0033 // Set show widget 0034 ui.kView->getShowWidget()->addGroupedItem(QStringLiteral("all"), i18n("All"), QLatin1String(""), QLatin1String(""), QLatin1String(""), Qt::META + Qt::Key_A); 0035 ui.kView->getShowWidget()->addGroupedItem(QStringLiteral("opened"), i18n("Opened"), QStringLiteral("vcs-normal"), QStringLiteral("t_close='N'"), QLatin1String(""), Qt::META + Qt::Key_O); 0036 ui.kView->getShowWidget()->addGroupedItem(QStringLiteral("closed"), i18n("Closed"), QStringLiteral("vcs-conflicting"), QStringLiteral("t_close='Y'"), QLatin1String(""), Qt::META + Qt::Key_C); 0037 ui.kView->getShowWidget()->setDefaultState(QStringLiteral("opened")); 0038 0039 ui.kNameLbl->setText(i18n("%1:", iDocument->getDisplay(QStringLiteral("t_name")))); 0040 ui.kCommentLabel->setText(i18n("%1:", iDocument->getDisplay(QStringLiteral("t_comment")))); 0041 0042 ui.kAddButton->setIcon(SKGServices::fromTheme(QStringLiteral("list-add"))); 0043 ui.kModifyButton->setIcon(SKGServices::fromTheme(QStringLiteral("dialog-ok"))); 0044 0045 ui.kView->setModel(new SKGObjectModel(qobject_cast<SKGDocumentBank*>(getDocument()), QStringLiteral("v_refund_display"), QStringLiteral("1=0"), this, QLatin1String(""), false)); 0046 0047 ui.kView->getView()->resizeColumnToContents(0); 0048 0049 connect(getDocument(), &SKGDocument::tableModified, this, &SKGTrackerPluginWidget::dataModified, Qt::QueuedConnection); 0050 connect(ui.kView->getView(), &SKGTreeView::clickEmptyArea, this, &SKGTrackerPluginWidget::cleanEditor); 0051 connect(ui.kView->getView(), &SKGTreeView::doubleClicked, SKGMainPanel::getMainPanel()->getGlobalAction(QStringLiteral("open")).data(), &QAction::trigger); 0052 connect(ui.kView->getView(), &SKGTreeView::selectionChangedDelayed, this, [ = ] {this->onSelectionChanged();}); 0053 0054 connect(ui.kAddButton, &QPushButton::clicked, this, &SKGTrackerPluginWidget::onAddTracker); 0055 connect(ui.kModifyButton, &QPushButton::clicked, this, &SKGTrackerPluginWidget::onModifyTracker); 0056 connect(ui.kNameInput, &QLineEdit::textChanged, this, &SKGTrackerPluginWidget::onEditorModified); 0057 0058 // Set Event filters to catch CTRL+ENTER or SHIFT+ENTER 0059 this->installEventFilter(this); 0060 0061 dataModified(QLatin1String(""), 0); 0062 } 0063 0064 SKGTrackerPluginWidget::~SKGTrackerPluginWidget() = default; 0065 0066 bool SKGTrackerPluginWidget::eventFilter(QObject* iObject, QEvent* iEvent) 0067 { 0068 if ((iEvent != nullptr) && iEvent->type() == QEvent::KeyPress) { 0069 auto* keyEvent = dynamic_cast<QKeyEvent*>(iEvent); 0070 if (keyEvent && (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) && iObject == this) { 0071 if ((QApplication::keyboardModifiers() & Qt::ControlModifier) != 0u && ui.kAddButton->isEnabled()) { 0072 ui.kAddButton->click(); 0073 } else if ((QApplication::keyboardModifiers() &Qt::ShiftModifier) != 0u && ui.kModifyButton->isEnabled()) { 0074 ui.kModifyButton->click(); 0075 } 0076 } 0077 } 0078 0079 return SKGTabPage::eventFilter(iObject, iEvent); 0080 } 0081 0082 void SKGTrackerPluginWidget::onSelectionChanged() 0083 { 0084 SKGTRACEINFUNC(10) 0085 0086 int nbSelect = ui.kView->getView()->getNbSelectedObjects(); 0087 if (nbSelect == 1) { 0088 SKGTrackerObject obj(ui.kView->getView()->getFirstSelectedObject()); 0089 0090 ui.kNameInput->setText(obj.getName()); 0091 ui.kCommentEdit->setText(obj.getComment()); 0092 } else if (nbSelect > 1) { 0093 ui.kNameInput->setText(NOUPDATE); 0094 ui.kCommentEdit->setText(NOUPDATE); 0095 } 0096 0097 onEditorModified(); 0098 Q_EMIT selectionChanged(); 0099 } 0100 0101 QString SKGTrackerPluginWidget::getState() 0102 { 0103 SKGTRACEINFUNC(10) 0104 QDomDocument doc(QStringLiteral("SKGML")); 0105 QDomElement root = doc.createElement(QStringLiteral("parameters")); 0106 doc.appendChild(root); 0107 root.setAttribute(QStringLiteral("view"), ui.kView->getState()); 0108 return doc.toString(); 0109 } 0110 0111 void SKGTrackerPluginWidget::setState(const QString& iState) 0112 { 0113 SKGTRACEINFUNC(10) 0114 QDomDocument doc(QStringLiteral("SKGML")); 0115 doc.setContent(iState); 0116 QDomElement root = doc.documentElement(); 0117 ui.kView->setState(root.attribute(QStringLiteral("view"))); 0118 } 0119 0120 QString SKGTrackerPluginWidget::getDefaultStateAttribute() 0121 { 0122 return QStringLiteral("SKGREFUND_DEFAULT_PARAMETERS"); 0123 } 0124 0125 QWidget* SKGTrackerPluginWidget::mainWidget() 0126 { 0127 return ui.kView->getView(); 0128 } 0129 0130 void SKGTrackerPluginWidget::onEditorModified() 0131 { 0132 _SKGTRACEINFUNC(10) 0133 int nb = ui.kView->getView()->getNbSelectedObjects(); 0134 ui.kModifyButton->setEnabled(!ui.kNameInput->text().isEmpty() && nb >= 1); 0135 ui.kAddButton->setEnabled(!ui.kNameInput->text().isEmpty() && 0136 !ui.kNameInput->text().startsWith(QLatin1Char('='))); 0137 } 0138 0139 void SKGTrackerPluginWidget::dataModified(const QString& iTableName, int iIdTransaction, bool iLightTransaction) 0140 { 0141 SKGTRACEINFUNC(10) 0142 Q_UNUSED(iIdTransaction) 0143 0144 if (!iLightTransaction) { 0145 if (iTableName == QStringLiteral("refund") || iTableName.isEmpty()) { 0146 // Set completions 0147 SKGMainPanel::fillWithDistinctValue(QList<QWidget*>() << ui.kNameInput, getDocument(), QStringLiteral("refund"), QStringLiteral("t_name"), QLatin1String(""), true); 0148 SKGMainPanel::fillWithDistinctValue(QList<QWidget*>() << ui.kCommentEdit, getDocument(), QStringLiteral("refund"), QStringLiteral("t_comment"), QLatin1String(""), true); 0149 } 0150 } 0151 } 0152 0153 void SKGTrackerPluginWidget::onAddTracker() 0154 { 0155 SKGError err; 0156 _SKGTRACEINFUNCRC(10, err) 0157 0158 QString name = ui.kNameInput->text(); 0159 SKGTrackerObject tracker; 0160 { 0161 SKGBEGINTRANSACTION(*getDocument(), i18nc("Noun, name of the user action", "Tracker creation '%1'", name), err) 0162 0163 err = SKGTrackerObject::createTracker(qobject_cast<SKGDocumentBank*>(getDocument()), name, tracker); 0164 IFOKDO(err, tracker.setComment(ui.kCommentEdit->text())) 0165 IFOKDO(err, tracker.save()) 0166 0167 // Send message 0168 IFOKDO(err, tracker.getDocument()->sendMessage(i18nc("An information to the user", "The tracker '%1' have been added", tracker.getDisplayName()), SKGDocument::Hidden)) 0169 } 0170 0171 // status bar 0172 IFOK(err) { 0173 err = SKGError(0, i18nc("Successful message after an user action", "Tracker '%1' created", name)); 0174 ui.kView->getView()->selectObject(tracker.getUniqueID()); 0175 } else { 0176 err.addError(ERR_FAIL, i18nc("Error message", "Tracker creation failed")); 0177 } 0178 0179 // Display error 0180 SKGMainPanel::displayErrorMessage(err, true); 0181 } 0182 0183 void SKGTrackerPluginWidget::onModifyTracker() 0184 { 0185 SKGError err; 0186 _SKGTRACEINFUNCRC(10, err) 0187 // Get Selection 0188 SKGObjectBase::SKGListSKGObjectBase selection = getSelectedObjects(); 0189 0190 int nb = selection.count(); 0191 { 0192 SKGBEGINPROGRESSTRANSACTION(*getDocument(), i18nc("Noun, name of the user action", "Tracker update"), err, nb) 0193 for (int i = 0; !err && i < nb; ++i) { 0194 // Modification of object 0195 SKGTrackerObject tracker(selection.at(i)); 0196 err = tracker.setName(ui.kNameInput->text()); 0197 IFOKDO(err, tracker.setComment(ui.kCommentEdit->text())) 0198 IFOKDO(err, tracker.save()) 0199 0200 // Send message 0201 IFOKDO(err, getDocument()->sendMessage(i18nc("An information to the user", "The tracker '%1' has been updated", tracker.getDisplayName()), SKGDocument::Hidden)) 0202 0203 IFOKDO(err, getDocument()->stepForward(i + 1)) 0204 } 0205 } 0206 // status bar 0207 IFOKDO(err, SKGError(0, i18nc("Successful message after an user action", "Tracker updated"))) 0208 else { 0209 err.addError(ERR_FAIL, i18nc("Error message", "Tracker update failed")); 0210 } 0211 0212 // Display error 0213 SKGMainPanel::displayErrorMessage(err, true); 0214 0215 // Set focus on table 0216 ui.kView->getView()->setFocus(); 0217 } 0218 0219 void SKGTrackerPluginWidget::cleanEditor() 0220 { 0221 if (getNbSelectedObjects() == 0) { 0222 ui.kNameInput->setText(QLatin1String("")); 0223 ui.kCommentEdit->setText(QLatin1String("")); 0224 } 0225 } 0226 0227 void SKGTrackerPluginWidget::activateEditor() 0228 { 0229 ui.kNameInput->setFocus(); 0230 } 0231 0232 bool SKGTrackerPluginWidget::isEditor() 0233 { 0234 return true; 0235 } 0236 0237