File indexing completed on 2025-02-02 04:56:55

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 select all
0008  *
0009  * @author Stephane MANKOWSKI
0010  */
0011 #include "skgselectallplugin.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 <qabstractitemview.h>
0020 
0021 #include "skgmainpanel.h"
0022 #include "skgtraces.h"
0023 #include "skgtreeview.h"
0024 /**
0025  * This plugin factory.
0026  */
0027 K_PLUGIN_CLASS_WITH_JSON(SKGSelectAllPlugin, "metadata.json")
0028 
0029 SKGSelectAllPlugin::SKGSelectAllPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) :
0030     SKGInterfacePlugin(iParent), m_currentDocument(nullptr), m_selectionMessage(nullptr)
0031 {
0032     Q_UNUSED(iWidget)
0033     SKGTRACEINFUNC(10)
0034 
0035     connect(SKGMainPanel::getMainPanel(), &SKGMainPanel::selectionChanged, this, &SKGSelectAllPlugin::onSelectionChanged);
0036 }
0037 
0038 SKGSelectAllPlugin::~SKGSelectAllPlugin()
0039 {
0040     SKGTRACEINFUNC(10)
0041     m_currentDocument = nullptr;
0042     m_selectionMessage = nullptr;
0043 }
0044 
0045 bool SKGSelectAllPlugin::setupActions(SKGDocument* iDocument)
0046 {
0047     SKGTRACEINFUNC(10)
0048 
0049     m_currentDocument = iDocument;
0050 
0051     setComponentName(QStringLiteral("skg_selectall"), title());
0052     setXMLFile(QStringLiteral("skg_selectall.rc"));
0053 
0054     // Menu
0055     registerGlobalAction(QStringLiteral("edit_select_all"), actionCollection()->addAction(KStandardAction::SelectAll, QStringLiteral("edit_select_all"), this, SLOT(onSelectAll())), QStringList(), 0);
0056     return true;
0057 }
0058 
0059 QString SKGSelectAllPlugin::title() const
0060 {
0061     return i18nc("Select all objects in a list", "Select all");
0062 }
0063 
0064 QString SKGSelectAllPlugin::icon() const
0065 {
0066     return QStringLiteral("edit-select-all");
0067 }
0068 
0069 QString SKGSelectAllPlugin::toolTip() const
0070 {
0071     return i18nc("Select all objects in a list", "Select all");
0072 }
0073 
0074 int SKGSelectAllPlugin::getOrder() const
0075 {
0076     return 6;
0077 }
0078 
0079 QStringList SKGSelectAllPlugin::tips() const
0080 {
0081     QStringList output;
0082     return output;
0083 }
0084 
0085 void SKGSelectAllPlugin::onSelectionChanged()
0086 {
0087     SKGTRACEINFUNC(10)
0088     if (SKGMainPanel::getMainPanel() != nullptr) {
0089         if (m_selectionMessage == nullptr) {
0090             // Add statusbar
0091             m_selectionMessage = new QLabel(SKGMainPanel::getMainPanel());
0092             SKGMainPanel::getMainPanel()->statusBar()->insertPermanentWidget(1, m_selectionMessage);
0093         }
0094         SKGObjectBase::SKGListSKGObjectBase selection = SKGMainPanel::getMainPanel()->getSelectedObjects();
0095         int nb = selection.count();
0096         if (nb != 0) {
0097             double sum = 0.0;
0098             double max = -std::numeric_limits<double>::max();
0099             double min = std::numeric_limits<double>::max();
0100             qlonglong nbdec = -1;
0101             int count = 0;
0102             for (int i = 0; i < nb; ++i) {
0103                 const SKGObjectBase& obj(selection.at(i));
0104                 QString val = obj.getAttribute(QStringLiteral("f_REALCURRENTAMOUNT"));
0105                 if (val.isEmpty()) {
0106                     val = obj.getAttribute(QStringLiteral("f_CURRENTAMOUNT"));
0107                 }
0108                 if (!val.isEmpty()) {
0109                     double vald = SKGServices::stringToDouble(val);
0110                     sum += vald;
0111                     max = qMax(max, vald);
0112                     min = qMin(min, vald);
0113                     count++;
0114                 }
0115                 auto dec = obj.getAttribute(QStringLiteral("i_NBDEC"));
0116                 if (!dec.isEmpty()) {
0117                     nbdec = qMax(nbdec, SKGServices::stringToInt(dec));
0118                 }
0119             }
0120             if (count > 1) {
0121                 m_selectionMessage->setText(i18n("Selection: %1 lines for %2 (min: %3, average: %4, max: %5)",
0122                                                  nb,
0123                                                  m_currentDocument->formatPrimaryMoney(sum, nbdec),
0124                                                  m_currentDocument->formatPrimaryMoney(min, nbdec),
0125                                                  m_currentDocument->formatPrimaryMoney(sum / count, nbdec),
0126                                                  m_currentDocument->formatPrimaryMoney(max, nbdec)));
0127             } else if (count > 0) {
0128                 m_selectionMessage->setText(i18n("Selection: %1 line for %2", nb, m_currentDocument->formatPrimaryMoney(sum, nbdec)));
0129             } else {
0130                 m_selectionMessage->setText(i18np("Selection: %1 line", "Selection: %1 lines", nb));
0131             }
0132         } else {
0133             m_selectionMessage->clear();
0134         }
0135     }
0136 }
0137 
0138 void SKGSelectAllPlugin::onSelectAll()
0139 {
0140     SKGTRACEINFUNC(10)
0141     if (SKGMainPanel::getMainPanel() != nullptr) {
0142         SKGTabPage* page = SKGMainPanel::getMainPanel()->currentPage();
0143         if (page != nullptr) {
0144             auto* view = qobject_cast<QAbstractItemView*>(page->mainWidget());
0145             if (view != nullptr) {
0146                 view->selectAll();
0147             }
0148         }
0149     }
0150 }
0151 
0152 #include <skgselectallplugin.moc>