]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
Amend change b5c5083c6c035294e8a762bd5bd737a56d27317a.
[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 #else
71         synchronizedViewCB->setChecked(false);
72 #endif
73
74         on_synchronizedViewCB_toggled();
75         QDoubleValidator * val = new QDoubleValidator(linespacingValue);
76         val->setNotation(QDoubleValidator::StandardNotation);
77         linespacingValue->setValidator(val);
78
79         labelWidth->setWhatsThis(qt_(
80                 "As described in the User Guide, the width of"
81                 " this text determines the width of the label part"
82                 " of each item in environments like List and"
83                 " Description.\n"
84                 "\n"
85                 " Normally, you won't need to set this,"
86                 " since the largest label width of all the"
87                 " items is used."
88         ));
89
90         radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
91         radioMap_[LYX_ALIGN_BLOCK]  = alignJustRB;
92         radioMap_[LYX_ALIGN_LEFT]   = alignLeftRB;
93         radioMap_[LYX_ALIGN_RIGHT]  = alignRightRB;
94         radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
95
96         alignDefaultLabel_ = alignDefaultRB->text();
97 }
98
99
100 void GuiParagraph::on_linespacing_activated(int index)
101 {
102         linespacingValue->setEnabled(index == 4);
103 }
104
105
106 void GuiParagraph::checkAlignmentRadioButtons()
107 {
108         static std::map<LyXAlignment, QString> labelMap_;
109         if (labelMap_.empty()) {
110                 labelMap_[LYX_ALIGN_BLOCK]  = qt_("Justified");
111                 labelMap_[LYX_ALIGN_LEFT]   = qt_("Left");
112                 labelMap_[LYX_ALIGN_RIGHT]  = qt_("Right");
113                 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
114         }
115
116         RadioMap::iterator it = radioMap_.begin();
117         for (; it != radioMap_.end(); ++it) {
118                 LyXAlignment const align = it->first;
119                 it->second->setEnabled(align & alignPossible());
120         }
121         if (haveMultiParSelection())
122                 alignDefaultRB->setText(alignDefaultLabel_);
123         else
124                 alignDefaultRB->setText(alignDefaultLabel_ + " ("
125                         + labelMap_[alignDefault()] + ")");
126 }
127
128
129 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
130 {
131         RadioMap::const_iterator it = radioMap_.begin();
132         for (;it != radioMap_.end(); ++it) {
133                 it->second->blockSignals(true);
134                 it->second->setChecked(align == it->first);
135                 it->second->blockSignals(false);
136         }
137 }
138
139
140 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
141 {
142         LyXAlignment alignment = LYX_ALIGN_NONE;
143         RadioMap::const_iterator it = radioMap_.begin();
144         for (; it != radioMap_.end(); ++it) {
145                 if (it->second->isChecked()) {
146                         alignment = it->first;
147                         break;
148                 }
149         }
150         return alignment;
151 }
152
153
154 void GuiParagraph::on_synchronizedViewCB_toggled()
155 {
156         bool in_sync = synchronizedViewCB->isChecked();
157         buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
158         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
159         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
160         if (!in_sync)
161                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
162         else
163                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
164 }
165
166
167 void GuiParagraph::changed()
168 {
169         QLocale loc;
170         // We apply immediately, except if we have custom line spacing
171         // with an intermediate result (trailing decimal separator) or
172         // an invalid value (which might as well be intermediate)
173         if (synchronizedViewCB->isChecked()
174             && (linespacing->currentIndex() != 4
175                 || (!linespacingValue->text().endsWith(loc.decimalPoint())
176                     && linespacingValue->hasAcceptableInput())))
177                 applyView();
178 }
179
180
181 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
182 {
183         switch (buttonBox->standardButton(button)) {
184         case QDialogButtonBox::Ok:
185                 applyView();
186                 hide();
187                 break;
188         case QDialogButtonBox::Apply:
189                 applyView();
190                 break;
191         case QDialogButtonBox::Cancel:
192                 hide();
193                 break;
194         case QDialogButtonBox::Reset:
195                 updateView();
196                 break;
197         default:
198                 break;
199         }
200 }
201
202
203 void GuiParagraph::applyView()
204 {
205         params_ = params();
206
207         params_.align(getAlignmentFromDialog());
208
209         // get spacing
210         Spacing::Space ls = Spacing::Default;
211         string other;
212         switch (linespacing->currentIndex()) {
213         case 0:
214                 ls = Spacing::Default;
215                 break;
216         case 1:
217                 ls = Spacing::Single;
218                 break;
219         case 2:
220                 ls = Spacing::Onehalf;
221                 break;
222         case 3:
223                 ls = Spacing::Double;
224                 break;
225         case 4:
226                 ls = Spacing::Other;
227                 other = widgetToDoubleStr(linespacingValue);
228                 break;
229         }
230
231         Spacing const spacing(ls, other);
232         params_.spacing(spacing);
233
234         // label width
235         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
236         // indentation
237         params_.noindent(!indentCB->isChecked());
238
239         dispatchParams();
240 }
241
242
243 void GuiParagraph::updateView()
244 {
245         on_synchronizedViewCB_toggled();
246
247         ParagraphParameters const & pp = params();
248
249         // label width
250         docstring const & labelwidth = pp.labelWidthString();
251         if (hasLabelwidth()) {
252                 labelwidthGB->setEnabled(true);
253                 labelWidth->setText(toqstr(labelwidth));
254         } else {
255                 labelwidthGB->setEnabled(false);
256                 labelWidth->setText(QString());
257         }
258
259         // alignment
260         checkAlignmentRadioButtons();
261         alignmentToRadioButtons(pp.align());
262
263         //indentation
264         bool const canindent = canIndent();
265         indentCB->setEnabled(canindent);
266         indentCB->setChecked(canindent && !pp.noindent());
267
268         // linespacing
269         int ls;
270         bool pending_input = false;
271         Spacing const & space = pp.spacing();
272         if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
273                 // The user is about to enter custom spacing.
274                 // We thus stay in Custom mode.
275                 // This prevents the combo from e.g. immediately
276                 // switching to single if a user enters "1" in the
277                 // linespacingValue widget while aiming at e.g. "1.3"
278                 ls = 4;
279                 pending_input = true;
280         } else {
281                 switch (space.getSpace()) {
282                 case Spacing::Single:
283                         ls = 1;
284                         break;
285                 case Spacing::Onehalf:
286                         ls = 2;
287                         break;
288                 case Spacing::Double:
289                         ls = 3;
290                         break;
291                 case Spacing::Other:
292                         ls = 4;
293                         break;
294                 default:
295                         ls = 0;
296                         break;
297                 }
298         }
299         linespacing->setCurrentIndex(ls);
300         if (space.getSpace() == Spacing::Other || pending_input) {
301                 doubleToWidget(linespacingValue, space.getValue());
302                 linespacingValue->setEnabled(true);
303         } else {
304                 linespacingValue->setText(QString());
305                 linespacingValue->setEnabled(false);
306         }
307 }
308
309
310 void GuiParagraph::enableView(bool enable)
311 {
312         indentCB->setEnabled(enable);
313         linespacing->setEnabled(enable);
314         labelWidth->setEnabled(enable);
315         synchronizedViewCB->setEnabled(enable);
316         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
317         buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
318         if (!enable)
319                 synchronizedViewCB->setChecked(true);
320         RadioMap::const_iterator it = radioMap_.begin();
321         for (; it != radioMap_.end(); ++it)
322                 it->second->setEnabled(enable);
323 }
324
325
326 ParagraphParameters const & GuiParagraph::params() const
327 {
328         if (haveMultiParSelection()) {
329                 // FIXME: in case of multi-paragraph selection, it would be nice to
330                 // initialise the parameters that are common to all paragraphs.
331                 static ParagraphParameters const empty;
332                 return empty;
333         }
334         return bufferview()->cursor().innerParagraph().params();
335 }
336
337
338 void GuiParagraph::dispatchParams()
339 {
340         ostringstream os;
341         params_.write(os);
342         FuncRequest const fr(getLfun(), os.str());
343         dispatch(fr);
344 }
345
346
347 bool GuiParagraph::haveMultiParSelection() const
348 {
349         Cursor const & cur = bufferview()->cursor();
350         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
351 }
352
353
354 bool GuiParagraph::canIndent() const
355 {
356         Layout const lay = bufferview()->cursor().innerParagraph().layout();
357         if (buffer().params().paragraph_separation
358                 == BufferParams::ParagraphIndentSeparation)
359                 return (lay.toggle_indent != ITOGGLE_NEVER);
360         return (lay.toggle_indent == ITOGGLE_ALWAYS);
361 }
362
363
364 LyXAlignment GuiParagraph::alignPossible() const
365 {
366         return bufferview()->cursor().innerParagraph().layout().alignpossible;
367 }
368
369
370 LyXAlignment GuiParagraph::alignDefault() const
371 {
372         return bufferview()->cursor().innerParagraph().layout().align;
373 }
374
375
376 bool GuiParagraph::hasLabelwidth() const
377 {
378         Layout layout = bufferview()->cursor().innerParagraph().layout();
379         return (layout.margintype == MARGIN_MANUAL
380                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
381 }
382
383
384 void GuiParagraph::saveSession(QSettings & settings) const
385 {
386         Dialog::saveSession(settings);
387         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
388 }
389
390
391 void GuiParagraph::restoreSession()
392 {
393         Dialog::restoreSession();
394         QSettings settings;
395         synchronizedViewCB->setChecked(
396                 settings.value(sessionKey() + "/autoapply").toBool());
397 }
398
399
400 Dialog * createGuiParagraph(GuiView & lv)
401 {
402         return new GuiParagraph(lv);
403 }
404
405
406 } // namespace frontend
407 } // namespace lyx
408
409 #include "moc_GuiParagraph.cpp"