]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QParagraph.cpp
delete unneeded Menubar virtual interface.
[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                 it->second->setEnabled(align & alignPossible);
121         }
122         docstring label = labelMap[LYX_ALIGN_LAYOUT];
123         if (!form_->controller().haveMulitParSelection())
124                 label += (" (" + labelMap[form_->controller().alignDefault()] + ")");
125         alignDefaultRB->setText(toqstr(label));
126 }
127
128
129 void QParagraphDialog::alignmentToRadioButtons(LyXAlignment align)
130 {
131         QPRadioMap::const_iterator it = radioMap.begin();
132         for (;it != radioMap.end(); ++it) {
133                 if (align == it->first) {
134                         it->second->blockSignals(true);
135                         it->second->setChecked(true);
136                         it->second->blockSignals(false);
137                         return;
138                 }
139         }
140
141         lyxerr << BOOST_CURRENT_FUNCTION << "Unknown alignment "
142                 << align << std::endl;
143 }
144
145
146 LyXAlignment QParagraphDialog::getAlignmentFromDialog()
147 {
148         LyXAlignment alignment = LYX_ALIGN_NONE;
149         QPRadioMap::const_iterator it = radioMap.begin();
150         for (; it != radioMap.end(); ++it) {
151                 if (it->second->isChecked()) {
152                         alignment = it->first;
153                         break;
154                 }
155         }
156         return alignment;
157 }
158
159
160 /////////////////////////////////////////////////////////////////////
161 //
162 // QParagraph
163 //
164 /////////////////////////////////////////////////////////////////////
165
166 typedef QController<ControlParagraph, QView<QParagraphDialog> >
167         ParagraphBase;
168
169
170 QParagraph::QParagraph(Dialog & parent)
171         : ParagraphBase(parent, _("Paragraph Settings"))
172 {}
173
174
175 void QParagraph::build_dialog()
176 {
177         // the dialog
178         dialog_.reset(new QParagraphDialog(this));
179
180         // Manage the ok, apply, restore and cancel/close buttons
181         bcview().setOK(dialog_->okPB);
182         bcview().setApply(dialog_->applyPB);
183         bcview().setCancel(dialog_->closePB);
184         bcview().setRestore(dialog_->restorePB);
185 }
186
187
188 void QParagraph::apply()
189 {
190         ParagraphParameters & params = controller().params();
191
192         params.align(dialog_->getAlignmentFromDialog());
193
194         // get spacing
195         Spacing::Space linespacing = Spacing::Default;
196         string other;
197         switch (dialog_->linespacing->currentIndex()) {
198         case 0:
199                 linespacing = Spacing::Default;
200                 break;
201         case 1:
202                 linespacing = Spacing::Single;
203                 break;
204         case 2:
205                 linespacing = Spacing::Onehalf;
206                 break;
207         case 3:
208                 linespacing = Spacing::Double;
209                 break;
210         case 4:
211                 linespacing = Spacing::Other;
212                 other = fromqstr(dialog_->linespacingValue->text());
213                 break;
214         }
215
216         Spacing const spacing(linespacing, other);
217         params.spacing(spacing);
218
219         // label width
220         params.labelWidthString(qstring_to_ucs4(dialog_->labelWidth->text()));
221         // indendation
222         params.noindent(!dialog_->indentCB->isChecked());
223 }
224
225
226 void QParagraph::update_contents()
227 {
228         ParagraphParameters const & params = controller().params();
229
230         // label width
231         docstring const & labelwidth = params.labelWidthString();
232         // FIXME We should not compare translated strings
233         if (labelwidth != _("Senseless with this layout!")) {
234                 dialog_->labelwidthGB->setEnabled(true);
235                 dialog_->labelWidth->setText(toqstr(labelwidth));
236         } else {
237                 dialog_->labelwidthGB->setEnabled(false);
238                 dialog_->labelWidth->setText("");
239         }
240
241         // alignment
242         dialog_->checkAlignmentRadioButtons();
243         dialog_->alignmentToRadioButtons(params.align());
244
245         //indentation
246         bool const canindent = controller().canIndent();
247         dialog_->indentCB->setEnabled(canindent);
248         dialog_->indentCB->setChecked(canindent && !params.noindent());
249
250         // linespacing
251         int linespacing;
252         Spacing const & space = params.spacing();
253         switch (space.getSpace()) {
254         case Spacing::Single:
255                 linespacing = 1;
256                 break;
257         case Spacing::Onehalf:
258                 linespacing = 2;
259                 break;
260         case Spacing::Double:
261                 linespacing = 3;
262                 break;
263         case Spacing::Other:
264                 linespacing = 4;
265                 break;
266         default:
267                 linespacing = 0;
268                 break;
269         }
270         dialog_->linespacing->setCurrentIndex(linespacing);
271         if (space.getSpace() == Spacing::Other) {
272                 dialog_->linespacingValue->setText(toqstr(space.getValueAsString()));
273                 dialog_->linespacingValue->setEnabled(true);
274         } else {
275                 dialog_->linespacingValue->setText("");
276                 dialog_->linespacingValue->setEnabled(false);
277         }
278 }
279
280 } // namespace frontend
281 } // namespace lyx
282
283 #include "QParagraph_moc.cpp"