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 "helper_funcs.h"
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
28 #include <qcombobox.h>
29 #include <qlineedit.h>
30 #include <qcheckbox.h>
31 #include <qpushbutton.h>
32 #include <qtabwidget.h>
33 #include <qbuttongroup.h>
35 using lyx::support::contains_functor;
36 using lyx::support::isStrDbl;
37 using lyx::support::subst;
38 using lyx::support::trim;
46 typedef QController<ControlParagraph, QView<QParagraphDialog> > base_class;
49 QParagraph::QParagraph(Dialog & parent)
50 : base_class(parent, _("LyX: Paragraph Settings"))
54 void QParagraph::build_dialog()
57 dialog_.reset(new QParagraphDialog(this));
59 // Create the contents of the unit choices
60 // Don't include the "%" terms...
61 units_ = getLatexUnits();
62 vector<string>::iterator del =
63 remove_if(units_.begin(), units_.end(),
64 bind2nd(contains_functor(), "%"));
65 units_.erase(del, units_.end());
67 for (vector<string>::const_iterator it = units_.begin();
68 it != units_.end(); ++it) {
69 dialog_->unitAbove->insertItem(toqstr(*it));
70 dialog_->unitBelow->insertItem(toqstr(*it));
73 // Manage the ok, apply, restore and cancel/close buttons
74 bcview().setOK(dialog_->okPB);
75 bcview().setApply(dialog_->applyPB);
76 bcview().setCancel(dialog_->closePB);
77 bcview().setRestore(dialog_->restorePB);
78 bcview().addReadOnly(dialog_->paragraphTab);
84 VSpace setVSpaceFromWidgets(int spacing,
93 space = VSpace(VSpace::NONE);
96 space = VSpace(VSpace::DEFSKIP);
99 space = VSpace(VSpace::SMALLSKIP);
102 space = VSpace(VSpace::MEDSKIP);
105 space = VSpace(VSpace::BIGSKIP);
108 space = VSpace(VSpace::VFILL);
112 string const length = trim(value);
113 if (isValidGlueLength(length)) {
115 } else if (!length.empty()){
116 string u = trim(unit);
117 u = subst(u, "%%", "%");
120 space = VSpace(LyXGlueLength(s));
132 void QParagraph::apply()
134 ParagraphParameters & params = controller().params();
137 // If a vspace kind is "Length" but there's no text in
138 // the input field, reset the kind to "None".
139 if (dialog_->spacingAbove->currentItem() == 6
140 && dialog_->valueAbove->text().isEmpty())
141 dialog_->spacingAbove->setCurrentItem(0);
143 VSpace const space_top =
144 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
145 fromqstr(dialog_->valueAbove->text()),
146 fromqstr(dialog_->unitAbove->currentText()),
147 dialog_->keepAbove->isChecked());
149 params.spaceTop(space_top);
152 if (dialog_->spacingBelow->currentItem() == 6
153 && dialog_->valueBelow->text().isEmpty())
154 dialog_->spacingBelow->setCurrentItem(0);
156 VSpace const space_bottom =
157 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
158 fromqstr(dialog_->valueBelow->text()),
159 fromqstr(dialog_->unitBelow->currentText()),
160 dialog_->keepBelow->isChecked());
162 params.spaceBottom(space_bottom);
166 switch (dialog_->align->currentItem()) {
168 align = LYX_ALIGN_BLOCK;
171 align = LYX_ALIGN_LEFT;
174 align = LYX_ALIGN_RIGHT;
177 align = LYX_ALIGN_CENTER;
180 align = LYX_ALIGN_BLOCK;
185 Spacing::Space linespacing = Spacing::Default;
187 switch (dialog_->linespacing->currentItem()) {
189 linespacing = Spacing::Default;
192 linespacing = Spacing::Single;
195 linespacing = Spacing::Onehalf;
198 linespacing = Spacing::Double;
201 linespacing = Spacing::Other;
202 other = fromqstr(dialog_->linespacingValue->text());
206 Spacing const spacing(linespacing, other);
207 params.spacing(spacing);
209 // lines and pagebreaks
210 params.lineTop(dialog_->lineAbove->isChecked());
211 params.lineBottom(dialog_->lineBelow->isChecked());
212 params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
213 params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
215 params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
217 params.noindent(!dialog_->indentCB->isChecked());
224 void setWidgetsFromVSpace(VSpace const & space,
228 QCheckBox * keep, vector<string> units_)
231 value->setEnabled(false);
232 unit->setEnabled(false);
235 switch (space.kind()) {
239 case VSpace::DEFSKIP:
242 case VSpace::SMALLSKIP:
245 case VSpace::MEDSKIP:
248 case VSpace::BIGSKIP:
256 value->setEnabled(true);
257 unit->setEnabled(true);
258 string length = space.length().asString();
259 string const default_unit =
260 (lyxrc.default_papersize > 3) ? "cm" : "in";
261 string supplied_unit = default_unit;
262 LyXLength len(length);
263 if ((isValidLength(length)
264 || isStrDbl(length)) && !len.zero()) {
265 length = tostr(len.value());
266 supplied_unit = subst(stringFromUnit(len.unit()),
272 for (vector<string>::const_iterator it = units_.begin();
273 it != units_.end(); ++it) {
274 if (*it == default_unit) {
277 if (*it == supplied_unit) {
283 value->setText(toqstr(length));
284 unit->setCurrentItem(unit_item);
287 spacing->setCurrentItem(item);
288 keep->setChecked(space.keep());
294 void QParagraph::update_contents()
296 ParagraphParameters const & params = controller().params();
299 string const & labelwidth = params.labelWidthString();
300 // _() is correct here (this is stupid though !)
301 if (labelwidth != _("Senseless with this layout!")) {
302 dialog_->labelwidthGB->setEnabled(true);
303 dialog_->labelWidth->setText(toqstr(labelwidth));
305 dialog_->labelwidthGB->setEnabled(false);
306 dialog_->labelWidth->setText("");
311 switch (params.align()) {
315 case LYX_ALIGN_RIGHT:
318 case LYX_ALIGN_CENTER:
325 dialog_->align->setCurrentItem(i);
328 //LyXAlignment alignpos = controller().alignPossible();
330 // no inset-text-owned paragraph may have pagebreaks
331 bool ininset = controller().inInset();
332 dialog_->pagebreakAbove->setEnabled(!ininset);
333 dialog_->pagebreakBelow->setEnabled(!ininset);
335 // lines, pagebreaks and indent
336 dialog_->lineAbove->setChecked(params.lineTop());
337 dialog_->lineBelow->setChecked(params.lineBottom());
338 dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
339 dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
340 dialog_->indentCB->setChecked(!params.noindent());
344 Spacing const & space = params.spacing();
345 switch (space.getSpace()) {
346 case Spacing::Single:
349 case Spacing::Onehalf:
352 case Spacing::Double:
362 dialog_->linespacing->setCurrentItem(linespacing);
363 if (space.getSpace() == Spacing::Other) {
364 string const sp = tostr(space.getValue());
365 dialog_->linespacingValue->setText(toqstr(sp));
366 dialog_->linespacingValue->setEnabled(true);
368 dialog_->linespacingValue->setText("");
369 dialog_->linespacingValue->setEnabled(false);
373 setWidgetsFromVSpace(params.spaceTop(),
374 dialog_->spacingAbove,
377 dialog_->keepAbove,units_);
380 setWidgetsFromVSpace(params.spaceBottom(),
381 dialog_->spacingBelow,
384 dialog_->keepBelow,units_);