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.
14 #include "ControlParagraph.h"
15 #include "QParagraph.h"
16 #include "QParagraphDialog.h"
18 #include "ParagraphParameters.h"
19 #include "lyxrc.h" // to set the deafult length values
20 #include "qt_helpers.h"
21 #include "helper_funcs.h"
26 #include "support/lstrings.h"
27 #include "support/tostr.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>
36 using lyx::support::contains_functor;
37 using lyx::support::isStrDbl;
38 using lyx::support::subst;
39 using lyx::support::trim;
47 typedef QController<ControlParagraph, QView<QParagraphDialog> > base_class;
50 QParagraph::QParagraph(Dialog & parent)
51 : base_class(parent, _("LyX: Paragraph Settings"))
55 void QParagraph::build_dialog()
58 dialog_.reset(new QParagraphDialog(this));
60 // Create the contents of the unit choices
61 // Don't include the "%" terms...
62 units_ = getLatexUnits();
63 vector<string>::iterator del =
64 remove_if(units_.begin(), units_.end(),
65 bind2nd(contains_functor(), "%"));
66 units_.erase(del, units_.end());
68 for (vector<string>::const_iterator it = units_.begin();
69 it != units_.end(); ++it) {
70 dialog_->unitAbove->insertItem(toqstr(*it));
71 dialog_->unitBelow->insertItem(toqstr(*it));
74 // Manage the ok, apply, restore and cancel/close buttons
75 bcview().setOK(dialog_->okPB);
76 bcview().setApply(dialog_->applyPB);
77 bcview().setCancel(dialog_->closePB);
78 bcview().setRestore(dialog_->restorePB);
79 bcview().addReadOnly(dialog_->paragraphTab);
85 VSpace setVSpaceFromWidgets(int spacing,
94 space = VSpace(VSpace::NONE);
97 space = VSpace(VSpace::DEFSKIP);
100 space = VSpace(VSpace::SMALLSKIP);
103 space = VSpace(VSpace::MEDSKIP);
106 space = VSpace(VSpace::BIGSKIP);
109 space = VSpace(VSpace::VFILL);
113 string const length = trim(value);
114 if (isValidGlueLength(length)) {
116 } else if (!length.empty()){
117 string u = trim(unit);
118 u = subst(u, "%%", "%");
121 space = VSpace(LyXGlueLength(s));
133 void QParagraph::apply()
135 ParagraphParameters & params = controller().params();
138 // If a vspace kind is "Length" but there's no text in
139 // the input field, reset the kind to "None".
140 if (dialog_->spacingAbove->currentItem() == 6
141 && dialog_->valueAbove->text().isEmpty())
142 dialog_->spacingAbove->setCurrentItem(0);
144 VSpace const space_top =
145 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
146 fromqstr(dialog_->valueAbove->text()),
147 fromqstr(dialog_->unitAbove->currentText()),
148 dialog_->keepAbove->isChecked());
150 params.spaceTop(space_top);
153 if (dialog_->spacingBelow->currentItem() == 6
154 && dialog_->valueBelow->text().isEmpty())
155 dialog_->spacingBelow->setCurrentItem(0);
157 VSpace const space_bottom =
158 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
159 fromqstr(dialog_->valueBelow->text()),
160 fromqstr(dialog_->unitBelow->currentText()),
161 dialog_->keepBelow->isChecked());
163 params.spaceBottom(space_bottom);
167 switch (dialog_->align->currentItem()) {
169 align = LYX_ALIGN_BLOCK;
172 align = LYX_ALIGN_LEFT;
175 align = LYX_ALIGN_RIGHT;
178 align = LYX_ALIGN_CENTER;
181 align = LYX_ALIGN_BLOCK;
186 Spacing::Space linespacing = Spacing::Default;
188 switch (dialog_->linespacing->currentItem()) {
190 linespacing = Spacing::Default;
193 linespacing = Spacing::Single;
196 linespacing = Spacing::Onehalf;
199 linespacing = Spacing::Double;
202 linespacing = Spacing::Other;
203 other = fromqstr(dialog_->linespacingValue->text());
207 Spacing const spacing(linespacing, other);
208 params.spacing(spacing);
210 // lines and pagebreaks
211 params.lineTop(dialog_->lineAbove->isChecked());
212 params.lineBottom(dialog_->lineBelow->isChecked());
213 params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
214 params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
216 params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
218 params.noindent(!dialog_->indentCB->isChecked());
225 void setWidgetsFromVSpace(VSpace const & space,
229 QCheckBox * keep, vector<string> units_)
232 value->setEnabled(false);
233 unit->setEnabled(false);
236 switch (space.kind()) {
240 case VSpace::DEFSKIP:
243 case VSpace::SMALLSKIP:
246 case VSpace::MEDSKIP:
249 case VSpace::BIGSKIP:
257 value->setEnabled(true);
258 unit->setEnabled(true);
259 string length = space.length().asString();
260 string const default_unit =
261 (lyxrc.default_papersize > 3) ? "cm" : "in";
262 string supplied_unit = default_unit;
263 LyXLength len(length);
264 if ((isValidLength(length)
265 || isStrDbl(length)) && !len.zero()) {
266 length = tostr(len.value());
267 supplied_unit = subst(stringFromUnit(len.unit()),
273 for (vector<string>::const_iterator it = units_.begin();
274 it != units_.end(); ++it) {
275 if (*it == default_unit) {
278 if (*it == supplied_unit) {
284 value->setText(toqstr(length));
285 unit->setCurrentItem(unit_item);
288 spacing->setCurrentItem(item);
289 keep->setChecked(space.keep());
295 void QParagraph::update_contents()
297 ParagraphParameters const & params = controller().params();
300 string const & labelwidth = params.labelWidthString();
301 // _() is correct here (this is stupid though !)
302 if (labelwidth != _("Senseless with this layout!")) {
303 dialog_->labelwidthGB->setEnabled(true);
304 dialog_->labelWidth->setText(toqstr(labelwidth));
306 dialog_->labelwidthGB->setEnabled(false);
307 dialog_->labelWidth->setText("");
312 switch (params.align()) {
316 case LYX_ALIGN_RIGHT:
319 case LYX_ALIGN_CENTER:
326 dialog_->align->setCurrentItem(i);
329 //LyXAlignment alignpos = controller().alignPossible();
331 // no inset-text-owned paragraph may have pagebreaks
332 bool ininset = controller().inInset();
333 dialog_->pagebreakAbove->setEnabled(!ininset);
334 dialog_->pagebreakBelow->setEnabled(!ininset);
336 // lines, pagebreaks and indent
337 dialog_->lineAbove->setChecked(params.lineTop());
338 dialog_->lineBelow->setChecked(params.lineBottom());
339 dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
340 dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
341 dialog_->indentCB->setChecked(!params.noindent());
345 Spacing const & space = params.spacing();
346 switch (space.getSpace()) {
347 case Spacing::Single:
350 case Spacing::Onehalf:
353 case Spacing::Double:
363 dialog_->linespacing->setCurrentItem(linespacing);
364 if (space.getSpace() == Spacing::Other) {
365 string const sp = tostr(space.getValue());
366 dialog_->linespacingValue->setText(toqstr(sp));
367 dialog_->linespacingValue->setEnabled(true);
369 dialog_->linespacingValue->setText("");
370 dialog_->linespacingValue->setEnabled(false);
374 setWidgetsFromVSpace(params.spaceTop(),
375 dialog_->spacingAbove,
378 dialog_->keepAbove,units_);
381 setWidgetsFromVSpace(params.spaceBottom(),
382 dialog_->spacingBelow,
385 dialog_->keepBelow,units_);