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(vtypeCB, SIGNAL(currentIndexChanged(int)),
46 this, SIGNAL(changed()));
47 connect(lyxonlyXB, SIGNAL(clicked()),
48 this, SIGNAL(changed()));
50 // These are hardcoded and do not change
51 std::map<std::string, std::string> const & ct =
52 InsetCounter::counterTable;
54 for (auto const & c : ct) {
55 docstring guistring = translateIfPossible(from_ascii(c.second));
56 actionCB->addItem(toqstr(guistring), toqstr(c.first));
59 std::map<std::string, std::string> const & vt =
60 InsetCounter::valueTable;
62 vtypeCB->addItem("", "");
63 for (auto const & v : vt) {
64 docstring guistring = translateIfPossible(from_ascii(v.second));
65 vtypeCB->addItem(toqstr(guistring), toqstr(v.first));
70 void GuiCounter::processParams(InsetCommandParams const & params)
72 QString const & counter = toqstr(params["counter"]);
73 int c = counterCB->findText(counter);
74 counterCB->setCurrentIndex(c);
76 QString cmd = toqstr(params.getCmdName());
77 c = actionCB->findData(cmd);
80 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
82 actionCB->setCurrentIndex(c);
84 int val = convert<int>(params["value"]);
85 valueSB->setValue(val);
87 cmd = toqstr(params["vtype"]);
88 c = cmd.isEmpty() ? 0 : vtypeCB->findData(cmd);
91 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
93 vtypeCB->setCurrentIndex(c);
94 lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
98 void GuiCounter::fillCombos()
101 BufferView * bv = guiview.documentBufferView();
102 // should not happen, but...
106 std::vector<docstring> counts =
107 bv->buffer().params().documentClass().counters().listOfCounters();
108 for (auto const & c : counts)
109 counterCB->addItem(toqstr(c));
113 void GuiCounter::paramsToDialog(Inset const * ip)
115 InsetCounter const * inset = static_cast<InsetCounter const *>(ip);
116 InsetCommandParams const & params = inset->params();
119 processParams(params);
123 bool GuiCounter::initialiseParams(std::string const & data)
125 InsetCommandParams params(insetCode());
126 if (!InsetCommand::string2params(data, params))
130 processParams(params);
135 docstring GuiCounter::dialogToParams() const
137 InsetCommandParams params(insetCode());
139 params["counter"] = qstring_to_ucs4(counterCB->currentText());
140 params["value"] = convert<docstring>(valueSB->value());
141 params.setCmdName(fromqstr(actionCB->currentData().toString()));
142 params["vtype"] = qstring_to_ucs4(vtypeCB->currentData().toString());
143 params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
144 return from_utf8(InsetCounter::params2string(params));
148 bool GuiCounter::checkWidgets(bool readonly) const
150 bool const cmdIsValue = actionCB->currentData().toString() == "value";
151 bool const cmdIsSet = actionCB->currentData().toString() == "set";
152 bool const cmdIsAddTo = actionCB->currentData().toString() == "addto";
153 counterCB->setEnabled(!readonly);
154 actionCB->setEnabled(!readonly);
155 valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
157 valueSB->setRange(-10000, 10000);
159 valueSB->setRange(0, 10000);
160 vtypeCB->setEnabled(!readonly && cmdIsValue);
161 if (!InsetParamsWidget::checkWidgets())
164 !counterCB->currentText().isEmpty() &&
165 !actionCB->currentText().isEmpty() &&
166 !(cmdIsValue && vtypeCB->currentText().isEmpty());
170 } // namespace frontend
174 #include "moc_GuiCounter.cpp"