]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt/GuiCounter.cpp
Fix readability
[lyx.git] / src / frontends / qt / GuiCounter.cpp
index 7470e0d9e022a7f785007b81ac40e1396100b3e1..04fab3a664baccb166f763d3ec82f6bde42f00da 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "GuiCounter.h"
 
+#include "GuiView.h"
 #include "qt_helpers.h"
 
 #include "Buffer.h"
@@ -22,6 +23,7 @@
 #include "insets/InsetCommandParams.h"
 
 #include "support/convert.h"
+#include "support/debug.h"
 #include "support/gettext.h"
 #include "support/lstrings.h"
 
@@ -42,35 +44,24 @@ GuiCounter::GuiCounter(GuiView & lv, QWidget * parent) :
                this, SIGNAL(changed()));
        connect(valueSB, SIGNAL(valueChanged(int)),
                this, SIGNAL(changed()));
-       connect(vtypeCB, SIGNAL(currentIndexChanged(int)),
-               this, SIGNAL(changed()));
        connect(lyxonlyXB, SIGNAL(clicked()),
                this, SIGNAL(changed()));
 
        // These are hardcoded and do not change
-       std::map<std::string, std::string> const & ct =
+       std::vector<std::pair<std::string, std::string>> const & ct =
                        InsetCounter::counterTable;
        actionCB->clear();
        for (auto const & c : ct) {
                docstring guistring = translateIfPossible(from_ascii(c.second));
                actionCB->addItem(toqstr(guistring), toqstr(c.first));
        }
-
-       std::map<std::string, std::string> const & vt =
-                       InsetCounter::valueTable;
-       vtypeCB->clear();
-       vtypeCB->addItem("", "");
-       for (auto const & v : vt) {
-               docstring guistring = translateIfPossible(from_ascii(v.second));
-               vtypeCB->addItem(toqstr(guistring), toqstr(v.first));
-       }
 }
 
 
 void GuiCounter::processParams(InsetCommandParams const & params)
 {
        QString const & counter = toqstr(params["counter"]);
-       int c = counterCB->findText(counter);
+       int c = counterCB->findData(counter);
        counterCB->setCurrentIndex(c);
 
        QString cmd = toqstr(params.getCmdName());
@@ -84,13 +75,6 @@ void GuiCounter::processParams(InsetCommandParams const & params)
        int val = convert<int>(params["value"]);
        valueSB->setValue(val);
 
-       cmd = toqstr(params["vtype"]);
-       c = cmd.isEmpty() ? 0 : vtypeCB->findData(cmd);
-       if (c < 0) {
-               c = 0;
-               LYXERR0("Unable to find " << cmd << " in GuiCounter!");
-       }
-       vtypeCB->setCurrentIndex(c);
        lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
 }
 
@@ -103,10 +87,18 @@ void GuiCounter::fillCombos()
        if (!bv)
                return;
        
-       std::vector<docstring> counts = 
-               bv->buffer().params().documentClass().counters().listOfCounters();
-       for (auto const & c : counts)
-               counterCB->addItem(toqstr(c));
+       Counters const & cntrs =
+               bv->buffer().params().documentClass().counters();
+       std::vector<docstring> counts = cntrs.listOfCounters();
+       // We use an intermediate map in order to sort at translated GUI strings.
+       QMap<QString, QString> items;
+       for (auto const & c : counts) {
+               docstring const & guiname = cntrs.guiName(c);
+               items.insert(qt_(toqstr(guiname)), toqstr(c));
+       }
+       for (QMap<QString, QString>::const_iterator it = items.constBegin();
+            it != items.constEnd(); ++it)
+               counterCB->addItem(it.key(), it.value());
 }
 
 
@@ -136,10 +128,9 @@ docstring GuiCounter::dialogToParams() const
 {
        InsetCommandParams params(insetCode());
 
-       params["counter"] = qstring_to_ucs4(counterCB->currentText());
+       params["counter"] = qstring_to_ucs4(counterCB->itemData(counterCB->currentIndex()).toString());
        params["value"] = convert<docstring>(valueSB->value());
-       params.setCmdName(fromqstr(actionCB->currentData().toString()));
-       params["vtype"] = qstring_to_ucs4(vtypeCB->currentData().toString());
+       params.setCmdName(fromqstr(actionCB->itemData(actionCB->currentIndex()).toString()));
        params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
        return from_utf8(InsetCounter::params2string(params));
 }
@@ -147,23 +138,17 @@ docstring GuiCounter::dialogToParams() const
 
 bool GuiCounter::checkWidgets(bool readonly) const
 {
-       bool const cmdIsValue = actionCB->currentData().toString() == "value";
-       bool const cmdIsSet = actionCB->currentData().toString() == "set";
-       bool const cmdIsAddTo = actionCB->currentData().toString() == "addto";
+       bool const cmdIsSet = actionCB->itemData(actionCB->currentIndex()).toString() == "set";
+       bool const cmdIsAddTo = actionCB->itemData(actionCB->currentIndex()).toString() == "addto";
        counterCB->setEnabled(!readonly);
        actionCB->setEnabled(!readonly);
        valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
-       if (cmdIsAddTo)
-               valueSB->setRange(-10000, 10000);
-       else
-               valueSB->setRange(0, 10000);
-       vtypeCB->setEnabled(!readonly && cmdIsValue);
-       if (!InsetParamsWidget::checkWidgets())
-               return false;
-       return !readonly &&
+       // enumi, for example, can be negative.
+       valueSB->setRange(-10000, 10000);
+
+       return InsetParamsWidget::checkWidgets() && !readonly &&
                        !counterCB->currentText().isEmpty() &&
-                       !actionCB->currentText().isEmpty() &&
-                       !(cmdIsValue && vtypeCB->currentText().isEmpty());
+                       !actionCB->currentText().isEmpty();
 }