2 * \file InsetParamsDialog.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Abdelrazak Younes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetParamsDialog.h"
16 #include "GuiBranch.h"
17 #include "GuiBibitem.h"
18 #include "GuiCounter.h"
20 #include "GuiHSpace.h"
21 #include "GuiHyperlink.h"
25 #include "GuiNomenclature.h"
26 #include "GuiPrintNomencl.h"
27 #include "GuiTabular.h"
28 #include "GuiVSpace.h"
29 #include "FloatPlacement.h"
31 #include "InsetParamsWidget.h"
32 #include "qt_helpers.h"
35 #include "buffer_funcs.h"
36 #include "BufferParams.h"
37 #include "BufferView.h"
39 #include "FuncRequest.h"
40 #include "FuncStatus.h"
43 #include "support/debug.h"
44 #include "support/lstrings.h"
46 #include <QDialogButtonBox>
49 using namespace lyx::support;
54 /////////////////////////////////////////////////////////////////
56 // InsetParamsDialog::Private
58 /////////////////////////////////////////////////////////////////
60 struct InsetParamsDialog::Private
62 Private() : widget_(nullptr), inset_(nullptr), changed_(false) {}
64 InsetParamsWidget * widget_;
65 /// The inset that was used at last Restore or Apply operation.
67 /// Set to true whenever the dialog is changed and set back to
68 /// false when the dialog is applied or restored.
72 /////////////////////////////////////////////////////////////////
76 /////////////////////////////////////////////////////////////////
78 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
79 : DialogView(lv, toqstr(insetName(widget->insetCode())),
80 widget->dialogTitle()), d(new Private)
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);
92 InsetParamsDialog::~InsetParamsDialog()
98 bool InsetParamsDialog::initialiseParams(std::string const & sdata)
100 if (!d->widget_->initialiseParams(sdata))
106 void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
109 stackedWidget->addWidget(widget);
110 stackedWidget->setCurrentWidget(widget);
111 connect(d->widget_, SIGNAL(changed()), this, SLOT(onWidget_changed()));
115 void InsetParamsDialog::on_buttonBox_clicked(QAbstractButton * button)
117 switch (buttonBox->buttonRole(button)) {
118 case QDialogButtonBox::AcceptRole: {// OK
119 Inset const * i = inset(d->widget_->insetCode());
127 case QDialogButtonBox::ApplyRole:
130 case QDialogButtonBox::RejectRole:// Cancel or Close
133 case QDialogButtonBox::ResetRole: {
137 case QDialogButtonBox::ActionRole:// New Inset
146 void InsetParamsDialog::resetDialog()
149 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
151 d->inset_ = inset(d->widget_->insetCode());
155 void InsetParamsDialog::newInset()
157 docstring const argument = d->widget_->dialogToParams();
158 dispatch(FuncRequest(d->widget_->creationCode(), argument));
162 bool InsetParamsDialog::newInsetAllowed() const
164 docstring const argument = d->widget_->dialogToParams();
165 FuncRequest const fr = FuncRequest(d->widget_->creationCode(), argument);
166 FuncStatus const fs(getStatus(fr));
171 void InsetParamsDialog::on_immediateApplyCB_stateChanged(int state)
173 checkWidgets(state == Qt::Checked);
177 void InsetParamsDialog::on_synchronizedCB_stateChanged(int)
183 docstring InsetParamsDialog::checkWidgets(bool immediate)
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();
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();
196 bool const changed_inset = ins && (ins != d->inset_ || d->changed_);
197 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!immediate && widget_ok
198 && !read_only && valid_argument
199 && (!ins || changed_inset));
200 bool const can_be_restored = !immediate && !read_only && changed_inset;
201 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(can_be_restored);
202 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!immediate
203 && lfun_ok && widget_ok
204 && !read_only && valid_argument
206 immediateApplyCB->setEnabled(ins && !read_only);
207 // This seems to be the only way to access custom buttons
208 QList<QAbstractButton*> buttons = buttonBox->buttons();
209 for (auto & button : buttons) {
210 if (buttonBox->buttonRole(button) == QDialogButtonBox::ActionRole)
211 button->setEnabled(widget_ok && !read_only
213 && newInsetAllowed());
215 synchronizedCB->setEnabled(!immediate);
220 void InsetParamsDialog::onWidget_changed()
223 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
224 if (immediateApplyCB->isChecked()
225 && d->widget_->checkWidgets(buffer().isReadonly()))
226 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
230 void InsetParamsDialog::applyView()
232 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
233 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
235 d->inset_ = inset(d->widget_->insetCode());
240 void InsetParamsDialog::updateView(bool update_widget)
243 Inset const * i = inset(d->widget_->insetCode());
245 d->widget_->blockSignals(true);
246 d->widget_->paramsToDialog(i);
247 d->widget_->blockSignals(false);
250 checkWidgets(immediateApplyCB->isChecked());
254 void InsetParamsDialog::updateView()
256 bool const update_widget =
257 (synchronizedCB->isChecked() || immediateApplyCB->isChecked());
258 updateView(update_widget);
260 // Somewhere in the chain this can lose default status (#11417)
261 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
264 } // namespace frontend
267 #include "moc_InsetParamsDialog.cpp"