diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
commit | d24f813f3f2a05c112e803e4256b53535895fc98 (patch) | |
tree | 601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/statistics/StatisticsView.cpp |
Diffstat (limited to 'src/statistics/StatisticsView.cpp')
-rw-r--r-- | src/statistics/StatisticsView.cpp | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/src/statistics/StatisticsView.cpp b/src/statistics/StatisticsView.cpp new file mode 100644 index 0000000..de1b16d --- /dev/null +++ b/src/statistics/StatisticsView.cpp @@ -0,0 +1,182 @@ +#include "StatisticsView.h"
+#include "StudiedPage.h"
+#include "ScheduledPage.h"
+#include "ProgressPage.h"
+#include "../dictionary/Dictionary.h"
+#include "../dictionary/CardPack.h"
+
+const QSize StatisticsView::GridSize(150, 110);
+
+StatisticsView::StatisticsView(const Dictionary* dict):
+ dict(dict), periodLabel(NULL), periodBox(NULL)
+{
+ init();
+ createContentsList();
+ createPages();
+ createListItems();
+ createUi();
+ loadSettings();
+}
+
+void StatisticsView::init()
+{
+ cardPack = dict->cardPacks().first();
+ timePeriod = Week;
+ setWindowTitle(tr("Statistics") + " - " + dict->shortName());
+ setWindowIcon(QIcon(":/images/statistics.png"));
+}
+
+void StatisticsView::closeEvent(QCloseEvent* /*event*/)
+{
+ saveSettings();
+}
+
+void StatisticsView::createPages()
+{
+ pagesWidget = new QStackedWidget;
+ pagesWidget->addWidget(new ProgressPage(this));
+ pagesWidget->addWidget(new StudiedPage(this));
+ pagesWidget->addWidget(new ScheduledPage(this));
+}
+
+void StatisticsView::createContentsList()
+{
+ contentsWidget = new QListWidget;
+ contentsWidget->setViewMode(QListView::IconMode);
+ contentsWidget->setGridSize(GridSize);
+ contentsWidget->setIconSize(QSize(IconSize, IconSize));
+ contentsWidget->setMovement(QListView::Static);
+ contentsWidget->setFixedWidth(GridSize.width() + 4);
+}
+
+void StatisticsView::createListItems()
+{
+ QStringList icons = {"pie-chart-3d", "chart-past", "chart-future"};
+ for(int i = 0; i < icons.size(); i++)
+ new QListWidgetItem( QIcon(QString(":/images/%1.png").arg(icons[i])),
+ static_cast<BaseStatPage*>(pagesWidget->widget(i))->getTitle(),
+ contentsWidget);
+ connect(contentsWidget,
+ SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)),
+ SLOT(changePage(QListWidgetItem*, QListWidgetItem*)));
+ contentsWidget->setCurrentRow(0);
+}
+
+void StatisticsView::changePage(QListWidgetItem* curPage, QListWidgetItem* prevPage)
+{
+ if(!curPage)
+ curPage = prevPage;
+ pagesWidget->setCurrentIndex(contentsWidget->row(curPage));
+ updateChart();
+ updatePeriodBox();
+}
+
+void StatisticsView::createUi()
+{
+ QVBoxLayout* verLt = new QVBoxLayout;
+ verLt->addLayout(createControlLayout());
+ verLt->addWidget(pagesWidget);
+
+ QHBoxLayout* horLt = new QHBoxLayout;
+ horLt->addWidget(contentsWidget);
+ horLt->addLayout(verLt);
+ setLayout(horLt);
+}
+
+void StatisticsView::loadSettings()
+{
+ QSettings settings;
+ QVariant pointVar = settings.value("stats-pos");
+ if(pointVar.isNull())
+ return;
+ move(pointVar.toPoint());
+ resize(settings.value("stats-size").toSize());
+}
+
+void StatisticsView::saveSettings()
+{
+ QSettings settings;
+ settings.setValue("stats-pos", pos());
+ settings.setValue("stats-size", size());
+}
+
+QBoxLayout* StatisticsView::createControlLayout()
+{
+ QHBoxLayout* lt = new QHBoxLayout;
+ lt->addWidget(new QLabel(tr("Card pack:")));
+ lt->addWidget(createPacksBox());
+ lt->addStretch();
+ periodLabel = new QLabel(tr("Period:"));
+ lt->addWidget(periodLabel);
+ lt->addWidget(createPeriodBox());
+ return lt;
+}
+
+QComboBox* StatisticsView::createPacksBox()
+{
+ QComboBox* packsBox = new QComboBox;
+ foreach(CardPack* pack, dict->cardPacks())
+ packsBox->addItem(pack->id());
+ connect(packsBox, SIGNAL(activated(int)), SLOT(setPack(int)));
+ return packsBox;
+}
+
+QComboBox* StatisticsView::createPeriodBox()
+{
+ periodBox = new QComboBox;
+ QPair<QString, int> period;
+ foreach(period, getPeriodsList())
+ periodBox->addItem(period.first, period.second);
+ periodBox->setMaxVisibleItems(12);
+ connect(periodBox, SIGNAL(activated(int)), SLOT(setPeriod(int)));
+ updatePeriodBox();
+ return periodBox;
+}
+
+QList<QPair<QString, int>> StatisticsView::getPeriodsList()
+{
+ static const int Week = 7;
+ static const int Month = 30;
+ static const int Year = 365;
+ return {
+ qMakePair(tr("%n week(s)", 0, 1), Week),
+ qMakePair(tr("%n week(s)", 0, 2), 2 * Week),
+ qMakePair(tr("%n week(s)", 0, 4), 4 * Week),
+ qMakePair(tr("%n month(s)", 0, 1), Month + 1),
+ qMakePair(tr("%n month(s)", 0, 2), 2 * Month + 1),
+ qMakePair(tr("%n month(s)", 0, 3), 3 * Month + 2),
+ qMakePair(tr("%n month(s)", 0, 6), 6 * Month + 3),
+ qMakePair(tr("%n year(s)", 0, 1), Year),
+ qMakePair(tr("%n year(s)", 0, 2), 2 * Year),
+ qMakePair(tr("%n year(s)", 0, 3), 3 * Year),
+ qMakePair(tr("All time"), -1)};
+}
+
+void StatisticsView::setPack(int packIndex)
+{
+ cardPack = dict->cardPacks().at(packIndex);
+ updateChart();
+}
+
+void StatisticsView::setPeriod(int index)
+{
+ timePeriod = periodBox->itemData(index).toInt();
+ updateChart();
+}
+
+void StatisticsView::updateChart()
+{
+ static_cast<BaseStatPage*>(pagesWidget->currentWidget())->updateDataSet();
+}
+
+void StatisticsView::updatePeriodBox()
+{
+ bool visiblePeriod = static_cast<BaseStatPage*>(
+ pagesWidget->currentWidget())->usesTimePeriod();
+ if(periodBox)
+ {
+ periodLabel->setVisible(visiblePeriod);
+ periodBox->setVisible(visiblePeriod);
+ }
+}
+
|