]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiParagraph.cpp
4a6c3d3bc30d223ffe6d2e859a3e65ba75f3189b
[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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiParagraph.h"
15 #include "Qt2BC.h"
16 #include "qt_helpers.h"
17
18 #include "debug.h"
19 #include "ParagraphParameters.h"
20 #include "Spacing.h"
21 #include "frontend_helpers.h"
22
23 #include <QCheckBox>
24 #include <QCloseEvent>
25 #include <QLineEdit>
26 #include <QPushButton>
27
28 using std::string;
29 using std::endl;
30
31 /////////////////////////////////////////////////////////////////////
32 //
33 // GuiParagraphDialog
34 //
35 /////////////////////////////////////////////////////////////////////
36
37
38 #include <boost/current_function.hpp>
39
40 namespace lyx {
41 namespace frontend {
42
43 GuiParagraphDialog::GuiParagraphDialog(GuiParagraph * form)
44         : form_(form)
45 {
46         setupUi(this);
47
48         connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
49         connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
50         connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
51         connect(restorePB, SIGNAL(clicked()), form_, SLOT(slotRestore()));
52         connect(alignDefaultRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
53         connect(alignJustRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
54         connect(alignLeftRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
55         connect(alignRightRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
56         connect(alignCenterRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
57         connect(linespacing, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
58         connect(linespacing, SIGNAL(activated(int)),
59                 this, SLOT(enableLinespacingValue(int)));
60         connect(linespacingValue, SIGNAL(textChanged(const QString &)),
61                 this, SLOT(change_adaptor()));
62         connect(indentCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
63         connect(labelWidth, SIGNAL(textChanged(const QString &)),
64                 this, SLOT(change_adaptor()));
65
66         linespacingValue->setValidator(new QDoubleValidator(linespacingValue));
67
68         labelWidth->setWhatsThis( qt_(
69                 "As described in the User Guide, the width of"
70                 " this text determines the width of the label part"
71                 " of each item in environments like List and"
72                 " Description.\n"
73                 "\n"
74                 " Normally, you won't need to set this,"
75                 " since the largest label width of all the"
76                 " items is used."
77         ));
78
79         radioMap[LYX_ALIGN_LAYOUT] = alignDefaultRB;
80         radioMap[LYX_ALIGN_BLOCK]  = alignJustRB;
81         radioMap[LYX_ALIGN_LEFT]   = alignLeftRB;
82         radioMap[LYX_ALIGN_RIGHT]  = alignRightRB;
83         radioMap[LYX_ALIGN_CENTER] = alignCenterRB;
84         
85         labelMap[LYX_ALIGN_LAYOUT] = _("Use Paragraph's Default Alignment");
86         labelMap[LYX_ALIGN_BLOCK]  = _("Justified");
87         labelMap[LYX_ALIGN_LEFT]   = _("Left");
88         labelMap[LYX_ALIGN_RIGHT]  = _("Right");
89         labelMap[LYX_ALIGN_CENTER] = _("Center");
90 }
91
92
93 void GuiParagraphDialog::closeEvent(QCloseEvent * e)
94 {
95         form_->slotWMHide();
96         e->accept();
97 }
98
99
100 void GuiParagraphDialog::change_adaptor()
101 {
102         form_->changed();
103 }
104
105
106 void GuiParagraphDialog::enableLinespacingValue(int)
107 {
108         bool const enable = linespacing->currentIndex() == 4;
109         linespacingValue->setEnabled(enable);
110 }
111
112
113 void GuiParagraphDialog::checkAlignmentRadioButtons() {
114         LyXAlignment const alignPossible = form_->controller().alignPossible();
115
116         QPRadioMap::iterator it = radioMap.begin();
117         for (; it != radioMap.end(); ++it) {
118                 LyXAlignment const align = it->first;
119                 it->second->setEnabled(align & alignPossible);
120         }
121         docstring label = labelMap[LYX_ALIGN_LAYOUT];
122         if (!form_->controller().haveMulitParSelection())
123                 label += (" (" + labelMap[form_->controller().alignDefault()] + ")");
124         alignDefaultRB->setText(toqstr(label));
125 }
126
127
128 void GuiParagraphDialog::alignmentToRadioButtons(LyXAlignment align)
129 {
130         QPRadioMap::const_iterator it = radioMap.begin();
131         for (;it != radioMap.end(); ++it) {
132                 if (align == it->first) {
133                         it->second->blockSignals(true);
134                         it->second->setChecked(true);
135                         it->second->blockSignals(false);
136                         return;
137                 }
138         }
139
140         lyxerr << BOOST_CURRENT_FUNCTION << "Unknown alignment "
141                 << align << std::endl;
142 }
143
144
145 LyXAlignment GuiParagraphDialog::getAlignmentFromDialog()
146 {
147         LyXAlignment alignment = LYX_ALIGN_NONE;
148         QPRadioMap::const_iterator it = radioMap.begin();
149         for (; it != radioMap.end(); ++it) {
150                 if (it->second->isChecked()) {
151                         alignment = it->first;
152                         break;
153                 }
154         }
155         return alignment;
156 }
157
158
159 /////////////////////////////////////////////////////////////////////
160 //
161 // GuiParagraph
162 //
163 /////////////////////////////////////////////////////////////////////
164
165
166 GuiParagraph::GuiParagraph(Dialog & parent)
167         : GuiView<GuiParagraphDialog>(parent, _("Paragraph Settings"))
168 {}
169
170
171 void GuiParagraph::build_dialog()
172 {
173         // the dialog
174         dialog_.reset(new GuiParagraphDialog(this));
175
176         // Manage the ok, apply, restore and cancel/close buttons
177         bcview().setOK(dialog_->okPB);
178         bcview().setApply(dialog_->applyPB);
179         bcview().setCancel(dialog_->closePB);
180         bcview().setRestore(dialog_->restorePB);
181 }
182
183
184 void GuiParagraph::apply()
185 {
186         ParagraphParameters & params = controller().params();
187
188         params.align(dialog_->getAlignmentFromDialog());
189
190         // get spacing
191         Spacing::Space linespacing = Spacing::Default;
192         string other;
193         switch (dialog_->linespacing->currentIndex()) {
194         case 0:
195                 linespacing = Spacing::Default;
196                 break;
197         case 1:
198                 linespacing = Spacing::Single;
199                 break;
200         case 2:
201                 linespacing = Spacing::Onehalf;
202                 break;
203         case 3:
204                 linespacing = Spacing::Double;
205                 break;
206         case 4:
207                 linespacing = Spacing::Other;
208                 other = fromqstr(dialog_->linespacingValue->text());
209                 break;
210         }
211
212         Spacing const spacing(linespacing, other);
213         params.spacing(spacing);
214
215         // label width
216         params.labelWidthString(qstring_to_ucs4(dialog_->labelWidth->text()));
217         // indendation
218         params.noindent(!dialog_->indentCB->isChecked());
219 }
220
221
222 void GuiParagraph::update_contents()
223 {
224         ParagraphParameters const & params = controller().params();
225
226         // label width
227         docstring const & labelwidth = params.labelWidthString();
228         // FIXME We should not compare translated strings
229         if (labelwidth != _("Senseless with this layout!")) {
230                 dialog_->labelwidthGB->setEnabled(true);
231                 dialog_->labelWidth->setText(toqstr(labelwidth));
232         } else {
233                 dialog_->labelwidthGB->setEnabled(false);
234                 dialog_->labelWidth->setText("");
235         }
236
237         // alignment
238         dialog_->checkAlignmentRadioButtons();
239         dialog_->alignmentToRadioButtons(params.align());
240
241         //indentation
242         bool const canindent = controller().canIndent();
243         dialog_->indentCB->setEnabled(canindent);
244         dialog_->indentCB->setChecked(canindent && !params.noindent());
245
246         // linespacing
247         int linespacing;
248         Spacing const & space = params.spacing();
249         switch (space.getSpace()) {
250         case Spacing::Single:
251                 linespacing = 1;
252                 break;
253         case Spacing::Onehalf:
254                 linespacing = 2;
255                 break;
256         case Spacing::Double:
257                 linespacing = 3;
258                 break;
259         case Spacing::Other:
260                 linespacing = 4;
261                 break;
262         default:
263                 linespacing = 0;
264                 break;
265         }
266         dialog_->linespacing->setCurrentIndex(linespacing);
267         if (space.getSpace() == Spacing::Other) {
268                 dialog_->linespacingValue->setText(toqstr(space.getValueAsString()));
269                 dialog_->linespacingValue->setEnabled(true);
270         } else {
271                 dialog_->linespacingValue->setText("");
272                 dialog_->linespacingValue->setEnabled(false);
273         }
274 }
275
276 } // namespace frontend
277 } // namespace lyx
278
279 #include "GuiParagraph_moc.cpp"