From e4ad766315879e1ff05bb111229f073f8f0ed68e Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Mon, 10 Oct 2022 21:30:02 +0200 Subject: 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. --- src/GraphemeCountValidator.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/GraphemeCountValidator.cpp (limited to 'src/GraphemeCountValidator.cpp') 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 + +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; +} -- cgit v1.2.3