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
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(_("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(it->c_str());
69 dialog_->unitBelow->insertItem(it->c_str());
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 string(dialog_->valueAbove->text()),
145 string(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 string(dialog_->valueBelow->text()),
158 string(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 = 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(string(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(length.c_str());
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(labelwidth.c_str());
302 dialog_->labelwidthGB->setEnabled(
303 labelwidth != _("Senseless with this layout!"));
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_->noindent->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(sp.c_str());
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_);
383 dialog_->noindent->setChecked(params.noindent());