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