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