aboutsummaryrefslogtreecommitdiff
path: root/src/GraphemeCountValidator.cpp
blob: 9fc643676c67e2c8edb1a6b0d117684a33775cf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
}