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