]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
Remove the .aux and .bbl files and update the citation labels
[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() const
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         params_ = params();
200
201         params_.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 = widgetToDoubleStr(linespacingValue);
222                 break;
223         }
224
225         Spacing const spacing(ls, other);
226         params_.spacing(spacing);
227
228         // label width
229         params_.labelWidthString(qstring_to_ucs4(labelWidth->text()));
230         // indendation
231         params_.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         if (hasLabelwidth()) {
246                 labelwidthGB->setEnabled(true);
247                 labelWidth->setText(toqstr(labelwidth));
248         } else {
249                 labelwidthGB->setEnabled(false);
250                 labelWidth->setText(QString());
251         }
252
253         // alignment
254         checkAlignmentRadioButtons();
255         alignmentToRadioButtons(pp.align());
256
257         //indentation
258         bool const canindent = canIndent();
259         indentCB->setEnabled(canindent);
260         indentCB->setChecked(canindent && !pp.noindent());
261
262         // linespacing
263         int ls;
264         Spacing const & space = pp.spacing();
265         switch (space.getSpace()) {
266         case Spacing::Single:
267                 ls = 1;
268                 break;
269         case Spacing::Onehalf:
270                 ls = 2;
271                 break;
272         case Spacing::Double:
273                 ls = 3;
274                 break;
275         case Spacing::Other:
276                 ls = 4;
277                 break;
278         default:
279                 ls = 0;
280                 break;
281         }
282         linespacing->setCurrentIndex(ls);
283         if (space.getSpace() == Spacing::Other) {
284                 doubleToWidget(linespacingValue, space.getValue());
285                 linespacingValue->setEnabled(true);
286         } else {
287                 linespacingValue->setText(QString());
288                 linespacingValue->setEnabled(false);
289         }
290 }
291
292
293 void GuiParagraph::enableView(bool enable)
294 {
295         indentCB->setEnabled(enable);
296         linespacing->setEnabled(enable);
297         labelWidth->setEnabled(enable);
298         synchronizedViewCB->setEnabled(enable);
299         applyPB->setEnabled(enable);
300         restorePB->setEnabled(enable);
301         if (!enable)
302                 synchronizedViewCB->setChecked(true);
303         RadioMap::const_iterator it = radioMap_.begin();
304         for (; it != radioMap_.end(); ++it)
305                 it->second->setEnabled(enable);
306 }
307
308
309 ParagraphParameters const & GuiParagraph::params() const
310 {
311         if (haveMultiParSelection()) {
312                 // FIXME: in case of multi-paragraph selection, it would be nice to
313                 // initialise the parameters that are common to all paragraphs.
314                 static ParagraphParameters empty;
315                 return empty;
316         }
317         return bufferview()->cursor().innerParagraph().params();
318 }
319
320
321 void GuiParagraph::dispatchParams()
322 {
323         ostringstream data;
324         params_.write(data);
325         FuncRequest const fr(getLfun(), data.str());
326         dispatch(fr);
327 }
328
329
330 bool GuiParagraph::haveMultiParSelection() const
331 {
332         Cursor const & cur = bufferview()->cursor();
333         return cur.selection() && cur.selBegin().pit() != cur.selEnd().pit();
334 }
335
336         
337 bool GuiParagraph::canIndent() const
338 {
339         return buffer().params().paragraph_separation
340                 == BufferParams::ParagraphIndentSeparation;
341 }
342
343
344 LyXAlignment GuiParagraph::alignPossible() const
345 {
346         return bufferview()->cursor().innerParagraph().layout().alignpossible;
347 }
348
349
350 LyXAlignment GuiParagraph::alignDefault() const
351 {
352         return bufferview()->cursor().innerParagraph().layout().align;
353 }
354
355
356 bool GuiParagraph::hasLabelwidth() const
357 {
358         Layout layout = bufferview()->cursor().innerParagraph().layout();
359         return (layout.margintype == MARGIN_MANUAL
360                 || layout.latextype == LATEX_BIB_ENVIRONMENT);
361 }
362
363
364 void GuiParagraph::saveSession() const
365 {
366         Dialog::saveSession();
367         QSettings settings;
368         settings.setValue(sessionKey() + "/autoapply", synchronizedViewCB->isChecked());
369 }
370
371
372 void GuiParagraph::restoreSession()
373 {
374         Dialog::restoreSession();
375         QSettings settings;
376         synchronizedViewCB->setChecked(
377                 settings.value(sessionKey() + "/autoapply").toBool());
378 }
379
380
381 Dialog * createGuiParagraph(GuiView & lv)
382 {
383         return new GuiParagraph(lv);
384 }
385
386
387 } // namespace frontend
388 } // namespace lyx
389
390 #include "moc_GuiParagraph.cpp"