]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCounter.cpp
Polish the counter UI a bit and add some to the documentation.
[lyx.git] / src / frontends / qt / GuiCounter.cpp
1 /**
2  * \file GuiCounter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Richard Kimberly Heck
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiCounter.h"
14
15 #include "qt_helpers.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "TextClass.h"
21 #include "insets/InsetCounter.h"
22 #include "insets/InsetCommandParams.h"
23
24 #include "support/convert.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27
28 #include <map>
29 //#include <vector>
30
31 namespace lyx {
32 namespace frontend {
33
34 GuiCounter::GuiCounter(GuiView & lv, QWidget * parent) : 
35         InsetParamsWidget(parent), guiview(lv)
36 {
37         setupUi(this);
38
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()));
49
50         // These are hardcoded and do not change
51         std::map<std::string, std::string> const & ct =
52                         InsetCounter::counterTable;
53         actionCB->clear();
54         for (auto const & c : ct) {
55                 docstring guistring = translateIfPossible(from_ascii(c.second));
56                 actionCB->addItem(toqstr(guistring), toqstr(c.first));
57         }
58
59         std::map<std::string, std::string> const & vt =
60                         InsetCounter::valueTable;
61         vtypeCB->clear();
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));
66         }
67 }
68
69
70 void GuiCounter::processParams(InsetCommandParams const & params)
71 {
72         QString const & counter = toqstr(params["counter"]);
73         int c = counterCB->findText(counter);
74         counterCB->setCurrentIndex(c);
75
76         QString cmd = toqstr(params.getCmdName());
77         c = actionCB->findData(cmd);
78         if (c < 0) {
79                 c = 0;
80                 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
81         }
82         actionCB->setCurrentIndex(c);
83
84         int val = convert<int>(params["value"]);
85         valueSB->setValue(val);
86
87         cmd = toqstr(params["vtype"]);
88         c = cmd.isEmpty() ? 0 : vtypeCB->findData(cmd);
89         if (c < 0) {
90                 c = 0;
91                 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
92         }
93         vtypeCB->setCurrentIndex(c);
94         lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
95 }
96
97
98 void GuiCounter::fillCombos()
99 {
100         counterCB->clear();
101         BufferView * bv = guiview.documentBufferView();
102         // should not happen, but...
103         if (!bv)
104                 return;
105         
106         std::vector<docstring> counts = 
107                 bv->buffer().params().documentClass().counters().listOfCounters();
108         for (auto const & c : counts)
109                 counterCB->addItem(toqstr(c));
110 }
111
112
113 void GuiCounter::paramsToDialog(Inset const * ip)
114 {
115         InsetCounter const * inset = static_cast<InsetCounter const *>(ip);
116         InsetCommandParams const & params = inset->params();
117
118         fillCombos();
119         processParams(params);
120 }
121
122
123 bool GuiCounter::initialiseParams(std::string const & data)
124 {
125         InsetCommandParams params(insetCode());
126         if (!InsetCommand::string2params(data, params))
127                 return false;
128
129         fillCombos();
130         processParams(params);
131         return true;
132 }
133
134
135 docstring GuiCounter::dialogToParams() const
136 {
137         InsetCommandParams params(insetCode());
138
139         params["counter"] = qstring_to_ucs4(counterCB->currentText());
140         params["value"] = convert<docstring>(valueSB->value());
141         params.setCmdName(fromqstr(actionCB->itemData(actionCB->currentIndex()).toString()));
142         params["vtype"] = qstring_to_ucs4(vtypeCB->itemData(vtypeCB->currentIndex()).toString());
143         params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
144         return from_utf8(InsetCounter::params2string(params));
145 }
146
147
148 bool GuiCounter::checkWidgets(bool readonly) const
149 {
150         bool const cmdIsValue = actionCB->itemData(actionCB->currentIndex()).toString() == "value";
151         bool const cmdIsSet = actionCB->itemData(actionCB->currentIndex()).toString() == "set";
152         bool const cmdIsAddTo = actionCB->itemData(actionCB->currentIndex()).toString() == "addto";
153         counterCB->setEnabled(!readonly);
154         actionCB->setEnabled(!readonly);
155         valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
156         if (cmdIsAddTo)
157                 valueSB->setRange(-10000, 10000);
158         else
159                 valueSB->setRange(0, 10000);
160         vtypeLA->setEnabled(!readonly && cmdIsValue);
161         vtypeCB->setEnabled(!readonly && cmdIsValue);
162
163         return InsetParamsWidget::checkWidgets() && !readonly &&
164                         !counterCB->currentText().isEmpty() &&
165                         !actionCB->currentText().isEmpty() &&
166                         !(cmdIsValue && vtypeCB->currentText().isEmpty());
167 }
168
169
170 } // namespace frontend
171 } // namespace lyx
172
173
174 #include "moc_GuiCounter.cpp"