New function for detecting mixed RTL-LTR texts

Qt's isRightToLeft() returns true only for all left-to-right characters
in the string. That prevents the automatic switch to LTR Text mode, when
the text contains one or more left-to-right characters.

See #1793 and #1929
This commit is contained in:
mgrojo
2019-07-07 19:26:40 +02:00
committed by Manuel
parent 5e965b1a20
commit 592fa91e25
3 changed files with 20 additions and 2 deletions

View File

@@ -43,6 +43,21 @@ bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest)
}
}
bool containsRightToLeft(const QString& text) {
for(QChar ch : text) {
switch(ch.direction()) {
case QChar::DirR:
case QChar::DirAL:
case QChar::DirRLE:
case QChar::DirRLO:
case QChar::DirRLI:
return true;
}
}
return false;
}
bool startsWithBom(const QByteArray& data)
{
if(data.startsWith(bom3) ||

View File

@@ -10,6 +10,9 @@
// text but makes it less reliable
bool isTextOnly(QByteArray data, const QString& encoding = QString(), bool quickTest = false);
// This returns true if text contains some character whose direction is right-to-left.
bool containsRightToLeft(const QString& text);
// This function returns true if the data in the data parameter starts with a Unicode BOM. Otherwise it returns false.
bool startsWithBom(const QByteArray& data);

View File

@@ -816,7 +816,7 @@ void EditDialog::editTextChanged()
// Switch to the Qt Editor if we detect right-to-left text,
// since the QScintilla editor does not support it.
if (sciEdit->text().isRightToLeft())
if (containsRightToLeft(sciEdit->text()))
ui->comboMode->setCurrentIndex(RtlTextEditor);
}
@@ -871,7 +871,7 @@ int EditDialog::checkDataType(const QByteArray& bArrdata)
if(!json::parse(cellData, nullptr, false).is_discarded())
return JSON;
else {
if (QString::fromUtf8(cellData).isRightToLeft())
if (containsRightToLeft(QString::fromUtf8(cellData)))
return RtlText;
else
return Text;