Albert
Loading...
Searching...
No Matches
property.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 Manuel Schneider
2// SPDX-License-Identifier: MIT
3
4#pragma once
5#include <QSettings>
6#define EXPAND_STRINGIZE(s) STRINGIZE(s)
7#define STRINGIZE(s) #s
8
36#define ALBERT_PROPERTY_BASE(type, name, defaultValue, settings) \
37 public: static type name##_default(){ return defaultValue; }; \
38 protected: void store_##name() { settings()->setValue(EXPAND_STRINGIZE(name), name()); } \
39 protected: void restore_##name(const auto &s = nullptr) { \
40 if (s) set_##name##_(s->value(EXPAND_STRINGIZE(name), name##_default()).template value<type>()); \
41 else set_##name##_(settings()->value(EXPAND_STRINGIZE(name), name##_default()).template value<type>()); \
42 } \
43 public: void reset_##name() { set_##name##_(name##_default()); settings()->remove(EXPAND_STRINGIZE(name)); } \
44 Q_SIGNAL void name##_changed(type); \
45 Q_PROPERTY(type name READ name WRITE set_##name RESET reset_##name NOTIFY name##_changed USER true) \
46 public: void set_##name(type val) { if (val != name()){ set_##name##_(val); store_##name(); emit name##_changed(val); } }
47
48
54#define ALBERT_PLUGIN_PROPERTY_BASE(type, name, defaultValue) \
55 ALBERT_PROPERTY_BASE(type, name, defaultValue, PluginInstance::settings)
56
57
58// -------------------------------------------------------------------------------------------------
59
77#define ALBERT_PROPERTY_GETSET(type, name, defaultValue, settings) \
78 ALBERT_PROPERTY_BASE(type, name, defaultValue, settings) \
79 public: type name() const; \
80 private: void set_##name##_(type);
81
82
88#define ALBERT_PLUGIN_PROPERTY_GETSET(type, name, defaultValue) \
89 ALBERT_PROPERTY_GETSET(type, name, defaultValue, PluginInstance::settings)
90
91
92// -------------------------------------------------------------------------------------------------
93
107#define ALBERT_PROPERTY_MEMBER(type, name, member, defaultValue, settings) \
108 ALBERT_PROPERTY_BASE(type, name, defaultValue, settings) \
109 public: type name() const { return member; } \
110 private: void set_##name##_(type val) { member=val; }
111
117#define ALBERT_PLUGIN_PROPERTY_MEMBER(type, name, member, defaultValue) \
118 ALBERT_PROPERTY_MEMBER(type, name, member, defaultValue, PluginInstance::settings)
119
120
121// -------------------------------------------------------------------------------------------------
122
134#define ALBERT_PROPERTY(type, name, defaultValue, settings) \
135 ALBERT_PROPERTY_MEMBER(type, name, name##_, defaultValue, settings) \
136 protected: type name##_{name##_default()};
137
143#define ALBERT_PLUGIN_PROPERTY(type, name, defaultValue) \
144 ALBERT_PROPERTY(type, name, defaultValue, PluginInstance::settings)
145
146
147// -------------------------------------------------------------------------------------------------
148
158#define ALBERT_PROPERTY_CONNECT_CHECKBOX(object, name, checkbox) \
159 checkbox->setChecked(object->name()); \
160 connect(object, &std::remove_pointer<decltype(object)>::type::name##_changed, \
161 checkbox, &QCheckBox::setChecked); \
162 connect(checkbox, &QCheckBox::toggled, \
163 object, &std::remove_pointer<decltype(object)>::type::set_##name);
164
174#define ALBERT_PROPERTY_CONNECT_SPINBOX(object, name, spinbox) \
175 spinbox->setValue(object->name()); \
176 connect(object, &std::remove_pointer<decltype(object)>::type::name##_changed, \
177 spinbox, &QSpinBox::setValue); \
178 connect(spinbox, &QSpinBox::valueChanged, \
179 object, &std::remove_pointer<decltype(object)>::type::set_##name);