]> git.lyx.org Git - features.git/commitdiff
Add bypass validation checkboxes to listings related dialogs
authorBo Peng <bpeng@lyx.org>
Tue, 12 Jun 2007 17:10:28 +0000 (17:10 +0000)
committerBo Peng <bpeng@lyx.org>
Tue, 12 Jun 2007 17:10:28 +0000 (17:10 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@18751 a592a061-630c-0410-9148-cb99ea01b6c8

src/BufferParams.cpp
src/frontends/qt4/QDocument.cpp
src/frontends/qt4/QInclude.cpp
src/frontends/qt4/QListings.cpp
src/frontends/qt4/ui/IncludeUi.ui
src/frontends/qt4/ui/ListingsUi.ui
src/frontends/qt4/ui/TextLayoutUi.ui
src/insets/InsetListingsParams.cpp
src/insets/InsetListingsParams.h

index f1ef2b4f89c84b1607ba89ee1b48eebe0204f2bb..c96270cadbbb47d21c7b699802de126285a54566 100644 (file)
@@ -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;
index a917134d2b76e55206a3950b3b6bea8bae26a5cc..f895d30d4d3cffad34244dad6f66f418999430a5 100644 (file)
@@ -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);
        }
index 31aaa115e6f689d9d9086abeba0a284369722fad..7437b600096c421784e465bf2a8ba818942f510c 100644 (file)
@@ -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.
index 80d2e53bb523826932072ed34fd50e01e1f633b3..f6d181f5936fc091b186ce134c69cbf609e6db83 100644 (file)
@@ -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);
        }
index 9190b2c0ecfbae050aaaaab17fdbde6d17dbb366..efe6430de9cfa6a0416139951f0ffe44907d8e90 100644 (file)
         </property>
        </widget>
       </item>
+      <item row="4" column="0" colspan="2" >
+       <widget class="QCheckBox" name="bypassCB" >
+        <property name="toolTip" >
+         <string>Check it to enter parameters that are not recognizable by lyx</string>
+        </property>
+        <property name="text" >
+         <string>&amp;Bypass validation</string>
+        </property>
+       </widget>
+      </item>
       <item row="0" column="1" >
        <widget class="QLineEdit" name="captionLE" >
         <property name="minimumSize" >
index 3832eaaaf7d28aaa0da6cc45fb2c95c3a1b0a54a..797148f30ae168bb1a90f9c0fb7ef80602a5bd5e 100644 (file)
   <property name="sizeGripEnabled" >
    <bool>true</bool>
   </property>
-  <layout class="QGridLayout" >
+  <layout class="QVBoxLayout" >
    <property name="margin" >
     <number>9</number>
    </property>
    <property name="spacing" >
     <number>6</number>
    </property>
-   <item row="0" column="0" >
+   <item>
     <widget class="QTabWidget" name="listingsTW" >
      <property name="currentIndex" >
       <number>0</number>
       <attribute name="title" >
        <string>Ad&amp;vanced</string>
       </attribute>
-      <layout class="QGridLayout" >
-       <property name="margin" >
-        <number>9</number>
+      <widget class="QGroupBox" name="listingsGB" >
+       <property name="geometry" >
+        <rect>
+         <x>9</x>
+         <y>9</y>
+         <width>540</width>
+         <height>260</height>
+        </rect>
        </property>
-       <property name="spacing" >
-        <number>6</number>
+       <property name="sizePolicy" >
+        <sizepolicy>
+         <hsizetype>7</hsizetype>
+         <vsizetype>7</vsizetype>
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
        </property>
-       <item row="0" column="0" >
-        <widget class="QGroupBox" name="listingsGB" >
-         <property name="sizePolicy" >
-          <sizepolicy>
-           <hsizetype>7</hsizetype>
-           <vsizetype>7</vsizetype>
-           <horstretch>0</horstretch>
-           <verstretch>0</verstretch>
-          </sizepolicy>
-         </property>
-         <property name="title" >
-          <string>More Parameters</string>
-         </property>
-         <layout class="QGridLayout" >
-          <property name="margin" >
-           <number>9</number>
+       <property name="title" >
+        <string>More Parameters</string>
+       </property>
+       <layout class="QVBoxLayout" >
+        <property name="margin" >
+         <number>9</number>
+        </property>
+        <property name="spacing" >
+         <number>6</number>
+        </property>
+        <item>
+         <widget class="QSplitter" name="splitter" >
+          <property name="orientation" >
+           <enum>Qt::Horizontal</enum>
           </property>
-          <property name="spacing" >
-           <number>6</number>
+          <widget class="QTextBrowser" name="listingsTB" >
+           <property name="maximumSize" >
+            <size>
+             <width>16777215</width>
+             <height>16777215</height>
+            </size>
+           </property>
+           <property name="cursor" >
+            <cursor>0</cursor>
+           </property>
+           <property name="acceptDrops" >
+            <bool>false</bool>
+           </property>
+           <property name="toolTip" >
+            <string>Feedback window</string>
+           </property>
+           <property name="frameShape" >
+            <enum>QFrame::NoFrame</enum>
+           </property>
+           <property name="frameShadow" >
+            <enum>QFrame::Plain</enum>
+           </property>
+           <property name="lineWidth" >
+            <number>0</number>
+           </property>
+           <property name="acceptRichText" >
+            <bool>false</bool>
+           </property>
+          </widget>
+          <widget class="QTextEdit" name="listingsED" >
+           <property name="toolTip" >
+            <string>Input listing parameters here. Enter ? for a list of parameters.</string>
+           </property>
+          </widget>
+         </widget>
+        </item>
+        <item>
+         <widget class="QCheckBox" name="bypassCB" >
+          <property name="toolTip" >
+           <string>Check it to enter parameters that are not recognizable by lyx</string>
           </property>
-          <item row="0" column="0" >
-           <widget class="QTextBrowser" name="listingsTB" >
-            <property name="maximumSize" >
-             <size>
-              <width>16777215</width>
-              <height>16777215</height>
-             </size>
-            </property>
-            <property name="cursor" >
-             <cursor>0</cursor>
-            </property>
-            <property name="acceptDrops" >
-             <bool>false</bool>
-            </property>
-            <property name="toolTip" >
-             <string>Feedback window</string>
-            </property>
-            <property name="frameShape" >
-             <enum>QFrame::NoFrame</enum>
-            </property>
-            <property name="frameShadow" >
-             <enum>QFrame::Plain</enum>
-            </property>
-            <property name="lineWidth" >
-             <number>0</number>
-            </property>
-            <property name="acceptRichText" >
-             <bool>false</bool>
-            </property>
-           </widget>
-          </item>
-          <item row="0" column="1" >
-           <widget class="QTextEdit" name="listingsED" >
-            <property name="toolTip" >
-             <string>Input listing parameters here. Enter ? for a list of parameters.</string>
-            </property>
-           </widget>
-          </item>
-         </layout>
-        </widget>
-       </item>
-      </layout>
+          <property name="text" >
+           <string>&amp;Bypass validation</string>
+          </property>
+         </widget>
+        </item>
+       </layout>
+      </widget>
      </widget>
     </widget>
    </item>
-   <item row="1" column="0" >
+   <item>
     <layout class="QHBoxLayout" >
      <property name="margin" >
       <number>0</number>
index 83f0cbd45b4d2459ffdb927165c4481f008cbae2..f4d03dba550c20dff7b8dee4aace1cfc004c063c 100644 (file)
       <property name="spacing" >
        <number>6</number>
       </property>
+      <item>
+       <widget class="QCheckBox" name="bypassCB" >
+        <property name="toolTip" >
+         <string>Check it to enter parameters that are not recognizable by lyx</string>
+        </property>
+        <property name="text" >
+         <string>&amp;Bypass validation</string>
+        </property>
+       </widget>
+      </item>
       <item>
        <widget class="QSplitter" name="splitter" >
         <property name="orientation" >
      </property>
      <property name="sizeHint" >
       <size>
-       <width>397</width>
+       <width>412</width>
        <height>16</height>
       </size>
      </property>
index 8e7aa2195cbcf69aae53ae2554d9eea7ba3067e5..9c084e0353eecbb7d065d840245893a1ac361c4f 100644 (file)
@@ -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<string, string>::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
index 341d73d507aa28b3e622e8e2e16a3a0f6a0649e8..dc76ed40ad1db4cd34b24582b50c1aa7bf6a37b7 100644 (file)
@@ -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