From 985ba3b296d55f8073d9fe9b61dae5e02811be49 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Tue, 12 Jun 2007 17:10:28 +0000 Subject: [PATCH] Add bypass validation checkboxes to listings related dialogs git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18751 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferParams.cpp | 9 +- src/frontends/qt4/QDocument.cpp | 29 +++--- src/frontends/qt4/QInclude.cpp | 38 +++---- src/frontends/qt4/QListings.cpp | 26 +++-- src/frontends/qt4/ui/IncludeUi.ui | 10 ++ src/frontends/qt4/ui/ListingsUi.ui | 147 ++++++++++++++------------- src/frontends/qt4/ui/TextLayoutUi.ui | 12 ++- src/insets/InsetListingsParams.cpp | 110 ++++++++++++-------- src/insets/InsetListingsParams.h | 19 +--- 9 files changed, 221 insertions(+), 179 deletions(-) diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index f1ef2b4f89..c96270cadb 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -608,14 +608,7 @@ string const BufferParams::readToken(Lexer & lex, string const & token) } else if (token == "\\listings_params") { string par; lex >> par; - // validate par and produce a valid listings parameter string - try { - listings_params = InsetListingsParams(par).params(); - } catch (invalidParam & e) { - lyxerr << "Invalid parameter string " << par << endl; - lyxerr << e.what() << endl; - listings_params = string(); - } + listings_params = InsetListingsParams(par).params(); } else if (token == "\\papersides") { int psides; lex >> psides; diff --git a/src/frontends/qt4/QDocument.cpp b/src/frontends/qt4/QDocument.cpp index a917134d2b..f895d30d4d 100644 --- a/src/frontends/qt4/QDocument.cpp +++ b/src/frontends/qt4/QDocument.cpp @@ -222,6 +222,8 @@ QDocumentDialog::QDocumentDialog(QDocument * form) this, SLOT(change_adaptor())); connect(textLayoutModule->listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor())); + connect(textLayoutModule->bypassCB, SIGNAL(clicked()), + this, SLOT(change_adaptor())); connect(textLayoutModule->listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params())); textLayoutModule->listingsTB->setPlainText( @@ -622,20 +624,23 @@ void QDocumentDialog::change_adaptor() void QDocumentDialog::validate_listings_params() { static bool isOK = true; - try { - InsetListingsParams par(fromqstr(textLayoutModule->listingsED->toPlainText())); - if (!isOK) { - isOK = true; - // listingsTB->setTextColor("black"); - textLayoutModule->listingsTB->setPlainText( - qt_("Input listings parameters on the right. Enter ? for a list of parameters.")); - okPB->setEnabled(true); - applyPB->setEnabled(true); - } - } catch (invalidParam & e) { + InsetListingsParams par(fromqstr(textLayoutModule->listingsED->toPlainText())); + docstring msg; + if (!textLayoutModule->bypassCB->isChecked()) + msg = par.validate(); + if (msg.empty()) { + if (isOK) + return; + isOK = true; + // listingsTB->setTextColor("black"); + textLayoutModule->listingsTB->setPlainText( + qt_("Input listings parameters on the right. Enter ? for a list of parameters.")); + okPB->setEnabled(true); + applyPB->setEnabled(true); + } else { isOK = false; // listingsTB->setTextColor("red"); - textLayoutModule->listingsTB->setPlainText(toqstr(e.what())); + textLayoutModule->listingsTB->setPlainText(toqstr(msg)); okPB->setEnabled(false); applyPB->setEnabled(false); } diff --git a/src/frontends/qt4/QInclude.cpp b/src/frontends/qt4/QInclude.cpp index 31aaa115e6..7437b60009 100644 --- a/src/frontends/qt4/QInclude.cpp +++ b/src/frontends/qt4/QInclude.cpp @@ -66,6 +66,7 @@ QIncludeDialog::QIncludeDialog(QInclude * form) connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params())); + connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); filenameED->setValidator(new PathValidator(true, filenameED)); setFocusProxy(filenameED); @@ -87,17 +88,20 @@ void QIncludeDialog::change_adaptor() void QIncludeDialog::validate_listings_params() { static bool isOK = true; - try { - InsetListingsParams par(fromqstr(listingsED->toPlainText())); - if (!isOK) { - isOK = true; - listingsTB->setPlainText( - qt_("Input listing parameters on the right. Enter ? for a list of parameters.")); - okPB->setEnabled(true); - } - } catch (invalidParam & e) { + InsetListingsParams par(fromqstr(listingsED->toPlainText())); + docstring msg; + if (!bypassCB->isChecked()) + msg = par.validate(); + if (msg.empty()) { + if (isOK) + return; + isOK = true; + listingsTB->setPlainText( + qt_("Input listing parameters on the right. Enter ? for a list of parameters.")); + okPB->setEnabled(true); + } else { isOK = false; - listingsTB->setPlainText(toqstr(e.what())); + listingsTB->setPlainText(toqstr(msg)); okPB->setEnabled(false); } } @@ -250,18 +254,16 @@ void QInclude::update_contents() it != pars.end(); ++it) { if (prefixIs(*it, "caption=")) { string cap = it->substr(8); - if (cap[0] == '{' && cap[cap.size()-1] == '}') + if (cap[0] == '{' && cap[cap.size()-1] == '}') { dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2))); - else - throw invalidParam(_("caption parameter is not quoted with braces")); - *it = ""; + *it = ""; + } } else if (prefixIs(*it, "label=")) { string lbl = it->substr(6); - if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') + if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') { dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2))); - else - throw invalidParam(_("label parameter is not quoted with braces")); - *it = ""; + *it = ""; + } } } // the rest is put to the extra edit box. diff --git a/src/frontends/qt4/QListings.cpp b/src/frontends/qt4/QListings.cpp index 80d2e53bb5..f6d181f593 100644 --- a/src/frontends/qt4/QListings.cpp +++ b/src/frontends/qt4/QListings.cpp @@ -191,6 +191,7 @@ QListingsDialog::QListingsDialog(QListings * form) connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor())); connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params())); + connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor())); for (int n = 0; languages[n][0]; ++n) languageCO->addItem(qt_(languages_gui[n])); @@ -320,18 +321,21 @@ string QListingsDialog::construct_params() void QListingsDialog::validate_listings_params() { static bool isOK = true; - try { - InsetListingsParams par(construct_params()); - if (!isOK) { - isOK = true; - listingsTB->setPlainText( - qt_("Input listing parameters on the right. Enter ? for a list of parameters.")); - okPB->setEnabled(true); - applyPB->setEnabled(true); - } - } catch (invalidParam & e) { + InsetListingsParams par(construct_params()); + docstring msg; + if (!bypassCB->isChecked()) + msg = par.validate(); + if (msg.empty()) { + if (isOK) + return; + isOK = true; + listingsTB->setPlainText( + qt_("Input listing parameters on the right. Enter ? for a list of parameters.")); + okPB->setEnabled(true); + applyPB->setEnabled(true); + } else { isOK = false; - listingsTB->setPlainText(toqstr(e.what())); + listingsTB->setPlainText(toqstr(msg)); okPB->setEnabled(false); applyPB->setEnabled(false); } diff --git a/src/frontends/qt4/ui/IncludeUi.ui b/src/frontends/qt4/ui/IncludeUi.ui index 9190b2c0ec..efe6430de9 100644 --- a/src/frontends/qt4/ui/IncludeUi.ui +++ b/src/frontends/qt4/ui/IncludeUi.ui @@ -60,6 +60,16 @@ + + + + Check it to enter parameters that are not recognizable by lyx + + + &Bypass validation + + + diff --git a/src/frontends/qt4/ui/ListingsUi.ui b/src/frontends/qt4/ui/ListingsUi.ui index 3832eaaaf7..797148f30a 100644 --- a/src/frontends/qt4/ui/ListingsUi.ui +++ b/src/frontends/qt4/ui/ListingsUi.ui @@ -18,14 +18,14 @@ true - + 9 6 - + 0 @@ -478,79 +478,90 @@ Ad&vanced - - - 9 + + + + 9 + 9 + 540 + 260 + - - 6 + + + 7 + 7 + 0 + 0 + - - - - - 7 - 7 - 0 - 0 - - - - More Parameters - - - - 9 + + More Parameters + + + + 9 + + + 6 + + + + + Qt::Horizontal - - 6 + + + + 16777215 + 16777215 + + + + 0 + + + false + + + Feedback window + + + QFrame::NoFrame + + + QFrame::Plain + + + 0 + + + false + + + + + Input listing parameters here. Enter ? for a list of parameters. + + + + + + + + Check it to enter parameters that are not recognizable by lyx - - - - - 16777215 - 16777215 - - - - 0 - - - false - - - Feedback window - - - QFrame::NoFrame - - - QFrame::Plain - - - 0 - - - false - - - - - - - Input listing parameters here. Enter ? for a list of parameters. - - - - - - - + + &Bypass validation + + + + + - + 0 diff --git a/src/frontends/qt4/ui/TextLayoutUi.ui b/src/frontends/qt4/ui/TextLayoutUi.ui index 83f0cbd45b..f4d03dba55 100644 --- a/src/frontends/qt4/ui/TextLayoutUi.ui +++ b/src/frontends/qt4/ui/TextLayoutUi.ui @@ -229,6 +229,16 @@ 6 + + + + Check it to enter parameters that are not recognizable by lyx + + + &Bypass validation + + + @@ -287,7 +297,7 @@ - 397 + 412 16 diff --git a/src/insets/InsetListingsParams.cpp b/src/insets/InsetListingsParams.cpp index 8e7aa2195c..9c084e0353 100644 --- a/src/insets/InsetListingsParams.cpp +++ b/src/insets/InsetListingsParams.cpp @@ -271,15 +271,13 @@ class ParValidator public: ParValidator(); - /// \return the associated \c ListingsParam. - /// \warning an \c invalidParamexception will be thrown - /// if the key is not found. - ListingsParam const & param(string const & key) const; + /// validate a parameter for a given name. + /// return an error message if \c par is an invalid parameter. + docstring validate(string const & name, string const & par) const; - /// validate a parameter for a given key. - /// \warning an \c invalidParam exception will be thrown if - /// \c par is an invalid parameter. - ListingsParam const & validate(string const & key, string const & par) const; + /// return the onoff status of a parameter \c key, if \c key is not found + /// return false + bool onoff(string const & key) const; private: /// key is the name of the parameter @@ -584,21 +582,11 @@ ParValidator::ParValidator() } -ListingsParam const & ParValidator::validate(string const & key, +docstring ParValidator::validate(string const & name, string const & par) const -{ - ListingsParam const & lparam = param(key); - docstring s = lparam.validate(par); - if (!s.empty()) - throw invalidParam(bformat(_("Parameter %1$s: "), from_utf8(key)) + s); - return lparam; -} - - -ListingsParam const & ParValidator::param(string const & name) const { if (name.empty()) - throw invalidParam(_("Invalid (empty) listing parameter name.")); + return _("Invalid (empty) listing parameter name."); if (name[0] == '?') { string suffix = trim(string(name, 1)); @@ -613,39 +601,59 @@ ListingsParam const & ParValidator::param(string const & name) const } } if (suffix.empty()) - throw invalidParam(bformat( - _("Available listing parameters are %1$s"), from_ascii(param_names))); + return bformat( + _("Available listing parameters are %1$s"), from_ascii(param_names)); else - throw invalidParam(bformat( + return bformat( _("Available listings parameters containing string \"%1$s\" are %2$s"), - from_utf8(suffix), from_utf8(param_names))); + from_utf8(suffix), from_utf8(param_names)); } // locate name in parameter table ListingsParams::const_iterator it = all_params_.find(name); - if (it != all_params_.end()) - return it->second; - - // otherwise, produce a meaningful error message. - string matching_names; - ListingsParams::const_iterator end = all_params_.end(); - for (it = all_params_.begin(); it != end; ++it) { - if (prefixIs(it->first, name)) { - if (!matching_names.empty()) - matching_names += ", "; - matching_names += it->first; + if (it != all_params_.end()) { + docstring msg = it->second.validate(par); + if (msg.empty()) + return msg; + else + return bformat(_("Parameter %1$s: "), from_utf8(name)) + msg; + } else { + // otherwise, produce a meaningful error message. + string matching_names; + ListingsParams::const_iterator end = all_params_.end(); + for (it = all_params_.begin(); it != end; ++it) { + if (prefixIs(it->first, name)) { + if (!matching_names.empty()) + matching_names += ", "; + matching_names += it->first; + } } + if (matching_names.empty()) + return bformat(_("Unknown listing parameter name: %1$s"), + from_utf8(name)); + else + return bformat(_("Parameters starting with '%1$s': %2$s"), + from_utf8(name), from_utf8(matching_names)); } - if (matching_names.empty()) - throw invalidParam(bformat(_("Unknown listing parameter name: %1$s"), - from_utf8(name))); + return docstring(); +} + + +bool ParValidator::onoff(string const & name) const +{ + // locate name in parameter table + ListingsParams::const_iterator it = all_params_.find(name); + if (it != all_params_.end()) + return it->second.onoff_; else - throw invalidParam(bformat(_("Parameters starting with '%1$s': %2$s"), - from_utf8(name), from_utf8(matching_names))); + return false; } } // namespace anon. +// define a global ParValidator +ParValidator * par_validator = NULL; + InsetListingsParams::InsetListingsParams() : inline_(false), params_(), status_(InsetCollapsable::Open) { @@ -706,10 +714,6 @@ void InsetListingsParams::addParam(string const & key, string const & value) if (key.empty()) return; - static ParValidator par_validator; - - // exception may be thown. - ListingsParam const & lparam = par_validator.validate(key, value); // duplicate parameters! string keyname = key; if (params_.find(key) != params_.end()) @@ -718,7 +722,9 @@ void InsetListingsParams::addParam(string const & key, string const & value) while (params_.find(keyname += '_') != params_.end()); // check onoff flag // onoff parameter with value false - if (lparam.onoff_ && (value == "false" || value == "{false}")) + if (!par_validator) + par_validator = new ParValidator(); + if (par_validator->onoff(key) && (value == "false" || value == "{false}")) params_[keyname] = string(); // if the parameter is surrounded with {}, good else if (prefixIs(value, "{") && suffixIs(value, "}")) @@ -839,4 +845,18 @@ string InsetListingsParams::getParamValue(string const & param) const } +docstring InsetListingsParams::validate() const +{ + docstring msg; + if (!par_validator) + par_validator = new ParValidator(); + for (map::const_iterator it = params_.begin(); + it != params_.end(); ++it) { + msg = par_validator->validate(it->first, it->second); + if (!msg.empty()) + return msg; + } + return msg; +} + } // namespace lyx diff --git a/src/insets/InsetListingsParams.h b/src/insets/InsetListingsParams.h index 341d73d507..dc76ed40ad 100644 --- a/src/insets/InsetListingsParams.h +++ b/src/insets/InsetListingsParams.h @@ -74,6 +74,9 @@ public: /// void clear() { params_.clear(); } + + /// validate parameter, return an error message + docstring validate() const; private: /// inline or normal listings @@ -87,22 +90,6 @@ private: }; -class invalidParam : public std::exception { -public: - invalidParam(docstring const & details) - : details_(to_utf8(details)) - {} - - virtual const char * what() const throw() { - return details_.c_str(); - } - - virtual ~invalidParam() throw() {} -private: - std::string const details_; -}; - - } // namespace lyx #endif -- 2.39.2