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"
28 #include "Paragraph.h"
29 #include "ParagraphParameters.h"
32 #include "support/debug.h"
33 #include "support/gettext.h"
36 #include <QDialogButtonBox>
38 #include <QPushButton>
49 GuiParagraph::GuiParagraph(GuiView & lv)
50 : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
54 // fix height to minimum
55 setFixedHeight(sizeHint().height());
57 connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
58 connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
59 connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
60 connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
61 connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
62 connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
63 connect(linespacingValue, SIGNAL(textChanged(QString)),
64 this, SLOT(changed()));
65 connect(noindentCB, SIGNAL(clicked()), this, SLOT(changed()));
66 connect(labelWidth, SIGNAL(textChanged(QString)),
67 this, SLOT(changed()));
70 // On Mac it's common to have tool windows which are always in the
71 // foreground and are hidden when the main window is not focused.
72 setWindowFlags(Qt::Tool);
73 synchronizedViewCB->setChecked(true);
75 synchronizedViewCB->setChecked(false);
78 setButtons(synchronizedViewCB->isChecked());
79 QDoubleValidator * val = new QDoubleValidator(linespacingValue);
80 val->setNotation(QDoubleValidator::StandardNotation);
81 linespacingValue->setValidator(val);
83 labelWidth->setWhatsThis(qt_(
84 "As described in the User Guide, the width of"
85 " this text determines the width of the label part"
86 " of each item in environments like List and"
89 " Normally, you won't need to set this,"
90 " since the largest label width of all the"
94 radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
95 radioMap_[LYX_ALIGN_BLOCK] = alignJustRB;
96 radioMap_[LYX_ALIGN_LEFT] = alignLeftRB;
97 radioMap_[LYX_ALIGN_RIGHT] = alignRightRB;
98 radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
100 alignDefaultLabel_ = alignDefaultRB->text();
104 void GuiParagraph::on_linespacing_activated(int index)
106 linespacingValue->setEnabled(index == 4);
110 void GuiParagraph::checkAlignmentRadioButtons()
112 static std::map<LyXAlignment, QString> labelMap_;
113 if (labelMap_.empty()) {
114 labelMap_[LYX_ALIGN_BLOCK] = qt_("Justified");
115 labelMap_[LYX_ALIGN_LEFT] = qt_("Left");
116 labelMap_[LYX_ALIGN_RIGHT] = qt_("Right");
117 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
120 RadioMap::iterator it = radioMap_.begin();
121 for (; it != radioMap_.end(); ++it) {
122 LyXAlignment const align = it->first;
123 it->second->setEnabled(align & alignPossible());
125 if (haveMultiParSelection())
126 alignDefaultRB->setText(alignDefaultLabel_);
128 alignDefaultRB->setText(alignDefaultLabel_ + " ("
129 + labelMap_[bufferview()->cursor().innerParagraph().getDefaultAlign(buffer().params())] + ")");
133 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
135 RadioMap::const_iterator it = radioMap_.begin();
136 for (;it != radioMap_.end(); ++it) {
137 it->second->blockSignals(true);
138 it->second->setChecked(align == it->first);
139 it->second->blockSignals(false);
144 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
146 LyXAlignment alignment = LYX_ALIGN_NONE;
147 RadioMap::const_iterator it = radioMap_.begin();
148 for (; it != radioMap_.end(); ++it) {
149 if (it->second->isChecked()) {
150 alignment = it->first;
158 void GuiParagraph::on_synchronizedViewCB_stateChanged(int state)
160 bool in_sync = state == Qt::Checked;
163 // Apply pending changes
169 void GuiParagraph::setButtons(bool const in_sync)
171 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
172 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
173 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
175 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
177 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
181 void GuiParagraph::changed()
184 // We apply immediately, except if we have custom line spacing
185 // with an intermediate result (trailing decimal separator) or
186 // an invalid value (which might as well be intermediate)
187 if (synchronizedViewCB->isChecked()
188 && (linespacing->currentIndex() != 4
189 || (!linespacingValue->text().endsWith(loc.decimalPoint())
190 && linespacingValue->hasAcceptableInput())))
195 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
197 switch (buttonBox->standardButton(button)) {
198 case QDialogButtonBox::Ok:
202 case QDialogButtonBox::Apply:
205 case QDialogButtonBox::Cancel:
208 case QDialogButtonBox::Reset:
217 void GuiParagraph::applyView()
221 params_.align(getAlignmentFromDialog());
224 Spacing::Space ls = Spacing::Default;
226 switch (linespacing->currentIndex()) {
228 ls = Spacing::Default;
231 ls = Spacing::Single;
234 ls = Spacing::Onehalf;
237 ls = Spacing::Double;
241 other = widgetToDoubleStr(linespacingValue);
245 Spacing const spacing(ls, other);
246 params_.spacing(spacing);
249 params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
251 params_.noindent(noindentCB->isChecked());
257 void GuiParagraph::updateView()
259 setButtons(synchronizedViewCB->isChecked());
261 ParagraphParameters const & pp = params();
264 docstring const & labelwidth = pp.labelWidthString();
265 if (hasLabelwidth()) {
266 labelwidthGB->setEnabled(true);
267 labelWidth->setText(toqstr(labelwidth));
269 labelwidthGB->setEnabled(false);
270 labelWidth->setText(QString());
274 checkAlignmentRadioButtons();
275 alignmentToRadioButtons(pp.align());
278 bool const canindent = canIndent();
279 noindentCB->setEnabled(canindent);
280 noindentCB->setChecked(canindent && pp.noindent());
284 bool pending_input = false;
285 Spacing const & space = pp.spacing();
286 if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
287 // The user is about to enter custom spacing.
288 // We thus stay in Custom mode.
289 // This prevents the combo from e.g. immediately
290 // switching to single if a user enters "1" in the
291 // linespacingValue widget while aiming at e.g. "1.3"
293 pending_input = true;
295 switch (space.getSpace()) {
296 case Spacing::Single:
299 case Spacing::Onehalf:
302 case Spacing::Double:
313 linespacing->setCurrentIndex(ls);
314 if (space.getSpace() == Spacing::Other || pending_input) {
315 doubleToWidget(linespacingValue, space.getValue());
316 linespacingValue->setEnabled(true);
318 linespacingValue->setText(QString());
319 linespacingValue->setEnabled(false);
321 // Somewhere in the chain this can lose default status (#11417)
322 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
326 void GuiParagraph::enableView(bool enable)
328 noindentCB->setEnabled(canIndent() && enable);
329 linespacing->setEnabled(enable);
330 labelWidth->setEnabled(enable);
331 synchronizedViewCB->setEnabled(enable);
332 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
333 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
335 synchronizedViewCB->setChecked(true);
336 RadioMap::const_iterator it = radioMap_.begin();
337 for (; it != radioMap_.end(); ++it)
338 it->second->setEnabled(enable);
342 ParagraphParameters const & GuiParagraph::params() const
344 if (haveMultiParSelection()) {
345 // FIXME: in case of multi-paragraph selection, it would be nice to
346 // initialise the parameters that are common to all paragraphs.
347 static ParagraphParameters const empty;
350 return bufferview()->cursor().innerParagraph().params();
354 void GuiParagraph::dispatchParams()
358 FuncRequest const fr(getLfun(), os.str());
363 bool GuiParagraph::haveMultiParSelection() const
365 Cursor const & cur = bufferview()->cursor();
366 return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
370 bool GuiParagraph::canIndent() const
372 Layout const lay = bufferview()->cursor().innerParagraph().layout();
373 if (buffer().params().paragraph_separation
374 == BufferParams::ParagraphIndentSeparation)
375 return (lay.toggle_indent != ITOGGLE_NEVER);
376 return (lay.toggle_indent == ITOGGLE_ALWAYS);
380 LyXAlignment GuiParagraph::alignPossible() const
382 return bufferview()->cursor().innerParagraph().layout().alignpossible;
386 bool GuiParagraph::hasLabelwidth() const
388 Layout layout = bufferview()->cursor().innerParagraph().layout();
389 return (layout.margintype == MARGIN_MANUAL
390 || layout.latextype == LATEX_BIB_ENVIRONMENT);
394 void GuiParagraph::saveSession(QSettings & settings) const
396 Dialog::saveSession(settings);
397 settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
401 void GuiParagraph::restoreSession()
403 Dialog::restoreSession();
405 synchronizedViewCB->setChecked(
406 settings.value(sessionKey() + "/autoapply").toBool());
410 } // namespace frontend
413 #include "moc_GuiParagraph.cpp"