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 #pragma implementation
17 #include "ControlParagraph.h"
18 #include "QParagraph.h"
19 #include "QParagraphDialog.h"
21 #include "ParagraphParameters.h"
22 #include "lyxrc.h" // to set the deafult length values
23 #include "qt_helpers.h"
24 #include "layout.h" // LyXAlignment
25 #include "helper_funcs.h"
26 #include "lyxgluelength.h"
29 #include "support/lstrings.h"
30 #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>
45 typedef Qt2CB<ControlParagraph, Qt2DB<QParagraphDialog> > base_class;
48 QParagraph::QParagraph()
49 : base_class(qt_("Paragraph Layout"))
53 void QParagraph::build_dialog()
56 dialog_.reset(new QParagraphDialog(this));
58 // Create the contents of the unit choices
59 // Don't include the "%" terms...
60 units_ = getLatexUnits();
61 vector<string>::iterator del =
62 remove_if(units_.begin(), units_.end(),
63 bind2nd(contains_functor(), "%"));
64 units_.erase(del, units_.end());
66 for (vector<string>::const_iterator it = units_.begin();
67 it != units_.end(); ++it) {
68 dialog_->unitAbove->insertItem(toqstr(*it));
69 dialog_->unitBelow->insertItem(toqstr(*it));
72 // Manage the ok, apply, restore and cancel/close buttons
73 bc().setOK(dialog_->okPB);
74 bc().setApply(dialog_->applyPB);
75 bc().setCancel(dialog_->closePB);
76 bc().setRestore(dialog_->restorePB);
77 bc().addReadOnly(dialog_->paragraphTab);
83 VSpace setVSpaceFromWidgets(int spacing,
92 space = VSpace(VSpace::NONE);
95 space = VSpace(VSpace::DEFSKIP);
98 space = VSpace(VSpace::SMALLSKIP);
101 space = VSpace(VSpace::MEDSKIP);
104 space = VSpace(VSpace::BIGSKIP);
107 space = VSpace(VSpace::VFILL);
111 string const length = trim(value);
112 if (isValidGlueLength(length)) {
114 } else if (!length.empty()){
115 string u = trim(unit);
116 u = subst(u, "%%", "%");
119 space = VSpace(LyXGlueLength(s));
131 void QParagraph::apply()
133 ParagraphParameters & params = controller().params();
136 // If a vspace kind is "Length" but there's no text in
137 // the input field, reset the kind to "None".
138 if (dialog_->spacingAbove->currentItem() == 5
139 && dialog_->valueAbove->text().isEmpty())
140 dialog_->spacingAbove->setCurrentItem(0);
142 VSpace const space_top =
143 setVSpaceFromWidgets(dialog_->spacingAbove->currentItem(),
144 fromqstr(dialog_->valueAbove->text()),
145 fromqstr(dialog_->unitAbove->currentText()),
146 dialog_->keepAbove->isChecked());
148 params.spaceTop(space_top);
151 if (dialog_->spacingBelow->currentItem() == 5
152 && dialog_->valueBelow->text().isEmpty())
153 dialog_->spacingBelow->setCurrentItem(0);
155 VSpace const space_bottom =
156 setVSpaceFromWidgets(dialog_->spacingBelow->currentItem(),
157 fromqstr(dialog_->valueBelow->text()),
158 fromqstr(dialog_->unitBelow->currentText()),
159 dialog_->keepBelow->isChecked());
161 params.spaceBottom(space_bottom);
165 switch (dialog_->align->currentItem()) {
167 align = LYX_ALIGN_BLOCK;
170 align = LYX_ALIGN_LEFT;
173 align = LYX_ALIGN_RIGHT;
176 align = LYX_ALIGN_CENTER;
179 align = LYX_ALIGN_BLOCK;
184 Spacing::Space linespacing = Spacing::Default;
186 switch (dialog_->linespacing->currentItem()) {
188 linespacing = Spacing::Default;
191 linespacing = Spacing::Single;
194 linespacing = Spacing::Onehalf;
197 linespacing = Spacing::Double;
200 linespacing = Spacing::Other;
201 other = fromqstr(dialog_->linespacingValue->text());
205 Spacing const spacing(linespacing, other);
206 params.spacing(spacing);
208 // lines and pagebreaks
209 params.lineTop(dialog_->lineAbove->isChecked());
210 params.lineBottom(dialog_->lineBelow->isChecked());
211 params.pagebreakTop(dialog_->pagebreakAbove->isChecked());
212 params.pagebreakBottom(dialog_->pagebreakBelow->isChecked());
214 params.labelWidthString(fromqstr(dialog_->labelWidth->text()));
216 params.noindent(dialog_->noindent->isChecked());
223 void setWidgetsFromVSpace(VSpace const & space,
227 QCheckBox * keep, vector<string> units_)
230 value->setEnabled(false);
231 unit->setEnabled(false);
234 switch (space.kind()) {
238 case VSpace::DEFSKIP:
241 case VSpace::SMALLSKIP:
244 case VSpace::MEDSKIP:
247 case VSpace::BIGSKIP:
255 value->setEnabled(true);
256 unit->setEnabled(true);
257 string length = space.length().asString();
258 string const default_unit =
259 (lyxrc.default_papersize > 3) ? "cm" : "in";
260 string supplied_unit = default_unit;
261 LyXLength len(length);
262 if ((isValidLength(length)
263 || isStrDbl(length)) && !len.zero()) {
264 ostringstream buffer;
265 buffer << len.value();
266 length = buffer.str();
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 dialog_->labelWidth->setText(toqstr(labelwidth));
302 // _() is correct here (this is stupid though !)
303 dialog_->labelwidthGB->setEnabled(
304 labelwidth != _("Senseless with this layout!"));
308 switch (params.align()) {
312 case LYX_ALIGN_RIGHT:
315 case LYX_ALIGN_CENTER:
322 dialog_->align->setCurrentItem(i);
325 //LyXAlignment alignpos = controller().alignPossible();
327 // no inset-text-owned paragraph may have pagebreaks
328 bool ininset = controller().inInset();
329 dialog_->pagebreakAbove->setEnabled(!ininset);
330 dialog_->pagebreakBelow->setEnabled(!ininset);
332 // lines, pagebreaks and indent
333 dialog_->lineAbove->setChecked(params.lineTop());
334 dialog_->lineBelow->setChecked(params.lineBottom());
335 dialog_->pagebreakAbove->setChecked(params.pagebreakTop());
336 dialog_->pagebreakBelow->setChecked(params.pagebreakBottom());
337 dialog_->noindent->setChecked(params.noindent());
341 Spacing const & space = params.spacing();
342 switch (space.getSpace()) {
343 case Spacing::Single:
346 case Spacing::Onehalf:
349 case Spacing::Double:
359 dialog_->linespacing->setCurrentItem(linespacing);
360 if (space.getSpace() == Spacing::Other) {
361 string const sp = tostr(space.getValue());
362 dialog_->linespacingValue->setText(toqstr(sp));
363 dialog_->linespacingValue->setEnabled(true);
365 dialog_->linespacingValue->setText("");
366 dialog_->linespacingValue->setEnabled(false);
370 setWidgetsFromVSpace(params.spaceTop(),
371 dialog_->spacingAbove,
374 dialog_->keepAbove,units_);
377 setWidgetsFromVSpace(params.spaceBottom(),
378 dialog_->spacingBelow,
381 dialog_->keepBelow,units_);
384 dialog_->noindent->setChecked(params.noindent());