]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QParagraph.C
dont use pragma impementation and interface anymore
[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
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 "layout.h" // LyXAlignment
22 #include "helper_funcs.h"
23 #include "lyxgluelength.h"
24 #include "vspace.h"
25
26 #include "support/lstrings.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 Qt2CB<ControlParagraph, Qt2DB<QParagraphDialog> > base_class;
43
44
45 QParagraph::QParagraph()
46         : base_class(qt_("Paragraph Layout"))
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         bc().setOK(dialog_->okPB);
71         bc().setApply(dialog_->applyPB);
72         bc().setCancel(dialog_->closePB);
73         bc().setRestore(dialog_->restorePB);
74         bc().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() == 5
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() == 5
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_->noindent->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                         ostringstream buffer;
262                         buffer << len.value();
263                         length = buffer.str();
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         dialog_->labelWidth->setText(toqstr(labelwidth));
299         // _() is correct here (this is stupid though !)
300         dialog_->labelwidthGB->setEnabled(
301                 labelwidth != _("Senseless with this layout!"));
302
303         // alignment
304         int i;
305         switch (params.align()) {
306         case LYX_ALIGN_LEFT:
307                 i = 1;
308                 break;
309         case LYX_ALIGN_RIGHT:
310                 i = 2;
311                 break;
312         case LYX_ALIGN_CENTER:
313                 i = 3;
314                 break;
315         default:
316                 i = 0;
317                 break;
318         }
319         dialog_->align->setCurrentItem(i);
320
321
322         //LyXAlignment alignpos = controller().alignPossible();
323
324         // no inset-text-owned paragraph may have pagebreaks
325         bool ininset = controller().inInset();
326         dialog_->pagebreakAbove->setEnabled(!ininset);
327         dialog_->pagebreakBelow->setEnabled(!ininset);
328
329         // lines, pagebreaks and indent
330         dialog_->lineAbove->setChecked(params.lineTop());
331         dialog_->lineBelow->setChecked(params.lineBottom());
332         dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
333         dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
334         dialog_->noindent->setChecked(params.noindent());
335
336         // linespacing
337         int linespacing;
338         Spacing const & space = params.spacing();
339         switch (space.getSpace()) {
340         case Spacing::Single:
341                 linespacing = 1;
342                 break;
343         case Spacing::Onehalf:
344                 linespacing = 2;
345                 break;
346         case Spacing::Double:
347                 linespacing = 3;
348                 break;
349         case Spacing::Other:
350                 linespacing = 4;
351                 break;
352         default:
353                 linespacing = 0;
354                 break;
355         }
356         dialog_->linespacing->setCurrentItem(linespacing);
357         if (space.getSpace() == Spacing::Other) {
358                 string const sp = tostr(space.getValue());
359                 dialog_->linespacingValue->setText(toqstr(sp));
360                 dialog_->linespacingValue->setEnabled(true);
361         } else {
362                 dialog_->linespacingValue->setText("");
363                 dialog_->linespacingValue->setEnabled(false);
364         }
365
366         // vspace top
367         setWidgetsFromVSpace(params.spaceTop(),
368                              dialog_->spacingAbove,
369                              dialog_->valueAbove,
370                              dialog_->unitAbove,
371                              dialog_->keepAbove,units_);
372
373         // vspace bottom
374         setWidgetsFromVSpace(params.spaceBottom(),
375                              dialog_->spacingBelow,
376                              dialog_->valueBelow,
377                              dialog_->unitBelow,
378                              dialog_->keepBelow,units_);
379
380         // no indent
381         dialog_->noindent->setChecked(params.noindent());
382 }