aboutsummaryrefslogtreecommitdiff
path: root/lib/configuration/qt_specialization.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2020-03-22 14:59:04 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2020-03-22 14:59:04 +0200
commit74870600fa54a48d4c1ce4b19861a9b0ce027fee (patch)
treed79eb50c18f1f62e937a1d1fe2e7180c7c14d06a /lib/configuration/qt_specialization.cpp
parentRemove ProfileInterface (diff)
downloadsmolbote-74870600fa54a48d4c1ce4b19861a9b0ce027fee.tar.xz
lib/configuration improvements
Configuration changes: - Configuration::value return type is now [[nodiscard]] - Configuration::value<T> is now a generic template that only works with the exact types of the underlying std::variant - Add Configuration::value<concept_value_t> for standard library types compatible with the types of std::variant - Add Configuration::shortcut<> placeholder, and QAction and QKeySequence specializations as a convenient way to set up shortcuts - Deprecate setShortcut - Add Configuration::read_file convenience member that takes file path as parameter Format changes: - Configuration files can now have sections, specified as [section name]. Section names are prepended to keys. Section names cannot be nested. - Configuration files can now have @@include directives, causing another file to be read as well. The included file is not treated as nested into a section, and will overwrite values previously set. Others: - add some tests for libconfiguration. QAction/QKeySequence require a QApplication be set up, so the test application may require running xorg/wayland. old coverage: lines: 15.6% (960 out of 6172) branches: 9.9% (1187 out of 12012) new coverage: lines: 17.1% (1067 out of 6254) branches: 11.0% (1388 out of 12644)
Diffstat (limited to 'lib/configuration/qt_specialization.cpp')
-rw-r--r--lib/configuration/qt_specialization.cpp55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/configuration/qt_specialization.cpp b/lib/configuration/qt_specialization.cpp
new file mode 100644
index 0000000..4e79492
--- /dev/null
+++ b/lib/configuration/qt_specialization.cpp
@@ -0,0 +1,55 @@
+#include "configuration.h"
+#include <stdexcept>
+
+template <>
+callable_when(unconsumed) [[nodiscard]] std::optional<QString> Configuration::value(const char *path) const
+{
+ const auto v = value<std::string>(path);
+ if(!v)
+ return std::nullopt;
+ else
+ return QString::fromStdString(v.value());
+}
+
+template <>
+callable_when(unconsumed) [[nodiscard]] std::optional<QStringList> Configuration::value(const char *path) const
+{
+ const auto v = value<std::string>(path);
+ if(!v)
+ return std::nullopt;
+ else
+ return QString::fromStdString(v.value()).split(';');
+}
+
+void setShortcut(QAction *action, const char *name)
+{
+ Configuration conf;
+
+ if(const auto shortcutText = conf.value<QString>(name)) {
+ const QString tooltip = action->toolTip();
+ action->setShortcut(QKeySequence::fromString(shortcutText.value()));
+ action->setToolTip(QString("%1 (%2)").arg(tooltip, shortcutText.value()));
+ } else {
+ throw new std::runtime_error(std::string("fixme: setShortcut found no such value for ") + name);
+ }
+}
+
+template <>
+callable_when(unconsumed) QAction &Configuration::shortcut(QAction &action, const char *name) const
+{
+ if(const auto shortcut = value<QString>(name)) {
+ const QString old_tooltip = action.toolTip();
+ action.setShortcut(QKeySequence::fromString(shortcut.value()));
+ action.setToolTip(QString("%1 (%2)").arg(old_tooltip, shortcut.value()));
+ }
+ return action;
+}
+
+template <>
+callable_when(unconsumed) QKeySequence &Configuration::shortcut(QKeySequence &sequence, const char *name) const
+{
+ if(const auto shortcut = value<QString>(name)) {
+ sequence = QKeySequence::fromString(shortcut.value());
+ }
+ return sequence;
+}