[libraries/ktextaddons] textautogeneratetext/core: Convert category

Laurent Montel <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit ac79e5756176bc195b27740107c88ef7cf6b70c6 by Laurent Montel.
Committed on 09/08/2026 at 09:42.
Pushed by mlaurent into branch 'master'.

Convert category

M  +20   -1    textautogeneratetext/core/autotests/textautogenerateprompttest.cpp
M  +2    -0    textautogeneratetext/core/autotests/textautogenerateprompttest.h
M  +30   -1    textautogeneratetext/core/prompts/textautogenerateprompt.cpp
M  +13   -0    textautogeneratetext/core/prompts/textautogenerateprompt.h

https://invent.kde.org/libraries/ktextaddons/-/commit/ac79e5756176bc195b27740107c88ef7cf6b70c6

diff --git a/textautogeneratetext/core/autotests/textautogenerateprompttest.cpp b/textautogeneratetext/core/autotests/textautogenerateprompttest.cpp
index f1a30d2b1..52a0bcfdc 100644
--- a/textautogeneratetext/core/autotests/textautogenerateprompttest.cpp
+++ b/textautogeneratetext/core/autotests/textautogenerateprompttest.cpp
@@ -7,7 +7,7 @@
 #include "core/prompts/textautogenerateprompt.h"
 #include <QTest>
 QTEST_GUILESS_MAIN(TextAutoGeneratePromptTest)
-
+using namespace Qt::Literals::StringLiterals;
 TextAutoGeneratePromptTest::TextAutoGeneratePromptTest(QObject *parent)
     : QObject{parent}
 {
@@ -23,4 +23,23 @@ void TextAutoGeneratePromptTest::shouldHaveDefaultValues()
     QCOMPARE(prompt.category(), TextAutoGenerateText::TextAutoGeneratePrompt::Category::Unknown);
 }
 
+void TextAutoGeneratePromptTest::shouldConvertCategoryToString()
+{
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertCategoryToString(TextAutoGenerateText::TextAutoGeneratePrompt::Category::Unknown), QString());
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertCategoryToString(TextAutoGenerateText::TextAutoGeneratePrompt::Category::Travel),
+             u"travel"_s);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertCategoryToString(TextAutoGenerateText::TextAutoGeneratePrompt::Category::Code), u"code"_s);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertCategoryToString(TextAutoGenerateText::TextAutoGeneratePrompt::Category::Misc), u"misc"_s);
+}
+
+void TextAutoGeneratePromptTest::shouldConvertStringToCategory()
+{
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertStringToCategory(u"ddd"_s), TextAutoGenerateText::TextAutoGeneratePrompt::Category::Unknown);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertStringToCategory(QString()), TextAutoGenerateText::TextAutoGeneratePrompt::Category::Unknown);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertStringToCategory(u"travel"_s),
+             TextAutoGenerateText::TextAutoGeneratePrompt::Category::Travel);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertStringToCategory(u"code"_s), TextAutoGenerateText::TextAutoGeneratePrompt::Category::Code);
+    QCOMPARE(TextAutoGenerateText::TextAutoGeneratePrompt::convertStringToCategory(u"misc"_s), TextAutoGenerateText::TextAutoGeneratePrompt::Category::Misc);
+}
+
 #include "moc_textautogenerateprompttest.cpp"
diff --git a/textautogeneratetext/core/autotests/textautogenerateprompttest.h b/textautogeneratetext/core/autotests/textautogenerateprompttest.h
index f200e8f6b..f8bbda9e9 100644
--- a/textautogeneratetext/core/autotests/textautogenerateprompttest.h
+++ b/textautogeneratetext/core/autotests/textautogenerateprompttest.h
@@ -16,4 +16,6 @@ public:
 
 private Q_SLOTS:
     void shouldHaveDefaultValues();
+    void shouldConvertCategoryToString();
+    void shouldConvertStringToCategory();
 };
diff --git a/textautogeneratetext/core/prompts/textautogenerateprompt.cpp b/textautogeneratetext/core/prompts/textautogenerateprompt.cpp
index fd5b340e8..72bca8ee6 100644
--- a/textautogeneratetext/core/prompts/textautogenerateprompt.cpp
+++ b/textautogeneratetext/core/prompts/textautogenerateprompt.cpp
@@ -6,7 +6,7 @@
 
 #include "textautogenerateprompt.h"
 #include <QDebug>
-
+using namespace Qt::Literals::StringLiterals;
 using namespace TextAutoGenerateText;
 TextAutoGeneratePrompt::TextAutoGeneratePrompt() = default;
 TextAutoGeneratePrompt::~TextAutoGeneratePrompt() = default;
@@ -61,6 +61,35 @@ void TextAutoGeneratePrompt::setCategory(Category newCategory)
     mCategory = newCategory;
 }
 
+QString TextAutoGeneratePrompt::convertCategoryToString(Category c)
+{
+    switch (c) {
+    case Category::Unknown:
+        return QString();
+    case Category::Travel:
+        return u"travel"_s;
+    case Category::Code:
+        return u"code"_s;
+    case Category::Misc:
+        return u"misc"_s;
+    }
+    return {};
+}
+
+TextAutoGeneratePrompt::Category TextAutoGeneratePrompt::convertStringToCategory(const QString &str)
+{
+    if (str.isEmpty()) {
+        return Category::Unknown;
+    } else if (str == "travel"_L1) {
+        return Category::Travel;
+    } else if (str == "code"_L1) {
+        return Category::Code;
+    } else if (str == "misc"_L1) {
+        return Category::Misc;
+    }
+    return Category::Unknown;
+}
+
 QDebug operator<<(QDebug d, const TextAutoGenerateText::TextAutoGeneratePrompt &t)
 {
     d.space() << "name:" << t.name();
diff --git a/textautogeneratetext/core/prompts/textautogenerateprompt.h b/textautogeneratetext/core/prompts/textautogenerateprompt.h
index 6dabc6b8b..868e0c8f9 100644
--- a/textautogeneratetext/core/prompts/textautogenerateprompt.h
+++ b/textautogeneratetext/core/prompts/textautogenerateprompt.h
@@ -73,6 +73,19 @@ public:
      */
     void setCategory(Category newCategory);
 
+    /*!
+     * \brief convertCategoryToString
+     * \param c
+     * \return
+     */
+    [[nodiscard]] static QString convertCategoryToString(Category c);
+    /*!
+     * \brief convertStringToCategory
+     * \param str
+     * \return
+     */
+    [[nodiscard]] static Category convertStringToCategory(const QString &str);
+
 private:
     Category mCategory = Category::Unknown;
     QString mName;
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.