aboutsummaryrefslogtreecommitdiff
path: root/src/GraphemeCountValidator.cpp
diff options
context:
space:
mode:
authorAndreas Grois <andi@grois.info>2022-10-10 21:30:02 +0200
committerAndreas Grois <andi@grois.info>2022-10-10 21:37:15 +0200
commite4ad766315879e1ff05bb111229f073f8f0ed68e (patch)
tree4b043ff47c78b2c00c80c94ebda622c32c8b6d3d /src/GraphemeCountValidator.cpp
PassFish: Initial Commit
Well, that's a lie. But nobody needs to see all the iterations I decided to sweep under the rug. That said, I think the repo is, while not clean, clean enough now, to not be embarrassed by uploading it to github.
Diffstat (limited to 'src/GraphemeCountValidator.cpp')
-rw-r--r--src/GraphemeCountValidator.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/GraphemeCountValidator.cpp b/src/GraphemeCountValidator.cpp
new file mode 100644
index 0000000..9fc6436
--- /dev/null
+++ b/src/GraphemeCountValidator.cpp
@@ -0,0 +1,42 @@
+#include "GraphemeCountValidator.h"
+#include <QTextBoundaryFinder>
+
+GraphemeCountValidator::GraphemeCountValidator(QObject *parent)
+ : GraphemeCountValidator(0, parent)
+{}
+
+GraphemeCountValidator::GraphemeCountValidator(uint min_count, QObject *parent)
+ : QValidator(parent)
+ , minCount(min_count)
+{}
+
+uint GraphemeCountValidator::minGraphemeCount() const
+{
+ return minCount;
+}
+
+void GraphemeCountValidator::setMinGraphemeCount(uint c)
+{
+ if(minCount != c)
+ {
+ minCount = c;
+ emit minGraphemeCountChanged(minCount);
+ emit changed();
+ }
+}
+
+QValidator::State GraphemeCountValidator::validate(QString & text, int&) const
+{
+ //One could write Rust FFI bindings and do this in Rust.
+ //But that would be way more work for no gain.
+ QTextBoundaryFinder boundsFinder{
+ QTextBoundaryFinder::BoundaryType::Grapheme,
+ text
+ };
+ int requiredBoundary = 0;
+ for(uint i = 0; i < minCount; ++i)
+ requiredBoundary = boundsFinder.toNextBoundary();
+ return requiredBoundary >= 0
+ ? QValidator::State::Acceptable
+ : QValidator::State::Intermediate;
+}