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