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