File indexing completed on 2024-05-26 03:52:44

0001 /*
0002     File                 : StatisticsDialog.cpp
0003     Project              : LabPlot
0004     Description          : Dialog showing statistics for column values
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016-2017 Fabian Kristof <fkristofszabolcs@gmail.com>)
0007     SPDX-FileCopyrightText: 2016-2021 Alexander Semke <alexander.semke@web.de>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "StatisticsDialog.h"
0012 #include "StatisticsColumnWidget.h"
0013 #include "backend/core/Settings.h"
0014 #include "backend/core/column/Column.h"
0015 #include "backend/lib/macros.h"
0016 
0017 #include <QDialogButtonBox>
0018 #include <QPushButton>
0019 #include <QTabWidget>
0020 #include <QTimer>
0021 #include <QVBoxLayout>
0022 #include <QWindow>
0023 
0024 #include <KLocalizedString>
0025 
0026 #include <KWindowConfig>
0027 
0028 #include <cmath>
0029 
0030 StatisticsDialog::StatisticsDialog(const QString& title, const QVector<Column*>& columns, QWidget* parent)
0031     : QDialog(parent)
0032     , m_twStatistics(new QTabWidget) {
0033     auto* btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0034 
0035     QPushButton* btnOk = btnBox->button(QDialogButtonBox::Ok);
0036     btnOk->setFocus();
0037 
0038     connect(btnOk, &QPushButton::clicked, this, &StatisticsDialog::close);
0039     connect(btnBox, &QDialogButtonBox::accepted, this, &StatisticsDialog::accept);
0040 
0041     auto* layout = new QVBoxLayout;
0042     layout->addWidget(m_twStatistics);
0043     layout->addWidget(btnBox);
0044 
0045     setLayout(layout);
0046 
0047     setWindowTitle(title);
0048     setWindowIcon(QIcon::fromTheme(QStringLiteral("view-statistics")));
0049     setAttribute(Qt::WA_DeleteOnClose);
0050 
0051     m_columns = columns;
0052 
0053     // create tab widgets for every column and show the initial text with the placeholders
0054     for (auto* col : m_columns) {
0055         auto* w = new StatisticsColumnWidget(col, this);
0056         connect(w, &StatisticsColumnWidget::tabChanged, this, &StatisticsDialog::currentWidgetTabChanged);
0057         m_twStatistics->addTab(w, col->name());
0058     }
0059 
0060     connect(m_twStatistics, &QTabWidget::currentChanged, this, &StatisticsDialog::currentTabChanged);
0061 
0062     // restore saved settings if available
0063     create(); // ensure there's a window created
0064     KConfigGroup conf = Settings::group(QStringLiteral("StatisticsDialog"));
0065     if (conf.exists()) {
0066         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0067         resize(windowHandle()->size()); // workaround for QTBUG-40584
0068     } else
0069         resize(QSize(490, 520));
0070 }
0071 
0072 StatisticsDialog::~StatisticsDialog() {
0073     KConfigGroup conf = Settings::group(QStringLiteral("StatisticsDialog"));
0074     KWindowConfig::saveWindowSize(windowHandle(), conf);
0075 }
0076 
0077 void StatisticsDialog::showStatistics() {
0078     QApplication::processEvents(QEventLoop::AllEvents, 0);
0079     QTimer::singleShot(0, this, [=]() {
0080         currentTabChanged(0);
0081     });
0082 }
0083 
0084 void StatisticsDialog::currentTabChanged(int) {
0085     auto* const w = static_cast<StatisticsColumnWidget*>(m_twStatistics->currentWidget());
0086     if (!w)
0087         return;
0088 
0089     w->setCurrentTab(m_currentWidgetTab);
0090 }
0091 
0092 /*!
0093  * slot called when the user switches between the different tabs in StatisticsWidget.
0094  * we keep the last used tab index and use it when the user navigates to a different column
0095  */
0096 void StatisticsDialog::currentWidgetTabChanged(int index) {
0097     m_currentWidgetTab = index;
0098 }