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