]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / 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 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 "Lexer.h"
27 #include "Paragraph.h"
28 #include "ParagraphParameters.h"
29 #include "Spacing.h"
30
31 #include "support/debug.h"
32 #include "support/gettext.h"
33
34 #include <QCheckBox>
35 #include <QDialogButtonBox>
36 #include <QLineEdit>
37 #include <QPushButton>
38 #include <QSettings>
39 #include <QVariant>
40
41 #include <sstream>
42
43 using namespace std;
44
45 namespace lyx {
46 namespace frontend {
47
48 GuiParagraph::GuiParagraph(GuiView & lv)
49         : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
50 {
51         setupUi(this);
52
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()));
64
65 #ifdef Q_OS_MAC
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"));
71 #else
72         synchronizedViewCB->setChecked(false);
73 #endif
74
75         on_synchronizedViewCB_toggled();
76         QDoubleValidator * val = new QDoubleValidator(linespacingValue);
77         val->setNotation(QDoubleValidator::StandardNotation);
78         linespacingValue->setValidator(val);
79
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"
84                 " Description.\n"
85                 "\n"
86                 " Normally, you won't need to set this,"
87                 " since the largest label width of all the"
88                 " items is used."
89         ));
90
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;
96
97         alignDefaultLabel_ = alignDefaultRB->text();
98 }
99
100
101 void GuiParagraph::on_linespacing_activated(int index)
102 {
103         linespacingValue->setEnabled(index == 4);
104 }
105
106
107 void GuiParagraph::checkAlignmentRadioButtons()
108 {
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");
115         }
116
117         RadioMap::iterator it = radioMap_.begin();
118         for (; it != radioMap_.end(); ++it) {
119                 LyXAlignment const align = it->first;
120                 it->second->setEnabled(align & alignPossible());
121         }
122         if (haveMultiParSelection())
123                 alignDefaultRB->setText(alignDefaultLabel_);
124         else
125                 alignDefaultRB->setText(alignDefaultLabel_ + " ("
126                         + labelMap_[alignDefault()] + ")");
127 }
128
129
130 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
131 {
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);
137         }
138 }
139
140
141 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
142 {
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;
148                         break;
149                 }
150         }
151         return alignment;
152 }
153
154
155 void GuiParagraph::on_synchronizedViewCB_toggled()
156 {
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);
161         if (!in_sync)
162                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
163         else
164                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
165 }
166
167
168 void GuiParagraph::changed()
169 {
170         QLocale loc;
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())))
178                 applyView();
179 }
180
181
182 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
183 {
184         switch (buttonBox->standardButton(button)) {
185         case QDialogButtonBox::Ok:
186                 applyView();
187                 hide();
188                 break;
189         case QDialogButtonBox::Apply:
190                 applyView();
191                 break;
192         case QDialogButtonBox::Cancel:
193                 hide();
194                 break;
195         case QDialogButtonBox::Reset:
196                 updateView();
197                 break;
198         default:
199                 break;
200         }
201 }
202
203
204 void GuiParagraph::applyView()
205 {
206         params_ = params();
207
208         params_.align(getAlignmentFromDialog());
209
210         // get spacing
211         Spacing::Space ls = Spacing::Default;
212         string other;
213         switch (linespacing->currentIndex()) {
214         case 0:
215                 ls = Spacing::Default;
216                 break;
217         case 1:
218                 ls = Spacing::Single;
219                 break;
220         case 2:
221                 ls = Spacing::Onehalf;
222                 break;
223         case 3:
224                 ls = Spacing::Double;
225                 break;
226         case 4:
227                 ls = Spacing::Other;
228                 other = widgetToDoubleStr(linespacingValue);
229                 break;
230         }
231
232         Spacing const spacing(ls, other);
233         params_.spacing(spacing);
234
235         // label width
236         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
237         // indentation
238         params_.noindent(!indentCB->isChecked());
239
240         dispatchParams();
241 }
242
243
244 void GuiParagraph::updateView()
245 {
246         on_synchronizedViewCB_toggled();
247
248         ParagraphParameters const & pp = params();
249
250         // label width
251         docstring const & labelwidth = pp.labelWidthString();
252         if (hasLabelwidth()) {
253                 labelwidthGB->setEnabled(true);
254                 labelWidth->setText(toqstr(labelwidth));
255         } else {
256                 labelwidthGB->setEnabled(false);
257                 labelWidth->setText(QString());
258         }
259
260         // alignment
261         checkAlignmentRadioButtons();
262         alignmentToRadioButtons(pp.align());
263
264         //indentation
265         bool const canindent = canIndent();
266         indentCB->setEnabled(canindent);
267         indentCB->setChecked(canindent && !pp.noindent());
268
269         // linespacing
270         int ls;
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"
279                 ls = 4;
280                 pending_input = true;
281         } else {
282                 switch (space.getSpace()) {
283                 case Spacing::Single:
284                         ls = 1;
285                         break;
286                 case Spacing::Onehalf:
287                         ls = 2;
288                         break;
289                 case Spacing::Double:
290                         ls = 3;
291                         break;
292                 case Spacing::Other:
293                         ls = 4;
294                         break;
295                 default:
296                         ls = 0;
297                         break;
298                 }
299         }
300         linespacing->setCurrentIndex(ls);
301         if (space.getSpace() == Spacing::Other || pending_input) {
302                 doubleToWidget(linespacingValue, space.getValue());
303                 linespacingValue->setEnabled(true);
304         } else {
305                 linespacingValue->setText(QString());
306                 linespacingValue->setEnabled(false);
307         }
308 }
309
310
311 void GuiParagraph::enableView(bool enable)
312 {
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);
319         if (!enable)
320                 synchronizedViewCB->setChecked(true);
321         RadioMap::const_iterator it = radioMap_.begin();
322         for (; it != radioMap_.end(); ++it)
323                 it->second->setEnabled(enable);
324 }
325
326
327 ParagraphParameters const & GuiParagraph::params() const
328 {
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;
333                 return empty;
334         }
335         return bufferview()->cursor().innerParagraph().params();
336 }
337
338
339 void GuiParagraph::dispatchParams()
340 {
341         ostringstream os;
342         params_.write(os);
343         FuncRequest const fr(getLfun(), os.str());
344         dispatch(fr);
345 }
346
347
348 bool GuiParagraph::haveMultiParSelection() const
349 {
350         Cursor const & cur = bufferview()->cursor();
351         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
352 }
353
354
355 bool GuiParagraph::canIndent() const
356 {
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);
362 }
363
364
365 LyXAlignment GuiParagraph::alignPossible() const
366 {
367         return bufferview()->cursor().innerParagraph().layout().alignpossible;
368 }
369
370
371 LyXAlignment GuiParagraph::alignDefault() const
372 {
373         return bufferview()->cursor().innerParagraph().layout().align;
374 }
375
376
377 bool GuiParagraph::hasLabelwidth() const
378 {
379         Layout layout = bufferview()->cursor().innerParagraph().layout();
380         return (layout.margintype == MARGIN_MANUAL
381                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
382 }
383
384
385 void GuiParagraph::saveSession(QSettings & settings) const
386 {
387         Dialog::saveSession(settings);
388         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
389 }
390
391
392 void GuiParagraph::restoreSession()
393 {
394         Dialog::restoreSession();
395         QSettings settings;
396         synchronizedViewCB->setChecked(
397                 settings.value(sessionKey() + "/autoapply").toBool());
398 }
399
400
401 Dialog * createGuiParagraph(GuiView & lv)
402 {
403         return new GuiParagraph(lv);
404 }
405
406
407 } // namespace frontend
408 } // namespace lyx
409
410 #include "moc_GuiParagraph.cpp"