File indexing completed on 2025-01-05 03:35:32

0001 /*
0002     File                 : StatisticsSpreadsheetDock.cpp
0003     Project              : LabPlot
0004     Description          : widget for statistics spreadsheet properties
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2023 Alexander Semke <alexander.semke@web.de>
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "StatisticsSpreadsheetDock.h"
0011 
0012 #include "backend/spreadsheet/Spreadsheet.h"
0013 #include "backend/spreadsheet/StatisticsSpreadsheet.h"
0014 #include "kdefrontend/TemplateHandler.h"
0015 
0016 #include <KConfig>
0017 #include <KConfigGroup>
0018 
0019 /*!
0020  \class StatisticsSpreadsheetDock
0021  \brief Provides a widget for editing which statistical metris of the parent spreadsheet should be shown in the statistics spreadsheet.
0022 
0023  \ingroup kdefrontend
0024 */
0025 
0026 StatisticsSpreadsheetDock::StatisticsSpreadsheetDock(QWidget* parent)
0027     : BaseDock(parent) {
0028     ui.setupUi(this);
0029 
0030     m_mappingComboBoxMetric[ui.cbCount] = StatisticsSpreadsheet::Metric::Count;
0031     m_mappingComboBoxMetric[ui.cbMinimum] = StatisticsSpreadsheet::Metric::Minimum;
0032     m_mappingComboBoxMetric[ui.cbMaximum] = StatisticsSpreadsheet::Metric::Maximum;
0033     m_mappingComboBoxMetric[ui.cbArithmeticMean] = StatisticsSpreadsheet::Metric::ArithmeticMean;
0034     m_mappingComboBoxMetric[ui.cbGeometricMean] = StatisticsSpreadsheet::Metric::GeometricMean;
0035     m_mappingComboBoxMetric[ui.cbHarmonicMean] = StatisticsSpreadsheet::Metric::HarmonicMean;
0036     m_mappingComboBoxMetric[ui.cbContraharmonicMean] = StatisticsSpreadsheet::Metric::ContraharmonicMean;
0037     m_mappingComboBoxMetric[ui.cbMode] = StatisticsSpreadsheet::Metric::Mode;
0038     m_mappingComboBoxMetric[ui.cbFirstQuartile] = StatisticsSpreadsheet::Metric::FirstQuartile;
0039     m_mappingComboBoxMetric[ui.cbMedian] = StatisticsSpreadsheet::Metric::Median;
0040     m_mappingComboBoxMetric[ui.cbThirdQuartile] = StatisticsSpreadsheet::Metric::ThirdQuartile;
0041     m_mappingComboBoxMetric[ui.cbTrimean] = StatisticsSpreadsheet::Metric::Trimean;
0042     m_mappingComboBoxMetric[ui.cbVariance] = StatisticsSpreadsheet::Metric::Variance;
0043     m_mappingComboBoxMetric[ui.cbStandardDeviation] = StatisticsSpreadsheet::Metric::StandardDeviation;
0044     m_mappingComboBoxMetric[ui.cbMeanDeviation] = StatisticsSpreadsheet::Metric::MeanDeviation;
0045     m_mappingComboBoxMetric[ui.cbMeanDeviationAroundMedian] = StatisticsSpreadsheet::Metric::MeanDeviationAroundMedian;
0046     m_mappingComboBoxMetric[ui.cbMedianDeviation] = StatisticsSpreadsheet::Metric::MedianDeviation;
0047     m_mappingComboBoxMetric[ui.cbIQR] = StatisticsSpreadsheet::Metric::IQR;
0048     m_mappingComboBoxMetric[ui.cbSkewness] = StatisticsSpreadsheet::Metric::Skewness;
0049     m_mappingComboBoxMetric[ui.cbKurtosis] = StatisticsSpreadsheet::Metric::Kurtosis;
0050     m_mappingComboBoxMetric[ui.cbEntropy] = StatisticsSpreadsheet::Metric::Entropy;
0051 
0052     connect(ui.cbCount, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0053     connect(ui.cbMinimum, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0054     connect(ui.cbMaximum, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0055     connect(ui.cbArithmeticMean, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0056     connect(ui.cbGeometricMean, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0057     connect(ui.cbHarmonicMean, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0058     connect(ui.cbContraharmonicMean, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0059     connect(ui.cbMode, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0060     connect(ui.cbFirstQuartile, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0061     connect(ui.cbMedian, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0062     connect(ui.cbThirdQuartile, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0063     connect(ui.cbTrimean, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0064     connect(ui.cbVariance, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0065     connect(ui.cbStandardDeviation, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0066     connect(ui.cbMeanDeviation, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0067     connect(ui.cbMeanDeviationAroundMedian, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0068     connect(ui.cbMedianDeviation, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0069     connect(ui.cbIQR, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0070     connect(ui.cbSkewness, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0071     connect(ui.cbKurtosis, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0072     connect(ui.cbEntropy, &QCheckBox::toggled, this, &StatisticsSpreadsheetDock::metricChanged);
0073 
0074     // templates
0075     auto* templateHandler = new TemplateHandler(this, QLatin1String("StatisticsSpreadsheet"));
0076     ui.verticalLayout->addWidget(templateHandler);
0077     templateHandler->show();
0078     connect(templateHandler, &TemplateHandler::loadConfigRequested, this, &StatisticsSpreadsheetDock::loadConfigFromTemplate);
0079     connect(templateHandler, &TemplateHandler::saveConfigRequested, this, &StatisticsSpreadsheetDock::saveConfigAsTemplate);
0080     connect(templateHandler, &TemplateHandler::info, this, &StatisticsSpreadsheetDock::info);
0081 }
0082 
0083 /*!
0084     set the current spreadsheet(s)
0085 */
0086 void StatisticsSpreadsheetDock::setSpreadsheets(const QList<StatisticsSpreadsheet*> list) {
0087     m_spreadsheets = list;
0088     m_spreadsheet = list.first();
0089     load();
0090 }
0091 
0092 void StatisticsSpreadsheetDock::metricChanged(bool state) {
0093     auto* cb = static_cast<QCheckBox*>(QObject::sender());
0094     if (!m_mappingComboBoxMetric.contains(cb))
0095         return;
0096 
0097     const auto metric = m_mappingComboBoxMetric[cb];
0098     auto metrics = m_spreadsheet->metrics();
0099     metrics.setFlag(metric, state);
0100 
0101     for (auto* spreadsheet : m_spreadsheets)
0102         spreadsheet->setMetrics(metrics);
0103 }
0104 
0105 //*************************************************************
0106 //******************** SETTINGS *******************************
0107 //*************************************************************
0108 void StatisticsSpreadsheetDock::load() {
0109     const auto metrics = m_spreadsheet->metrics();
0110     ui.cbCount->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Count));
0111     ui.cbMinimum->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Minimum));
0112     ui.cbMaximum->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Maximum));
0113     ui.cbArithmeticMean->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::ArithmeticMean));
0114     ui.cbGeometricMean->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::GeometricMean));
0115     ui.cbHarmonicMean->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::HarmonicMean));
0116     ui.cbContraharmonicMean->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::ContraharmonicMean));
0117     ui.cbMode->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Mode));
0118     ui.cbFirstQuartile->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::FirstQuartile));
0119     ui.cbMedian->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Median));
0120     ui.cbThirdQuartile->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::ThirdQuartile));
0121     ui.cbTrimean->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Trimean));
0122     ui.cbVariance->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Variance));
0123     ui.cbStandardDeviation->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::StandardDeviation));
0124     ui.cbMeanDeviation->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::MeanDeviation));
0125     ui.cbMeanDeviationAroundMedian->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::MeanDeviationAroundMedian));
0126     ui.cbMedianDeviation->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::MedianDeviation));
0127     ui.cbIQR->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::IQR));
0128     ui.cbSkewness->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Skewness));
0129     ui.cbKurtosis->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Kurtosis));
0130     ui.cbEntropy->setChecked(metrics.testFlag(StatisticsSpreadsheet::Metric::Entropy));
0131 }
0132 
0133 void StatisticsSpreadsheetDock::loadConfigFromTemplate(KConfig& config) {
0134     KConfigGroup group = config.group(QLatin1String("StatisticsSpreadsheet"));
0135 
0136     auto metrics = static_cast<StatisticsSpreadsheet::Metrics>(group.readEntry(QStringLiteral("Metrics"), static_cast<int>(m_spreadsheet->metrics())));
0137     m_spreadsheet->setMetrics(metrics);
0138     load();
0139 }
0140 
0141 /*!
0142     saves spreadsheet properties to \c config.
0143  */
0144 void StatisticsSpreadsheetDock::saveConfigAsTemplate(KConfig& config) {
0145     KConfigGroup group = config.group(QLatin1String("StatisticsSpreadsheet"));
0146     group.writeEntry(QStringLiteral("Metrics"), static_cast<int>(m_spreadsheet->metrics()));
0147 }