]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/InsetParamsDialog.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / InsetParamsDialog.cpp
1 /**
2  * \file InsetParamsDialog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Abdelrazak Younes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetParamsDialog.h"
14
15 #include "GuiBox.h"
16 #include "GuiBranch.h"
17 #include "GuiBibitem.h"
18 #include "GuiERT.h"
19 #include "GuiHSpace.h"
20 #include "GuiHyperlink.h"
21 #include "GuiInfo.h"
22 #include "GuiLabel.h"
23 #include "GuiLine.h"
24 #include "GuiNomenclature.h"
25 #include "GuiPrintNomencl.h"
26 #include "GuiTabular.h"
27 #include "GuiVSpace.h"
28 #include "FloatPlacement.h"
29
30 #include "InsetParamsWidget.h"
31 #include "qt_helpers.h"
32
33 #include "Buffer.h"
34 #include "buffer_funcs.h"
35 #include "BufferParams.h"
36 #include "BufferView.h"
37 #include "Cursor.h"
38 #include "FuncRequest.h"
39 #include "FuncStatus.h"
40 #include "LyX.h"
41
42 #include "support/debug.h"
43 #include "support/lstrings.h"
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49 namespace frontend {
50
51 /////////////////////////////////////////////////////////////////
52 //
53 // InsetParamsDialog::Private
54 //
55 /////////////////////////////////////////////////////////////////
56
57 struct InsetParamsDialog::Private
58 {
59         Private() : widget_(0), inset_(0), changed_(false) {}
60         ///
61         InsetParamsWidget * widget_;
62         /// The inset that was used at last Restore or Apply operation.
63         Inset const * inset_;
64         /// Set to true whenever the dialog is changed and set back to
65         /// false when the dialog is applied or restored.
66         bool changed_;
67 };
68
69 /////////////////////////////////////////////////////////////////
70 //
71 // InsetParamsDialog
72 //
73 /////////////////////////////////////////////////////////////////
74
75 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
76         : DialogView(lv, toqstr(insetName(widget->insetCode())),
77         toqstr(insetName(widget->insetCode()))), d(new Private)
78 {
79         setupUi(this);
80         setInsetParamsWidget(widget);
81         immediateApplyCB->setChecked(false);
82         synchronizedCB->setChecked(true);
83         on_immediateApplyCB_stateChanged(false);
84         setFocusProxy(widget);
85 }
86
87 InsetParamsDialog::~InsetParamsDialog()
88 {
89         delete d;
90 }
91
92
93 bool InsetParamsDialog::initialiseParams(std::string const & data)
94 {
95         if (!d->widget_->initialiseParams(data))
96                 on_restorePB_clicked();
97         return true;
98 }
99
100
101 void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
102 {
103         d->widget_ = widget;
104         stackedWidget->addWidget(widget);
105         stackedWidget->setCurrentWidget(widget);
106         connect(d->widget_, SIGNAL(changed()), this, SLOT(onWidget_changed()));
107 }
108
109
110 void InsetParamsDialog::on_restorePB_clicked()
111 {
112         updateView(true);
113         restorePB->setEnabled(false);
114         d->changed_ = false;
115         d->inset_ = inset(d->widget_->insetCode());
116 }
117
118
119 void InsetParamsDialog::on_okPB_clicked()
120 {
121         Inset const * i = inset(d->widget_->insetCode());
122         if (i)
123                 applyView();
124         else
125                 newInset();
126         hide();
127 }
128
129
130 void InsetParamsDialog::newInset()
131 {
132         docstring const argument = d->widget_->dialogToParams();
133         dispatch(FuncRequest(d->widget_->creationCode(), argument));
134 }
135
136
137 void InsetParamsDialog::on_newPB_clicked()
138 {
139         newInset();
140 }
141
142
143 void InsetParamsDialog::on_applyPB_clicked()
144 {
145         applyView();
146 }
147
148
149 void InsetParamsDialog::on_closePB_clicked()
150 {
151         hide();
152 }
153
154
155 void InsetParamsDialog::on_immediateApplyCB_stateChanged(int state)
156 {
157         checkWidgets(state == Qt::Checked);
158 }
159
160
161 void InsetParamsDialog::on_synchronizedCB_stateChanged(int)
162 {
163         checkWidgets(false);
164 }
165
166
167 docstring InsetParamsDialog::checkWidgets(bool immediate)
168 {
169         bool const widget_ok = d->widget_->checkWidgets();
170         Inset const * ins = inset(d->widget_->insetCode());
171         docstring const argument = d->widget_->dialogToParams();
172         bool valid_argument = !argument.empty();
173         if (ins)
174                 valid_argument &= ins->validateModifyArgument(argument);
175         FuncCode const code = immediate
176                 ? d->widget_->creationCode() : LFUN_INSET_MODIFY;
177         bool const lfun_ok = lyx::getStatus(FuncRequest(code, argument)).enabled();
178         bool const read_only = buffer().isReadonly();
179
180         okPB->setEnabled(!immediate && widget_ok && !read_only && valid_argument);
181         bool const can_be_restored = !immediate && !read_only
182                         && ins && (ins != d->inset_ || d->changed_);
183         restorePB->setEnabled(can_be_restored);
184         applyPB->setEnabled(!immediate && lfun_ok && widget_ok && !read_only && valid_argument);
185         d->widget_->setEnabled(!read_only);
186         synchronizedCB->setEnabled(!immediate);
187         return argument;
188 }
189
190
191 void InsetParamsDialog::onWidget_changed()
192 {
193         d->changed_ = true;
194         docstring const argument = checkWidgets(immediateApplyCB->isChecked());
195         if (immediateApplyCB->isChecked())
196                 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
197 }
198
199
200 void InsetParamsDialog::applyView()
201 {
202         docstring const argument = checkWidgets(immediateApplyCB->isChecked());
203         dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
204         d->changed_ = false;
205         d->inset_ = inset(d->widget_->insetCode());
206         updateView(true);
207 }
208
209
210 void InsetParamsDialog::updateView(bool update_widget)
211 {
212         if (update_widget) {
213                 Inset const * i = inset(d->widget_->insetCode());
214                 if (i) {
215                         d->widget_->blockSignals(true);
216                         d->widget_->paramsToDialog(i);
217                         d->widget_->blockSignals(false);
218                 }
219         }
220         checkWidgets(immediateApplyCB->isChecked());
221 }
222
223
224 void InsetParamsDialog::updateView()
225 {
226         bool const update_widget =
227                 (synchronizedCB->isChecked() || immediateApplyCB->isChecked());
228         updateView(update_widget);
229 }
230
231
232 Dialog * createDialog(GuiView & lv, InsetCode code)
233 {
234         InsetParamsWidget * widget;
235         switch (code) {
236         case ERT_CODE:
237                 widget = new GuiERT;
238                 break;
239         case FLOAT_CODE:
240                 widget = new FloatPlacement(true);
241                 break;
242         case BIBITEM_CODE:
243                 widget = new GuiBibitem;
244                 break;
245         case BRANCH_CODE:
246                 widget = new GuiBranch;
247                 break;
248         case BOX_CODE:
249                 widget = new GuiBox;
250                 break;
251         case HYPERLINK_CODE:
252                 widget = new GuiHyperlink;
253                 break;
254         case INFO_CODE:
255                 widget = new GuiInfo;
256                 break;
257         case LABEL_CODE:
258                 widget = new GuiLabel;
259                 break;
260         case LINE_CODE:
261                 widget = new GuiLine;
262                 break;
263         case MATH_SPACE_CODE:
264                 widget = new GuiHSpace(true);
265                 break;
266         case NOMENCL_CODE:
267                 widget = new GuiNomenclature;
268                 break;
269         case NOMENCL_PRINT_CODE:
270                 widget = new GuiPrintNomencl;
271                 break;
272         case SPACE_CODE:
273                 widget = new GuiHSpace(false);
274                 break;
275         case TABULAR_CODE:
276                 widget = new GuiTabular;
277                 break;
278         case VSPACE_CODE:
279                 widget = new GuiVSpace;
280                 break;
281         default: return 0;
282         }
283         InsetParamsDialog * dialog = new InsetParamsDialog(lv, widget);
284         return dialog;
285 }
286
287
288 Dialog * createDialog(GuiView & lv, string const & name)
289 {
290         return createDialog(lv, insetCode(name));
291 }
292
293 } // namespace frontend
294 } // namespace lyx
295
296 #include "moc_InsetParamsDialog.cpp"