| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							|  |  |  | #include "statsmanager.h"
 | 
					
						
							| 
									
										
										
										
											2022-01-07 17:28:09 +01:00
										 |  |  | #include "themeinterface.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | #include "stats/chartlistmodel.h"
 | 
					
						
							| 
									
										
										
										
											2024-03-24 21:03:08 +01:00
										 |  |  | #include "core/errorhelper.h"
 | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 17:28:09 +01:00
										 |  |  | StatsManager::StatsManager() : view(nullptr), charts(nullptr), themeInitialized(false) | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | 	updateUi(); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | StatsManager::~StatsManager() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | void StatsManager::init(StatsView *v, ChartListModel *m) | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (!v) | 
					
						
							| 
									
										
										
										
											2024-03-24 21:03:08 +01:00
										 |  |  | 		report_info("StatsManager::init(): no StatsView - statistics will not work."); | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | 	if (!m) | 
					
						
							| 
									
										
										
										
											2024-03-24 21:03:08 +01:00
										 |  |  | 		report_info("StatsManager::init(): no ChartListModel - statistics will not work."); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | 	view = v; | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | 	charts = m; | 
					
						
							| 
									
										
										
										
											2022-01-07 17:28:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	connect(ThemeInterface::instance(), &ThemeInterface::currentThemeChanged, | 
					
						
							|  |  |  | 		this, &StatsManager::themeChanged); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StatsManager::doit() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | 	updateUi(); | 
					
						
							| 
									
										
										
										
											2021-01-07 15:12:25 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | static void setVariableList(const StatsState::VariableList &list, QStringList &stringList, int &idx) | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	stringList.clear(); | 
					
						
							|  |  |  | 	for (const StatsState::Variable &v: list.variables) | 
					
						
							|  |  |  | 		stringList.push_back(v.name); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | 	idx = list.selected; | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | static void setBinnerList(const StatsState::BinnerList &list, QStringList &stringList, int &idx) | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	stringList.clear(); | 
					
						
							|  |  |  | 	for (const QString &v: list.binners) | 
					
						
							|  |  |  | 		stringList.push_back(v); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | 	idx = list.selected; | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void StatsManager::updateUi() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	uiState = state.getUIState(); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | 	setVariableList(uiState.var1, var1List, var1Index); | 
					
						
							|  |  |  | 	setBinnerList(uiState.binners1, binner1List, binner1Index); | 
					
						
							|  |  |  | 	setVariableList(uiState.var2, var2List, var2Index); | 
					
						
							|  |  |  | 	setBinnerList(uiState.binners2, binner2List, binner2Index); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:28:20 +01:00
										 |  |  | 	setVariableList(uiState.operations2, operation2List, operation2Index); | 
					
						
							| 
									
										
										
										
											2021-12-31 21:13:50 +01:00
										 |  |  | 	setVariableList(uiState.sortMode1, sortMode1List, sortMode1Index); | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | 	var1ListChanged(); | 
					
						
							|  |  |  | 	binner1ListChanged(); | 
					
						
							|  |  |  | 	var2ListChanged(); | 
					
						
							|  |  |  | 	binner2ListChanged(); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:28:20 +01:00
										 |  |  | 	operation2ListChanged(); | 
					
						
							| 
									
										
										
										
											2021-12-31 21:13:50 +01:00
										 |  |  | 	sortMode1ListChanged(); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:00:19 +01:00
										 |  |  | 	var1IndexChanged(); | 
					
						
							|  |  |  | 	binner1IndexChanged(); | 
					
						
							|  |  |  | 	var2IndexChanged(); | 
					
						
							|  |  |  | 	binner2IndexChanged(); | 
					
						
							| 
									
										
										
										
											2021-01-10 18:28:20 +01:00
										 |  |  | 	operation2IndexChanged(); | 
					
						
							| 
									
										
										
										
											2021-12-31 21:13:50 +01:00
										 |  |  | 	sortMode1IndexChanged(); | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-07 17:28:09 +01:00
										 |  |  | 	if (view && !std::exchange(themeInitialized, true)) | 
					
						
							|  |  |  | 		themeChanged(); | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | 	if (charts) | 
					
						
							|  |  |  | 		charts->update(uiState.charts); | 
					
						
							| 
									
										
										
										
											2021-01-04 15:47:33 +01:00
										 |  |  | 	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(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-10 18:28:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void StatsManager::var2OperationChanged(int idx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-11 08:46:46 -08:00
										 |  |  | 	if (uiState.operations2.variables.empty()) | 
					
						
							| 
									
										
										
										
											2021-01-10 18:28:20 +01:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	idx = std::clamp(idx, 0, (int)uiState.operations2.variables.size()); | 
					
						
							|  |  |  | 	state.var2OperationChanged(uiState.operations2.variables[idx].id); | 
					
						
							|  |  |  | 	updateUi(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 21:13:50 +01:00
										 |  |  | void StatsManager::sortMode1Changed(int idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (uiState.sortMode1.variables.empty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	idx = std::clamp(idx, 0, (int)uiState.sortMode1.variables.size()); | 
					
						
							|  |  |  | 	state.sortMode1Changed(uiState.sortMode1.variables[idx].id); | 
					
						
							|  |  |  | 	updateUi(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 10:28:04 -08:00
										 |  |  | void StatsManager::setChart(int idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	state.chartChanged(idx); | 
					
						
							|  |  |  | 	updateUi(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-07 17:28:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void StatsManager::themeChanged() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!view) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// We could just make currentTheme accessible instead of
 | 
					
						
							|  |  |  | 	// using Qt's inane propertySystem. Whatever.
 | 
					
						
							|  |  |  | 	QString theme = ThemeInterface::instance()->property("currentTheme").toString(); | 
					
						
							|  |  |  | 	view->setTheme(theme == "Dark"); | 
					
						
							|  |  |  | } |