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 // This seems to be the only way to access custom buttons
204 QList<QAbstractButton*> buttons = buttonBox->buttons();
205 for (int i = 0; i < buttons.size(); ++i) {
206 if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::ActionRole)
207 buttons.at(i)->setEnabled(widget_ok && !read_only
209 && newInsetAllowed());
211 synchronizedCB->setEnabled(!immediate);
216 void InsetParamsDialog::onWidget_changed()
219 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
220 if (immediateApplyCB->isChecked()
221 && d->widget_->checkWidgets(buffer().isReadonly()))
222 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
226 void InsetParamsDialog::applyView()
228 docstring const argument = checkWidgets(immediateApplyCB->isChecked());
229 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
231 d->inset_ = inset(d->widget_->insetCode());
236 void InsetParamsDialog::updateView(bool update_widget)
239 Inset const * i = inset(d->widget_->insetCode());
241 d->widget_->blockSignals(true);
242 d->widget_->paramsToDialog(i);
243 d->widget_->blockSignals(false);
246 checkWidgets(immediateApplyCB->isChecked());
250 void InsetParamsDialog::updateView()
252 bool const update_widget =
253 (synchronizedCB->isChecked() || immediateApplyCB->isChecked());
254 updateView(update_widget);
258 Dialog * createDialog(GuiView & lv, InsetCode code)
260 InsetParamsWidget * widget;
266 widget = new FloatPlacement(true);
269 widget = new GuiBibitem;
272 widget = new GuiBranch;
278 widget = new GuiHyperlink;
281 widget = new GuiInfo;
284 widget = new GuiLabel;
287 widget = new GuiLine;
289 case MATH_SPACE_CODE:
290 widget = new GuiHSpace(true);
293 widget = new GuiNomenclature;
295 case NOMENCL_PRINT_CODE:
296 widget = new GuiPrintNomencl;
299 widget = new GuiHSpace(false);
302 widget = new GuiTabular;
305 widget = new GuiVSpace;
309 InsetParamsDialog * dialog = new InsetParamsDialog(lv, widget);
314 Dialog * createDialog(GuiView & lv, string const & name)
316 return createDialog(lv, insetCode(name));
319 } // namespace frontend
322 #include "moc_InsetParamsDialog.cpp"