]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiParagraph.cpp
Rename frontend qt4 to qt
[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 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         // fix height to minimum
54         setFixedHeight(sizeHint().height());
55
56         connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(changed()));
57         connect(alignJustRB, SIGNAL(clicked()), this, SLOT(changed()));
58         connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(changed()));
59         connect(alignRightRB, SIGNAL(clicked()), this, SLOT(changed()));
60         connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(changed()));
61         connect(linespacing, SIGNAL(activated(int)), this, SLOT(changed()));
62         connect(linespacingValue, SIGNAL(textChanged(QString)),
63                 this, SLOT(changed()));
64         connect(indentCB, SIGNAL(clicked()), this, SLOT(changed()));
65         connect(labelWidth, SIGNAL(textChanged(QString)),
66                 this, SLOT(changed()));
67
68 #ifdef Q_OS_MAC
69         // On Mac it's common to have tool windows which are always in the
70         // foreground and are hidden when the main window is not focused.
71         setWindowFlags(Qt::Tool);
72         synchronizedViewCB->setChecked(true);
73 #else
74         synchronizedViewCB->setChecked(false);
75 #endif
76
77         on_synchronizedViewCB_toggled();
78         QDoubleValidator * val = new QDoubleValidator(linespacingValue);
79         val->setNotation(QDoubleValidator::StandardNotation);
80         linespacingValue->setValidator(val);
81
82         labelWidth->setWhatsThis(qt_(
83                 "As described in the User Guide, the width of"
84                 " this text determines the width of the label part"
85                 " of each item in environments like List and"
86                 " Description.\n"
87                 "\n"
88                 " Normally, you won't need to set this,"
89                 " since the largest label width of all the"
90                 " items is used."
91         ));
92
93         radioMap_[LYX_ALIGN_LAYOUT] = alignDefaultRB;
94         radioMap_[LYX_ALIGN_BLOCK]  = alignJustRB;
95         radioMap_[LYX_ALIGN_LEFT]   = alignLeftRB;
96         radioMap_[LYX_ALIGN_RIGHT]  = alignRightRB;
97         radioMap_[LYX_ALIGN_CENTER] = alignCenterRB;
98
99         alignDefaultLabel_ = alignDefaultRB->text();
100 }
101
102
103 void GuiParagraph::on_linespacing_activated(int index)
104 {
105         linespacingValue->setEnabled(index == 4);
106 }
107
108
109 void GuiParagraph::checkAlignmentRadioButtons()
110 {
111         static std::map<LyXAlignment, QString> labelMap_;
112         if (labelMap_.empty()) {
113                 labelMap_[LYX_ALIGN_BLOCK]  = qt_("Justified");
114                 labelMap_[LYX_ALIGN_LEFT]   = qt_("Left");
115                 labelMap_[LYX_ALIGN_RIGHT]  = qt_("Right");
116                 labelMap_[LYX_ALIGN_CENTER] = qt_("Center");
117         }
118
119         RadioMap::iterator it = radioMap_.begin();
120         for (; it != radioMap_.end(); ++it) {
121                 LyXAlignment const align = it->first;
122                 it->second->setEnabled(align & alignPossible());
123         }
124         if (haveMultiParSelection())
125                 alignDefaultRB->setText(alignDefaultLabel_);
126         else
127                 alignDefaultRB->setText(alignDefaultLabel_ + " ("
128                         + labelMap_[bufferview()->cursor().innerParagraph().getDefaultAlign(buffer().params())] + ")");
129 }
130
131
132 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
133 {
134         RadioMap::const_iterator it = radioMap_.begin();
135         for (;it != radioMap_.end(); ++it) {
136                 it->second->blockSignals(true);
137                 it->second->setChecked(align == it->first);
138                 it->second->blockSignals(false);
139         }
140 }
141
142
143 LyXAlignment GuiParagraph::getAlignmentFromDialog() const
144 {
145         LyXAlignment alignment = LYX_ALIGN_NONE;
146         RadioMap::const_iterator it = radioMap_.begin();
147         for (; it != radioMap_.end(); ++it) {
148                 if (it->second->isChecked()) {
149                         alignment = it->first;
150                         break;
151                 }
152         }
153         return alignment;
154 }
155
156
157 void GuiParagraph::on_synchronizedViewCB_toggled()
158 {
159         bool in_sync = synchronizedViewCB->isChecked();
160         buttonBox->button(QDialogButtonBox::Reset)->setEnabled(!in_sync);
161         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!in_sync);
162         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!in_sync);
163         if (!in_sync)
164                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Cancel"));
165         else
166                 buttonBox->button(QDialogButtonBox::Cancel)->setText(qt_("&Close"));
167 }
168
169
170 void GuiParagraph::changed()
171 {
172         QLocale loc;
173         // We apply immediately, except if we have custom line spacing
174         // with an intermediate result (trailing decimal separator) or
175         // an invalid value (which might as well be intermediate)
176         if (synchronizedViewCB->isChecked()
177             && (linespacing->currentIndex() != 4
178                 || (!linespacingValue->text().endsWith(loc.decimalPoint())
179                     && linespacingValue->hasAcceptableInput())))
180                 applyView();
181 }
182
183
184 void GuiParagraph::on_buttonBox_clicked(QAbstractButton * button)
185 {
186         switch (buttonBox->standardButton(button)) {
187         case QDialogButtonBox::Ok:
188                 applyView();
189                 hide();
190                 break;
191         case QDialogButtonBox::Apply:
192                 applyView();
193                 break;
194         case QDialogButtonBox::Cancel:
195                 hide();
196                 break;
197         case QDialogButtonBox::Reset:
198                 updateView();
199                 break;
200         default:
201                 break;
202         }
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         // Somewhere in the chain this can lose default status (#11417)
311         buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
312 }
313
314
315 void GuiParagraph::enableView(bool enable)
316 {
317         indentCB->setEnabled(enable);
318         linespacing->setEnabled(enable);
319         labelWidth->setEnabled(enable);
320         synchronizedViewCB->setEnabled(enable);
321         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
322         buttonBox->button(QDialogButtonBox::Reset)->setEnabled(enable);
323         if (!enable)
324                 synchronizedViewCB->setChecked(true);
325         RadioMap::const_iterator it = radioMap_.begin();
326         for (; it != radioMap_.end(); ++it)
327                 it->second->setEnabled(enable);
328 }
329
330
331 ParagraphParameters const & GuiParagraph::params() const
332 {
333         if (haveMultiParSelection()) {
334                 // FIXME: in case of multi-paragraph selection, it would be nice to
335                 // initialise the parameters that are common to all paragraphs.
336                 static ParagraphParameters const empty;
337                 return empty;
338         }
339         return bufferview()->cursor().innerParagraph().params();
340 }
341
342
343 void GuiParagraph::dispatchParams()
344 {
345         ostringstream os;
346         params_.write(os);
347         FuncRequest const fr(getLfun(), os.str());
348         dispatch(fr);
349 }
350
351
352 bool GuiParagraph::haveMultiParSelection() const
353 {
354         Cursor const & cur = bufferview()->cursor();
355         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
356 }
357
358
359 bool GuiParagraph::canIndent() const
360 {
361         Layout const lay = bufferview()->cursor().innerParagraph().layout();
362         if (buffer().params().paragraph_separation
363                 == BufferParams::ParagraphIndentSeparation)
364                 return (lay.toggle_indent != ITOGGLE_NEVER);
365         return (lay.toggle_indent == ITOGGLE_ALWAYS);
366 }
367
368
369 LyXAlignment GuiParagraph::alignPossible() const
370 {
371         return bufferview()->cursor().innerParagraph().layout().alignpossible;
372 }
373
374
375 bool GuiParagraph::hasLabelwidth() const
376 {
377         Layout layout = bufferview()->cursor().innerParagraph().layout();
378         return (layout.margintype == MARGIN_MANUAL
379                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
380 }
381
382
383 void GuiParagraph::saveSession(QSettings & settings) const
384 {
385         Dialog::saveSession(settings);
386         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
387 }
388
389
390 void GuiParagraph::restoreSession()
391 {
392         Dialog::restoreSession();
393         QSettings settings;
394         synchronizedViewCB->setChecked(
395                 settings.value(sessionKey() + "/autoapply").toBool());
396 }
397
398
399 Dialog * createGuiParagraph(GuiView & lv)
400 {
401         return new GuiParagraph(lv);
402 }
403
404
405 } // namespace frontend
406 } // namespace lyx
407
408 #include "moc_GuiParagraph.cpp"