8#include <QtWidgets/qlineedit.h>
9#include <QtGui/qvalidator.h>
10#include <QtGui/qevent.h>
11#include <QtWidgets/qcompleter.h>
12#include <QtWidgets/qabstractitemview.h>
13#include <QtCore/qregularexpression.h>
14#include <QtCore/qurl.h>
15#include <QtCore/qfile.h>
16#include <QtCore/qdebug.h>
20using namespace Qt::StringLiterals;
23 const QChar NewLineChar(u'\n');
24 const auto EscapedNewLine =
"\\n"_L1;
27 class ReplacementValidator :
public QValidator {
29 ReplacementValidator (QObject * parent,
30 const QString &offending,
31 const QString &replacement);
32 void fixup ( QString & input )
const override;
33 State validate ( QString & input,
int &pos)
const override;
35 const QString m_offending;
36 const QString m_replacement;
39 ReplacementValidator::ReplacementValidator (QObject * parent,
40 const QString &offending,
41 const QString &replacement) :
43 m_offending(offending),
44 m_replacement(replacement)
48 void ReplacementValidator::fixup ( QString & input )
const {
49 input.replace(m_offending, m_replacement);
52 QValidator::State ReplacementValidator::validate ( QString & input,
int &)
const {
58 class StyleSheetValidator :
public ReplacementValidator {
60 StyleSheetValidator (QObject * parent);
61 State validate(QString & input,
int &pos)
const override;
64 StyleSheetValidator::StyleSheetValidator (QObject * parent) :
65 ReplacementValidator(parent, NewLineChar, EscapedNewLine)
69 QValidator::State StyleSheetValidator::validate ( QString & input,
int &pos)
const
72 const State state = ReplacementValidator:: validate(input, pos);
73 if (state != Acceptable)
76 const QString styleSheet = qdesigner_internal::TextPropertyEditor::editorStringToString(input, qdesigner_internal::ValidationStyleSheet);
77 const bool valid = qdesigner_internal::StyleSheetEditorDialog::isStyleSheetValid(styleSheet);
78 return valid ? Acceptable : Intermediate;
83 class UrlValidator :
public QValidator {
85 UrlValidator(QCompleter *completer, QObject *parent);
87 State validate(QString &input,
int &pos)
const override;
88 void fixup(QString &input)
const override;
90 QUrl guessUrlFromString(
const QString &string)
const;
91 QCompleter *m_completer;
94 UrlValidator::UrlValidator(QCompleter *completer, QObject *parent) :
96 m_completer(completer)
100 QValidator::State UrlValidator::validate(QString &input,
int &pos)
const
107 const QUrl url(input, QUrl::StrictMode);
109 if (!url.isValid() || url.isEmpty())
112 if (url.scheme().isEmpty())
115 if (url.host().isEmpty() && url.path().isEmpty())
121 void UrlValidator::fixup(QString &input)
const
124 if (
const QAbstractItemView *iv = m_completer->popup()) {
129 input = guessUrlFromString(input).toString();
132 QUrl UrlValidator::guessUrlFromString(
const QString &string)
const
134 const QString urlStr = string.trimmed();
135 const QRegularExpression qualifiedUrl(u"^[a-zA-Z]+\\:.*$"_s);
136 Q_ASSERT(qualifiedUrl.isValid());
139 const bool hasSchema = qualifiedUrl.match(urlStr).hasMatch();
141 const QUrl url(urlStr, QUrl::TolerantMode);
147 if (string.startsWith(
":/"_L1))
148 return QUrl(
"qrc"_L1 + string);
151 if (QFile::exists(urlStr))
152 return QUrl::fromLocalFile(urlStr);
156 const int dotIndex = urlStr.indexOf(u'.');
157 if (dotIndex != -1) {
158 const QString prefix = urlStr.left(dotIndex).toLower();
160 if (prefix ==
"ftp"_L1)
163 urlString +=
"http"_L1;
164 urlString +=
"://"_L1;
166 const QUrl url(urlString, QUrl::TolerantMode);
173 return QUrl(string, QUrl::TolerantMode);
246 u"data:text/html,"_s,
Combined button and popup list for selecting options.
Auxiliary methods to store/retrieve settings.