]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiHSpace.cpp
Add accelerators
[lyx.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_("Normal 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         // When Non-Breaking is disabled indicate that this is a non-breaking state
116         // by enabling the check-box
117         if (!enable_keep)
118                 keepCB->setCheckState(Qt::Checked);
119 }
120
121
122 void GuiHSpace::paramsToDialog(Inset const * inset)
123 {
124         InsetSpaceParams const params = math_mode_
125                 ? static_cast<InsetMathSpace const *>(inset)->params()
126                 : static_cast<InsetSpace const *>(inset)->params();
127
128         QString item;
129         int pattern = 0;
130         bool protect = false;
131         switch (params.kind) {
132                 case InsetSpaceParams::NORMAL:
133                         item = "normal";
134                         break;
135                 case InsetSpaceParams::PROTECTED:
136                         item = "normal";
137                         protect = true;
138                         break;
139                 case InsetSpaceParams::VISIBLE:
140                         item = "visible";
141                         protect = true;
142                         break;
143                 case InsetSpaceParams::THIN:
144                         item = "thinspace";
145                         break;
146                 case InsetSpaceParams::MEDIUM:
147                         item = "medspace";
148                         break;
149                 case InsetSpaceParams::THICK:
150                         item = "thickspace";
151                         break;
152                 case InsetSpaceParams::NEGTHIN:
153                         item = "negthinspace";
154                         break;
155                 case InsetSpaceParams::NEGMEDIUM:
156                         item = "negmedspace";
157                         break;
158                 case InsetSpaceParams::NEGTHICK:
159                         item = "negthickspace";
160                         break;
161                 case InsetSpaceParams::ENSKIP:
162                         item = "halfquad";
163                         break;
164                 case InsetSpaceParams::ENSPACE:
165                         item = "halfquad";
166                         protect = true;
167                         break;
168                 case InsetSpaceParams::QUAD:
169                         item = "quad";
170                         break;
171                 case InsetSpaceParams::QQUAD:
172                         item = "qquad";
173                         break;
174                 case InsetSpaceParams::HFILL:
175                         item = "hfill";
176                         break;
177                 case InsetSpaceParams::HFILL_PROTECTED:
178                         item = "hfill";
179                         protect = true;
180                         break;
181                 case InsetSpaceParams::DOTFILL:
182                         item = "hfill";
183                         pattern = 1;
184                         break;
185                 case InsetSpaceParams::HRULEFILL:
186                         item = "hfill";
187                         pattern = 2;
188                         break;
189                 case InsetSpaceParams::LEFTARROWFILL:
190                         item = "hfill";
191                         pattern = 3;
192                         break;
193                 case InsetSpaceParams::RIGHTARROWFILL:
194                         item = "hfill";
195                         pattern = 4;
196                         break;
197                 case InsetSpaceParams::UPBRACEFILL:
198                         item = "hfill";
199                         pattern = 5;
200                         break;
201                 case InsetSpaceParams::DOWNBRACEFILL:
202                         item = "hfill";
203                         pattern = 6;
204                         break;
205                 case InsetSpaceParams::CUSTOM:
206                         item = "custom";
207                         break;
208                 case InsetSpaceParams::CUSTOM_PROTECTED:
209                         item = "custom";
210                         protect = true;
211                         break;
212         }
213         spacingCO->setCurrentIndex(spacingCO->findData(item));
214         fillPatternCO->setCurrentIndex(pattern);
215         keepCB->setChecked(protect);
216         if (item == "halfquad") {
217                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
218                                        "Note that a protected Half Quad will be turned into\n"
219                                        "a vertical space if used at the beginning of a paragraph!"));
220         } else {
221                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
222         }
223         Length::UNIT const default_unit = Length::defaultUnit();
224         if (item == "custom") {
225                 string length = params.length.asString();
226                 lengthToWidgets(valueLE, unitCO, length, default_unit);
227         } else
228                 lengthToWidgets(valueLE, unitCO, "", default_unit);
229
230         enableWidgets();
231 }
232
233
234 docstring GuiHSpace::dialogToParams() const
235 {
236         InsetSpaceParams params = math_mode_ ?
237                 InsetSpaceParams(true) : InsetSpaceParams(false);
238
239         QString const item =
240                 spacingCO->itemData(spacingCO->currentIndex()).toString();
241
242         if (item == "normal")
243                 params.kind = keepCB->isChecked() ?
244                         InsetSpaceParams::PROTECTED : InsetSpaceParams::NORMAL;
245         else if (item == "thinspace")
246                 params.kind = InsetSpaceParams::THIN;
247         else if (item == "medspace")
248                 params.kind = InsetSpaceParams::MEDIUM;
249         else if (item == "thickspace")
250                 params.kind = InsetSpaceParams::THICK;
251         else if (item == "negthinspace")
252                 params.kind = InsetSpaceParams::NEGTHIN;
253         else if (item == "negmedspace")
254                 params.kind = InsetSpaceParams::NEGMEDIUM;
255         else if (item == "negthickspace")
256                 params.kind = InsetSpaceParams::NEGTHICK;
257         else if (item == "halfquad")
258                 params.kind = keepCB->isChecked() ?
259                         InsetSpaceParams::ENSPACE : InsetSpaceParams::ENSKIP;
260         else if (item == "quad")
261                         params.kind = InsetSpaceParams::QUAD;
262         else if (item == "qquad")
263                         params.kind = InsetSpaceParams::QQUAD;
264         else if (item == "hfill") {
265                 switch (fillPatternCO->currentIndex()) {
266                 case 1:
267                         params.kind = InsetSpaceParams::DOTFILL;
268                         break;
269                 case 2:
270                         params.kind = InsetSpaceParams::HRULEFILL;
271                         break;
272                 case 3:
273                         params.kind = InsetSpaceParams::LEFTARROWFILL;
274                         break;
275                 case 4:
276                         params.kind = InsetSpaceParams::RIGHTARROWFILL;
277                         break;
278                 case 5:
279                         params.kind = InsetSpaceParams::UPBRACEFILL;
280                         break;
281                 case 6:
282                         params.kind = InsetSpaceParams::DOWNBRACEFILL;
283                         break;
284                 default:
285                         if (keepCB->isChecked())
286                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
287                         else
288                                 params.kind = InsetSpaceParams::HFILL;
289                         break;
290                 }
291         } else if (item == "custom") {
292                         params.kind = keepCB->isChecked() ?
293                                 InsetSpaceParams::CUSTOM_PROTECTED : InsetSpaceParams::CUSTOM;
294                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
295         } else if (item == "visible")
296                 params.kind = InsetSpaceParams::VISIBLE;
297
298         return from_ascii(InsetSpace::params2string(params));
299 }
300
301
302 bool GuiHSpace::checkWidgets(bool readonly) const
303 {
304         valueLE->setReadOnly(readonly);
305         spacingCO->setEnabled(!readonly);
306         spacingL->setEnabled(!readonly);
307         
308         if (readonly) {
309                 fillPatternCO->setEnabled(false);
310                 fillPatternL->setEnabled(false);
311                 keepCB->setEnabled(false);
312                 keepL->setEnabled(false);
313                 valueLE->setEnabled(false);
314                 valueL->setEnabled(false);
315                 unitCO->setEnabled(false);
316         } else
317                 enableWidgets();
318
319         if (!InsetParamsWidget::checkWidgets())
320                 return false;
321         return spacingCO->itemData(spacingCO->currentIndex()).toString() != "custom"
322                 || !valueLE->text().isEmpty();
323 }
324
325 } // namespace frontend
326 } // namespace lyx
327
328
329 #include "moc_GuiHSpace.cpp"