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