]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiParagraph.cpp
Remove a workaround that seems to be useless since Qt 4.8
[lyx.git] / src / frontends / qt / GuiParagraph.cpp
1 /**
2  * \file GuiParagraph.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Richard Kimberly Heck
8  * \author Abdelrazak Younes
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiParagraph.h"
17
18 #include "qt_helpers.h"
19
20 #include "Buffer.h"
21 #include "BufferParams.h"
22 #include "BufferView.h"
23 #include "Cursor.h"
24 #include "FuncRequest.h"
25 #include "GuiView.h"
26 #include "Layout.h"
27 #include "Lexer.h"
28 #include "Paragraph.h"
29 #include "ParagraphParameters.h"
30 #include "Spacing.h"
31
32 #include "support/debug.h"
33 #include "support/gettext.h"
34
35 #include <QCheckBox>
36 #include <QDialogButtonBox>
37 #include <QLineEdit>
38 #include <QPushButton>
39 #include <QSettings>
40 #include <QVariant>
41
42 #include <sstream>
43
44 using namespace std;
45
46 namespace lyx {
47 namespace frontend {
48
49 GuiParagraph::GuiParagraph(GuiView & lv)
50         : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
51 {
52         setupUi(this);
53
54         // fix height to minimum
55         setFixedHeight(sizeHint().height());
56
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()));
68
69 #ifdef Q_OS_MAC
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);
74 #else
75         synchronizedViewCB->setChecked(false);
76 #endif
77
78         setButtons(synchronizedViewCB->isChecked());
79         QDoubleValidator * val = new QDoubleValidator(linespacingValue);
80         val->setNotation(QDoubleValidator::StandardNotation);
81         linespacingValue->setValidator(val);
82
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"
87                 " Description.\n"
88                 "\n"
89                 " Normally, you won't need to set this,"
90                 " since the largest label width of all the"
91                 " items is used."
92         ));
93
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;
99
100         alignDefaultLabel_ = alignDefaultRB->text();
101 }
102
103
104 void GuiParagraph::on_linespacing_activated(int index)
105 {
106         linespacingValue->setEnabled(index == 4);
107 }
108
109
110 void GuiParagraph::checkAlignmentRadioButtons()
111 {
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");
118         }
119
120         RadioMap::iterator it = radioMap_.begin();
121         for (; it != radioMap_.end(); ++it) {
122                 LyXAlignment const align = it->first;
123                 it->second->setEnabled(align & alignPossible());
124         }
125         if (haveMultiParSelection())
126                 alignDefaultRB->setText(alignDefaultLabel_);
127         else
128                 alignDefaultRB->setText(alignDefaultLabel_ + " ("
129                         + labelMap_[bufferview()->cursor().innerParagraph().getDefaultAlign(buffer().params())] + ")");
130 }
131
132
133 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
134 {
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);
140         }
141 }
142
143
144 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
145 {
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;
151                         break;
152                 }
153         }
154         return alignment;
155 }
156
157
158 void GuiParagraph::on_synchronizedViewCB_stateChanged(int state)
159 {
160         bool in_sync = state == Qt::Checked;
161         setButtons(in_sync);
162
163         // Apply pending changes
164         if (in_sync) 
165                 changed();
166 }
167
168
169 void GuiParagraph::setButtons(bool const in_sync)
170 {
171         buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
172         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
173         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
174         if (in_sync) 
175                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
176         else
177                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
178 }
179
180
181 void GuiParagraph::changed()
182 {
183         QLocale loc;
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())))
191                 applyView();
192 }
193
194
195 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
196 {
197         switch (buttonBox->standardButton(button)) {
198         case QDialogButtonBox::Ok:
199                 applyView();
200                 hide();
201                 break;
202         case QDialogButtonBox::Apply:
203                 applyView();
204                 break;
205         case QDialogButtonBox::Cancel:
206                 hide();
207                 break;
208         case QDialogButtonBox::Reset:
209                 updateView();
210                 break;
211         default:
212                 break;
213         }
214 }
215
216
217 void GuiParagraph::applyView()
218 {
219         params_ = params();
220
221         params_.align(getAlignmentFromDialog());
222
223         // get spacing
224         Spacing::Space ls = Spacing::Default;
225         string other;
226         switch (linespacing->currentIndex()) {
227         case 0:
228                 ls = Spacing::Default;
229                 break;
230         case 1:
231                 ls = Spacing::Single;
232                 break;
233         case 2:
234                 ls = Spacing::Onehalf;
235                 break;
236         case 3:
237                 ls = Spacing::Double;
238                 break;
239         case 4:
240                 ls = Spacing::Other;
241                 other = widgetToDoubleStr(linespacingValue);
242                 break;
243         }
244
245         Spacing const spacing(ls, other);
246         params_.spacing(spacing);
247
248         // label width
249         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
250         // indentation
251         params_.noindent(noindentCB->isChecked());
252
253         dispatchParams();
254 }
255
256
257 void GuiParagraph::updateView()
258 {
259         setButtons(synchronizedViewCB->isChecked());
260
261         ParagraphParameters const & pp = params();
262
263         // label width
264         docstring const & labelwidth = pp.labelWidthString();
265         if (hasLabelwidth()) {
266                 labelwidthGB->setEnabled(true);
267                 labelWidth->setText(toqstr(labelwidth));
268         } else {
269                 labelwidthGB->setEnabled(false);
270                 labelWidth->setText(QString());
271         }
272
273         // alignment
274         checkAlignmentRadioButtons();
275         alignmentToRadioButtons(pp.align());
276
277         //indentation
278         bool const canindent = canIndent();
279         noindentCB->setEnabled(canindent);
280         noindentCB->setChecked(canindent && pp.noindent());
281
282         // linespacing
283         int ls;
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"
292                 ls = 4;
293                 pending_input = true;
294         } else {
295                 switch (space.getSpace()) {
296                 case Spacing::Single:
297                         ls = 1;
298                         break;
299                 case Spacing::Onehalf:
300                         ls = 2;
301                         break;
302                 case Spacing::Double:
303                         ls = 3;
304                         break;
305                 case Spacing::Other:
306                         ls = 4;
307                         break;
308                 default:
309                         ls = 0;
310                         break;
311                 }
312         }
313         linespacing->setCurrentIndex(ls);
314         if (space.getSpace() == Spacing::Other || pending_input) {
315                 doubleToWidget(linespacingValue, space.getValue());
316                 linespacingValue->setEnabled(true);
317         } else {
318                 linespacingValue->setText(QString());
319                 linespacingValue->setEnabled(false);
320         }
321         // Somewhere in the chain this can lose default status (#11417)
322         buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
323 }
324
325
326 void GuiParagraph::enableView(bool enable)
327 {
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);
334         if (!enable)
335                 synchronizedViewCB->setChecked(true);
336         RadioMap::const_iterator it = radioMap_.begin();
337         for (; it != radioMap_.end(); ++it)
338                 it->second->setEnabled(enable);
339 }
340
341
342 ParagraphParameters const & GuiParagraph::params() const
343 {
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;
348                 return empty;
349         }
350         return bufferview()->cursor().innerParagraph().params();
351 }
352
353
354 void GuiParagraph::dispatchParams()
355 {
356         ostringstream os;
357         params_.write(os);
358         FuncRequest const fr(getLfun(), os.str());
359         dispatch(fr);
360 }
361
362
363 bool GuiParagraph::haveMultiParSelection() const
364 {
365         Cursor const & cur = bufferview()->cursor();
366         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
367 }
368
369
370 bool GuiParagraph::canIndent() const
371 {
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);
377 }
378
379
380 LyXAlignment GuiParagraph::alignPossible() const
381 {
382         return bufferview()->cursor().innerParagraph().layout().alignpossible;
383 }
384
385
386 bool GuiParagraph::hasLabelwidth() const
387 {
388         Layout layout = bufferview()->cursor().innerParagraph().layout();
389         return (layout.margintype == MARGIN_MANUAL
390                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
391 }
392
393
394 void GuiParagraph::saveSession(QSettings & settings) const
395 {
396         Dialog::saveSession(settings);
397         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
398 }
399
400
401 void GuiParagraph::restoreSession()
402 {
403         Dialog::restoreSession();
404         QSettings settings;
405         synchronizedViewCB->setChecked(
406                 settings.value(sessionKey() + "/autoapply").toBool());
407 }
408
409
410 } // namespace frontend
411 } // namespace lyx
412
413 #include "moc_GuiParagraph.cpp"