2 * \file GuiParagraph.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Richard Kimberly Heck
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/Lexer.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(noindentCB, 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 setButtons(synchronizedViewCB->isChecked());
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_[bufferview()->cursor().innerParagraph().getDefaultAlign(buffer().params())] + ")");
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_stateChanged(int state)
159 bool in_sync = state == Qt::Checked;
162 // Apply pending changes
168 void GuiParagraph::setButtons(bool const in_sync)
170 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
171 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
172 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
174 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
176 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
180 void GuiParagraph::changed()
183 // We apply immediately, except if we have custom line spacing
184 // with an intermediate result (trailing decimal separator) or
185 // an invalid value (which might as well be intermediate)
186 if (synchronizedViewCB->isChecked()
187 && (linespacing->currentIndex() != 4
188 || (!linespacingValue->text().endsWith(loc.decimalPoint())
189 && linespacingValue->hasAcceptableInput())))
194 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
196 switch (buttonBox->standardButton(button)) {
197 case QDialogButtonBox::Ok:
201 case QDialogButtonBox::Apply:
204 case QDialogButtonBox::Cancel:
207 case QDialogButtonBox::Reset:
216 void GuiParagraph::applyView()
220 params_.align(getAlignmentFromDialog());
223 Spacing::Space ls = Spacing::Default;
225 switch (linespacing->currentIndex()) {
227 ls = Spacing::Default;
230 ls = Spacing::Single;
233 ls = Spacing::Onehalf;
236 ls = Spacing::Double;
240 other = widgetToDoubleStr(linespacingValue);
244 Spacing const spacing(ls, other);
245 params_.spacing(spacing);
248 params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
250 params_.noindent(noindentCB->isChecked());
256 void GuiParagraph::updateView()
258 setButtons(synchronizedViewCB->isChecked());
260 ParagraphParameters const & pp = params();
263 docstring const & labelwidth = pp.labelWidthString();
264 if (hasLabelwidth()) {
265 labelwidthGB->setEnabled(true);
266 labelWidth->setText(toqstr(labelwidth));
268 labelwidthGB->setEnabled(false);
269 labelWidth->setText(QString());
273 checkAlignmentRadioButtons();
274 alignmentToRadioButtons(pp.align());
277 bool const canindent = canIndent();
278 noindentCB->setEnabled(canindent);
279 noindentCB->setChecked(canindent && pp.noindent());
283 bool pending_input = false;
284 Spacing const & space = pp.spacing();
285 if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
286 // The user is about to enter custom spacing.
287 // We thus stay in Custom mode.
288 // This prevents the combo from e.g. immediately
289 // switching to single if a user enters "1" in the
290 // linespacingValue widget while aiming at e.g. "1.3"
292 pending_input = true;
294 switch (space.getSpace()) {
295 case Spacing::Single:
298 case Spacing::Onehalf:
301 case Spacing::Double:
312 linespacing->setCurrentIndex(ls);
313 if (space.getSpace() == Spacing::Other || pending_input) {
314 doubleToWidget(linespacingValue, space.getValue());
315 linespacingValue->setEnabled(true);
317 linespacingValue->setText(QString());
318 linespacingValue->setEnabled(false);
320 // Somewhere in the chain this can lose default status (#11417)
321 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
325 void GuiParagraph::enableView(bool enable)
327 noindentCB->setEnabled(canIndent() && enable);
328 linespacing->setEnabled(enable);
329 labelWidth->setEnabled(enable);
330 synchronizedViewCB->setEnabled(enable);
331 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
332 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
334 synchronizedViewCB->setChecked(true);
335 RadioMap::const_iterator it = radioMap_.begin();
336 for (; it != radioMap_.end(); ++it)
337 it->second->setEnabled(enable);
341 ParagraphParameters const & GuiParagraph::params() const
343 if (haveMultiParSelection()) {
344 // FIXME: in case of multi-paragraph selection, it would be nice to
345 // initialise the parameters that are common to all paragraphs.
346 static ParagraphParameters const empty;
349 return bufferview()->cursor().innerParagraph().params();
353 void GuiParagraph::dispatchParams()
357 FuncRequest const fr(getLfun(), os.str());
362 bool GuiParagraph::haveMultiParSelection() const
364 Cursor const & cur = bufferview()->cursor();
365 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
369 bool GuiParagraph::canIndent() const
371 Layout const lay = bufferview()->cursor().innerParagraph().layout();
372 if (buffer().params().paragraph_separation
373 == BufferParams::ParagraphIndentSeparation)
374 return (lay.toggle_indent != ITOGGLE_NEVER);
375 return (lay.toggle_indent == ITOGGLE_ALWAYS);
379 LyXAlignment GuiParagraph::alignPossible() const
381 return bufferview()->cursor().innerParagraph().layout().alignpossible;
385 bool GuiParagraph::hasLabelwidth() const
387 Layout layout = bufferview()->cursor().innerParagraph().layout();
388 return (layout.margintype == MARGIN_MANUAL
389 || layout.latextype == LATEX_BIB_ENVIRONMENT);
393 void GuiParagraph::saveSession(QSettings & settings) const
395 Dialog::saveSession(settings);
396 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
400 void GuiParagraph::restoreSession()
402 Dialog::restoreSession();
404 synchronizedViewCB->setChecked(
405 settings.value(sessionKey() + "/autoapply").toBool());
409 } // namespace frontend
412 #include "moc_GuiParagraph.cpp"