]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCounter.cpp
Fix up 'Reduce metrics updates from 4 to 1 when loading file'
[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 "GuiView.h"
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "TextClass.h"
22 #include "insets/InsetCounter.h"
23 #include "insets/InsetCommandParams.h"
24
25 #include "support/convert.h"
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 #include <map>
31 //#include <vector>
32
33 namespace lyx {
34 namespace frontend {
35
36 GuiCounter::GuiCounter(GuiView & lv, QWidget * parent) : 
37         InsetParamsWidget(parent), guiview(lv)
38 {
39         setupUi(this);
40
41         connect(counterCB, SIGNAL(currentIndexChanged(int)),
42                 this, SIGNAL(changed()));
43         connect(actionCB, SIGNAL(currentIndexChanged(int)),
44                 this, SIGNAL(changed()));
45         connect(valueSB, SIGNAL(valueChanged(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::vector<std::pair<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
60
61 void GuiCounter::processParams(InsetCommandParams const & params)
62 {
63         QString const & counter = toqstr(params["counter"]);
64         int c = counterCB->findData(counter);
65         counterCB->setCurrentIndex(c);
66
67         QString cmd = toqstr(params.getCmdName());
68         c = actionCB->findData(cmd);
69         if (c < 0) {
70                 c = 0;
71                 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
72         }
73         actionCB->setCurrentIndex(c);
74
75         int val = convert<int>(params["value"]);
76         valueSB->setValue(val);
77
78         lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
79 }
80
81
82 void GuiCounter::fillCombos()
83 {
84         counterCB->clear();
85         BufferView * bv = guiview.documentBufferView();
86         // should not happen, but...
87         if (!bv)
88                 return;
89         
90         Counters const & cntrs =
91                 bv->buffer().params().documentClass().counters();
92         std::vector<docstring> counts = cntrs.listOfCounters();
93         // We use an intermediate map in order to sort at translated GUI strings.
94         QMap<QString, QString> items;
95         for (auto const & c : counts) {
96                 docstring const & guiname = cntrs.guiName(c);
97                 items.insert(qt_(toqstr(guiname)), toqstr(c));
98         }
99         for (QMap<QString, QString>::const_iterator it = items.constBegin();
100              it != items.constEnd(); ++it)
101                 counterCB->addItem(it.key(), it.value());
102 }
103
104
105 void GuiCounter::paramsToDialog(Inset const * ip)
106 {
107         InsetCounter const * inset = static_cast<InsetCounter const *>(ip);
108         InsetCommandParams const & params = inset->params();
109
110         fillCombos();
111         processParams(params);
112 }
113
114
115 bool GuiCounter::initialiseParams(std::string const & data)
116 {
117         InsetCommandParams params(insetCode());
118         if (!InsetCommand::string2params(data, params))
119                 return false;
120
121         fillCombos();
122         processParams(params);
123         return true;
124 }
125
126
127 docstring GuiCounter::dialogToParams() const
128 {
129         InsetCommandParams params(insetCode());
130
131         params["counter"] = qstring_to_ucs4(counterCB->itemData(counterCB->currentIndex()).toString());
132         params["value"] = convert<docstring>(valueSB->value());
133         params.setCmdName(fromqstr(actionCB->itemData(actionCB->currentIndex()).toString()));
134         params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
135         return from_utf8(InsetCounter::params2string(params));
136 }
137
138
139 bool GuiCounter::checkWidgets(bool readonly) const
140 {
141         bool const cmdIsSet = actionCB->itemData(actionCB->currentIndex()).toString() == "set";
142         bool const cmdIsAddTo = actionCB->itemData(actionCB->currentIndex()).toString() == "addto";
143         counterCB->setEnabled(!readonly);
144         actionCB->setEnabled(!readonly);
145         valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
146         // enumi, for example, can be negative.
147         valueSB->setRange(-10000, 10000);
148
149         return InsetParamsWidget::checkWidgets() && !readonly &&
150                         !counterCB->currentText().isEmpty() &&
151                         !actionCB->currentText().isEmpty();
152 }
153
154
155 } // namespace frontend
156 } // namespace lyx
157
158
159 #include "moc_GuiCounter.cpp"