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