2 * \file GuiParagraph.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
9 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
16 #include "GuiParagraph.h"
18 #include "qt_helpers.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
24 #include "FuncRequest.h"
27 #include "Paragraph.h"
28 #include "ParagraphParameters.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
36 #include <QPushButton>
47 GuiParagraph::GuiParagraph(GuiView & lv)
48 : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
52 connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
53 connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
54 connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
55 connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
56 connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
57 connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
58 connect(linespacingValue, SIGNAL(textChanged(QString)),
59 this, SLOT(changed()));
60 connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
61 connect(labelWidth, SIGNAL(textChanged(QString)),
62 this, SLOT(changed()));
65 // On Mac it's common to have tool windows which are always in the
66 // foreground and are hidden when the main window is not focused.
67 setWindowFlags(Qt::Tool);
68 synchronizedViewCB->setChecked(true);
69 closePB->setText(qt_("&Cancel"));
71 synchronizedViewCB->setChecked(false);
74 on_synchronizedViewCB_toggled();
75 QDoubleValidator * val = new QDoubleValidator(linespacingValue);
76 val->setNotation(QDoubleValidator::StandardNotation);
77 linespacingValue->setValidator(val);
79 labelWidth->setWhatsThis(qt_(
80 "As described in the User Guide, the width of"
81 " this text determines the width of the label part"
82 " of each item in environments like List and"
85 " Normally, you won't need to set this,"
86 " since the largest label width of all the"
90 radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
91 radioMap_[LYX_ALIGN_BLOCK] = alignJustRB;
92 radioMap_[LYX_ALIGN_LEFT] = alignLeftRB;
93 radioMap_[LYX_ALIGN_RIGHT] = alignRightRB;
94 radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
96 alignDefaultLabel_ = alignDefaultRB->text();
100 void GuiParagraph::on_linespacing_activated(int index)
102 linespacingValue->setEnabled(index == 4);
106 void GuiParagraph::checkAlignmentRadioButtons()
108 static std::map<LyXAlignment, QString> labelMap_;
109 if (labelMap_.empty()) {
110 labelMap_[LYX_ALIGN_BLOCK] = qt_("Justified");
111 labelMap_[LYX_ALIGN_LEFT] = qt_("Left");
112 labelMap_[LYX_ALIGN_RIGHT] = qt_("Right");
113 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
116 RadioMap::iterator it = radioMap_.begin();
117 for (; it != radioMap_.end(); ++it) {
118 LyXAlignment const align = it->first;
119 it->second->setEnabled(align & alignPossible());
121 if (haveMultiParSelection())
122 alignDefaultRB->setText(alignDefaultLabel_);
124 alignDefaultRB->setText(alignDefaultLabel_ + " ("
125 + labelMap_[alignDefault()] + ")");
129 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
131 RadioMap::const_iterator it = radioMap_.begin();
132 for (;it != radioMap_.end(); ++it) {
133 it->second->blockSignals(true);
134 it->second->setChecked(align == it->first);
135 it->second->blockSignals(false);
140 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
142 LyXAlignment alignment = LYX_ALIGN_NONE;
143 RadioMap::const_iterator it = radioMap_.begin();
144 for (; it != radioMap_.end(); ++it) {
145 if (it->second->isChecked()) {
146 alignment = it->first;
154 void GuiParagraph::on_synchronizedViewCB_toggled()
156 bool in_sync = synchronizedViewCB->isChecked();
157 restorePB->setEnabled(!in_sync);
158 applyPB->setEnabled(!in_sync);
159 okPB->setEnabled(!in_sync);
161 closePB->setText(qt_("&Cancel"));
163 closePB->setText(qt_("&Close"));
167 void GuiParagraph::changed()
170 // We apply immediately, except if we have custom line spacing
171 // with an intermediate result (trailing decimal separator) or
172 // an invalid value (which might as well be intermediate)
173 if (synchronizedViewCB->isChecked()
174 && (linespacing->currentIndex() != 4
175 || (!linespacingValue->text().endsWith(loc.decimalPoint())
176 && linespacingValue->hasAcceptableInput())))
177 on_applyPB_clicked();
181 void GuiParagraph::on_applyPB_clicked()
187 void GuiParagraph::on_okPB_clicked()
194 void GuiParagraph::on_closePB_clicked()
200 void GuiParagraph::on_restorePB_clicked()
206 void GuiParagraph::applyView()
210 params_.align(getAlignmentFromDialog());
213 Spacing::Space ls = Spacing::Default;
215 switch (linespacing->currentIndex()) {
217 ls = Spacing::Default;
220 ls = Spacing::Single;
223 ls = Spacing::Onehalf;
226 ls = Spacing::Double;
230 other = widgetToDoubleStr(linespacingValue);
234 Spacing const spacing(ls, other);
235 params_.spacing(spacing);
238 params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
240 params_.noindent(!indentCB->isChecked());
246 void GuiParagraph::updateView()
248 on_synchronizedViewCB_toggled();
250 ParagraphParameters const & pp = params();
253 docstring const & labelwidth = pp.labelWidthString();
254 if (hasLabelwidth()) {
255 labelwidthGB->setEnabled(true);
256 labelWidth->setText(toqstr(labelwidth));
258 labelwidthGB->setEnabled(false);
259 labelWidth->setText(QString());
263 checkAlignmentRadioButtons();
264 alignmentToRadioButtons(pp.align());
267 bool const canindent = canIndent();
268 indentCB->setEnabled(canindent);
269 indentCB->setChecked(canindent && !pp.noindent());
273 bool pending_input = false;
274 Spacing const & space = pp.spacing();
275 if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
276 // The user is about to enter custom spacing.
277 // We thus stay in Custom mode.
278 // This prevents the combo from e.g. immediately
279 // switching to single if a user enters "1" in the
280 // linespacingValue widget while aiming at e.g. "1.3"
282 pending_input = true;
284 switch (space.getSpace()) {
285 case Spacing::Single:
288 case Spacing::Onehalf:
291 case Spacing::Double:
302 linespacing->setCurrentIndex(ls);
303 if (space.getSpace() == Spacing::Other || pending_input) {
304 doubleToWidget(linespacingValue, space.getValue());
305 linespacingValue->setEnabled(true);
307 linespacingValue->setText(QString());
308 linespacingValue->setEnabled(false);
313 void GuiParagraph::enableView(bool enable)
315 indentCB->setEnabled(enable);
316 linespacing->setEnabled(enable);
317 labelWidth->setEnabled(enable);
318 synchronizedViewCB->setEnabled(enable);
319 applyPB->setEnabled(enable);
320 restorePB->setEnabled(enable);
322 synchronizedViewCB->setChecked(true);
323 RadioMap::const_iterator it = radioMap_.begin();
324 for (; it != radioMap_.end(); ++it)
325 it->second->setEnabled(enable);
329 ParagraphParameters const & GuiParagraph::params() const
331 if (haveMultiParSelection()) {
332 // FIXME: in case of multi-paragraph selection, it would be nice to
333 // initialise the parameters that are common to all paragraphs.
334 static ParagraphParameters const empty;
337 return bufferview()->cursor().innerParagraph().params();
341 void GuiParagraph::dispatchParams()
345 FuncRequest const fr(getLfun(), data.str());
350 bool GuiParagraph::haveMultiParSelection() const
352 Cursor const & cur = bufferview()->cursor();
353 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
357 bool GuiParagraph::canIndent() const
359 Layout const lay = bufferview()->cursor().innerParagraph().layout();
360 if (buffer().params().paragraph_separation
361 == BufferParams::ParagraphIndentSeparation)
362 return (lay.toggle_indent != ITOGGLE_NEVER);
363 return (lay.toggle_indent == ITOGGLE_ALWAYS);
367 LyXAlignment GuiParagraph::alignPossible() const
369 return bufferview()->cursor().innerParagraph().layout().alignpossible;
373 LyXAlignment GuiParagraph::alignDefault() const
375 return bufferview()->cursor().innerParagraph().layout().align;
379 bool GuiParagraph::hasLabelwidth() const
381 Layout layout = bufferview()->cursor().innerParagraph().layout();
382 return (layout.margintype == MARGIN_MANUAL
383 || layout.latextype == LATEX_BIB_ENVIRONMENT);
387 void GuiParagraph::saveSession() const
389 Dialog::saveSession();
391 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
395 void GuiParagraph::restoreSession()
397 Dialog::restoreSession();
399 synchronizedViewCB->setChecked(
400 settings.value(sessionKey() + "/autoapply").toBool());
404 Dialog * createGuiParagraph(GuiView & lv)
406 return new GuiParagraph(lv);
410 } // namespace frontend
413 #include "moc_GuiParagraph.cpp"