2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file Validator.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
2020-12-05 17:17:02 -05:00
|
|
|
* \author Richard Kimberly Heck
|
2006-03-05 17:24:44 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 03:53:02 +00:00
|
|
|
#include "Validator.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2020-11-30 22:46:46 +02:00
|
|
|
#include "Dialog.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyXRC.h"
|
2020-11-30 22:46:46 +02:00
|
|
|
#include "qt_helpers.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-04-28 20:44:46 +00:00
|
|
|
#include "frontends/alert.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
#include "support/docstring.h"
|
2020-11-30 22:46:46 +02:00
|
|
|
#include "support/gettext.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
2006-08-17 09:03:26 +00:00
|
|
|
#include <QLineEdit>
|
2009-07-12 15:44:26 +00:00
|
|
|
#include <QLocale>
|
2006-08-17 09:03:26 +00:00
|
|
|
#include <QWidget>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
2007-11-14 00:21:31 +00:00
|
|
|
namespace frontend {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2006-08-17 09:03:26 +00:00
|
|
|
LengthValidator::LengthValidator(QWidget * parent)
|
2021-01-29 18:17:16 +01:00
|
|
|
: QValidator(parent)
|
2006-03-05 17:24:44 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State LengthValidator::validate(QString & qtext, int &) const
|
|
|
|
{
|
2015-03-15 11:48:36 +01:00
|
|
|
QLocale loc;
|
2009-07-12 15:44:26 +00:00
|
|
|
bool ok;
|
2015-05-20 19:12:23 +02:00
|
|
|
double d = loc.toDouble(qtext.trimmed(), &ok);
|
|
|
|
// QLocale::toDouble accepts something like "1."
|
|
|
|
// We don't.
|
|
|
|
bool dp = qtext.endsWith(loc.decimalPoint());
|
2015-03-15 11:48:36 +01:00
|
|
|
if (!ok) {
|
|
|
|
// Fall back to C
|
|
|
|
QLocale c(QLocale::C);
|
2015-05-20 19:12:23 +02:00
|
|
|
d = c.toDouble(qtext.trimmed(), &ok);
|
|
|
|
dp = qtext.endsWith(c.decimalPoint());
|
2015-03-15 11:48:36 +01:00
|
|
|
}
|
|
|
|
|
2015-05-20 19:12:23 +02:00
|
|
|
if (ok && unsigned_ && d < 0)
|
|
|
|
return QValidator::Invalid;
|
|
|
|
|
2022-03-17 08:15:09 +01:00
|
|
|
if (ok && positive_ && d == 0)
|
|
|
|
// A plausible intermediate value, see #12508
|
|
|
|
return QValidator::Intermediate;
|
|
|
|
|
2022-03-17 08:16:54 +01:00
|
|
|
if (ok && positive_ && d < 0)
|
2021-01-10 02:40:12 -05:00
|
|
|
return QValidator::Invalid;
|
|
|
|
|
2015-05-20 19:12:23 +02:00
|
|
|
if (qtext.isEmpty() || (ok && !dp))
|
2006-03-05 17:24:44 +00:00
|
|
|
return QValidator::Acceptable;
|
|
|
|
|
2009-07-12 15:44:26 +00:00
|
|
|
string const text = fromqstr(qtext);
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
if (glue_length_) {
|
2007-04-28 12:58:49 +00:00
|
|
|
GlueLength gl;
|
2015-05-20 19:12:23 +02:00
|
|
|
if (unsigned_ && gl.len().value() < 0)
|
|
|
|
return QValidator::Invalid;
|
2006-03-05 17:24:44 +00:00
|
|
|
return (isValidGlueLength(text, &gl)) ?
|
|
|
|
QValidator::Acceptable : QValidator::Intermediate;
|
2007-09-10 22:32:59 +00:00
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
Length l;
|
2006-03-05 17:24:44 +00:00
|
|
|
bool const valid_length = isValidLength(text, &l);
|
|
|
|
if (!valid_length)
|
|
|
|
return QValidator::Intermediate;
|
|
|
|
|
|
|
|
if (no_bottom_)
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
|
2015-05-20 19:12:23 +02:00
|
|
|
if (unsigned_ && l.value() < 0)
|
|
|
|
return QValidator::Invalid;
|
|
|
|
|
2021-01-10 02:40:12 -05:00
|
|
|
if (positive_ && l.value() <= 0)
|
|
|
|
return QValidator::Invalid;
|
|
|
|
|
|
|
|
return bottom_.inPixels(100) <= l.inPixels(100) ?
|
2006-03-05 17:24:44 +00:00
|
|
|
QValidator::Acceptable : QValidator::Intermediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
void LengthValidator::setBottom(Length const & b)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2021-01-10 02:40:12 -05:00
|
|
|
bottom_ = b;
|
2006-03-05 17:24:44 +00:00
|
|
|
no_bottom_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-28 12:58:49 +00:00
|
|
|
void LengthValidator::setBottom(GlueLength const & g)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2021-01-10 02:40:12 -05:00
|
|
|
glue_bottom_ = g;
|
2006-03-05 17:24:44 +00:00
|
|
|
no_bottom_ = false;
|
|
|
|
glue_length_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LengthValidator * unsignedLengthValidator(QLineEdit * ed)
|
|
|
|
{
|
|
|
|
LengthValidator * v = new LengthValidator(ed);
|
2007-04-28 12:58:49 +00:00
|
|
|
v->setBottom(Length());
|
2015-05-20 19:12:23 +02:00
|
|
|
v->setUnsigned(true);
|
2006-03-05 17:24:44 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 16:53:50 +00:00
|
|
|
LengthValidator * unsignedGlueLengthValidator(QLineEdit * ed)
|
|
|
|
{
|
|
|
|
LengthValidator * v = new LengthValidator(ed);
|
|
|
|
v->setBottom(GlueLength());
|
2015-05-20 19:12:23 +02:00
|
|
|
v->setUnsigned(true);
|
2009-07-19 16:53:50 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-28 22:41:32 +02:00
|
|
|
LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const & autotext)
|
2009-01-14 17:53:31 +00:00
|
|
|
: LengthValidator(parent),
|
|
|
|
autotext_(autotext)
|
2007-04-05 14:58:15 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
|
|
|
|
{
|
2009-01-14 17:53:31 +00:00
|
|
|
if (qtext == autotext_)
|
2007-04-05 14:58:15 +00:00
|
|
|
return QValidator::Acceptable;
|
|
|
|
return LengthValidator::validate(qtext, dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-28 22:41:32 +02:00
|
|
|
LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed, QString const & autotext)
|
2007-04-05 14:58:15 +00:00
|
|
|
{
|
2009-01-14 17:53:31 +00:00
|
|
|
LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
|
2007-04-28 12:58:49 +00:00
|
|
|
v->setBottom(Length());
|
2015-05-20 19:12:23 +02:00
|
|
|
v->setUnsigned(true);
|
2007-04-05 14:58:15 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-10 02:40:12 -05:00
|
|
|
LengthAutoValidator * positiveLengthAutoValidator(QLineEdit * ed, QString const & autotext)
|
|
|
|
{
|
|
|
|
LengthAutoValidator * v = new LengthAutoValidator(ed, autotext);
|
|
|
|
v->setBottom(Length());
|
|
|
|
v->setPositive(true);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-28 22:41:32 +02:00
|
|
|
DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const & autotext)
|
2009-01-14 17:53:31 +00:00
|
|
|
: QDoubleValidator(parent),
|
|
|
|
autotext_(autotext)
|
2007-11-14 00:09:52 +00:00
|
|
|
{}
|
2007-04-05 14:58:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
DoubleAutoValidator::DoubleAutoValidator(double bottom,
|
2007-11-14 00:09:52 +00:00
|
|
|
double top, int decimals, QObject * parent)
|
|
|
|
: QDoubleValidator(bottom, top, decimals, parent)
|
|
|
|
{}
|
2007-04-05 14:58:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
|
2009-01-14 17:53:31 +00:00
|
|
|
if (input == autotext_)
|
2007-04-05 14:58:15 +00:00
|
|
|
return QValidator::Acceptable;
|
|
|
|
return QDoubleValidator::validate(input, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-24 16:39:17 +00:00
|
|
|
NoNewLineValidator::NoNewLineValidator(QWidget * parent)
|
|
|
|
: QValidator(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State NoNewLineValidator::validate(QString & qtext, int &) const
|
|
|
|
{
|
Allow compiling with Qt6
This commit allows compiling LyX with Qt6 when using autotools.
For a successful compilation the following 2 conditions must be met.
1) The Qt6 qmake has to come first in PATH, so that the command
"qmake -v | grep -o 'Qt version .'" returns "Qt version 6".
2) The --enable-qt6 switch has to be passed to the configure command.
If --enable-qt6 is used but Qt6 is not found, Qt5 is tried as a fallback.
If also Qt5 is not found, configuring for Qt4 is attempted.
If --enable-qt6 is not used, then things go as usual. This means that Qt5
is tried first and then Qt4, unless --disable-qt5 is used, in which case
Qt4 is directly attempted. This means that existing scripts should
continue working unmodified.
LyX should compile with Qt6 on windows and linux, and possibly also on
mac, but I could not test that. However, it is not guaranteed that it
works as it should. In particular I am not sure that I got right the
conversion from QRegExp to QRegularExpression. For sure, the syntax
highlighting seems to not work right. Someone in the know should take
a look at that. I am able to load documents and compile them but some
thourough testing is needed. However, when compiling for Qt5 or Qt4,
I tried to make sure that the functionality is preserved.
2021-03-15 17:09:09 +01:00
|
|
|
#if QT_VERSION < 0x060000
|
2011-09-24 16:39:17 +00:00
|
|
|
qtext.remove(QRegExp("[\\n\\r]"));
|
Allow compiling with Qt6
This commit allows compiling LyX with Qt6 when using autotools.
For a successful compilation the following 2 conditions must be met.
1) The Qt6 qmake has to come first in PATH, so that the command
"qmake -v | grep -o 'Qt version .'" returns "Qt version 6".
2) The --enable-qt6 switch has to be passed to the configure command.
If --enable-qt6 is used but Qt6 is not found, Qt5 is tried as a fallback.
If also Qt5 is not found, configuring for Qt4 is attempted.
If --enable-qt6 is not used, then things go as usual. This means that Qt5
is tried first and then Qt4, unless --disable-qt5 is used, in which case
Qt4 is directly attempted. This means that existing scripts should
continue working unmodified.
LyX should compile with Qt6 on windows and linux, and possibly also on
mac, but I could not test that. However, it is not guaranteed that it
works as it should. In particular I am not sure that I got right the
conversion from QRegExp to QRegularExpression. For sure, the syntax
highlighting seems to not work right. Someone in the know should take
a look at that. I am able to load documents and compile them but some
thourough testing is needed. However, when compiling for Qt5 or Qt4,
I tried to make sure that the functionality is preserved.
2021-03-15 17:09:09 +01:00
|
|
|
#else
|
|
|
|
qtext.remove(QRegularExpression("[\\n\\r]"));
|
|
|
|
#endif
|
2011-09-24 16:39:17 +00:00
|
|
|
return QValidator::Acceptable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
PathValidator::PathValidator(bool acceptable_if_empty,
|
2006-08-17 09:03:26 +00:00
|
|
|
QWidget * parent)
|
|
|
|
: QValidator(parent),
|
2006-03-05 17:24:44 +00:00
|
|
|
acceptable_if_empty_(acceptable_if_empty),
|
|
|
|
latex_doc_(false),
|
|
|
|
tex_allows_spaces_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-04-25 16:39:21 +00:00
|
|
|
static docstring const printable_list(docstring const & invalid_chars)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2006-10-09 09:15:37 +00:00
|
|
|
docstring s;
|
|
|
|
docstring::const_iterator const begin = invalid_chars.begin();
|
|
|
|
docstring::const_iterator const end = invalid_chars.end();
|
|
|
|
docstring::const_iterator it = begin;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (it != begin)
|
2006-12-21 19:02:17 +00:00
|
|
|
s += ", ";
|
|
|
|
if (*it == ' ')
|
2006-10-09 09:15:37 +00:00
|
|
|
s += _("space");
|
2006-03-05 17:24:44 +00:00
|
|
|
else
|
2006-10-09 09:15:37 +00:00
|
|
|
s += *it;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
return s;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State PathValidator::validate(QString & qtext, int &) const
|
|
|
|
{
|
|
|
|
if (!latex_doc_)
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
|
2007-11-14 00:09:52 +00:00
|
|
|
docstring const text = support::trim(qstring_to_ucs4(qtext));
|
2006-03-05 17:24:44 +00:00
|
|
|
if (text.empty())
|
2007-11-14 00:09:52 +00:00
|
|
|
return acceptable_if_empty_ ?
|
2006-03-05 17:24:44 +00:00
|
|
|
QValidator::Acceptable : QValidator::Intermediate;
|
|
|
|
|
2006-12-21 19:02:17 +00:00
|
|
|
docstring invalid_chars = from_ascii("#$%{}()[]\"^");
|
2006-03-05 17:24:44 +00:00
|
|
|
if (!tex_allows_spaces_)
|
|
|
|
invalid_chars += ' ';
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
if (text.find_first_of(invalid_chars) != docstring::npos) {
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
static int counter = 0;
|
|
|
|
if (counter == 0) {
|
2007-11-14 00:21:31 +00:00
|
|
|
Alert::error(_("Invalid filename"),
|
2007-03-27 17:40:05 +00:00
|
|
|
_("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
|
2006-10-09 09:15:37 +00:00
|
|
|
printable_list(invalid_chars));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
++counter;
|
|
|
|
return QValidator::Intermediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-22 00:33:28 -05:00
|
|
|
void PathValidator::setChecker(KernelDocType const & type, LyXRC const & rc)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2020-12-01 00:00:40 +02:00
|
|
|
latex_doc_ = type == KernelDocType::LaTeX;
|
2018-02-22 00:33:28 -05:00
|
|
|
tex_allows_spaces_ = rc.tex_allows_spaces;
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PathValidator * getPathValidator(QLineEdit * ed)
|
|
|
|
{
|
|
|
|
if (!ed)
|
2021-01-10 02:40:12 -05:00
|
|
|
return nullptr;
|
2006-03-05 17:24:44 +00:00
|
|
|
QValidator * validator = const_cast<QValidator *>(ed->validator());
|
|
|
|
if (!validator)
|
2021-01-10 02:40:12 -05:00
|
|
|
return nullptr;
|
2006-03-05 17:24:44 +00:00
|
|
|
return dynamic_cast<PathValidator *>(validator);
|
|
|
|
}
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2007-11-14 00:21:31 +00:00
|
|
|
} // namespace frontend
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_Validator.cpp"
|