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>
42 typedef QController<ControlParagraph, QView<QParagraphDialog> > base_class;
45 QParagraph::QParagraph(Dialog & parent)
46 : base_class(parent, _("LyX: Paragraph Settings"))
50 void QParagraph::build_dialog()
53 dialog_.reset(new QParagraphDialog(this));
55 // Create the contents of the unit choices
56 // Don't include the "%" terms...
57 units_ = getLatexUnits();
58 vector<string>::iterator del =
59 remove_if(units_.begin(), units_.end(),
60 bind2nd(contains_functor(), "%"));
61 units_.erase(del, units_.end());
63 for (vector<string>::const_iterator it = units_.begin();
64 it != units_.end(); ++it) {
65 dialog_->unitAbove->insertItem(toqstr(*it));
66 dialog_->unitBelow->insertItem(toqstr(*it));
69 // Manage the ok, apply, restore and cancel/close buttons
70 bcview().setOK(dialog_->okPB);
71 bcview().setApply(dialog_->applyPB);
72 bcview().setCancel(dialog_->closePB);
73 bcview().setRestore(dialog_->restorePB);
74 bcview().addReadOnly(dialog_->paragraphTab);
80 VSpace setVSpaceFromWidgets(int spacing,
89 space = VSpace(VSpace::NONE);
92 space = VSpace(VSpace::DEFSKIP);
95 space = VSpace(VSpace::SMALLSKIP);
98 space = VSpace(VSpace::MEDSKIP);
101 space = VSpace(VSpace::BIGSKIP);
104 space = VSpace(VSpace::VFILL);
108 string const length = trim(value);
109 if (isValidGlueLength(length)) {
111 } else if (!length.empty()){
112 string u = trim(unit);
113 u = subst(u, "%%", "%");
116 space = VSpace(LyXGlueLength(s));
128 void QParagraph::apply()
130 ParagraphParameters & params = controller().params();
133 // If a vspace kind is "Length" but there's no text in
134 // the input field, reset the kind to "None".
135 if (dialog_->spacingAbove->currentItem() == 6
136 && dialog_->valueAbove->text().isEmpty())
137 dialog_->spacingAbove->setCurrentItem(0);
139 VSpace const space_top =
140 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
141 fromqstr(dialog_->valueAbove->text()),
142 fromqstr(dialog_->unitAbove->currentText()),
143 dialog_->keepAbove->isChecked());
145 params.spaceTop(space_top);
148 if (dialog_->spacingBelow->currentItem() == 6
149 && dialog_->valueBelow->text().isEmpty())
150 dialog_->spacingBelow->setCurrentItem(0);
152 VSpace const space_bottom =
153 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
154 fromqstr(dialog_->valueBelow->text()),
155 fromqstr(dialog_->unitBelow->currentText()),
156 dialog_->keepBelow->isChecked());
158 params.spaceBottom(space_bottom);
162 switch (dialog_->align->currentItem()) {
164 align = LYX_ALIGN_BLOCK;
167 align = LYX_ALIGN_LEFT;
170 align = LYX_ALIGN_RIGHT;
173 align = LYX_ALIGN_CENTER;
176 align = LYX_ALIGN_BLOCK;
181 Spacing::Space linespacing = Spacing::Default;
183 switch (dialog_->linespacing->currentItem()) {
185 linespacing = Spacing::Default;
188 linespacing = Spacing::Single;
191 linespacing = Spacing::Onehalf;
194 linespacing = Spacing::Double;
197 linespacing = Spacing::Other;
198 other = fromqstr(dialog_->linespacingValue->text());
202 Spacing const spacing(linespacing, other);
203 params.spacing(spacing);
205 // lines and pagebreaks
206 params.lineTop(dialog_->lineAbove->isChecked());
207 params.lineBottom(dialog_->lineBelow->isChecked());
208 params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
209 params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
211 params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
213 params.noindent(!dialog_->indentCB->isChecked());
220 void setWidgetsFromVSpace(VSpace const & space,
224 QCheckBox * keep, vector<string> units_)
227 value->setEnabled(false);
228 unit->setEnabled(false);
231 switch (space.kind()) {
235 case VSpace::DEFSKIP:
238 case VSpace::SMALLSKIP:
241 case VSpace::MEDSKIP:
244 case VSpace::BIGSKIP:
252 value->setEnabled(true);
253 unit->setEnabled(true);
254 string length = space.length().asString();
255 string const default_unit =
256 (lyxrc.default_papersize > 3) ? "cm" : "in";
257 string supplied_unit = default_unit;
258 LyXLength len(length);
259 if ((isValidLength(length)
260 || isStrDbl(length)) && !len.zero()) {
261 length = tostr(len.value());
262 supplied_unit = subst(stringFromUnit(len.unit()),
268 for (vector<string>::const_iterator it = units_.begin();
269 it != units_.end(); ++it) {
270 if (*it == default_unit) {
273 if (*it == supplied_unit) {
279 value->setText(toqstr(length));
280 unit->setCurrentItem(unit_item);
283 spacing->setCurrentItem(item);
284 keep->setChecked(space.keep());
290 void QParagraph::update_contents()
292 ParagraphParameters const & params = controller().params();
295 string const & labelwidth = params.labelWidthString();
296 // _() is correct here (this is stupid though !)
297 if (labelwidth != _("Senseless with this layout!")) {
298 dialog_->labelwidthGB->setEnabled(true);
299 dialog_->labelWidth->setText(toqstr(labelwidth));
301 dialog_->labelwidthGB->setEnabled(false);
302 dialog_->labelWidth->setText("");
307 switch (params.align()) {
311 case LYX_ALIGN_RIGHT:
314 case LYX_ALIGN_CENTER:
321 dialog_->align->setCurrentItem(i);
324 //LyXAlignment alignpos = controller().alignPossible();
326 // no inset-text-owned paragraph may have pagebreaks
327 bool ininset = controller().inInset();
328 dialog_->pagebreakAbove->setEnabled(!ininset);
329 dialog_->pagebreakBelow->setEnabled(!ininset);
331 // lines, pagebreaks and indent
332 dialog_->lineAbove->setChecked(params.lineTop());
333 dialog_->lineBelow->setChecked(params.lineBottom());
334 dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
335 dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
336 dialog_->indentCB->setChecked(!params.noindent());
340 Spacing const & space = params.spacing();
341 switch (space.getSpace()) {
342 case Spacing::Single:
345 case Spacing::Onehalf:
348 case Spacing::Double:
358 dialog_->linespacing->setCurrentItem(linespacing);
359 if (space.getSpace() == Spacing::Other) {
360 string const sp = tostr(space.getValue());
361 dialog_->linespacingValue->setText(toqstr(sp));
362 dialog_->linespacingValue->setEnabled(true);
364 dialog_->linespacingValue->setText("");
365 dialog_->linespacingValue->setEnabled(false);
369 setWidgetsFromVSpace(params.spaceTop(),
370 dialog_->spacingAbove,
373 dialog_->keepAbove,units_);
376 setWidgetsFromVSpace(params.spaceBottom(),
377 dialog_->spacingBelow,
380 dialog_->keepBelow,units_);