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