]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
Speed up exit time
[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 <QLineEdit>
36 #include <QPushButton>
37 #include <QSettings>
38 #include <QVariant>
39
40 #include <sstream>
41
42 using namespace std;
43
44 namespace lyx {
45 namespace frontend {
46
47 GuiParagraph::GuiParagraph(GuiView & lv)
48         : DialogView(lv, "paragraph", qt_("Paragraph Settings"))
49 {
50         setupUi(this);
51
52         connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
53         connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
54         connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
55         connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
56         connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
57         connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
58         connect(linespacingValue, SIGNAL(textChanged(QString)),
59                 this, SLOT(changed()));
60         connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
61         connect(labelWidth, SIGNAL(textChanged(QString)),
62                 this, SLOT(changed()));
63
64 #ifdef Q_OS_MAC
65         // On Mac it's common to have tool windows which are always in the
66         // foreground and are hidden when the main window is not focused.
67         setWindowFlags(Qt::Tool);
68         synchronizedViewCB->setChecked(true);
69         closePB->setText(qt_("&Cancel"));
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         restorePB->setEnabled(!in_sync);
158         applyPB->setEnabled(!in_sync);
159         okPB->setEnabled(!in_sync);
160         if (!in_sync)
161                 closePB->setText(qt_("&Cancel"));
162         else
163                 closePB->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                 on_applyPB_clicked();
178 }
179
180
181 void GuiParagraph::on_applyPB_clicked()
182 {
183         applyView();
184 }
185
186
187 void GuiParagraph::on_okPB_clicked()
188 {
189         applyView();
190         hide();
191 }
192
193
194 void GuiParagraph::on_closePB_clicked()
195 {
196         hide();
197 }
198
199
200 void GuiParagraph::on_restorePB_clicked()
201 {
202         updateView();
203 }
204
205
206 void GuiParagraph::applyView()
207 {
208         params_ = params();
209
210         params_.align(getAlignmentFromDialog());
211
212         // get spacing
213         Spacing::Space ls = Spacing::Default;
214         string other;
215         switch (linespacing->currentIndex()) {
216         case 0:
217                 ls = Spacing::Default;
218                 break;
219         case 1:
220                 ls = Spacing::Single;
221                 break;
222         case 2:
223                 ls = Spacing::Onehalf;
224                 break;
225         case 3:
226                 ls = Spacing::Double;
227                 break;
228         case 4:
229                 ls = Spacing::Other;
230                 other = widgetToDoubleStr(linespacingValue);
231                 break;
232         }
233
234         Spacing const spacing(ls, other);
235         params_.spacing(spacing);
236
237         // label width
238         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
239         // indentation
240         params_.noindent(!indentCB->isChecked());
241
242         dispatchParams();
243 }
244
245
246 void GuiParagraph::updateView()
247 {
248         on_synchronizedViewCB_toggled();
249
250         ParagraphParameters const & pp = params();
251
252         // label width
253         docstring const & labelwidth = pp.labelWidthString();
254         if (hasLabelwidth()) {
255                 labelwidthGB->setEnabled(true);
256                 labelWidth->setText(toqstr(labelwidth));
257         } else {
258                 labelwidthGB->setEnabled(false);
259                 labelWidth->setText(QString());
260         }
261
262         // alignment
263         checkAlignmentRadioButtons();
264         alignmentToRadioButtons(pp.align());
265
266         //indentation
267         bool const canindent = canIndent();
268         indentCB->setEnabled(canindent);
269         indentCB->setChecked(canindent && !pp.noindent());
270
271         // linespacing
272         int ls;
273         bool pending_input = false;
274         Spacing const & space = pp.spacing();
275         if (synchronizedViewCB->isChecked() && linespacingValue->hasFocus()) {
276                 // The user is about to enter custom spacing.
277                 // We thus stay in Custom mode.
278                 // This prevents the combo from e.g. immediately
279                 // switching to single if a user enters "1" in the
280                 // linespacingValue widget while aiming at e.g. "1.3"
281                 ls = 4;
282                 pending_input = true;
283         } else {
284                 switch (space.getSpace()) {
285                 case Spacing::Single:
286                         ls = 1;
287                         break;
288                 case Spacing::Onehalf:
289                         ls = 2;
290                         break;
291                 case Spacing::Double:
292                         ls = 3;
293                         break;
294                 case Spacing::Other:
295                         ls = 4;
296                         break;
297                 default:
298                         ls = 0;
299                         break;
300                 }
301         }
302         linespacing->setCurrentIndex(ls);
303         if (space.getSpace() == Spacing::Other || pending_input) {
304                 doubleToWidget(linespacingValue, space.getValue());
305                 linespacingValue->setEnabled(true);
306         } else {
307                 linespacingValue->setText(QString());
308                 linespacingValue->setEnabled(false);
309         }
310 }
311
312
313 void GuiParagraph::enableView(bool enable)
314 {
315         indentCB->setEnabled(enable);
316         linespacing->setEnabled(enable);
317         labelWidth->setEnabled(enable);
318         synchronizedViewCB->setEnabled(enable);
319         applyPB->setEnabled(enable);
320         restorePB->setEnabled(enable);
321         if (!enable)
322                 synchronizedViewCB->setChecked(true);
323         RadioMap::const_iterator it = radioMap_.begin();
324         for (; it != radioMap_.end(); ++it)
325                 it->second->setEnabled(enable);
326 }
327
328
329 ParagraphParameters const & GuiParagraph::params() const
330 {
331         if (haveMultiParSelection()) {
332                 // FIXME: in case of multi-paragraph selection, it would be nice to
333                 // initialise the parameters that are common to all paragraphs.
334                 static ParagraphParameters const empty;
335                 return empty;
336         }
337         return bufferview()->cursor().innerParagraph().params();
338 }
339
340
341 void GuiParagraph::dispatchParams()
342 {
343         ostringstream data;
344         params_.write(data);
345         FuncRequest const fr(getLfun(), data.str());
346         dispatch(fr);
347 }
348
349
350 bool GuiParagraph::haveMultiParSelection() const
351 {
352         Cursor const & cur = bufferview()->cursor();
353         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
354 }
355
356         
357 bool GuiParagraph::canIndent() const
358 {
359         Layout const lay = bufferview()->cursor().innerParagraph().layout();
360         if (buffer().params().paragraph_separation
361                 == BufferParams::ParagraphIndentSeparation)
362                 return (lay.toggle_indent != ITOGGLE_NEVER);
363         return (lay.toggle_indent == ITOGGLE_ALWAYS);
364 }
365
366
367 LyXAlignment GuiParagraph::alignPossible() const
368 {
369         return bufferview()->cursor().innerParagraph().layout().alignpossible;
370 }
371
372
373 LyXAlignment GuiParagraph::alignDefault() const
374 {
375         return bufferview()->cursor().innerParagraph().layout().align;
376 }
377
378
379 bool GuiParagraph::hasLabelwidth() const
380 {
381         Layout layout = bufferview()->cursor().innerParagraph().layout();
382         return (layout.margintype == MARGIN_MANUAL
383                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
384 }
385
386
387 void GuiParagraph::saveSession(QSettings & settings) const
388 {
389         Dialog::saveSession(settings);
390         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
391 }
392
393
394 void GuiParagraph::restoreSession()
395 {
396         Dialog::restoreSession();
397         QSettings settings;
398         synchronizedViewCB->setChecked(
399                 settings.value(sessionKey() + "/autoapply").toBool());
400 }
401
402
403 Dialog * createGuiParagraph(GuiView & lv)
404 {
405         return new GuiParagraph(lv);
406 }
407
408
409 } // namespace frontend
410 } // namespace lyx
411
412 #include "moc_GuiParagraph.cpp"