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);
70 closePB->setText(qt_("&Cancel"));
72 synchronizedViewCB->setChecked(false);
75 on_synchronizedViewCB_toggled();
76 QDoubleValidator * val = new QDoubleValidator(linespacingValue);
77 val->setNotation(QDoubleValidator::StandardNotation);
78 linespacingValue->setValidator(val);
80 labelWidth->setWhatsThis(qt_(
81 "As described in the User Guide, the width of"
82 " this text determines the width of the label part"
83 " of each item in environments like List and"
86 " Normally, you won't need to set this,"
87 " since the largest label width of all the"
91 radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
92 radioMap_[LYX_ALIGN_BLOCK] = alignJustRB;
93 radioMap_[LYX_ALIGN_LEFT] = alignLeftRB;
94 radioMap_[LYX_ALIGN_RIGHT] = alignRightRB;
95 radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
97 alignDefaultLabel_ = alignDefaultRB->text();
101 void GuiParagraph::on_linespacing_activated(int index)
103 linespacingValue->setEnabled(index == 4);
107 void GuiParagraph::checkAlignmentRadioButtons()
109 static std::map<LyXAlignment, QString> labelMap_;
110 if (labelMap_.empty()) {
111 labelMap_[LYX_ALIGN_BLOCK] = qt_("Justified");
112 labelMap_[LYX_ALIGN_LEFT] = qt_("Left");
113 labelMap_[LYX_ALIGN_RIGHT] = qt_("Right");
114 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
117 RadioMap::iterator it = radioMap_.begin();
118 for (; it != radioMap_.end(); ++it) {
119 LyXAlignment const align = it->first;
120 it->second->setEnabled(align & alignPossible());
122 if (haveMultiParSelection())
123 alignDefaultRB->setText(alignDefaultLabel_);
125 alignDefaultRB->setText(alignDefaultLabel_ + " ("
126 + labelMap_[alignDefault()] + ")");
130 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
132 RadioMap::const_iterator it = radioMap_.begin();
133 for (;it != radioMap_.end(); ++it) {
134 it->second->blockSignals(true);
135 it->second->setChecked(align == it->first);
136 it->second->blockSignals(false);
141 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
143 LyXAlignment alignment = LYX_ALIGN_NONE;
144 RadioMap::const_iterator it = radioMap_.begin();
145 for (; it != radioMap_.end(); ++it) {
146 if (it->second->isChecked()) {
147 alignment = it->first;
155 void GuiParagraph::on_synchronizedViewCB_toggled()
157 bool in_sync = synchronizedViewCB->isChecked();
158 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
159 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
160 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
162 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
164 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
168 void GuiParagraph::changed()
171 // We apply immediately, except if we have custom line spacing
172 // with an intermediate result (trailing decimal separator) or
173 // an invalid value (which might as well be intermediate)
174 if (synchronizedViewCB->isChecked()
175 && (linespacing->currentIndex() != 4
176 || (!linespacingValue->text().endsWith(loc.decimalPoint())
177 && linespacingValue->hasAcceptableInput())))
182 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
184 switch (buttonBox->standardButton(button)) {
185 case QDialogButtonBox::Ok:
189 case QDialogButtonBox::Apply:
192 case QDialogButtonBox::Cancel:
195 case QDialogButtonBox::Reset:
204 void GuiParagraph::applyView()
208 params_.align(getAlignmentFromDialog());
211 Spacing::Space ls = Spacing::Default;
213 switch (linespacing->currentIndex()) {
215 ls = Spacing::Default;
218 ls = Spacing::Single;
221 ls = Spacing::Onehalf;
224 ls = Spacing::Double;
228 other = widgetToDoubleStr(linespacingValue);
232 Spacing const spacing(ls, other);
233 params_.spacing(spacing);
236 params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
238 params_.noindent(!indentCB->isChecked());
244 void GuiParagraph::updateView()
246 on_synchronizedViewCB_toggled();
248 ParagraphParameters const & pp = params();
251 docstring const & labelwidth = pp.labelWidthString();
252 if (hasLabelwidth()) {
253 labelwidthGB->setEnabled(true);
254 labelWidth->setText(toqstr(labelwidth));
256 labelwidthGB->setEnabled(false);
257 labelWidth->setText(QString());
261 checkAlignmentRadioButtons();
262 alignmentToRadioButtons(pp.align());
265 bool const canindent = canIndent();
266 indentCB->setEnabled(canindent);
267 indentCB->setChecked(canindent && !pp.noindent());
271 bool pending_input = false;
272 Spacing const & space = pp.spacing();
273 if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
274 // The user is about to enter custom spacing.
275 // We thus stay in Custom mode.
276 // This prevents the combo from e.g. immediately
277 // switching to single if a user enters "1" in the
278 // linespacingValue widget while aiming at e.g. "1.3"
280 pending_input = true;
282 switch (space.getSpace()) {
283 case Spacing::Single:
286 case Spacing::Onehalf:
289 case Spacing::Double:
300 linespacing->setCurrentIndex(ls);
301 if (space.getSpace() == Spacing::Other || pending_input) {
302 doubleToWidget(linespacingValue, space.getValue());
303 linespacingValue->setEnabled(true);
305 linespacingValue->setText(QString());
306 linespacingValue->setEnabled(false);
311 void GuiParagraph::enableView(bool enable)
313 indentCB->setEnabled(enable);
314 linespacing->setEnabled(enable);
315 labelWidth->setEnabled(enable);
316 synchronizedViewCB->setEnabled(enable);
317 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
318 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
320 synchronizedViewCB->setChecked(true);
321 RadioMap::const_iterator it = radioMap_.begin();
322 for (; it != radioMap_.end(); ++it)
323 it->second->setEnabled(enable);
327 ParagraphParameters const & GuiParagraph::params() const
329 if (haveMultiParSelection()) {
330 // FIXME: in case of multi-paragraph selection, it would be nice to
331 // initialise the parameters that are common to all paragraphs.
332 static ParagraphParameters const empty;
335 return bufferview()->cursor().innerParagraph().params();
339 void GuiParagraph::dispatchParams()
343 FuncRequest const fr(getLfun(), os.str());
348 bool GuiParagraph::haveMultiParSelection() const
350 Cursor const & cur = bufferview()->cursor();
351 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
355 bool GuiParagraph::canIndent() const
357 Layout const lay = bufferview()->cursor().innerParagraph().layout();
358 if (buffer().params().paragraph_separation
359 == BufferParams::ParagraphIndentSeparation)
360 return (lay.toggle_indent != ITOGGLE_NEVER);
361 return (lay.toggle_indent == ITOGGLE_ALWAYS);
365 LyXAlignment GuiParagraph::alignPossible() const
367 return bufferview()->cursor().innerParagraph().layout().alignpossible;
371 LyXAlignment GuiParagraph::alignDefault() const
373 return bufferview()->cursor().innerParagraph().layout().align;
377 bool GuiParagraph::hasLabelwidth() const
379 Layout layout = bufferview()->cursor().innerParagraph().layout();
380 return (layout.margintype == MARGIN_MANUAL
381 || layout.latextype == LATEX_BIB_ENVIRONMENT);
385 void GuiParagraph::saveSession(QSettings & settings) const
387 Dialog::saveSession(settings);
388 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
392 void GuiParagraph::restoreSession()
394 Dialog::restoreSession();
396 synchronizedViewCB->setChecked(
397 settings.value(sessionKey() + "/autoapply").toBool());
401 Dialog * createGuiParagraph(GuiView & lv)
403 return new GuiParagraph(lv);
407 } // namespace frontend
410 #include "moc_GuiParagraph.cpp"