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