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