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