lyx_mirror/src/frontends/qt4/validators.C
Richard Heck 11697004e8 Fix for bug 3215. All changes in src/frontends/qt4/.
Changed dialog and added routines to deal with an "auto" label
in cases where defaults are used.

ui/MarginsUi.ui: Changed labels for width and height to checkboxes. Removed connections.
checkwidgets.[Ch]: Extended checkedLineEdit routines to take a QWidget.
qt_helpers.[Ch]: 
  Added void lengthToWidgets(QLineEdit *, LengthCombo *, LyXLength const &, LyXLength::UNIT)
  Added void lengthAutoToWidgets(QLineEdit *, LengthCombo *, LyXLength const &, LyXLength::UNIT)
  Added void setAutoTextCB(QCheckBox *, QLineEdit *, LengthCombo *)
validators.[Ch]:
  Added class LengthAutoValidator : public LengthValidator
  Added class DoubleAutoValidator : public QDoubleValidator
QGraphicsDialog.[Ch]
  Added virtual void setAutoText()
  Added virtual void on_WidthCB_toggled(bool)
  Added virtual void on_HeightCB_toggled(bool)
  Used the new functions to set "auto" in default cases and toggle checkboxes as needed.
  Set validator for scale.
  Re-organized connect routines.
QGraphics.C:
  Completely re-worked update_contents().
  Significant changes to apply().


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17735 a592a061-630c-0410-9148-cb99ea01b6c8
2007-04-05 14:58:15 +00:00

220 lines
4.5 KiB
C

/**
* \file validators.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Angus Leeming
* \author Richard Heck
*
* 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"
#include "support/docstring.h"
#include "support/lstrings.h"
#include "support/std_ostream.h"
#include <QLineEdit>
#include <QWidget>
#include <sstream>
using lyx::support::isStrDbl;
using lyx::docstring;
using std::string;
namespace lyx {
LengthValidator::LengthValidator(QWidget * parent)
: QValidator(parent),
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;
}
LengthAutoValidator::LengthAutoValidator(QWidget * parent)
: LengthValidator(parent)
{}
QValidator::State LengthAutoValidator::validate(QString & qtext, int & dummy) const
{
string const text = fromqstr(qtext);
if (text == "auto")
return QValidator::Acceptable;
return LengthValidator::validate(qtext, dummy);
}
LengthAutoValidator * unsignedLengthAutoValidator(QLineEdit * ed)
{
LengthAutoValidator * v = new LengthAutoValidator(ed);
v->setBottom(LyXLength());
return v;
}
DoubleAutoValidator::DoubleAutoValidator(QWidget * parent) :
QDoubleValidator(parent) {}
DoubleAutoValidator::DoubleAutoValidator(double bottom,
double top, int decimals, QObject * parent) :
QDoubleValidator(bottom, top, decimals, parent) {}
QValidator::State DoubleAutoValidator::validate(QString & input, int & pos) const {
string const text = fromqstr(input);
if (text == "auto")
return QValidator::Acceptable;
return QDoubleValidator::validate(input, pos);
}
PathValidator::PathValidator(bool acceptable_if_empty,
QWidget * parent)
: QValidator(parent),
acceptable_if_empty_(acceptable_if_empty),
latex_doc_(false),
tex_allows_spaces_(false)
{}
namespace {
docstring const printable_list(docstring const & invalid_chars)
{
docstring s;
docstring::const_iterator const begin = invalid_chars.begin();
docstring::const_iterator const end = invalid_chars.end();
docstring::const_iterator it = begin;
for (; it != end; ++it) {
if (it != begin)
s += ", ";
if (*it == ' ')
s += _("space");
else
s += *it;
}
return s;
}
} // namespace anon
QValidator::State PathValidator::validate(QString & qtext, int &) const
{
if (!latex_doc_)
return QValidator::Acceptable;
docstring const text = lyx::support::trim(qstring_to_ucs4(qtext));
if (text.empty())
return acceptable_if_empty_ ?
QValidator::Acceptable : QValidator::Intermediate;
docstring invalid_chars = from_ascii("#$%{}()[]\"^");
if (!tex_allows_spaces_)
invalid_chars += ' ';
if (text.find_first_of(invalid_chars) != docstring::npos) {
static int counter = 0;
if (counter == 0) {
lyx::frontend::Alert::error(_("Invalid filename"),
_("LyX does not provide LaTeX support for file names containing any of these characters:\n") +
printable_list(invalid_chars));
}
++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);
}
} // namespace lyx
#include "validators_moc.cpp"