2006-03-05 17:24:44 +00:00
|
|
|
/**
|
|
|
|
* \file validators.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "validators.h"
|
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
|
|
|
#include "gettext.h"
|
|
|
|
#include "lyxrc.h"
|
|
|
|
|
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
|
|
|
#include "frontends/controllers/Dialog.h"
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
#include "support/docstring.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/std_ostream.h"
|
|
|
|
|
2006-08-17 09:03:26 +00:00
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QWidget>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using lyx::support::isStrDbl;
|
2006-10-09 09:15:37 +00:00
|
|
|
using lyx::docstring;
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
using std::string;
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
string const text = fromqstr(qtext);
|
|
|
|
if (text.empty() || isStrDbl(text))
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
|
|
|
|
if (glue_length_) {
|
|
|
|
LyXGlueLength gl;
|
|
|
|
return (isValidGlueLength(text, &gl)) ?
|
|
|
|
QValidator::Acceptable : QValidator::Intermediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
LyXLength l;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LengthValidator::setBottom(LyXLength const & b)
|
|
|
|
{
|
|
|
|
b_ = b;
|
|
|
|
no_bottom_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LengthValidator::setBottom(LyXGlueLength const & g)
|
|
|
|
{
|
|
|
|
g_ = g;
|
|
|
|
no_bottom_ = false;
|
|
|
|
glue_length_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LengthValidator * unsignedLengthValidator(QLineEdit * ed)
|
|
|
|
{
|
|
|
|
LengthValidator * v = new LengthValidator(ed);
|
|
|
|
v->setBottom(LyXLength());
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
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-10-09 09:15:37 +00:00
|
|
|
s += lyx::from_ascii(", ");
|
|
|
|
if (*it == lyx::char_type(' '))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
QValidator::State PathValidator::validate(QString & qtext, int &) const
|
|
|
|
{
|
|
|
|
if (!latex_doc_)
|
|
|
|
return QValidator::Acceptable;
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
|
2006-03-05 17:24:44 +00:00
|
|
|
if (text.empty())
|
2006-04-05 23:56:29 +00:00
|
|
|
return acceptable_if_empty_ ?
|
2006-03-05 17:24:44 +00:00
|
|
|
QValidator::Acceptable : QValidator::Intermediate;
|
|
|
|
|
2006-10-09 09:15:37 +00:00
|
|
|
docstring invalid_chars = lyx::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) {
|
2006-10-07 16:47:54 +00:00
|
|
|
lyx::frontend::Alert::error(_("Invalid filename"),
|
2006-09-11 08:54:10 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PathValidator::setChecker(lyx::frontend::KernelDocType const & type,
|
|
|
|
LyXRC const & lyxrc)
|
|
|
|
{
|
|
|
|
latex_doc_ = type == lyx::frontend::Kernel::LATEX;
|
|
|
|
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
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2006-05-18 08:51:12 +00:00
|
|
|
#include "validators_moc.cpp"
|
2006-10-21 00:16:43 +00:00
|
|
|
|