summaryrefslogtreecommitdiffstats
path: root/mobile-widgets/statsmanager.cpp
blob: d149c9978205552615b30a8cd1f38a62842a4a6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-License-Identifier: GPL-2.0
#include "statsmanager.h"

StatsManager::StatsManager() : view(nullptr)
{
	// Test: show some random data. Let's see what happens.
	state.var1Changed(2);
	state.var2Changed(3);
	state.binner2Changed(2);
	updateUi();
}

StatsManager::~StatsManager()
{
}

void StatsManager::init(StatsView *v, QObject *o)
{
	if (!v)
		fprintf(stderr, "StatsManager::init(): no StatsView - statistics will not work.\n");
	view = v;
}

void StatsManager::doit()
{
	if (!view)
		return;
	view->plot(state);
}

static void setVariableList(const StatsState::VariableList &list, QStringList &stringList)
{
	stringList.clear();
	for (const StatsState::Variable &v: list.variables)
		stringList.push_back(v.name);
}

static void setBinnerList(const StatsState::BinnerList &list, QStringList &stringList)
{
	stringList.clear();
	for (const QString &v: list.binners)
		stringList.push_back(v);
}

void StatsManager::updateUi()
{
	uiState = state.getUIState();
	setVariableList(uiState.var1, var1List);
	setBinnerList(uiState.binners1, binner1List);
	setVariableList(uiState.var2, var2List);
	setBinnerList(uiState.binners2, binner2List);
	var1ListChanged();
	binner1ListChanged();
	var2ListChanged();
	binner2ListChanged();

	if (view)
		view->plot(state);
}

void StatsManager::var1Changed(int idx)
{
	if (uiState.var1.variables.empty())
		return;
	idx = std::clamp(idx, 0, (int)uiState.var1.variables.size());
	state.var1Changed(uiState.var1.variables[idx].id);
	updateUi();
}

void StatsManager::var1BinnerChanged(int idx)
{
	state.binner1Changed(idx);
	updateUi();
}

void StatsManager::var2Changed(int idx)
{
	if (uiState.var2.variables.empty())
		return;
	idx = std::clamp(idx, 0, (int)uiState.var2.variables.size());
	state.var2Changed(uiState.var2.variables[idx].id);
	updateUi();
}

void StatsManager::var2BinnerChanged(int idx)
{
	state.binner2Changed(idx);
	updateUi();
}