]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QParagraph.cpp
* src/frontends/qt4/QParagraph.cpp: reduce the verbosity of a tooltip
[features.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(alignDefaultCB, 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_BLOCK] = alignJustRB;
81         radioMap[LYX_ALIGN_LEFT] = alignLeftRB;
82         radioMap[LYX_ALIGN_RIGHT] = alignRightRB;
83         radioMap[LYX_ALIGN_CENTER] = alignCenterRB;
84 }
85
86
87 void QParagraphDialog::closeEvent(QCloseEvent * e)
88 {
89         form_->slotWMHide();
90         e->accept();
91 }
92
93
94 void QParagraphDialog::change_adaptor()
95 {
96         form_->changed();
97 }
98
99
100 void QParagraphDialog::enableLinespacingValue(int)
101 {
102         bool const enable = linespacing->currentIndex() == 4;
103         linespacingValue->setEnabled(enable);
104 }
105
106
107 void QParagraphDialog::checkAlignmentRadioButtons() {
108         if (alignDefaultCB->isChecked()) {
109                 QPRadioMap::const_iterator it = radioMap.begin();
110                 for (; it != radioMap.end(); ++it)
111                         it->second->setDisabled(true);
112         } else {
113                 LyXAlignment alignPossible = form_->controller().alignPossible();
114                 QPRadioMap::const_iterator it = radioMap.begin();
115                 for (; it != radioMap.end(); ++it)
116                         it->second->setEnabled(it->first & alignPossible);
117         }
118 }
119
120
121 void QParagraphDialog::on_alignDefaultCB_toggled(bool)
122 {
123         checkAlignmentRadioButtons();
124         alignmentToRadioButtons();
125 }
126
127
128 void QParagraphDialog::alignmentToRadioButtons(LyXAlignment align)
129 {
130         if (align == LYX_ALIGN_LAYOUT)
131                 align = form_->controller().alignDefault();
132
133         QPRadioMap::const_iterator it = radioMap.begin();
134         for (;it != radioMap.end(); ++it) {
135                 if (align == it->first) {
136                         it->second->setChecked(true);
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         if (alignDefaultCB->isChecked()) 
149                 return LYX_ALIGN_LAYOUT;
150         LyXAlignment alignment = LYX_ALIGN_NONE;
151         QPRadioMap::const_iterator it = radioMap.begin();
152         for (; it != radioMap.end(); ++it) {
153                 if (it->second->isChecked()) {
154                         alignment = it->first;
155                         break;
156                 }
157         }
158         if (alignment == form_->controller().alignDefault())
159                 return LYX_ALIGN_LAYOUT;
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         LyXAlignment newAlignment = params.align();
247         LyXAlignment defaultAlignment = controller().alignDefault();
248         bool alignmentIsDefault = 
249                 newAlignment == LYX_ALIGN_LAYOUT || newAlignment == defaultAlignment;
250         dialog_->alignDefaultCB->blockSignals(true);
251         dialog_->alignDefaultCB->setChecked(alignmentIsDefault);
252         dialog_->alignDefaultCB->blockSignals(false);
253         dialog_->checkAlignmentRadioButtons();
254         dialog_->alignmentToRadioButtons(newAlignment);
255
256         //indentation
257         dialog_->indentCB->setChecked(!params.noindent());
258
259         // linespacing
260         int linespacing;
261         Spacing const & space = params.spacing();
262         switch (space.getSpace()) {
263         case Spacing::Single:
264                 linespacing = 1;
265                 break;
266         case Spacing::Onehalf:
267                 linespacing = 2;
268                 break;
269         case Spacing::Double:
270                 linespacing = 3;
271                 break;
272         case Spacing::Other:
273                 linespacing = 4;
274                 break;
275         default:
276                 linespacing = 0;
277                 break;
278         }
279         dialog_->linespacing->setCurrentIndex(linespacing);
280         if (space.getSpace() == Spacing::Other) {
281                 dialog_->linespacingValue->setText(toqstr(space.getValueAsString()));
282                 dialog_->linespacingValue->setEnabled(true);
283         } else {
284                 dialog_->linespacingValue->setText("");
285                 dialog_->linespacingValue->setEnabled(false);
286         }
287 }
288
289 } // namespace frontend
290 } // namespace lyx
291
292 #include "QParagraph_moc.cpp"