]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QParagraph.C
Some string(widget->text()) fixes. Weirdness
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "ControlParagraph.h"
18 #include "QParagraph.h"
19 #include "QParagraphDialog.h"
20 #include "Qt2BC.h"
21 #include "ParagraphParameters.h"
22 #include "lyxrc.h" // to set the deafult length values
23 #include "gettext.h"
24 #include "layout.h" // LyXAlignment
25 #include "helper_funcs.h"
26 #include "lyxgluelength.h"
27 #include "vspace.h"
28
29 #include "support/lstrings.h"
30 #include "support/LAssert.h"
31
32 #include <qcombobox.h>
33 #include <qlineedit.h>
34 #include <qcheckbox.h>
35 #include <qpushbutton.h>
36 #include <qtabwidget.h>
37 #include <qbuttongroup.h>
38
39 #include <functional>
40
41 using std::vector;
42 using std::bind2nd;
43 using std::remove_if;
44
45 typedef Qt2CB<ControlParagraph, Qt2DB<QParagraphDialog> > base_class;
46
47
48 QParagraph::QParagraph()
49         : base_class(_("Paragraph Layout"))
50 {}
51
52
53 void QParagraph::build_dialog()
54 {
55         // the tabbed folder
56         dialog_.reset(new QParagraphDialog(this));
57
58         // Create the contents of the unit choices
59         // Don't include the "%" terms...
60         units_ = getLatexUnits();
61         vector<string>::iterator del =
62                 remove_if(units_.begin(), units_.end(),
63                           bind2nd(contains_functor(), "%"));
64         units_.erase(del, units_.end());
65
66         for (vector<string>::const_iterator it = units_.begin();
67                 it != units_.end(); ++it) {
68                 dialog_->unitAbove->insertItem(it->c_str());
69                 dialog_->unitBelow->insertItem(it->c_str());
70         }
71
72         // Manage the ok, apply, restore and cancel/close buttons
73         bc().setOK(dialog_->okPB);
74         bc().setApply(dialog_->applyPB);
75         bc().setCancel(dialog_->closePB);
76         bc().setRestore(dialog_->restorePB);
77         bc().addReadOnly(dialog_->paragraphTab);
78 }
79
80
81 namespace {
82
83 VSpace setVSpaceFromWidgets(int spacing,
84                             string value,
85                             string unit,
86                             bool keep)
87 {
88         VSpace space;
89
90         switch (spacing) {
91         case 0:
92                 space = VSpace(VSpace::NONE);
93                 break;
94         case 1:
95                 space = VSpace(VSpace::DEFSKIP);
96                 break;
97         case 2:
98                 space = VSpace(VSpace::SMALLSKIP);
99                 break;
100         case 3:
101                 space = VSpace(VSpace::MEDSKIP);
102                 break;
103         case 4:
104                 space = VSpace(VSpace::BIGSKIP);
105                 break;
106         case 5:
107                 space = VSpace(VSpace::VFILL);
108                 break;
109         case 6:
110                 string s;
111                 string const length = trim(value);
112                 if (isValidGlueLength(length)) {
113                         s = length;
114                 } else if (!length.empty()){
115                         string u = trim(unit);
116                         u = subst(u, "%%", "%");
117                         s = length + u;
118                 }
119                 space = VSpace(LyXGlueLength(s));
120                 break;
121         }
122
123         space.setKeep(keep);
124
125         return space;
126 }
127
128 } // namespace anon
129
130
131 void QParagraph::apply()
132 {
133         ParagraphParameters & params = controller().params();
134
135         // SPACING ABOVE
136         // If a vspace kind is "Length" but there's no text in
137         // the input field, reset the kind to "None".
138         if (dialog_->spacingAbove->currentItem() == 5
139             && dialog_->valueAbove->text().isEmpty())
140                 dialog_->spacingAbove->setCurrentItem(0);
141
142         VSpace const space_top =
143                 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
144                                      dialog_->valueAbove->text().latin1(),
145                                      dialog_->unitAbove->currentText().latin1(),
146                                      dialog_->keepAbove->isChecked());
147
148         params.spaceTop(space_top);
149
150         // SPACING BELOW
151         if (dialog_->spacingBelow->currentItem() == 5
152             && dialog_->valueBelow->text().isEmpty())
153                 dialog_->spacingBelow->setCurrentItem(0);
154
155         VSpace const space_bottom =
156         setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
157                              dialog_->valueBelow->text().latin1(),
158                              dialog_->unitBelow->currentText().latin1(),
159                              dialog_->keepBelow->isChecked());
160
161         params.spaceBottom(space_bottom);
162
163         // alignment
164         LyXAlignment align;
165         switch (dialog_->align->currentItem()) {
166         case 0:
167                 align = LYX_ALIGN_BLOCK;
168                 break;
169         case 1:
170                 align = LYX_ALIGN_LEFT;
171                 break;
172         case 2:
173                 align = LYX_ALIGN_RIGHT;
174                 break;
175         case 3:
176                 align = LYX_ALIGN_CENTER;
177                 break;
178         default:
179                 align = LYX_ALIGN_BLOCK;
180         }
181         params.align(align);
182
183         // get spacing
184         Spacing::Space linespacing = Spacing::Default;
185         string other;
186         switch (dialog_->linespacing->currentItem()) {
187         case 0:
188                 linespacing = Spacing::Default;
189                 break;
190         case 1:
191                 linespacing = Spacing::Single;
192                 break;
193         case 2:
194                 linespacing = Spacing::Onehalf;
195                 break;
196         case 3:
197                 linespacing = Spacing::Double;
198                 break;
199         case 4:
200                 linespacing = Spacing::Other;
201                 other = dialog_->linespacingValue->text().latin1();
202                 break;
203         }
204
205         Spacing const spacing(linespacing, other);
206         params.spacing(spacing);
207
208         // lines and pagebreaks
209         params.lineTop(dialog_->lineAbove->isChecked());
210         params.lineBottom(dialog_->lineBelow->isChecked());
211         params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
212         params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
213         // label width
214         params.labelWidthString(string(dialog_->labelWidth->text()));
215         // indendation
216         params.noindent(dialog_->noindent->isChecked());
217
218 }
219
220
221 namespace {
222
223 void setWidgetsFromVSpace(VSpace const & space,
224                           QComboBox * spacing,
225                           QLineEdit * value,
226                           QComboBox * unit,
227                           QCheckBox * keep, vector<string> units_)
228 {
229         value->setText("");
230         value->setEnabled(false);
231         unit->setEnabled(false);
232
233         int item = 0;
234         switch (space.kind()) {
235         case VSpace::NONE:
236                 item = 0;
237                 break;
238         case VSpace::DEFSKIP:
239                 item = 1;
240                 break;
241         case VSpace::SMALLSKIP:
242                 item = 2;
243                 break;
244         case VSpace::MEDSKIP:
245                 item = 3;
246                 break;
247         case VSpace::BIGSKIP:
248                 item = 4;
249                 break;
250         case VSpace::VFILL:
251                 item = 5;
252                 break;
253         case VSpace::LENGTH:
254                 item = 6;
255                 value->setEnabled(true);
256                 unit->setEnabled(true);
257                 string length = space.length().asString();
258                 string const default_unit =
259                         (lyxrc.default_papersize > 3) ? "cm" : "in";
260                 string supplied_unit = default_unit;
261                 LyXLength len(length);
262                 if ((isValidLength(length)
263                      || isStrDbl(length)) && !len.zero()) {
264                         ostringstream buffer;
265                         buffer << len.value();
266                         length = buffer.str();
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(length.c_str());
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         dialog_->labelWidth->setText(labelwidth.c_str());
302         dialog_->labelwidthGB->setEnabled(
303                 labelwidth != _("Senseless with this layout!"));
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_->noindent->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(sp.c_str());
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
382         // no indent
383         dialog_->noindent->setChecked(params.noindent());
384 }