]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
getting rid of superfluous lyx::support:: statements.
[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 "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Cursor.h"
22 //#include "DialogView.h"
23 //#include "DockView.h"
24 #include "FuncRequest.h"
25 #include "GuiView.h"
26 #include "Lexer.h"
27 #include "Paragraph.h"
28 #include "ParagraphParameters.h"
29 #include "qt_helpers.h"
30 #include "Spacing.h"
31
32 #include "support/debug.h"
33 #include "support/gettext.h"
34
35 #include <QCheckBox>
36 #include <QCloseEvent>
37 #include <QLineEdit>
38 #include <QPushButton>
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")
49 {
50         setupUi(this);
51         setWindowTitle(qt_("Paragraph Settings"));
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
66         synchronizedViewCB->setChecked(false);
67         on_synchronizedViewCB_toggled();
68         linespacingValue->setValidator(new QDoubleValidator(linespacingValue));
69
70         labelWidth->setWhatsThis( qt_(
71                 "As described in the User Guide, the width of"
72                 " this text determines the width of the label part"
73                 " of each item in environments like List and"
74                 " Description.\n"
75                 "\n"
76                 " Normally, you won't need to set this,"
77                 " since the largest label width of all the"
78                 " items is used."
79         ));
80
81         radioMap[LYX_ALIGN_LAYOUT] = alignDefaultRB;
82         radioMap[LYX_ALIGN_BLOCK]  = alignJustRB;
83         radioMap[LYX_ALIGN_LEFT]   = alignLeftRB;
84         radioMap[LYX_ALIGN_RIGHT]  = alignRightRB;
85         radioMap[LYX_ALIGN_CENTER] = alignCenterRB;
86
87         labelMap[LYX_ALIGN_BLOCK]  = _("Justified");
88         labelMap[LYX_ALIGN_LEFT]   = _("Left");
89         labelMap[LYX_ALIGN_RIGHT]  = _("Right");
90         labelMap[LYX_ALIGN_CENTER] = _("Center");
91
92         const_cast<QString &>(alignDefaultLabel) = alignDefaultRB->text();
93 }
94
95
96 void GuiParagraph::on_linespacing_activated(int index)
97 {
98         linespacingValue->setEnabled(index == 4);
99 }
100
101
102 void GuiParagraph::checkAlignmentRadioButtons()
103 {
104         RadioMap::iterator it = radioMap.begin();
105         for (; it != radioMap.end(); ++it) {
106                 LyXAlignment const align = it->first;
107                 it->second->setEnabled(align & alignPossible());
108         }
109         if (haveMulitParSelection())
110                 alignDefaultRB->setText(alignDefaultLabel);
111         else
112                 alignDefaultRB->setText(alignDefaultLabel + " ("
113                         + toqstr(labelMap[alignDefault()]) + ")");
114 }
115
116
117 void GuiParagraph::alignmentToRadioButtons(LyXAlignment align)
118 {
119         RadioMap::const_iterator it = radioMap.begin();
120         for (;it != radioMap.end(); ++it) {
121                 if (!it->second->isEnabled())
122                         continue;
123                 it->second->blockSignals(true);
124                 it->second->setChecked(align == it->first);
125                 it->second->blockSignals(false);
126         }
127 }
128
129
130 LyXAlignment GuiParagraph::getAlignmentFromDialog()
131 {
132         LyXAlignment alignment = LYX_ALIGN_NONE;
133         RadioMap::const_iterator it = radioMap.begin();
134         for (; it != radioMap.end(); ++it) {
135                 if (it->second->isChecked()) {
136                         alignment = it->first;
137                         break;
138                 }
139         }
140         return alignment;
141 }
142
143
144 void GuiParagraph::on_synchronizedViewCB_toggled()
145 {
146         bool in_sync = synchronizedViewCB->isChecked();
147         restorePB->setEnabled(!in_sync);
148         applyPB->setEnabled(!in_sync);
149 }
150
151
152 void GuiParagraph::changed()
153 {
154         if (synchronizedViewCB->isChecked())
155                 on_applyPB_clicked();
156 }
157
158
159 void GuiParagraph::on_applyPB_clicked()
160 {
161         ParagraphParameters & pp = params();
162
163         pp.align(getAlignmentFromDialog());
164
165         // get spacing
166         Spacing::Space ls = Spacing::Default;
167         string other;
168         switch (linespacing->currentIndex()) {
169         case 0:
170                 ls = Spacing::Default;
171                 break;
172         case 1:
173                 ls = Spacing::Single;
174                 break;
175         case 2:
176                 ls = Spacing::Onehalf;
177                 break;
178         case 3:
179                 ls = Spacing::Double;
180                 break;
181         case 4:
182                 ls = Spacing::Other;
183                 other = fromqstr(linespacingValue->text());
184                 break;
185         }
186
187         Spacing const spacing(ls, other);
188         pp.spacing(spacing);
189
190         // label width
191         pp.labelWidthString(qstring_to_ucs4(labelWidth->text()));
192         // indendation
193         pp.noindent(!indentCB->isChecked());
194
195         dispatchParams();
196 }
197
198
199 void GuiParagraph::on_restorePB_clicked()
200 {
201         updateView();
202 }
203
204
205 void GuiParagraph::enableView(bool enable)
206 {
207 }
208
209
210 void GuiParagraph::updateView()
211 {
212         on_synchronizedViewCB_toggled();
213
214         ParagraphParameters const & pp = params();
215
216         // label width
217         docstring const & labelwidth = pp.labelWidthString();
218         // FIXME We should not compare translated strings
219         if (labelwidth != _("Senseless with this layout!")) {
220                 labelwidthGB->setEnabled(true);
221                 labelWidth->setText(toqstr(labelwidth));
222         } else {
223                 labelwidthGB->setEnabled(false);
224                 labelWidth->setText("");
225         }
226
227         // alignment
228         checkAlignmentRadioButtons();
229         alignmentToRadioButtons(pp.align());
230
231         //indentation
232         bool const canindent = canIndent();
233         indentCB->setEnabled(canindent);
234         indentCB->setChecked(canindent && !pp.noindent());
235
236         // linespacing
237         int ls;
238         Spacing const & space = pp.spacing();
239         switch (space.getSpace()) {
240         case Spacing::Single:
241                 ls = 1;
242                 break;
243         case Spacing::Onehalf:
244                 ls = 2;
245                 break;
246         case Spacing::Double:
247                 ls = 3;
248                 break;
249         case Spacing::Other:
250                 ls = 4;
251                 break;
252         default:
253                 ls = 0;
254                 break;
255         }
256         linespacing->setCurrentIndex(ls);
257         if (space.getSpace() == Spacing::Other) {
258                 linespacingValue->setText(toqstr(space.getValueAsString()));
259                 linespacingValue->setEnabled(true);
260         } else {
261                 linespacingValue->setText(QString());
262                 linespacingValue->setEnabled(false);
263         }
264 }
265
266
267 ParagraphParameters & GuiParagraph::params()
268 {
269         if (haveMulitParSelection()) {
270                 multiparsel_ = ParagraphParameters();
271                 // FIXME: It would be nice to initialise the parameters that
272                 // are common to all paragraphs.
273                 return multiparsel_;
274         }
275
276         return bufferview()->cursor().innerParagraph().params();
277 }
278
279
280 ParagraphParameters const & GuiParagraph::params() const
281 {
282         return bufferview()->cursor().innerParagraph().params();
283 }
284
285
286 void GuiParagraph::dispatchParams()
287 {
288         if (haveMulitParSelection()) {
289                 ostringstream data;
290                 multiparsel_.write(data);
291                 FuncRequest const fr(LFUN_PARAGRAPH_PARAMS_APPLY, data.str());
292                 dispatch(fr);
293                 return;
294         }
295
296         bufferview()->updateMetrics();
297         bufferview()->buffer().changed();
298 }
299
300
301 bool GuiParagraph::haveMulitParSelection()
302 {
303         Cursor cur = bufferview()->cursor();
304         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
305 }
306
307         
308 bool GuiParagraph::canIndent() const
309 {
310         return buffer().params().paragraph_separation ==
311                 BufferParams::PARSEP_INDENT;
312 }
313
314
315 LyXAlignment GuiParagraph::alignPossible() const
316 {
317         return bufferview()->cursor().innerParagraph().layout()->alignpossible;
318 }
319
320
321 LyXAlignment GuiParagraph::alignDefault() const
322 {
323         return bufferview()->cursor().innerParagraph().layout()->align;
324 }
325
326
327 Dialog * createGuiParagraph(GuiView & lv)
328 {
329 #if 0
330         GuiView & guiview = static_cast<GuiView &>(lv);
331 #ifdef USE_DOCK_WIDGET
332         return new DockView<ControlParagraph, GuiParagraph>(guiview, "paragraph",
333                 Qt::TopDockWidgetArea);
334 #else
335         return new DialogView<ControlParagraph, GuiParagraph>(guiview, "paragraph");
336 #endif
337 #endif
338
339         return new GuiParagraph(lv);
340 }
341
342
343 } // namespace frontend
344 } // namespace lyx
345
346 #include "GuiParagraph_moc.cpp"