aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/patternvalidator.h
blob: b83804911c5c5989946d14a8ea12c7f8095c2cfd (plain)
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
// Copyright (C) 2025 Andre Hartmann.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include <QValidator>

/**
 * Replaces \a forbiddenChars with \a replacementChar while typing to avoid invalid pattern.
 */
class PatternValidator : public QValidator {
public:
    PatternValidator(QChar replacementChar, const QList<QChar> &forbiddenChars, QObject *parent = nullptr)
        : QValidator(parent), m_replacementChar(replacementChar), m_forbiddenChars(forbiddenChars) {}

    State validate(QString &input, int &) const override
    {
        for (QChar c : m_forbiddenChars)
            input.replace(c, m_replacementChar);

        return QValidator::Acceptable;
    }

private:
    const QChar m_replacementChar;
    const QList<QChar> m_forbiddenChars;
};