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"
19 #include "GuiHSpace.h"
20 #include "GuiHyperlink.h"
24 #include "GuiNomenclature.h"
25 #include "GuiPrintNomencl.h"
26 #include "GuiTabular.h"
27 #include "GuiVSpace.h"
28 #include "FloatPlacement.h"
30 #include "InsetParamsWidget.h"
31 #include "qt_helpers.h"
34 #include "buffer_funcs.h"
35 #include "BufferParams.h"
36 #include "BufferView.h"
38 #include "FuncRequest.h"
39 #include "FuncStatus.h"
42 #include "support/debug.h"
43 #include "support/lstrings.h"
45 #include <QDialogButtonBox>
48 using namespace lyx::support;
53 /////////////////////////////////////////////////////////////////
55 // InsetParamsDialog::Private
57 /////////////////////////////////////////////////////////////////
59 struct InsetParamsDialog::Private
61 Private() : widget_(0), inset_(0), changed_(false) {}
63 InsetParamsWidget * widget_;
64 /// The inset that was used at last Restore or Apply operation.
66 /// Set to true whenever the dialog is changed and set back to
67 /// false when the dialog is applied or restored.
71 /////////////////////////////////////////////////////////////////
75 /////////////////////////////////////////////////////////////////
77 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
78 : DialogView(lv, toqstr(insetName(widget->insetCode())),
79 widget->dialogTitle()), d(new Private)
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);
91 InsetParamsDialog::~InsetParamsDialog()
97 bool InsetParamsDialog::initialiseParams(std::string const & sdata)
99 if (!d->widget_->initialiseParams(sdata))
105 void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
108 stackedWidget->addWidget(widget);
109 stackedWidget->setCurrentWidget(widget);
110 connect(d->widget_, SIGNAL(changed()), this, SLOT(onWidget_changed()));
114 void InsetParamsDialog::on_buttonBox_clicked(QAbstractButton * button)
116 switch (buttonBox->buttonRole(button)) {
117 case QDialogButtonBox::AcceptRole: {// OK
118 Inset const * i = inset(d->widget_->insetCode());
126 case QDialogButtonBox::ApplyRole:
129 case QDialogButtonBox::RejectRole:// Cancel or Close
132 case QDialogButtonBox::ResetRole: {
136 case QDialogButtonBox::ActionRole:// New Inset
145 void InsetParamsDialog::resetDialog()
148 buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
150 d->inset_ = inset(d->widget_->insetCode());
154 void InsetParamsDialog::newInset()
156 docstring const argument = d->widget_->dialogToParams();
157 dispatch(FuncRequest(d->widget_->creationCode(), argument));
161 bool InsetParamsDialog::newInsetAllowed() const
163 docstring const argument = d->widget_->dialogToParams();
164 FuncRequest const fr = FuncRequest(d->widget_->creationCode(), argument);
165 FuncStatus const fs(getStatus(fr));
170 void InsetParamsDialog::on_immediateApplyCB_stateChanged(int state)
172 checkWidgets(state == Qt::Checked);
176 void InsetParamsDialog::on_synchronizedCB_stateChanged(int)
182 docstring InsetParamsDialog::checkWidgets(bool immediate)
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();
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();
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
210 && newInsetAllowed());
212 synchronizedCB->setEnabled(!immediate);
217 void InsetParamsDialog::onWidget_changed()
220 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
221 if (immediateApplyCB->isChecked()
222 && d->widget_->checkWidgets(buffer().isReadonly()))
223 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
227 void InsetParamsDialog::applyView()
229 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
230 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
232 d->inset_ = inset(d->widget_->insetCode());
237 void InsetParamsDialog::updateView(bool update_widget)
240 Inset const * i = inset(d->widget_->insetCode());
242 d->widget_->blockSignals(true);
243 d->widget_->paramsToDialog(i);
244 d->widget_->blockSignals(false);
247 checkWidgets(immediateApplyCB->isChecked());
251 void InsetParamsDialog::updateView()
253 bool const update_widget =
254 (synchronizedCB->isChecked() || immediateApplyCB->isChecked());
255 updateView(update_widget);
257 // Somewhere in the chain this can lose default status (#11417)
258 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
262 Dialog * createDialog(GuiView & lv, InsetCode code)
264 InsetParamsWidget * widget;
270 widget = new FloatPlacement(true);
273 widget = new GuiBibitem;
276 widget = new GuiBranch;
282 widget = new GuiHyperlink;
285 widget = new GuiInfo;
288 widget = new GuiLabel;
291 widget = new GuiLine;
293 case MATH_SPACE_CODE:
294 widget = new GuiHSpace(true);
297 widget = new GuiNomenclature;
299 case NOMENCL_PRINT_CODE:
300 widget = new GuiPrintNomencl;
303 widget = new GuiHSpace(false);
306 widget = new GuiTabular;
309 widget = new GuiVSpace;
313 InsetParamsDialog * dialog = new InsetParamsDialog(lv, widget);
318 Dialog * createDialog(GuiView & lv, string const & name)
320 return createDialog(lv, insetCode(name));
323 } // namespace frontend
326 #include "moc_InsetParamsDialog.cpp"