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"
35 #include <QDialogButtonBox>
37 #include <QPushButton>
48 GuiParagraph::GuiParagraph(GuiView & lv)
49 : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
53 connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
54 connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
55 connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
56 connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
57 connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
58 connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
59 connect(linespacingValue, SIGNAL(textChanged(QString)),
60 this, SLOT(changed()));
61 connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
62 connect(labelWidth, SIGNAL(textChanged(QString)),
63 this, SLOT(changed()));
66 // On Mac it's common to have tool windows which are always in the
67 // foreground and are hidden when the main window is not focused.
68 setWindowFlags(Qt::Tool);
69 synchronizedViewCB->setChecked(true);
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 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
158 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
159 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
161 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
163 buttonBox->button(QDialogButtonBox::Cancel)->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())))
181 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
183 switch (buttonBox->standardButton(button)) {
184 case QDialogButtonBox::Ok:
188 case QDialogButtonBox::Apply:
191 case QDialogButtonBox::Cancel:
194 case QDialogButtonBox::Reset:
203 void GuiParagraph::applyView()
207 params_.align(getAlignmentFromDialog());
210 Spacing::Space ls = Spacing::Default;
212 switch (linespacing->currentIndex()) {
214 ls = Spacing::Default;
217 ls = Spacing::Single;
220 ls = Spacing::Onehalf;
223 ls = Spacing::Double;
227 other = widgetToDoubleStr(linespacingValue);
231 Spacing const spacing(ls, other);
232 params_.spacing(spacing);
235 params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
237 params_.noindent(!indentCB->isChecked());
243 void GuiParagraph::updateView()
245 on_synchronizedViewCB_toggled();
247 ParagraphParameters const & pp = params();
250 docstring const & labelwidth = pp.labelWidthString();
251 if (hasLabelwidth()) {
252 labelwidthGB->setEnabled(true);
253 labelWidth->setText(toqstr(labelwidth));
255 labelwidthGB->setEnabled(false);
256 labelWidth->setText(QString());
260 checkAlignmentRadioButtons();
261 alignmentToRadioButtons(pp.align());
264 bool const canindent = canIndent();
265 indentCB->setEnabled(canindent);
266 indentCB->setChecked(canindent && !pp.noindent());
270 bool pending_input = false;
271 Spacing const & space = pp.spacing();
272 if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
273 // The user is about to enter custom spacing.
274 // We thus stay in Custom mode.
275 // This prevents the combo from e.g. immediately
276 // switching to single if a user enters "1" in the
277 // linespacingValue widget while aiming at e.g. "1.3"
279 pending_input = true;
281 switch (space.getSpace()) {
282 case Spacing::Single:
285 case Spacing::Onehalf:
288 case Spacing::Double:
299 linespacing->setCurrentIndex(ls);
300 if (space.getSpace() == Spacing::Other || pending_input) {
301 doubleToWidget(linespacingValue, space.getValue());
302 linespacingValue->setEnabled(true);
304 linespacingValue->setText(QString());
305 linespacingValue->setEnabled(false);
310 void GuiParagraph::enableView(bool enable)
312 indentCB->setEnabled(enable);
313 linespacing->setEnabled(enable);
314 labelWidth->setEnabled(enable);
315 synchronizedViewCB->setEnabled(enable);
316 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
317 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
319 synchronizedViewCB->setChecked(true);
320 RadioMap::const_iterator it = radioMap_.begin();
321 for (; it != radioMap_.end(); ++it)
322 it->second->setEnabled(enable);
326 ParagraphParameters const & GuiParagraph::params() const
328 if (haveMultiParSelection()) {
329 // FIXME: in case of multi-paragraph selection, it would be nice to
330 // initialise the parameters that are common to all paragraphs.
331 static ParagraphParameters const empty;
334 return bufferview()->cursor().innerParagraph().params();
338 void GuiParagraph::dispatchParams()
342 FuncRequest const fr(getLfun(), os.str());
347 bool GuiParagraph::haveMultiParSelection() const
349 Cursor const & cur = bufferview()->cursor();
350 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
354 bool GuiParagraph::canIndent() const
356 Layout const lay = bufferview()->cursor().innerParagraph().layout();
357 if (buffer().params().paragraph_separation
358 == BufferParams::ParagraphIndentSeparation)
359 return (lay.toggle_indent != ITOGGLE_NEVER);
360 return (lay.toggle_indent == ITOGGLE_ALWAYS);
364 LyXAlignment GuiParagraph::alignPossible() const
366 return bufferview()->cursor().innerParagraph().layout().alignpossible;
370 LyXAlignment GuiParagraph::alignDefault() const
372 return bufferview()->cursor().innerParagraph().layout().align;
376 bool GuiParagraph::hasLabelwidth() const
378 Layout layout = bufferview()->cursor().innerParagraph().layout();
379 return (layout.margintype == MARGIN_MANUAL
380 || layout.latextype == LATEX_BIB_ENVIRONMENT);
384 void GuiParagraph::saveSession(QSettings & settings) const
386 Dialog::saveSession(settings);
387 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
391 void GuiParagraph::restoreSession()
393 Dialog::restoreSession();
395 synchronizedViewCB->setChecked(
396 settings.value(sessionKey() + "/autoapply").toBool());
400 Dialog * createGuiParagraph(GuiView & lv)
402 return new GuiParagraph(lv);
406 } // namespace frontend
409 #include "moc_GuiParagraph.cpp"