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

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 plugin to highlight objects
0008  *
0009  * @author Stephane MANKOWSKI
0010  */
0011 #include "skghighlightplugin.h"
0012 
0013 #include <kaboutdata.h>
0014 #include <kactioncollection.h>
0015 #include <klocalizedstring.h>
0016 #include <kpluginfactory.h>
0017 #include <kstandardaction.h>
0018 
0019 #include "skgmainpanel.h"
0020 #include "skgtraces.h"
0021 #include "skgtransactionmng.h"
0022 
0023 /**
0024  * This plugin factory.
0025  */
0026 K_PLUGIN_CLASS_WITH_JSON(SKGHighlightPlugin, "metadata.json")
0027 
0028 SKGHighlightPlugin::SKGHighlightPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) :
0029     SKGInterfacePlugin(iParent), m_currentDocument(nullptr)
0030 {
0031     Q_UNUSED(iWidget)
0032     SKGTRACEINFUNC(10)
0033 }
0034 
0035 SKGHighlightPlugin::~SKGHighlightPlugin()
0036 {
0037     SKGTRACEINFUNC(10)
0038     m_currentDocument = nullptr;
0039 }
0040 
0041 bool SKGHighlightPlugin::setupActions(SKGDocument* iDocument)
0042 {
0043     SKGTRACEINFUNC(10)
0044 
0045     m_currentDocument = iDocument;
0046 
0047     setComponentName(QStringLiteral("skg_highlight"), title());
0048     setXMLFile(QStringLiteral("skg_highlight.rc"));
0049 
0050     // ------------
0051     auto actSwitchHighLight = new QAction(SKGServices::fromTheme(QStringLiteral("bookmarks")), i18nc("Verb", "Switch highlight"), this);
0052     connect(actSwitchHighLight, &QAction::triggered, this, &SKGHighlightPlugin::onSwitchHighLight);
0053     actionCollection()->setDefaultShortcut(actSwitchHighLight, Qt::CTRL + Qt::Key_H);
0054     registerGlobalAction(QStringLiteral("edit_switch_highlight"), actSwitchHighLight, QStringList() << QStringLiteral("query:type='table' AND sql LIKE '%t_bookmarked%'"), 1, -1, 301);
0055 
0056     // ------------
0057     auto actSwitchClose = new QAction(SKGServices::fromTheme(QStringLiteral("dialog-close")), i18nc("Verb", "Switch close"), this);
0058     connect(actSwitchClose, &QAction::triggered, this, &SKGHighlightPlugin::onSwitchClose);
0059     registerGlobalAction(QStringLiteral("edit_switch_close"), actSwitchClose, QStringList() << QStringLiteral("query:type='table' AND sql LIKE '%t_close%'"), 1, -1, 301);
0060 
0061     // Create yours actions here
0062     return true;
0063 }
0064 
0065 QString SKGHighlightPlugin::title() const
0066 {
0067     return toolTip();
0068 }
0069 
0070 QString SKGHighlightPlugin::icon() const
0071 {
0072     return QStringLiteral("bookmarks");
0073 }
0074 
0075 QString SKGHighlightPlugin::toolTip() const
0076 {
0077     return i18nc("The tool tip", "Highlight");
0078 }
0079 
0080 int SKGHighlightPlugin::getOrder() const
0081 {
0082     return 6;
0083 }
0084 
0085 void SKGHighlightPlugin::onSwitchClose()
0086 {
0087     SKGError err;
0088     SKGTRACEINFUNCRC(10, err)
0089     // Get Selection
0090     if ((SKGMainPanel::getMainPanel() != nullptr) && (m_currentDocument != nullptr)) {
0091         SKGObjectBase::SKGListSKGObjectBase selection = SKGMainPanel::getMainPanel()->getSelectedObjects();
0092         int nb = selection.count();
0093         {
0094             SKGBEGINPROGRESSTRANSACTION(*m_currentDocument, i18nc("Noun, name of the user action", "Close"), err, nb)
0095             for (int i = 0; !err && i < nb; ++i) {
0096                 SKGObjectBase obj(selection.at(i));
0097                 IFOKDO(err, obj.setAttribute(QStringLiteral("t_close"), obj.getAttribute(QStringLiteral("t_close")) == QStringLiteral("Y") ? QStringLiteral("N") : QStringLiteral("Y")))
0098                 IFOKDO(err, obj.save())
0099 
0100                 // Send message
0101                 IFOKDO(err, m_currentDocument->sendMessage(i18nc("An information to the user", "The close status of '%1' has been changed", obj.getDisplayName()), SKGDocument::Hidden))
0102 
0103                 IFOKDO(err, m_currentDocument->stepForward(i + 1))
0104             }
0105         }
0106 
0107         // status bar
0108         IFOKDO(err, SKGError(0, i18nc("Successful message after an user action", "Closed.")))
0109         else {
0110             err.addError(ERR_FAIL, i18nc("Error message",  "Closure failed"));
0111         }
0112 
0113         // Display error
0114         SKGMainPanel::displayErrorMessage(err);
0115     }
0116 }
0117 
0118 void SKGHighlightPlugin::onSwitchHighLight()
0119 {
0120     SKGError err;
0121     SKGTRACEINFUNCRC(10, err)
0122     // Get Selection
0123     if ((SKGMainPanel::getMainPanel() != nullptr) && (m_currentDocument != nullptr)) {
0124         SKGObjectBase::SKGListSKGObjectBase selection = SKGMainPanel::getMainPanel()->getSelectedObjects();
0125         int nb = selection.count();
0126         {
0127             SKGBEGINPROGRESSTRANSACTION(*m_currentDocument, i18nc("Noun, name of the user action", "Highlight"), err, nb)
0128             for (int i = 0; !err && i < nb; ++i) {
0129                 SKGObjectBase obj(selection.at(i));
0130                 IFOKDO(err, obj.setAttribute(QStringLiteral("t_bookmarked"), obj.getAttribute(QStringLiteral("t_bookmarked")) == QStringLiteral("Y") ? QStringLiteral("N") : QStringLiteral("Y")))
0131                 IFOKDO(err, obj.save())
0132 
0133                 // Send message
0134                 IFOKDO(err, m_currentDocument->sendMessage(i18nc("An information to the user", "The highlight status of '%1' has been changed", obj.getDisplayName()), SKGDocument::Hidden))
0135 
0136                 IFOKDO(err, m_currentDocument->stepForward(i + 1))
0137             }
0138         }
0139 
0140         // status bar
0141         IFOKDO(err, SKGError(0, i18nc("Successful message after an user action", "Highlighted.")))
0142         else {
0143             err.addError(ERR_FAIL, i18nc("Error message",  "Highlight failed"));
0144         }
0145 
0146         // Display error
0147         SKGMainPanel::displayErrorMessage(err);
0148     }
0149 }
0150 
0151 #include <skghighlightplugin.moc>