]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiHSpace.cpp
3c4c62a5d7afc27481fae43f8cf37eab2deaa3bb
[features.git] / src / frontends / qt / 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         spacingCO->addItem(qt_("Interword Space"), "normal");
45         spacingCO->addItem(qt_("Thin Space (1/6 em)"), "thinspace");
46         spacingCO->addItem(qt_("Medium Space (2/9 em)"), "medspace");
47         spacingCO->addItem(qt_("Thick Space (5/18 em)"), "thickspace");
48         spacingCO->addItem(qt_("Negative Thin Space (−1/6 em)"), "negthinspace");
49         spacingCO->addItem(qt_("Negative Medium Space (−2/9 em)"), "negmedspace");
50         spacingCO->addItem(qt_("Negative Thick Space (−5/18 em)"), "negthickspace");
51         spacingCO->addItem(qt_("Half Quad (1/2 em)"), "halfquad");
52         spacingCO->addItem(qt_("Quad (1 em)"), "quad");
53         spacingCO->addItem(qt_("Double Quad (2 em)"), "qquad");
54         spacingCO->addItem(qt_("Horizontal Fill"), "hfill");
55         if (!math_mode_)
56                 spacingCO->addItem(qt_("Visible Space"), "visible");
57         spacingCO->addItem(qt_("Custom"), "custom");
58
59 #if QT_VERSION < 0x050e00
60         connect(spacingCO, SIGNAL(highlighted(QString)),
61                 this, SLOT(changedSlot()));
62 #else
63         connect(spacingCO, SIGNAL(textHighlighted(QString)),
64                 this, SLOT(changedSlot()));
65 #endif
66         connect(valueLE, SIGNAL(textChanged(QString)),
67                 this, SLOT(changedSlot()));
68         connect(spacingCO, SIGNAL(activated(int)),
69                 this, SLOT(changedSlot()));
70         connect(keepCB, SIGNAL(clicked()),
71                 this, SLOT(changedSlot()));
72         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
73                 this, SLOT(changedSlot()));
74         connect(fillPatternCO, SIGNAL(activated(int)),
75                 this, SLOT(changedSlot()));
76
77         // Set up a signed (glue) length validator
78         LengthValidator * v = new LengthValidator(valueLE);
79         if (math_mode_)
80                 v->setBottom(Length());
81         else
82                 v->setBottom(GlueLength());
83         valueLE->setValidator(v);
84
85         // initialize the length validator
86         addCheckedWidget(valueLE, valueL);
87         enableWidgets();
88 }
89
90
91 void GuiHSpace::changedSlot()
92 {
93         enableWidgets();
94         changed();
95 }
96
97
98 void GuiHSpace::enableWidgets() const
99 {
100         QString const selection = spacingCO->itemData(spacingCO->currentIndex()).toString();
101         bool const custom = selection == "custom";
102         valueLE->setEnabled(custom);
103         if (custom)
104                 valueLE->setFocus();
105         valueL->setEnabled(custom);
106         unitCO->setEnabled(custom);
107         fillPatternCO->setEnabled(!math_mode_ && selection == "hfill");
108         fillPatternL->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"
112                 || (selection == "hfill" && no_pattern) || custom;
113         keepCB->setEnabled(enable_keep);
114         keepL->setEnabled(enable_keep);
115 }
116
117
118 void GuiHSpace::paramsToDialog(Inset const * inset)
119 {
120         InsetSpaceParams const params = math_mode_
121                 ? static_cast<InsetMathSpace const *>(inset)->params()
122                 : static_cast<InsetSpace const *>(inset)->params();
123
124         QString item;
125         int pattern = 0;
126         bool protect = false;
127         switch (params.kind) {
128                 case InsetSpaceParams::NORMAL:
129                         item = "normal";
130                         break;
131                 case InsetSpaceParams::PROTECTED:
132                         item = "normal";
133                         protect = true;
134                         break;
135                 case InsetSpaceParams::VISIBLE:
136                         item = "visible";
137                         protect = true;
138                         break;
139                 case InsetSpaceParams::THIN:
140                         item = "thinspace";
141                         break;
142                 case InsetSpaceParams::MEDIUM:
143                         item = "medspace";
144                         break;
145                 case InsetSpaceParams::THICK:
146                         item = "thickspace";
147                         break;
148                 case InsetSpaceParams::NEGTHIN:
149                         item = "negthinspace";
150                         break;
151                 case InsetSpaceParams::NEGMEDIUM:
152                         item = "negmedspace";
153                         break;
154                 case InsetSpaceParams::NEGTHICK:
155                         item = "negthickspace";
156                         break;
157                 case InsetSpaceParams::ENSKIP:
158                         item = "halfquad";
159                         break;
160                 case InsetSpaceParams::ENSPACE:
161                         item = "halfquad";
162                         protect = true;
163                         break;
164                 case InsetSpaceParams::QUAD:
165                         item = "quad";
166                         break;
167                 case InsetSpaceParams::QQUAD:
168                         item = "qquad";
169                         break;
170                 case InsetSpaceParams::HFILL:
171                         item = "hfill";
172                         break;
173                 case InsetSpaceParams::HFILL_PROTECTED:
174                         item = "hfill";
175                         protect = true;
176                         break;
177                 case InsetSpaceParams::DOTFILL:
178                         item = "hfill";
179                         pattern = 1;
180                         break;
181                 case InsetSpaceParams::HRULEFILL:
182                         item = "hfill";
183                         pattern = 2;
184                         break;
185                 case InsetSpaceParams::LEFTARROWFILL:
186                         item = "hfill";
187                         pattern = 3;
188                         break;
189                 case InsetSpaceParams::RIGHTARROWFILL:
190                         item = "hfill";
191                         pattern = 4;
192                         break;
193                 case InsetSpaceParams::UPBRACEFILL:
194                         item = "hfill";
195                         pattern = 5;
196                         break;
197                 case InsetSpaceParams::DOWNBRACEFILL:
198                         item = "hfill";
199                         pattern = 6;
200                         break;
201                 case InsetSpaceParams::CUSTOM:
202                         item = "custom";
203                         break;
204                 case InsetSpaceParams::CUSTOM_PROTECTED:
205                         item = "custom";
206                         protect = true;
207                         break;
208         }
209         spacingCO->setCurrentIndex(spacingCO->findData(item));
210         fillPatternCO->setCurrentIndex(pattern);
211         keepCB->setChecked(protect);
212         if (item == "halfquad") {
213                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
214                                        "Note that a protected Half Quad will be turned into\n"
215                                        "a vertical space if used at the beginning of a paragraph!"));
216         } else {
217                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
218         }
219         Length::UNIT const default_unit = Length::defaultUnit();
220         if (item == "custom") {
221                 string length = params.length.asString();
222                 lengthToWidgets(valueLE, unitCO, length, default_unit);
223         } else
224                 lengthToWidgets(valueLE, unitCO, "", default_unit);
225
226         enableWidgets();
227 }
228
229
230 docstring GuiHSpace::dialogToParams() const
231 {
232         InsetSpaceParams params = math_mode_ ?
233                 InsetSpaceParams(true) : InsetSpaceParams(false);
234
235         QString const item =
236                 spacingCO->itemData(spacingCO->currentIndex()).toString();
237
238         if (item == "normal")
239                 params.kind = keepCB->isChecked() ?
240                         InsetSpaceParams::PROTECTED : InsetSpaceParams::NORMAL;
241         else if (item == "thinspace")
242                 params.kind = InsetSpaceParams::THIN;
243         else if (item == "medspace")
244                 params.kind = InsetSpaceParams::MEDIUM;
245         else if (item == "thickspace")
246                 params.kind = InsetSpaceParams::THICK;
247         else if (item == "negthinspace")
248                 params.kind = InsetSpaceParams::NEGTHIN;
249         else if (item == "negmedspace")
250                 params.kind = InsetSpaceParams::NEGMEDIUM;
251         else if (item == "negthickspace")
252                 params.kind = InsetSpaceParams::NEGTHICK;
253         else if (item == "halfquad")
254                 params.kind = keepCB->isChecked() ?
255                         InsetSpaceParams::ENSPACE : InsetSpaceParams::ENSKIP;
256         else if (item == "quad")
257                         params.kind = InsetSpaceParams::QUAD;
258         else if (item == "qquad")
259                         params.kind = InsetSpaceParams::QQUAD;
260         else if (item == "hfill") {
261                 switch (fillPatternCO->currentIndex()) {
262                 case 1:
263                         params.kind = InsetSpaceParams::DOTFILL;
264                         break;
265                 case 2:
266                         params.kind = InsetSpaceParams::HRULEFILL;
267                         break;
268                 case 3:
269                         params.kind = InsetSpaceParams::LEFTARROWFILL;
270                         break;
271                 case 4:
272                         params.kind = InsetSpaceParams::RIGHTARROWFILL;
273                         break;
274                 case 5:
275                         params.kind = InsetSpaceParams::UPBRACEFILL;
276                         break;
277                 case 6:
278                         params.kind = InsetSpaceParams::DOWNBRACEFILL;
279                         break;
280                 default:
281                         if (keepCB->isChecked())
282                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
283                         else
284                                 params.kind = InsetSpaceParams::HFILL;
285                         break;
286                 }
287         } else if (item == "custom") {
288                         params.kind = keepCB->isChecked() ?
289                                 InsetSpaceParams::CUSTOM_PROTECTED : InsetSpaceParams::CUSTOM;
290                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
291         } else if (item == "visible")
292                 params.kind = InsetSpaceParams::VISIBLE;
293
294         return from_ascii(InsetSpace::params2string(params));
295 }
296
297
298 bool GuiHSpace::checkWidgets(bool readonly) const
299 {
300         valueLE->setReadOnly(readonly);
301         spacingCO->setEnabled(!readonly);
302         spacingL->setEnabled(!readonly);
303         
304         if (readonly) {
305                 fillPatternCO->setEnabled(false);
306                 fillPatternL->setEnabled(false);
307                 keepCB->setEnabled(false);
308                 keepL->setEnabled(false);
309                 valueLE->setEnabled(false);
310                 valueL->setEnabled(false);
311                 unitCO->setEnabled(false);
312         } else
313                 enableWidgets();
314
315         if (!InsetParamsWidget::checkWidgets())
316                 return false;
317         return spacingCO->itemData(spacingCO->currentIndex()).toString() != "custom"
318                 || !valueLE->text().isEmpty();
319 }
320
321 } // namespace frontend
322 } // namespace lyx
323
324
325 #include "moc_GuiHSpace.cpp"