[graphics/krita] libs/global: Implement kiszug::has_value

Dmitry Kazakov <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 98ea24d9519af29c3ec3a9d7b2ae0a49dbf7631a by Dmitry Kazakov.
Committed on 07/08/2026 at 09:19.
Pushed by dkazakov into branch 'master'.

Implement kiszug::has_value

To be used when unwrapping std::optional values in the GUI

M  +3    -0    libs/global/KisZug.h
M  +1    -0    libs/global/tests/CMakeLists.txt
A  +38   -0    libs/global/tests/KisLagerTest.cpp     [License: GPL(v2.0+)]
A  +18   -0    libs/global/tests/KisLagerTest.h     [License: GPL(v2.0+)]

https://invent.kde.org/graphics/krita/-/commit/98ea24d9519af29c3ec3a9d7b2ae0a49dbf7631a

diff --git a/libs/global/KisZug.h b/libs/global/KisZug.h
index 4d37d3e0a80..840b26ceaf6 100644
--- a/libs/global/KisZug.h
+++ b/libs/global/KisZug.h
@@ -63,6 +63,9 @@ inline constexpr auto map_less_equal<qreal> = [] (qreal value) { return zug::map
 
 constexpr auto map_round = zug::map([](qreal x) -> int { return qRound(x); });
 
+// a state getter for an std::optional
+constexpr auto has_value = zug::map([](auto&& x) -> bool { return x.has_value(); });
+
 struct empty_t
 {
 };
diff --git a/libs/global/tests/CMakeLists.txt b/libs/global/tests/CMakeLists.txt
index a947485d667..a53fb39acd4 100644
--- a/libs/global/tests/CMakeLists.txt
+++ b/libs/global/tests/CMakeLists.txt
@@ -2,6 +2,7 @@ kis_add_tests(KisSignalAutoConnectionTest.cpp
     KisSignalCompressorTest.cpp
     KisForestTest.cpp
     KisMplTest.cpp
+    KisLagerTest.cpp
     KisRectsGridTest.cpp
     KisLazyStorageTest.cpp
     KisValueCacheTest.cpp
diff --git a/libs/global/tests/KisLagerTest.cpp b/libs/global/tests/KisLagerTest.cpp
new file mode 100644
index 00000000000..19f5d4ed8ab
--- /dev/null
+++ b/libs/global/tests/KisLagerTest.cpp
@@ -0,0 +1,38 @@
+/*
+ *  SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "KisLagerTest.h"
+
+#include <simpletest.h>
+
+#include <kis_debug.h>
+
+#include <optional>
+
+#include <lager/state.hpp>
+
+#include <KisLager.h>
+#include <KisZug.h>
+
+
+void KisLagerTest::testOptionalHasValue()
+{
+    auto x = lager::state<std::optional<int>, lager::automatic_tag>{};
+    x.set(11);
+
+    // connect to the presence state
+    auto isPresentReader = lager::reader<bool>{x.xform(kiszug::has_value)};
+    auto valueReader = lager::reader<int>{x.zoom(lager::lenses::value_or(42))};
+
+    QCOMPARE(isPresentReader.get(), true);
+    QCOMPARE(valueReader.get(), 11);
+
+    x.set(std::nullopt);
+
+    QCOMPARE(isPresentReader.get(), false);
+    QCOMPARE(valueReader.get(), 42);
+}
+
+SIMPLE_TEST_MAIN(KisLagerTest)
diff --git a/libs/global/tests/KisLagerTest.h b/libs/global/tests/KisLagerTest.h
new file mode 100644
index 00000000000..e9d9342fd7c
--- /dev/null
+++ b/libs/global/tests/KisLagerTest.h
@@ -0,0 +1,18 @@
+/*
+ *  SPDX-FileCopyrightText: 2026 Dmitry Kazakov <[email protected]>
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef KISLAGERTEST_H
+#define KISLAGERTEST_H
+
+#include <QTest>
+
+class KisLagerTest : public QObject
+{
+    Q_OBJECT
+private Q_SLOTS:
+    void testOptionalHasValue();
+};
+
+#endif // KISLAGERTEST_H
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.