]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
Fix handling of the add branch textfield in GuiBranches
[lyx.git] / src / frontends / qt4 / GuiHSpace.cpp
1 /**
2  * \file GuiHSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiHSpace.h"
14
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
17 #include "Validator.h"
18
19 #include "Spacing.h"
20
21 #include "insets/InsetSpace.h"
22
23 #include "mathed/InsetMathSpace.h"
24
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27
28 #include <QCheckBox>
29 #include <QLineEdit>
30 #include <QPushButton>
31 #include <QValidator>
32
33 using namespace std;
34
35 namespace lyx {
36 namespace frontend {
37
38 GuiHSpace::GuiHSpace(bool math_mode, QWidget * parent)
39         : InsetParamsWidget(parent), math_mode_(math_mode)
40 {
41         setupUi(this);
42
43         spacingCO->clear();
44         if (math_mode_) {
45                 spacingCO->addItem(qt_("Interword Space"), "normal");
46                 spacingCO->addItem(qt_("Thin Space"), "thinspace");
47                 spacingCO->addItem(qt_("Medium Space"), "medspace");
48                 spacingCO->addItem(qt_("Thick Space"), "thickspace");
49                 spacingCO->addItem(qt_("Negative Thin Space"), "negthinspace");
50                 spacingCO->addItem(qt_("Negative Medium Space"), "negmedspace");
51                 spacingCO->addItem(qt_("Negative Thick Space"), "negthickspace");
52                 spacingCO->addItem(qt_("Half Quad (0.5 em)"), "halfquad");
53                 spacingCO->addItem(qt_("Quad (1 em)"), "quad");
54                 spacingCO->addItem(qt_("Double Quad (2 em)"), "qquad");
55                 spacingCO->addItem(qt_("Horizontal Fill"), "hfill");
56                 spacingCO->addItem(qt_("Custom"), "custom");
57         } else {
58                 spacingCO->addItem(qt_("Interword Space"), "normal");
59                 spacingCO->addItem(qt_("Thin Space"), "thinspace");
60                 spacingCO->addItem(qt_("Negative Thin Space"), "negthinspace");
61                 spacingCO->addItem(qt_("Negative Medium Space"), "negmedspace");
62                 spacingCO->addItem(qt_("Negative Thick Space"), "negthickspace");
63                 spacingCO->addItem(qt_("Half Quad (0.5 em)"), "halfquad");
64                 spacingCO->addItem(qt_("Quad (1 em)"), "quad");
65                 spacingCO->addItem(qt_("Double Quad (2 em)"), "qquad");
66                 spacingCO->addItem(qt_("Horizontal Fill"), "hfill");
67                 spacingCO->addItem(qt_("Custom"), "custom");
68                 spacingCO->addItem(qt_("Visible Space"), "visible");
69         }
70
71         connect(spacingCO, SIGNAL(highlighted(QString)),
72                 this, SLOT(changedSlot()));
73         connect(valueLE, SIGNAL(textChanged(QString)),
74                 this, SLOT(changedSlot()));
75         connect(spacingCO, SIGNAL(activated(int)),
76                 this, SLOT(changedSlot()));
77         connect(keepCB, SIGNAL(clicked()),
78                 this, SLOT(changedSlot()));
79         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
80                 this, SLOT(changedSlot()));
81         connect(fillPatternCO, SIGNAL(activated(int)),
82                 this, SLOT(changedSlot()));
83
84         if (math_mode_)
85                 valueLE->setValidator(unsignedLengthValidator(valueLE));
86         else
87                 valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
88
89         // initialize the length validator
90         addCheckedWidget(valueLE, valueL);
91         enableWidgets();
92 }
93
94
95 void GuiHSpace::changedSlot()
96 {
97         enableWidgets();
98         changed();
99 }
100
101
102 void GuiHSpace::enableWidgets()
103 {
104         QString const selection = spacingCO->itemData(spacingCO->currentIndex()).toString();
105         bool const custom = selection == "custom";
106         valueLE->setEnabled(custom);
107         unitCO->setEnabled(custom);
108         fillPatternCO->setEnabled(!math_mode_ && selection == "hfill");
109         bool const no_pattern = fillPatternCO->currentIndex() == 0 || math_mode_;
110         bool const enable_keep =
111                 selection == "normal" || selection == "halfquad" || (selection == "hfill" && no_pattern) || custom;
112         keepCB->setEnabled(enable_keep);
113 }
114
115
116 void GuiHSpace::paramsToDialog(Inset const * inset)
117 {
118         InsetSpaceParams const params = math_mode_
119                 ? static_cast<InsetMathSpace const *>(inset)->params()
120                 : static_cast<InsetSpace const *>(inset)->params();
121
122         QString item;
123         int pattern = 0;
124         bool protect = false;
125         switch (params.kind) {
126                 case InsetSpaceParams::NORMAL:
127                         item = "normal";
128                         break;
129                 case InsetSpaceParams::PROTECTED:
130                         item = "normal";
131                         protect = true;
132                         break;
133                 case InsetSpaceParams::VISIBLE:
134                         item = "visible";
135                         protect = true;
136                         break;
137                 case InsetSpaceParams::THIN:
138                         item = "thinspace";
139                         break;
140                 case InsetSpaceParams::MEDIUM:
141                         item = "medspace";
142                         break;
143                 case InsetSpaceParams::THICK:
144                         item = "thickspace";
145                         break;
146                 case InsetSpaceParams::NEGTHIN:
147                         item = "negthinspace";
148                         break;
149                 case InsetSpaceParams::NEGMEDIUM:
150                         item = "negmedspace";
151                         break;
152                 case InsetSpaceParams::NEGTHICK:
153                         item = "negthickspace";
154                         break;
155                 case InsetSpaceParams::ENSKIP:
156                         item = "halfquad";
157                         break;
158                 case InsetSpaceParams::ENSPACE:
159                         item = "halfquad";
160                         protect = true;
161                         break;
162                 case InsetSpaceParams::QUAD:
163                         item = "quad";
164                         break;
165                 case InsetSpaceParams::QQUAD:
166                         item = "qquad";
167                         break;
168                 case InsetSpaceParams::HFILL:
169                         item = "hfill";
170                         break;
171                 case InsetSpaceParams::HFILL_PROTECTED:
172                         item = "hfill";
173                         protect = true;
174                         break;
175                 case InsetSpaceParams::DOTFILL:
176                         item = "hfill";
177                         pattern = 1;
178                         break;
179                 case InsetSpaceParams::HRULEFILL:
180                         item = "hfill";
181                         pattern = 2;
182                         break;
183                 case InsetSpaceParams::LEFTARROWFILL:
184                         item = "hfill";
185                         pattern = 3;
186                         break;
187                 case InsetSpaceParams::RIGHTARROWFILL:
188                         item = "hfill";
189                         pattern = 4;
190                         break;
191                 case InsetSpaceParams::UPBRACEFILL:
192                         item = "hfill";
193                         pattern = 5;
194                         break;
195                 case InsetSpaceParams::DOWNBRACEFILL:
196                         item = "hfill";
197                         pattern = 6;
198                         break;
199                 case InsetSpaceParams::CUSTOM:
200                         item = "custom";
201                         break;
202                 case InsetSpaceParams::CUSTOM_PROTECTED:
203                         item = "custom";
204                         protect = true;
205                         break;
206         }
207         spacingCO->setCurrentIndex(spacingCO->findData(item));
208         fillPatternCO->setCurrentIndex(pattern);
209         keepCB->setChecked(protect);
210         if (item == "halfquad") {
211                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
212                                        "Note that a protected Half Quad will be turned into\n"
213                                        "a vertical space if used at the beginning of a paragraph!"));
214         } else {
215                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
216         }
217         Length::UNIT const default_unit = Length::defaultUnit();
218         if (item == "custom") {
219                 string length = params.length.asString();
220                 lengthToWidgets(valueLE, unitCO, length, default_unit);
221         } else
222                 lengthToWidgets(valueLE, unitCO, "", default_unit);
223
224         enableWidgets();
225 }
226
227
228 docstring GuiHSpace::dialogToParams() const
229 {
230         InsetSpaceParams params = math_mode_ ?
231                 InsetSpaceParams(true) : InsetSpaceParams(false);
232
233         QString const item =
234                 spacingCO->itemData(spacingCO->currentIndex()).toString();
235
236         if (item == "normal")
237                 params.kind = keepCB->isChecked() ?
238                         InsetSpaceParams::PROTECTED : InsetSpaceParams::NORMAL;
239         else if (item == "thinspace")
240                 params.kind = InsetSpaceParams::THIN;
241         else if (item == "medspace")
242                 params.kind = InsetSpaceParams::MEDIUM;
243         else if (item == "thickspace")
244                 params.kind = InsetSpaceParams::THICK;
245         else if (item == "negthinspace")
246                 params.kind = InsetSpaceParams::NEGTHIN;
247         else if (item == "negmedspace")
248                 params.kind = InsetSpaceParams::NEGMEDIUM;
249         else if (item == "negthickspace")
250                 params.kind = InsetSpaceParams::NEGTHICK;
251         else if (item == "halfquad")
252                 params.kind = keepCB->isChecked() ?
253                         InsetSpaceParams::ENSPACE : InsetSpaceParams::ENSKIP;
254         else if (item == "quad")
255                         params.kind = InsetSpaceParams::QUAD;
256         else if (item == "qquad")
257                         params.kind = InsetSpaceParams::QQUAD;
258         else if (item == "hfill") {
259                 switch (fillPatternCO->currentIndex()) {
260                 case 1:
261                         params.kind = InsetSpaceParams::DOTFILL;
262                         break;
263                 case 2:
264                         params.kind = InsetSpaceParams::HRULEFILL;
265                         break;
266                 case 3:
267                         params.kind = InsetSpaceParams::LEFTARROWFILL;
268                         break;
269                 case 4:
270                         params.kind = InsetSpaceParams::RIGHTARROWFILL;
271                         break;
272                 case 5:
273                         params.kind = InsetSpaceParams::UPBRACEFILL;
274                         break;
275                 case 6:
276                         params.kind = InsetSpaceParams::DOWNBRACEFILL;
277                         break;
278                 default:
279                         if (keepCB->isChecked())
280                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
281                         else
282                                 params.kind = InsetSpaceParams::HFILL;
283                         break;
284                 }
285         } else if (item == "custom") {
286                         params.kind = keepCB->isChecked() ?
287                                 InsetSpaceParams::CUSTOM_PROTECTED : InsetSpaceParams::CUSTOM;
288                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
289         } else if (item == "visible") 
290                 params.kind = InsetSpaceParams::VISIBLE;
291
292         return from_ascii(InsetSpace::params2string(params));
293 }
294
295
296 bool GuiHSpace::checkWidgets() const
297 {
298         if (!InsetParamsWidget::checkWidgets())
299                 return false;
300         return spacingCO->itemData(spacingCO->currentIndex()).toString() != "custom"
301                 || !valueLE->text().isEmpty();
302 }
303
304 } // namespace frontend
305 } // namespace lyx
306
307
308 #include "moc_GuiHSpace.cpp"