]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiCounter.cpp
More polishing of counter dialog. Thanks to Jürgen for help.
[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(lyxonlyXB, SIGNAL(clicked()),
46                 this, SIGNAL(changed()));
47
48         // These are hardcoded and do not change
49         std::map<std::string, std::string> const & ct =
50                         InsetCounter::counterTable;
51         actionCB->clear();
52         for (auto const & c : ct) {
53                 docstring guistring = translateIfPossible(from_ascii(c.second));
54                 actionCB->addItem(toqstr(guistring), toqstr(c.first));
55         }
56 }
57
58
59 void GuiCounter::processParams(InsetCommandParams const & params)
60 {
61         QString const & counter = toqstr(params["counter"]);
62         int c = counterCB->findText(counter);
63         counterCB->setCurrentIndex(c);
64
65         QString cmd = toqstr(params.getCmdName());
66         c = actionCB->findData(cmd);
67         if (c < 0) {
68                 c = 0;
69                 LYXERR0("Unable to find " << cmd << " in GuiCounter!");
70         }
71         actionCB->setCurrentIndex(c);
72
73         int val = convert<int>(params["value"]);
74         valueSB->setValue(val);
75
76         lyxonlyXB->setChecked(support::lowercase(params["lyxonly"]) == "true");
77 }
78
79
80 void GuiCounter::fillCombos()
81 {
82         counterCB->clear();
83         BufferView * bv = guiview.documentBufferView();
84         // should not happen, but...
85         if (!bv)
86                 return;
87         
88         std::vector<docstring> counts = 
89                 bv->buffer().params().documentClass().counters().listOfCounters();
90         for (auto const & c : counts)
91                 counterCB->addItem(toqstr(c));
92 }
93
94
95 void GuiCounter::paramsToDialog(Inset const * ip)
96 {
97         InsetCounter const * inset = static_cast<InsetCounter const *>(ip);
98         InsetCommandParams const & params = inset->params();
99
100         fillCombos();
101         processParams(params);
102 }
103
104
105 bool GuiCounter::initialiseParams(std::string const & data)
106 {
107         InsetCommandParams params(insetCode());
108         if (!InsetCommand::string2params(data, params))
109                 return false;
110
111         fillCombos();
112         processParams(params);
113         return true;
114 }
115
116
117 docstring GuiCounter::dialogToParams() const
118 {
119         InsetCommandParams params(insetCode());
120
121         params["counter"] = qstring_to_ucs4(counterCB->currentText());
122         params["value"] = convert<docstring>(valueSB->value());
123         params.setCmdName(fromqstr(actionCB->itemData(actionCB->currentIndex()).toString()));
124         params["lyxonly"] = from_ascii(lyxonlyXB->isChecked() ? "true" : "false");
125         return from_utf8(InsetCounter::params2string(params));
126 }
127
128
129 bool GuiCounter::checkWidgets(bool readonly) const
130 {
131         bool const cmdIsSet = actionCB->itemData(actionCB->currentIndex()).toString() == "set";
132         bool const cmdIsAddTo = actionCB->itemData(actionCB->currentIndex()).toString() == "addto";
133         counterCB->setEnabled(!readonly);
134         actionCB->setEnabled(!readonly);
135         valueSB->setEnabled(!readonly && (cmdIsSet || cmdIsAddTo));
136         if (cmdIsAddTo)
137                 valueSB->setRange(-10000, 10000);
138         else
139                 valueSB->setRange(0, 10000);
140
141         return InsetParamsWidget::checkWidgets() && !readonly &&
142                         !counterCB->currentText().isEmpty() &&
143                         !actionCB->currentText().isEmpty();
144 }
145
146
147 } // namespace frontend
148 } // namespace lyx
149
150
151 #include "moc_GuiCounter.cpp"