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