src/settings.h

Tue, 28 Jun 2022 19:25:45 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 28 Jun 2022 19:25:45 +0300
changeset 295
4241d948af28
parent 286
04478da357d0
child 296
38f6fad61bad
permissions
-rw-r--r--

cleanup version.h

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

template<class>
struct SettingInfo{};

#define SETTING(NAME, DEFVALUE) \
	namespace Setting { class 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

Q_DECLARE_METATYPE(QMdiArea::ViewMode)

// 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(Wireframe, false)
SETTING(DrawAxes, true)
SETTING(MainWindowGeometry, QByteArray{})
SETTING(MainWindowState, QByteArray{})
SETTING(MainSplitterState, QByteArray{})
SETTING(RecentFiles, QStringList{})
SETTING(ViewMode, QMdiArea::TabbedView)
SETTING(LogOpenGLDebugMessages, false)

// File management options
SETTING(Libraries, Libraries{})

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

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

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

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

// get() - get setting by enumerator
template<class 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<class X>
inline void setSetting(const SettingType_t<X>& value)
{
	if (false/*value == settingDefaultValue<X>()*/) {
		QSettings{}.remove(settingName<X>);
	}
	else {
		QSettings{}.setValue(settingName<X>, QVariant::fromValue(value));
	}
}

#endif // SETTINGS_H

mercurial