]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/InsetParamsDialog.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / InsetParamsDialog.cpp
index f90d8006701ddb6e92ff489f01cd6168bf5c84e2..a36de56988a3f84bcc4877944a53a8d804f85411 100644 (file)
 #include "FuncStatus.h"
 #include "LyX.h"
 
+#include "support/debug.h"
 #include "support/lstrings.h"
 
+#include <QDialogButtonBox>
+
 using namespace std;
 using namespace lyx::support;
 
@@ -73,7 +76,7 @@ struct InsetParamsDialog::Private
 
 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
        : DialogView(lv, toqstr(insetName(widget->insetCode())),
-       toqstr(insetName(widget->insetCode()))), d(new Private)
+       widget->dialogTitle()), d(new Private)
 {
        setupUi(this);
        setInsetParamsWidget(widget);
@@ -81,6 +84,8 @@ InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
        synchronizedCB->setChecked(true);
        on_immediateApplyCB_stateChanged(false);
        setFocusProxy(widget);
+       newPB = buttonBox->addButton(qt_("Ne&w Inset"),
+                            QDialogButtonBox::ActionRole);
 }
 
 InsetParamsDialog::~InsetParamsDialog()
@@ -89,10 +94,10 @@ InsetParamsDialog::~InsetParamsDialog()
 }
 
 
-bool InsetParamsDialog::initialiseParams(std::string const & data)
+bool InsetParamsDialog::initialiseParams(std::string const & sdata)
 {
-       if (!d->widget_->initialiseParams(data))
-               on_restorePB_clicked();
+       if (!d->widget_->initialiseParams(sdata))
+               resetDialog();
        return true;
 }
 
@@ -106,23 +111,43 @@ void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
 }
 
 
-void InsetParamsDialog::on_restorePB_clicked()
+void InsetParamsDialog::on_buttonBox_clicked(QAbstractButton * button)
 {
-       updateView(true);
-       restorePB->setEnabled(false);
-       d->changed_ = false;
-       d->inset_ = inset(d->widget_->insetCode());
+       switch (buttonBox->buttonRole(button)) {
+       case QDialogButtonBox::AcceptRole: {// OK
+               Inset const * i = inset(d->widget_->insetCode());
+               if (i)
+                       applyView();
+               else
+                       newInset();
+               hide();
+               break;
+       }
+       case QDialogButtonBox::ApplyRole:
+               applyView();
+               break;
+       case QDialogButtonBox::RejectRole:// Cancel or Close
+               hide();
+               break;
+       case QDialogButtonBox::ResetRole: {
+               resetDialog();
+               break;
+       }
+       case QDialogButtonBox::ActionRole:// New Inset
+               newInset();
+               break;
+       default:
+               break;
+       }
 }
 
 
-void InsetParamsDialog::on_okPB_clicked()
+void InsetParamsDialog::resetDialog()
 {
-       Inset const * i = inset(d->widget_->insetCode());
-       if (i)
-               applyView();
-       else
-               newInset();
-       hide();
+       updateView(true);
+       buttonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
+       d->changed_ = false;
+       d->inset_ = inset(d->widget_->insetCode());
 }
 
 
@@ -133,21 +158,12 @@ void InsetParamsDialog::newInset()
 }
 
 
-void InsetParamsDialog::on_newPB_clicked()
+bool InsetParamsDialog::newInsetAllowed() const
 {
-       newInset();
-}
-
-
-void InsetParamsDialog::on_applyPB_clicked()
-{
-       applyView();
-}
-
-
-void InsetParamsDialog::on_closePB_clicked()
-{
-       hide();
+       docstring const argument = d->widget_->dialogToParams();
+       FuncRequest const fr = FuncRequest(d->widget_->creationCode(), argument);
+       FuncStatus const fs(getStatus(fr));
+       return fs.enabled();
 }
 
 
@@ -165,7 +181,8 @@ void InsetParamsDialog::on_synchronizedCB_stateChanged(int)
 
 docstring InsetParamsDialog::checkWidgets(bool immediate)
 {
-       bool const widget_ok = d->widget_->checkWidgets();
+       bool const read_only = buffer().isReadonly();
+       bool const widget_ok = d->widget_->checkWidgets(read_only);
        Inset const * ins = inset(d->widget_->insetCode());
        docstring const argument = d->widget_->dialogToParams();
        bool valid_argument = !argument.empty();
@@ -174,14 +191,24 @@ docstring InsetParamsDialog::checkWidgets(bool immediate)
        FuncCode const code = immediate
                ? d->widget_->creationCode() : LFUN_INSET_MODIFY;
        bool const lfun_ok = lyx::getStatus(FuncRequest(code, argument)).enabled();
-       bool const read_only = buffer().isReadonly();
 
-       okPB->setEnabled(!immediate && widget_ok && !read_only && valid_argument);
+       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!immediate && widget_ok
+                                                           && !read_only && valid_argument);
        bool const can_be_restored = !immediate && !read_only
                        && ins && (ins != d->inset_ || d->changed_);
-       restorePB->setEnabled(can_be_restored);
-       applyPB->setEnabled(!immediate && lfun_ok && widget_ok && !read_only && valid_argument);
-       d->widget_->setEnabled(!read_only);
+       buttonBox->button(QDialogButtonBox::Reset)->setEnabled(can_be_restored);
+       buttonBox->button(QDialogButtonBox::Apply)->setEnabled(ins && !immediate
+                                                              && lfun_ok && widget_ok
+                                                              && !read_only && valid_argument);
+       immediateApplyCB->setEnabled(ins && !read_only);
+       // This seems to be the only way to access custom buttons
+       QList<QAbstractButton*> buttons = buttonBox->buttons();
+       for (int i = 0; i < buttons.size(); ++i) {
+               if (buttonBox->buttonRole(buttons.at(i)) == QDialogButtonBox::ActionRole)
+                       buttons.at(i)->setEnabled(widget_ok && !read_only
+                                                 && valid_argument
+                                                 && newInsetAllowed());
+       }
        synchronizedCB->setEnabled(!immediate);
        return argument;
 }
@@ -191,7 +218,8 @@ void InsetParamsDialog::onWidget_changed()
 {
        d->changed_ = true;
        docstring const argument = checkWidgets(immediateApplyCB->isChecked());
-       if (immediateApplyCB->isChecked())
+       if (immediateApplyCB->isChecked()
+           && d->widget_->checkWidgets(buffer().isReadonly()))
                dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
 }
 
@@ -225,6 +253,9 @@ void InsetParamsDialog::updateView()
        bool const update_widget =
                (synchronizedCB->isChecked() || immediateApplyCB->isChecked());
        updateView(update_widget);
+
+       // Somewhere in the chain this can lose default status (#11417)
+       buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
 }