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