]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/InsetParamsDialog.cpp
Fix bug #7106: iterator out of range while copying multi-row math.
[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 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49 namespace frontend {
50
51 /////////////////////////////////////////////////////////////////
52 //
53 // InsetParamsDialog::Private
54 //
55 /////////////////////////////////////////////////////////////////
56
57 struct InsetParamsDialog::Private
58 {
59         Private() : widget_(0), inset_(0), changed_(false) {}
60         ///
61         InsetParamsWidget * widget_;
62         /// The inset that was used at last Restore or Apply operation.
63         Inset const * inset_;
64         /// Set to true whenever the dialog is changed and set back to
65         /// false when the dialog is applied or restored.
66         bool changed_;
67 };
68
69 /////////////////////////////////////////////////////////////////
70 //
71 // InsetParamsDialog
72 //
73 /////////////////////////////////////////////////////////////////
74
75 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
76         : DialogView(lv, toqstr(insetName(widget->insetCode())),
77         toqstr(insetName(widget->insetCode()))), d(new Private)
78 {
79         setupUi(this);
80         setInsetParamsWidget(widget);
81         synchronizedViewCB->setChecked(false);
82         on_synchronizedViewCB_stateChanged(false);
83         setFocusProxy(widget);
84 }
85
86 InsetParamsDialog::~InsetParamsDialog()
87 {
88         delete d;
89 }
90
91
92 bool InsetParamsDialog::initialiseParams(std::string const & data)
93 {
94         if (!d->widget_->initialiseParams(data))
95                 on_restorePB_clicked();
96         return true;
97 }
98
99
100 void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
101 {
102         d->widget_ = widget;
103         stackedWidget->addWidget(widget);
104         stackedWidget->setCurrentWidget(widget);
105         connect(d->widget_, SIGNAL(changed()), this, SLOT(onWidget_changed()));
106 }
107
108
109 void InsetParamsDialog::on_restorePB_clicked()
110 {
111         updateView(true);
112         restorePB->setEnabled(false);
113         d->changed_ = false;
114         d->inset_ = inset(d->widget_->insetCode());
115 }
116
117
118 void InsetParamsDialog::on_okPB_clicked()
119 {
120         Inset const * i = inset(d->widget_->insetCode());
121         if (i)
122                 applyView();
123         else
124                 newInset();
125         hide();
126 }
127
128
129 void InsetParamsDialog::newInset()
130 {
131         docstring const argument = d->widget_->dialogToParams();
132         dispatch(FuncRequest(d->widget_->creationCode(), argument));
133 }
134
135
136 void InsetParamsDialog::on_newPB_clicked()
137 {
138         newInset();
139 }
140
141
142 void InsetParamsDialog::on_applyPB_clicked()
143 {
144         applyView();
145 }
146
147
148 void InsetParamsDialog::on_closePB_clicked()
149 {
150         hide();
151 }
152
153
154 void InsetParamsDialog::on_synchronizedViewCB_stateChanged(int state)
155 {
156         checkWidgets(state == Qt::Checked);
157 }
158
159
160 docstring InsetParamsDialog::checkWidgets(bool synchronized_view)
161 {
162         bool const widget_ok = d->widget_->checkWidgets();
163         Inset const * ins = inset(d->widget_->insetCode());
164         docstring const argument = d->widget_->dialogToParams();
165         bool valid_argument = !argument.empty();
166         if (ins)
167                 valid_argument &= ins->validateModifyArgument(argument);
168         FuncCode const code = synchronized_view
169                 ? d->widget_->creationCode() : LFUN_INSET_MODIFY;
170         bool const lfun_ok = lyx::getStatus(FuncRequest(code, argument)).enabled();
171         bool const read_only = buffer().isReadonly();
172
173         okPB->setEnabled(!synchronized_view && widget_ok && !read_only && valid_argument);
174         bool const can_be_restored = !synchronized_view && !read_only
175                         && ins && (ins != d->inset_ || d->changed_);
176         restorePB->setEnabled(can_be_restored);
177         applyPB->setEnabled(lfun_ok && widget_ok && !read_only && valid_argument);
178         d->widget_->setEnabled(!read_only);
179         return argument;
180 }
181
182
183 void InsetParamsDialog::onWidget_changed()
184 {
185         d->changed_ = true;
186         docstring const argument = checkWidgets(synchronizedViewCB->isChecked());
187         if (synchronizedViewCB->isChecked())
188                 dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
189 }
190
191
192 void InsetParamsDialog::applyView()
193 {
194         docstring const argument = checkWidgets(synchronizedViewCB->isChecked());
195         dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
196         d->changed_ = false;
197         d->inset_ = inset(d->widget_->insetCode());
198         updateView(true);
199 }
200
201
202 void InsetParamsDialog::updateView(bool update_widget)
203 {
204         if (update_widget) {
205                 Inset const * i = inset(d->widget_->insetCode());
206                 if (i) {
207                         d->widget_->blockSignals(true);
208                         d->widget_->paramsToDialog(i);
209                         d->widget_->blockSignals(false);
210                 }
211         }
212         checkWidgets(synchronizedViewCB->isChecked());
213 }
214
215
216 void InsetParamsDialog::updateView()
217 {
218         updateView(synchronizedViewCB->isChecked());
219 }
220
221
222 Dialog * createDialog(GuiView & lv, InsetCode code)
223 {
224         InsetParamsWidget * widget;
225         switch (code) {
226         case ERT_CODE:
227                 widget = new GuiERT;
228                 break;
229         case FLOAT_CODE:
230                 widget = new FloatPlacement(true);
231                 break;
232         case BIBITEM_CODE:
233                 widget = new GuiBibitem;
234                 break;
235         case BRANCH_CODE:
236                 widget = new GuiBranch;
237                 break;
238         case BOX_CODE:
239                 widget = new GuiBox;
240                 break;
241         case HYPERLINK_CODE:
242                 widget = new GuiHyperlink;
243                 break;
244         case INFO_CODE:
245                 widget = new GuiInfo;
246                 break;
247         case LABEL_CODE:
248                 widget = new GuiLabel;
249                 break;
250         case LINE_CODE:
251                 widget = new GuiLine;
252                 break;
253         case MATH_SPACE_CODE:
254                 widget = new GuiHSpace(true);
255                 break;
256         case NOMENCL_CODE:
257                 widget = new GuiNomenclature;
258                 break;
259         case NOMENCL_PRINT_CODE:
260                 widget = new GuiPrintNomencl;
261                 break;
262         case SPACE_CODE:
263                 widget = new GuiHSpace(false);
264                 break;
265         case TABULAR_CODE:
266                 widget = new GuiTabular;
267                 break;
268         case VSPACE_CODE:
269                 widget = new GuiVSpace;
270                 break;
271         default: return 0;
272         }
273         InsetParamsDialog * dialog = new InsetParamsDialog(lv, widget);
274         return dialog;
275 }
276
277
278 Dialog * createDialog(GuiView & lv, string const & name)
279 {
280         return createDialog(lv, insetCode(name));
281 }
282
283 } // namespace frontend
284 } // namespace lyx
285
286 #include "moc_InsetParamsDialog.cpp"