3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Edwin Leuven <leuven@fee.uva.nl>
12 #pragma implementation
15 #include "ControlParagraph.h"
16 #include "QParagraph.h"
17 #include "QParagraphDialog.h"
19 #include "ParagraphParameters.h"
20 #include "lyxrc.h" // to set the deafult length values
22 #include "layout.h" // LyXAlignment
23 #include "helper_funcs.h"
24 #include "lyxgluelength.h"
27 #include "support/lstrings.h"
28 #include "support/LAssert.h"
32 #include <qcombobox.h>
33 #include <qlineedit.h>
34 #include <qcheckbox.h>
35 #include <qpushbutton.h>
36 #include <qtabwidget.h>
37 #include <qbuttongroup.h>
43 typedef Qt2CB<ControlParagraph, Qt2DB<QParagraphDialog> > base_class;
45 QParagraph::QParagraph(ControlParagraph & c, Dialogs &)
46 : base_class(c, _("Paragraph Layout"))
49 void QParagraph::build_dialog()
52 dialog_.reset(new QParagraphDialog(this));
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());
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());
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);
77 VSpace setVSpaceFromWidgets(int spacing,
86 space = VSpace(VSpace::NONE);
89 space = VSpace(VSpace::DEFSKIP);
92 space = VSpace(VSpace::SMALLSKIP);
95 space = VSpace(VSpace::MEDSKIP);
98 space = VSpace(VSpace::BIGSKIP);
101 space = VSpace(VSpace::VFILL);
105 string const length = strip(frontStrip(value));
106 if (isValidGlueLength(length)) {
108 } else if (!length.empty()){
109 string u = strip(frontStrip(unit));
110 u = subst(u, "%%", "%");
113 space = VSpace(LyXGlueLength(s));
124 void QParagraph::apply()
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);
133 VSpace const space_top =
134 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
135 string(dialog_->valueAbove->text()),
136 string(dialog_->unitAbove->currentText()),
137 dialog_->keepAbove->isChecked());
140 controller().params().spaceTop(space_top);
143 if (dialog_->spacingBelow->currentItem()==5
144 && dialog_->valueBelow->text().isEmpty())
145 dialog_->spacingBelow->setCurrentItem(0);
147 VSpace const space_bottom =
148 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
149 string(dialog_->valueBelow->text()),
150 string(dialog_->unitBelow->currentText()),
151 dialog_->keepBelow->isChecked());
153 controller().params().spaceBottom(space_bottom);
157 switch (dialog_->align->currentItem()) {
159 align = LYX_ALIGN_BLOCK;
162 align = LYX_ALIGN_LEFT;
165 align = LYX_ALIGN_RIGHT;
168 align = LYX_ALIGN_CENTER;
171 align = LYX_ALIGN_BLOCK;
173 controller().params().align(align);
176 Spacing::Space linespacing = Spacing::Default;
178 switch (dialog_->linespacing->currentItem()) {
180 linespacing = Spacing::Default;
183 linespacing = Spacing::Single;
186 linespacing = Spacing::Onehalf;
189 linespacing = Spacing::Double;
192 linespacing = Spacing::Other;
193 other = dialog_->linespacingValue->text();
197 Spacing const spacing(linespacing, other);
198 controller().params().spacing(spacing);
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());
206 controller().params().labelWidthString(string(dialog_->labelWidth->text()));
208 controller().params().noindent(dialog_->noindent->isChecked());
214 void setWidgetsFromVSpace(VSpace const & space,
218 QCheckBox * keep, vector<string> units_)
221 value->setEnabled(false);
222 unit->setEnabled(false);
225 switch (space.kind()) {
229 case VSpace::DEFSKIP:
232 case VSpace::SMALLSKIP:
235 case VSpace::MEDSKIP:
238 case VSpace::BIGSKIP:
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()),"%","%%");
260 for (vector<string>::const_iterator it = units_.begin();
261 it != units_.end(); ++it) {
262 if (*it==default_unit) {
265 if (*it==supplied_unit) {
271 value->setText(length.c_str());
272 unit->setCurrentItem(unit_item);
275 spacing->setCurrentItem(item);
276 keep->setChecked(space.keep());
281 void QParagraph::update_contents()
284 string labelwidth = controller().params().labelWidthString();
285 dialog_->labelWidth->setText(labelwidth.c_str());
286 dialog_->labelwidthGB->setEnabled(
287 labelwidth != _("Senseless with this layout!"));
291 switch (controller().params().align()) {
295 case LYX_ALIGN_RIGHT:
298 case LYX_ALIGN_CENTER:
305 dialog_->align->setCurrentItem(i);
308 //LyXAlignment alignpos = controller().alignPossible();
310 // no inset-text-owned paragraph may have pagebreaks
311 bool ininset = controller().inInset();
312 dialog_->pagebreakAbove->setEnabled(!ininset);
313 dialog_->pagebreakBelow->setEnabled(!ininset);
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());
324 Spacing const space = controller().params().spacing();
325 switch (space.getSpace()) {
326 case Spacing::Single:
329 case Spacing::Onehalf:
332 case Spacing::Double:
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);
348 dialog_->linespacingValue->setText("");
349 dialog_->linespacingValue->setEnabled(false);
353 setWidgetsFromVSpace(controller().params().spaceTop(),
354 dialog_->spacingAbove,
357 dialog_->keepAbove,units_);
360 setWidgetsFromVSpace(controller().params().spaceBottom(),
361 dialog_->spacingBelow,
364 dialog_->keepBelow,units_);
367 dialog_->noindent->setChecked(controller().params().noindent());