3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Richard Kimberly Heck
8 * Full author contact details are available in file CREDITS.
13 #include "GuiCounter.h"
15 #include "qt_helpers.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "TextClass.h"
21 #include "insets/InsetCounter.h"
22 #include "insets/InsetCommandParams.h"
24 #include "support/convert.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
34 GuiCounter::GuiCounter(GuiView & lv, QWidget * parent) :
35 InsetParamsWidget(parent), guiview(lv)
39 connect(counterCB, SIGNAL(currentIndexChanged(int)),
40 this, SIGNAL(changed()));
41 connect(actionCB, SIGNAL(currentIndexChanged(int)),
42 this, SIGNAL(changed()));
43 connect(valueSB, SIGNAL(valueChanged(int)),
44 this, SIGNAL(changed()));
45 connect(lyxonlyXB, SIGNAL(clicked()),
46 this, SIGNAL(changed()));
48 // These are hardcoded and do not change
49 std::vector<std::pair<std::string, std::string>> const & ct =
50 InsetCounter::counterTable;
52 for (auto const & c : ct) {
53 docstring guistring = translateIfPossible(from_ascii(c.second));
54 actionCB->addItem(toqstr(guistring), toqstr(c.first));
59 void GuiCounter::processParams(InsetCommandParams const & params)
61 QString const & counter = toqstr(params["counter"]);
62 int c = counterCB->findData(counter);
63 counterCB->setCurrentIndex(c);
65 QString cmd = toqstr(params.getCmdName());
66 c = actionCB->findData(cmd);
69 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
71 actionCB->setCurrentIndex(c);
73 int val = convert<int>(params["value"]);
74 valueSB->setValue(val);
76 lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
80 void GuiCounter::fillCombos()
83 BufferView * bv = guiview.documentBufferView();
84 // should not happen, but...
88 Counters const & cntrs =
89 bv->buffer().params().documentClass().counters();
90 std::vector<docstring> counts = cntrs.listOfCounters();
91 // We use an intermediate map in order to sort at translated GUI strings.
92 QMap<QString, QString> items;
93 for (auto const & c : counts) {
94 docstring const & guiname = cntrs.guiName(c);
95 items.insert(qt_(toqstr(guiname)), toqstr(c));
97 for (QMap<QString, QString>::const_iterator it = items.constBegin();
98 it != items.constEnd(); ++it)
99 counterCB->addItem(it.key(), it.value());
103 void GuiCounter::paramsToDialog(Inset const * ip)
105 InsetCounter const * inset = static_cast<InsetCounter const *>(ip);
106 InsetCommandParams const & params = inset->params();
109 processParams(params);
113 bool GuiCounter::initialiseParams(std::string const & data)
115 InsetCommandParams params(insetCode());
116 if (!InsetCommand::string2params(data, params))
120 processParams(params);
125 docstring GuiCounter::dialogToParams() const
127 InsetCommandParams params(insetCode());
129 params["counter"] = qstring_to_ucs4(counterCB->itemData(counterCB->currentIndex()).toString());
130 params["value"] = convert<docstring>(valueSB->value());
131 params.setCmdName(fromqstr(actionCB->itemData(actionCB->currentIndex()).toString()));
132 params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
133 return from_utf8(InsetCounter::params2string(params));
137 bool GuiCounter::checkWidgets(bool readonly) const
139 bool const cmdIsSet = actionCB->itemData(actionCB->currentIndex()).toString() == "set";
140 bool const cmdIsAddTo = actionCB->itemData(actionCB->currentIndex()).toString() == "addto";
141 counterCB->setEnabled(!readonly);
142 actionCB->setEnabled(!readonly);
143 valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
145 valueSB->setRange(-10000, 10000);
147 valueSB->setRange(0, 10000);
149 return InsetParamsWidget::checkWidgets() && !readonly &&
150 !counterCB->currentText().isEmpty() &&
151 !actionCB->currentText().isEmpty();
155 } // namespace frontend
159 #include "moc_GuiCounter.cpp"