]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QParagraph.C
Get rid of the static_casts.
[lyx.git] / src / frontends / qt2 / QParagraph.C
1 /**
2  * \file QParagraph.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "ControlParagraph.h"
14 #include "QParagraph.h"
15 #include "QParagraphDialog.h"
16 #include "Qt2BC.h"
17 #include "ParagraphParameters.h"
18 #include "lyxrc.h" // to set the deafult length values
19 #include "qt_helpers.h"
20 #include "layout.h" // LyXAlignment
21 #include "helper_funcs.h"
22 #include "lyxgluelength.h"
23 #include "vspace.h"
24
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
27 #include "support/LAssert.h"
28
29 #include <qcombobox.h>
30 #include <qlineedit.h>
31 #include <qcheckbox.h>
32 #include <qpushbutton.h>
33 #include <qtabwidget.h>
34 #include <qbuttongroup.h>
35
36 #include <functional>
37
38 using std::vector;
39 using std::bind2nd;
40 using std::remove_if;
41
42 typedef QController<ControlParagraph, QView<QParagraphDialog> > base_class;
43
44
45 QParagraph::QParagraph(Dialog & parent)
46         : base_class(parent, _("LyX: Paragraph Settings"))
47 {}
48
49
50 void QParagraph::build_dialog()
51 {
52         // the tabbed folder
53         dialog_.reset(new QParagraphDialog(this));
54
55         // Create the contents of the unit choices
56         // Don't include the "%" terms...
57         units_ = getLatexUnits();
58         vector<string>::iterator del =
59                 remove_if(units_.begin(), units_.end(),
60                           bind2nd(contains_functor(), "%"));
61         units_.erase(del, units_.end());
62
63         for (vector<string>::const_iterator it = units_.begin();
64                 it != units_.end(); ++it) {
65                 dialog_->unitAbove->insertItem(toqstr(*it));
66                 dialog_->unitBelow->insertItem(toqstr(*it));
67         }
68
69         // Manage the ok, apply, restore and cancel/close buttons
70         bcview().setOK(dialog_->okPB);
71         bcview().setApply(dialog_->applyPB);
72         bcview().setCancel(dialog_->closePB);
73         bcview().setRestore(dialog_->restorePB);
74         bcview().addReadOnly(dialog_->paragraphTab);
75 }
76
77
78 namespace {
79
80 VSpace setVSpaceFromWidgets(int spacing,
81                             string value,
82                             string unit,
83                             bool keep)
84 {
85         VSpace space;
86
87         switch (spacing) {
88         case 0:
89                 space = VSpace(VSpace::NONE);
90                 break;
91         case 1:
92                 space = VSpace(VSpace::DEFSKIP);
93                 break;
94         case 2:
95                 space = VSpace(VSpace::SMALLSKIP);
96                 break;
97         case 3:
98                 space = VSpace(VSpace::MEDSKIP);
99                 break;
100         case 4:
101                 space = VSpace(VSpace::BIGSKIP);
102                 break;
103         case 5:
104                 space = VSpace(VSpace::VFILL);
105                 break;
106         case 6:
107                 string s;
108                 string const length = trim(value);
109                 if (isValidGlueLength(length)) {
110                         s = length;
111                 } else if (!length.empty()){
112                         string u = trim(unit);
113                         u = subst(u, "%%", "%");
114                         s = length + u;
115                 }
116                 space = VSpace(LyXGlueLength(s));
117                 break;
118         }
119
120         space.setKeep(keep);
121
122         return space;
123 }
124
125 } // namespace anon
126
127
128 void QParagraph::apply()
129 {
130         ParagraphParameters & params = controller().params();
131
132         // SPACING ABOVE
133         // If a vspace kind is "Length" but there's no text in
134         // the input field, reset the kind to "None".
135         if (dialog_->spacingAbove->currentItem() == 6
136             && dialog_->valueAbove->text().isEmpty())
137                 dialog_->spacingAbove->setCurrentItem(0);
138
139         VSpace const space_top =
140                 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
141                                      fromqstr(dialog_->valueAbove->text()),
142                                      fromqstr(dialog_->unitAbove->currentText()),
143                                      dialog_->keepAbove->isChecked());
144
145         params.spaceTop(space_top);
146
147         // SPACING BELOW
148         if (dialog_->spacingBelow->currentItem() == 6
149             && dialog_->valueBelow->text().isEmpty())
150                 dialog_->spacingBelow->setCurrentItem(0);
151
152         VSpace const space_bottom =
153         setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
154                              fromqstr(dialog_->valueBelow->text()),
155                              fromqstr(dialog_->unitBelow->currentText()),
156                              dialog_->keepBelow->isChecked());
157
158         params.spaceBottom(space_bottom);
159
160         // alignment
161         LyXAlignment align;
162         switch (dialog_->align->currentItem()) {
163         case 0:
164                 align = LYX_ALIGN_BLOCK;
165                 break;
166         case 1:
167                 align = LYX_ALIGN_LEFT;
168                 break;
169         case 2:
170                 align = LYX_ALIGN_RIGHT;
171                 break;
172         case 3:
173                 align = LYX_ALIGN_CENTER;
174                 break;
175         default:
176                 align = LYX_ALIGN_BLOCK;
177         }
178         params.align(align);
179
180         // get spacing
181         Spacing::Space linespacing = Spacing::Default;
182         string other;
183         switch (dialog_->linespacing->currentItem()) {
184         case 0:
185                 linespacing = Spacing::Default;
186                 break;
187         case 1:
188                 linespacing = Spacing::Single;
189                 break;
190         case 2:
191                 linespacing = Spacing::Onehalf;
192                 break;
193         case 3:
194                 linespacing = Spacing::Double;
195                 break;
196         case 4:
197                 linespacing = Spacing::Other;
198                 other = fromqstr(dialog_->linespacingValue->text());
199                 break;
200         }
201
202         Spacing const spacing(linespacing, other);
203         params.spacing(spacing);
204
205         // lines and pagebreaks
206         params.lineTop(dialog_->lineAbove->isChecked());
207         params.lineBottom(dialog_->lineBelow->isChecked());
208         params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
209         params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
210         // label width
211         params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
212         // indendation
213         params.noindent(!dialog_->indentCB->isChecked());
214
215 }
216
217
218 namespace {
219
220 void setWidgetsFromVSpace(VSpace const & space,
221                           QComboBox * spacing,
222                           QLineEdit * value,
223                           QComboBox * unit,
224                           QCheckBox * keep, vector<string> units_)
225 {
226         value->setText("");
227         value->setEnabled(false);
228         unit->setEnabled(false);
229
230         int item = 0;
231         switch (space.kind()) {
232         case VSpace::NONE:
233                 item = 0;
234                 break;
235         case VSpace::DEFSKIP:
236                 item = 1;
237                 break;
238         case VSpace::SMALLSKIP:
239                 item = 2;
240                 break;
241         case VSpace::MEDSKIP:
242                 item = 3;
243                 break;
244         case VSpace::BIGSKIP:
245                 item = 4;
246                 break;
247         case VSpace::VFILL:
248                 item = 5;
249                 break;
250         case VSpace::LENGTH:
251                 item = 6;
252                 value->setEnabled(true);
253                 unit->setEnabled(true);
254                 string length = space.length().asString();
255                 string const default_unit =
256                         (lyxrc.default_papersize > 3) ? "cm" : "in";
257                 string supplied_unit = default_unit;
258                 LyXLength len(length);
259                 if ((isValidLength(length)
260                      || isStrDbl(length)) && !len.zero()) {
261                         length = tostr(len.value());
262                         supplied_unit = subst(stringFromUnit(len.unit()),
263                                               "%", "%%");
264                 }
265
266                 int unit_item = 0;
267                 int i = 0;
268                 for (vector<string>::const_iterator it = units_.begin();
269                      it != units_.end(); ++it) {
270                         if (*it == default_unit) {
271                                 unit_item = i;
272                         }
273                         if (*it == supplied_unit) {
274                                 unit_item = i;
275                                 break;
276                         }
277                         i += 1;
278                 }
279                 value->setText(toqstr(length));
280                 unit->setCurrentItem(unit_item);
281                 break;
282         }
283         spacing->setCurrentItem(item);
284         keep->setChecked(space.keep());
285 }
286
287 } // namespace anon
288
289
290 void QParagraph::update_contents()
291 {
292         ParagraphParameters const & params = controller().params();
293
294         // label width
295         string const & labelwidth = params.labelWidthString();
296         // _() is correct here (this is stupid though !)
297         if (labelwidth != _("Senseless with this layout!")) {
298                 dialog_->labelwidthGB->setEnabled(true);
299                 dialog_->labelWidth->setText(toqstr(labelwidth));
300         } else {
301                 dialog_->labelwidthGB->setEnabled(false);
302                 dialog_->labelWidth->setText("");
303         }
304
305         // alignment
306         int i;
307         switch (params.align()) {
308         case LYX_ALIGN_LEFT:
309                 i = 1;
310                 break;
311         case LYX_ALIGN_RIGHT:
312                 i = 2;
313                 break;
314         case LYX_ALIGN_CENTER:
315                 i = 3;
316                 break;
317         default:
318                 i = 0;
319                 break;
320         }
321         dialog_->align->setCurrentItem(i);
322
323
324         //LyXAlignment alignpos = controller().alignPossible();
325
326         // no inset-text-owned paragraph may have pagebreaks
327         bool ininset = controller().inInset();
328         dialog_->pagebreakAbove->setEnabled(!ininset);
329         dialog_->pagebreakBelow->setEnabled(!ininset);
330
331         // lines, pagebreaks and indent
332         dialog_->lineAbove->setChecked(params.lineTop());
333         dialog_->lineBelow->setChecked(params.lineBottom());
334         dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
335         dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
336         dialog_->indentCB->setChecked(!params.noindent());
337
338         // linespacing
339         int linespacing;
340         Spacing const & space = params.spacing();
341         switch (space.getSpace()) {
342         case Spacing::Single:
343                 linespacing = 1;
344                 break;
345         case Spacing::Onehalf:
346                 linespacing = 2;
347                 break;
348         case Spacing::Double:
349                 linespacing = 3;
350                 break;
351         case Spacing::Other:
352                 linespacing = 4;
353                 break;
354         default:
355                 linespacing = 0;
356                 break;
357         }
358         dialog_->linespacing->setCurrentItem(linespacing);
359         if (space.getSpace() == Spacing::Other) {
360                 string const sp = tostr(space.getValue());
361                 dialog_->linespacingValue->setText(toqstr(sp));
362                 dialog_->linespacingValue->setEnabled(true);
363         } else {
364                 dialog_->linespacingValue->setText("");
365                 dialog_->linespacingValue->setEnabled(false);
366         }
367
368         // vspace top
369         setWidgetsFromVSpace(params.spaceTop(),
370                              dialog_->spacingAbove,
371                              dialog_->valueAbove,
372                              dialog_->unitAbove,
373                              dialog_->keepAbove,units_);
374
375         // vspace bottom
376         setWidgetsFromVSpace(params.spaceBottom(),
377                              dialog_->spacingBelow,
378                              dialog_->valueBelow,
379                              dialog_->unitBelow,
380                              dialog_->keepBelow,units_);
381 }