]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiParagraph.cpp
Fix problems with immediate apply and length validators
[features.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         if (synchronizedViewCB->isChecked()
171             && !linespacingValue->text().endsWith(loc.decimalPoint())
172             && linespacingValue->hasAcceptableInput())
173                 on_applyPB_clicked();
174 }
175
176
177 void GuiParagraph::on_applyPB_clicked()
178 {
179         applyView();
180 }
181
182
183 void GuiParagraph::on_okPB_clicked()
184 {
185         applyView();
186         hide();
187 }
188
189
190 void GuiParagraph::on_closePB_clicked()
191 {
192         hide();
193 }
194
195
196 void GuiParagraph::on_restorePB_clicked()
197 {
198         updateView();
199 }
200
201
202 void GuiParagraph::applyView()
203 {
204         params_ = params();
205
206         params_.align(getAlignmentFromDialog());
207
208         // get spacing
209         Spacing::Space ls = Spacing::Default;
210         string other;
211         switch (linespacing->currentIndex()) {
212         case 0:
213                 ls = Spacing::Default;
214                 break;
215         case 1:
216                 ls = Spacing::Single;
217                 break;
218         case 2:
219                 ls = Spacing::Onehalf;
220                 break;
221         case 3:
222                 ls = Spacing::Double;
223                 break;
224         case 4:
225                 ls = Spacing::Other;
226                 other = widgetToDoubleStr(linespacingValue);
227                 break;
228         }
229
230         Spacing const spacing(ls, other);
231         params_.spacing(spacing);
232
233         // label width
234         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
235         // indentation
236         params_.noindent(!indentCB->isChecked());
237
238         dispatchParams();
239 }
240
241
242 void GuiParagraph::updateView()
243 {
244         on_synchronizedViewCB_toggled();
245
246         ParagraphParameters const & pp = params();
247
248         // label width
249         docstring const & labelwidth = pp.labelWidthString();
250         if (hasLabelwidth()) {
251                 labelwidthGB->setEnabled(true);
252                 labelWidth->setText(toqstr(labelwidth));
253         } else {
254                 labelwidthGB->setEnabled(false);
255                 labelWidth->setText(QString());
256         }
257
258         // alignment
259         checkAlignmentRadioButtons();
260         alignmentToRadioButtons(pp.align());
261
262         //indentation
263         bool const canindent = canIndent();
264         indentCB->setEnabled(canindent);
265         indentCB->setChecked(canindent && !pp.noindent());
266
267         // linespacing
268         int ls;
269         Spacing const & space = pp.spacing();
270         switch (space.getSpace()) {
271         case Spacing::Single:
272                 ls = 1;
273                 break;
274         case Spacing::Onehalf:
275                 ls = 2;
276                 break;
277         case Spacing::Double:
278                 ls = 3;
279                 break;
280         case Spacing::Other:
281                 ls = 4;
282                 break;
283         default:
284                 ls = 0;
285                 break;
286         }
287         linespacing->setCurrentIndex(ls);
288         if (space.getSpace() == Spacing::Other) {
289                 doubleToWidget(linespacingValue, space.getValue());
290                 linespacingValue->setEnabled(true);
291         } else {
292                 linespacingValue->setText(QString());
293                 linespacingValue->setEnabled(false);
294         }
295 }
296
297
298 void GuiParagraph::enableView(bool enable)
299 {
300         indentCB->setEnabled(enable);
301         linespacing->setEnabled(enable);
302         labelWidth->setEnabled(enable);
303         synchronizedViewCB->setEnabled(enable);
304         applyPB->setEnabled(enable);
305         restorePB->setEnabled(enable);
306         if (!enable)
307                 synchronizedViewCB->setChecked(true);
308         RadioMap::const_iterator it = radioMap_.begin();
309         for (; it != radioMap_.end(); ++it)
310                 it->second->setEnabled(enable);
311 }
312
313
314 ParagraphParameters const & GuiParagraph::params() const
315 {
316         if (haveMultiParSelection()) {
317                 // FIXME: in case of multi-paragraph selection, it would be nice to
318                 // initialise the parameters that are common to all paragraphs.
319                 static ParagraphParameters const empty;
320                 return empty;
321         }
322         return bufferview()->cursor().innerParagraph().params();
323 }
324
325
326 void GuiParagraph::dispatchParams()
327 {
328         ostringstream data;
329         params_.write(data);
330         FuncRequest const fr(getLfun(), data.str());
331         dispatch(fr);
332 }
333
334
335 bool GuiParagraph::haveMultiParSelection() const
336 {
337         Cursor const & cur = bufferview()->cursor();
338         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
339 }
340
341         
342 bool GuiParagraph::canIndent() const
343 {
344         Layout const lay = bufferview()->cursor().innerParagraph().layout();
345         if (buffer().params().paragraph_separation
346                 == BufferParams::ParagraphIndentSeparation)
347                 return (lay.toggle_indent != ITOGGLE_NEVER);
348         return (lay.toggle_indent == ITOGGLE_ALWAYS);
349 }
350
351
352 LyXAlignment GuiParagraph::alignPossible() const
353 {
354         return bufferview()->cursor().innerParagraph().layout().alignpossible;
355 }
356
357
358 LyXAlignment GuiParagraph::alignDefault() const
359 {
360         return bufferview()->cursor().innerParagraph().layout().align;
361 }
362
363
364 bool GuiParagraph::hasLabelwidth() const
365 {
366         Layout layout = bufferview()->cursor().innerParagraph().layout();
367         return (layout.margintype == MARGIN_MANUAL
368                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
369 }
370
371
372 void GuiParagraph::saveSession() const
373 {
374         Dialog::saveSession();
375         QSettings settings;
376         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
377 }
378
379
380 void GuiParagraph::restoreSession()
381 {
382         Dialog::restoreSession();
383         QSettings settings;
384         synchronizedViewCB->setChecked(
385                 settings.value(sessionKey() + "/autoapply").toBool());
386 }
387
388
389 Dialog * createGuiParagraph(GuiView & lv)
390 {
391         return new GuiParagraph(lv);
392 }
393
394
395 } // namespace frontend
396 } // namespace lyx
397
398 #include "moc_GuiParagraph.cpp"