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
|
2007-04-05 14:58:15 +00:00
|
|
|
* \author Richard 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
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "LyXRC.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"
|
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)
|
|
|
|
: QValidator(parent),
|
2006-03-05 17:24:44 +00:00
|
|
|
no_bottom_(true), glue_length_(false)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State LengthValidator::validate(QString & qtext, int &) const
|
|
|
|
{
|
2009-07-12 15:44:26 +00:00
|
|
|
bool ok;
|
2009-07-12 17:32:23 +00:00
|
|
|
qtext.trimmed().toDouble(&ok);
|
2009-07-12 15:44:26 +00:00
|
|
|
if (qtext.isEmpty() || ok)
|
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;
|
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;
|
|
|
|
|
|
|
|
return b_.inPixels(100) <= l.inPixels(100) ?
|
|
|
|
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
|
|
|
{
|
|
|
|
b_ = b;
|
|
|
|
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
|
|
|
{
|
|
|
|
g_ = g;
|
|
|
|
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());
|
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());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 17:53:31 +00:00
|
|
|
LengthAutoValidator::LengthAutoValidator(QWidget * parent, QString const autotext)
|
|
|
|
: 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 17:53:31 +00: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());
|
2007-04-05 14:58:15 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-14 17:53:31 +00:00
|
|
|
DoubleAutoValidator::DoubleAutoValidator(QWidget * parent, QString const autotext)
|
|
|
|
: 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-14 00:21:31 +00:00
|
|
|
void PathValidator::setChecker(KernelDocType const & type, LyXRC const & lyxrc)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-11-14 00:21:31 +00:00
|
|
|
latex_doc_ = type == LATEX;
|
2006-03-05 17:24:44 +00:00
|
|
|
tex_allows_spaces_ = lyxrc.tex_allows_spaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PathValidator * getPathValidator(QLineEdit * ed)
|
|
|
|
{
|
|
|
|
if (!ed)
|
|
|
|
return 0;
|
|
|
|
QValidator * validator = const_cast<QValidator *>(ed->validator());
|
|
|
|
if (!validator)
|
|
|
|
return 0;
|
|
|
|
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"
|