src/settings.h

Wed, 15 Jun 2022 12:33:16 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 15 Jun 2022 12:33:16 +0300
changeset 226
60d6b797a12e
parent 224
54fd83d4db9c
child 231
a9bf6bab5ea2
permissions
-rw-r--r--

remove old todo

#ifndef SETTINGS_H
#define SETTINGS_H
#include <QSettings>
#include <QMdiArea>
#include "libraries.h"
#include "gl/common.h"

// SettingType - get type of setting by enumerator
template<typename>
struct SettingId{};

template<SettingId>
struct SettingInfo{};

#define SETTING(NAME, DEFVALUE) \
	namespace SettingIdTypes { class NAME; } \
	namespace Setting { constexpr SettingId<class NAME##_SettingType> NAME; } \
	template<> \
	struct SettingInfo<Setting::NAME> \
	{ \
		using type = decltype((DEFVALUE)); \
		static constexpr char name[] = #NAME; \
		static const auto& defaultValue() { \
			static const auto value = DEFVALUE; \
			return value; \
		}; \
	};

// -----------------------------------------------------------------------------
// Setting definitions

// Rendering options
SETTING(Locale, QString{"system"})
SETTING(BackgroundColor, (QColor{48, 48, 48}))
SETTING(MainColor, (QColor{255, 255, 64}))
SETTING(SelectedColor, (QColor{32, 32, 224}))
SETTING(LineThickness, 2.0f)
SETTING(LineAntiAliasing, true)
SETTING(RenderStyle, gl::RenderStyle::Normal)
SETTING(DrawWireframe, false)
SETTING(DrawAxes, true)
SETTING(MainWindowGeometry, QByteArray{})
SETTING(MainSplitterState, QByteArray{})
SETTING(RecentFiles, QStringList{})
SETTING(ViewMode, QMdiArea::TabbedView)

// File management options
SETTING(Libraries, QVector<Library>{})

// End of setting definitions
// -----------------------------------------------------------------------------
#undef SETTING

template<SettingId X>
using SettingType_t = typename SettingInfo<X>::type;
static_assert(std::is_same_v<SettingType_t<Setting::DrawAxes>, bool>);

template<SettingId X>
const char* settingName = SettingInfo<X>::name;

template<SettingId X>
inline auto settingDefaultValue()
{
	return SettingInfo<X>::defaultValue();
}

// get() - get setting by enumerator
template<SettingId X>
inline SettingType_t<X> setting()
{
	static const QVariant defvariant = QVariant::fromValue(settingDefaultValue<X>());
	const QVariant var = QSettings{}.value(settingName<X>, defvariant);
	return var.value<SettingType_t<X>>();
}

// setSetting() - set value of setting
template<SettingId X>
inline void setSetting(const SettingType_t<X>& value)
{
	if (value == settingDefaultValue<X>()) {
		QSettings{}.remove(settingName<X>);
	}
	else {
		QSettings{}.setValue(settingName<X>, QVariant::fromValue(value));
	}
}

#endif // SETTINGS_H

mercurial