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 // fix height to minimum
54 setFixedHeight(sizeHint().height());
56 connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
57 connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
58 connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
59 connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
60 connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
61 connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
62 connect(linespacingValue, SIGNAL(textChanged(QString)),
63 this, SLOT(changed()));
64 connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
65 connect(labelWidth, SIGNAL(textChanged(QString)),
66 this, SLOT(changed()));
69 // On Mac it's common to have tool windows which are always in the
70 // foreground and are hidden when the main window is not focused.
71 setWindowFlags(Qt::Tool);
72 synchronizedViewCB->setChecked(true);
74 synchronizedViewCB->setChecked(false);
77 on_synchronizedViewCB_toggled();
78 QDoubleValidator * val = new QDoubleValidator(linespacingValue);
79 val->setNotation(QDoubleValidator::StandardNotation);
80 linespacingValue->setValidator(val);
82 labelWidth->setWhatsThis(qt_(
83 "As described in the User Guide, the width of"
84 " this text determines the width of the label part"
85 " of each item in environments like List and"
88 " Normally, you won't need to set this,"
89 " since the largest label width of all the"
93 radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
94 radioMap_[LYX_ALIGN_BLOCK] = alignJustRB;
95 radioMap_[LYX_ALIGN_LEFT] = alignLeftRB;
96 radioMap_[LYX_ALIGN_RIGHT] = alignRightRB;
97 radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
99 alignDefaultLabel_ = alignDefaultRB->text();
103 void GuiParagraph::on_linespacing_activated(int index)
105 linespacingValue->setEnabled(index == 4);
109 void GuiParagraph::checkAlignmentRadioButtons()
111 static std::map<LyXAlignment, QString> labelMap_;
112 if (labelMap_.empty()) {
113 labelMap_[LYX_ALIGN_BLOCK] = qt_("Justified");
114 labelMap_[LYX_ALIGN_LEFT] = qt_("Left");
115 labelMap_[LYX_ALIGN_RIGHT] = qt_("Right");
116 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
119 RadioMap::iterator it = radioMap_.begin();
120 for (; it != radioMap_.end(); ++it) {
121 LyXAlignment const align = it->first;
122 it->second->setEnabled(align & alignPossible());
124 if (haveMultiParSelection())
125 alignDefaultRB->setText(alignDefaultLabel_);
127 alignDefaultRB->setText(alignDefaultLabel_ + " ("
128 + labelMap_[alignDefault()] + ")");
132 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
134 RadioMap::const_iterator it = radioMap_.begin();
135 for (;it != radioMap_.end(); ++it) {
136 it->second->blockSignals(true);
137 it->second->setChecked(align == it->first);
138 it->second->blockSignals(false);
143 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
145 LyXAlignment alignment = LYX_ALIGN_NONE;
146 RadioMap::const_iterator it = radioMap_.begin();
147 for (; it != radioMap_.end(); ++it) {
148 if (it->second->isChecked()) {
149 alignment = it->first;
157 void GuiParagraph::on_synchronizedViewCB_toggled()
159 bool in_sync = synchronizedViewCB->isChecked();
160 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
161 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
162 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
164 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
166 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
170 void GuiParagraph::changed()
173 // We apply immediately, except if we have custom line spacing
174 // with an intermediate result (trailing decimal separator) or
175 // an invalid value (which might as well be intermediate)
176 if (synchronizedViewCB->isChecked()
177 && (linespacing->currentIndex() != 4
178 || (!linespacingValue->text().endsWith(loc.decimalPoint())
179 && linespacingValue->hasAcceptableInput())))
184 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
186 switch (buttonBox->standardButton(button)) {
187 case QDialogButtonBox::Ok:
191 case QDialogButtonBox::Apply:
194 case QDialogButtonBox::Cancel:
197 case QDialogButtonBox::Reset:
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);
310 // Somewhere in the chain this can lose default status (#11417)
311 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
315 void GuiParagraph::enableView(bool enable)
317 indentCB->setEnabled(enable);
318 linespacing->setEnabled(enable);
319 labelWidth->setEnabled(enable);
320 synchronizedViewCB->setEnabled(enable);
321 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
322 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
324 synchronizedViewCB->setChecked(true);
325 RadioMap::const_iterator it = radioMap_.begin();
326 for (; it != radioMap_.end(); ++it)
327 it->second->setEnabled(enable);
331 ParagraphParameters const & GuiParagraph::params() const
333 if (haveMultiParSelection()) {
334 // FIXME: in case of multi-paragraph selection, it would be nice to
335 // initialise the parameters that are common to all paragraphs.
336 static ParagraphParameters const empty;
339 return bufferview()->cursor().innerParagraph().params();
343 void GuiParagraph::dispatchParams()
347 FuncRequest const fr(getLfun(), os.str());
352 bool GuiParagraph::haveMultiParSelection() const
354 Cursor const & cur = bufferview()->cursor();
355 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
359 bool GuiParagraph::canIndent() const
361 Layout const lay = bufferview()->cursor().innerParagraph().layout();
362 if (buffer().params().paragraph_separation
363 == BufferParams::ParagraphIndentSeparation)
364 return (lay.toggle_indent != ITOGGLE_NEVER);
365 return (lay.toggle_indent == ITOGGLE_ALWAYS);
369 LyXAlignment GuiParagraph::alignPossible() const
371 return bufferview()->cursor().innerParagraph().layout().alignpossible;
375 LyXAlignment GuiParagraph::alignDefault() const
377 return bufferview()->cursor().innerParagraph().layout().align;
381 bool GuiParagraph::hasLabelwidth() const
383 Layout layout = bufferview()->cursor().innerParagraph().layout();
384 return (layout.margintype == MARGIN_MANUAL
385 || layout.latextype == LATEX_BIB_ENVIRONMENT);
389 void GuiParagraph::saveSession(QSettings & settings) const
391 Dialog::saveSession(settings);
392 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
396 void GuiParagraph::restoreSession()
398 Dialog::restoreSession();
400 synchronizedViewCB->setChecked(
401 settings.value(sessionKey() + "/autoapply").toBool());
405 Dialog * createGuiParagraph(GuiView & lv)
407 return new GuiParagraph(lv);
411 } // namespace frontend
414 #include "moc_GuiParagraph.cpp"