3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "ControlParagraph.h"
14 #include "QParagraph.h"
15 #include "QParagraphDialog.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"
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
27 #include "support/LAssert.h"
29 #include <qcombobox.h>
30 #include <qlineedit.h>
31 #include <qcheckbox.h>
32 #include <qpushbutton.h>
33 #include <qtabwidget.h>
34 #include <qbuttongroup.h>
38 using namespace lyx::support;
44 typedef QController<ControlParagraph, QView<QParagraphDialog> > base_class;
47 QParagraph::QParagraph(Dialog & parent)
48 : base_class(parent, _("LyX: Paragraph Settings"))
52 void QParagraph::build_dialog()
55 dialog_.reset(new QParagraphDialog(this));
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());
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));
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);
82 VSpace setVSpaceFromWidgets(int spacing,
91 space = VSpace(VSpace::NONE);
94 space = VSpace(VSpace::DEFSKIP);
97 space = VSpace(VSpace::SMALLSKIP);
100 space = VSpace(VSpace::MEDSKIP);
103 space = VSpace(VSpace::BIGSKIP);
106 space = VSpace(VSpace::VFILL);
110 string const length = trim(value);
111 if (isValidGlueLength(length)) {
113 } else if (!length.empty()){
114 string u = trim(unit);
115 u = subst(u, "%%", "%");
118 space = VSpace(LyXGlueLength(s));
130 void QParagraph::apply()
132 ParagraphParameters & params = controller().params();
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);
141 VSpace const space_top =
142 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
143 fromqstr(dialog_->valueAbove->text()),
144 fromqstr(dialog_->unitAbove->currentText()),
145 dialog_->keepAbove->isChecked());
147 params.spaceTop(space_top);
150 if (dialog_->spacingBelow->currentItem() == 6
151 && dialog_->valueBelow->text().isEmpty())
152 dialog_->spacingBelow->setCurrentItem(0);
154 VSpace const space_bottom =
155 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
156 fromqstr(dialog_->valueBelow->text()),
157 fromqstr(dialog_->unitBelow->currentText()),
158 dialog_->keepBelow->isChecked());
160 params.spaceBottom(space_bottom);
164 switch (dialog_->align->currentItem()) {
166 align = LYX_ALIGN_BLOCK;
169 align = LYX_ALIGN_LEFT;
172 align = LYX_ALIGN_RIGHT;
175 align = LYX_ALIGN_CENTER;
178 align = LYX_ALIGN_BLOCK;
183 Spacing::Space linespacing = Spacing::Default;
185 switch (dialog_->linespacing->currentItem()) {
187 linespacing = Spacing::Default;
190 linespacing = Spacing::Single;
193 linespacing = Spacing::Onehalf;
196 linespacing = Spacing::Double;
199 linespacing = Spacing::Other;
200 other = fromqstr(dialog_->linespacingValue->text());
204 Spacing const spacing(linespacing, other);
205 params.spacing(spacing);
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());
213 params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
215 params.noindent(!dialog_->indentCB->isChecked());
222 void setWidgetsFromVSpace(VSpace const & space,
226 QCheckBox * keep, vector<string> units_)
229 value->setEnabled(false);
230 unit->setEnabled(false);
233 switch (space.kind()) {
237 case VSpace::DEFSKIP:
240 case VSpace::SMALLSKIP:
243 case VSpace::MEDSKIP:
246 case VSpace::BIGSKIP:
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()),
270 for (vector<string>::const_iterator it = units_.begin();
271 it != units_.end(); ++it) {
272 if (*it == default_unit) {
275 if (*it == supplied_unit) {
281 value->setText(toqstr(length));
282 unit->setCurrentItem(unit_item);
285 spacing->setCurrentItem(item);
286 keep->setChecked(space.keep());
292 void QParagraph::update_contents()
294 ParagraphParameters const & params = controller().params();
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));
303 dialog_->labelwidthGB->setEnabled(false);
304 dialog_->labelWidth->setText("");
309 switch (params.align()) {
313 case LYX_ALIGN_RIGHT:
316 case LYX_ALIGN_CENTER:
323 dialog_->align->setCurrentItem(i);
326 //LyXAlignment alignpos = controller().alignPossible();
328 // no inset-text-owned paragraph may have pagebreaks
329 bool ininset = controller().inInset();
330 dialog_->pagebreakAbove->setEnabled(!ininset);
331 dialog_->pagebreakBelow->setEnabled(!ininset);
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());
342 Spacing const & space = params.spacing();
343 switch (space.getSpace()) {
344 case Spacing::Single:
347 case Spacing::Onehalf:
350 case Spacing::Double:
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);
366 dialog_->linespacingValue->setText("");
367 dialog_->linespacingValue->setEnabled(false);
371 setWidgetsFromVSpace(params.spaceTop(),
372 dialog_->spacingAbove,
375 dialog_->keepAbove,units_);
378 setWidgetsFromVSpace(params.spaceBottom(),
379 dialog_->spacingBelow,
382 dialog_->keepBelow,units_);