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 {
Privatelyx::frontend::InsetParamsDialog::Private59 	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 
InsetParamsDialog(GuiView & lv,InsetParamsWidget * widget)75 InsetParamsDialog::InsetParamsDialog(GuiView & lv, InsetParamsWidget * widget)
76 	: DialogView(lv, toqstr(insetName(widget->insetCode())),
77 	widget->dialogTitle()), d(new Private)
78 {
79 	setupUi(this);
80 	setInsetParamsWidget(widget);
81 	immediateApplyCB->setChecked(false);
82 	synchronizedCB->setChecked(true);
83 	on_immediateApplyCB_stateChanged(false);
84 	setFocusProxy(widget);
85 }
86 
~InsetParamsDialog()87 InsetParamsDialog::~InsetParamsDialog()
88 {
89 	delete d;
90 }
91 
92 
initialiseParams(std::string const & data)93 bool InsetParamsDialog::initialiseParams(std::string const & data)
94 {
95 	if (!d->widget_->initialiseParams(data))
96 		on_restorePB_clicked();
97 	return true;
98 }
99 
100 
setInsetParamsWidget(InsetParamsWidget * widget)101 void InsetParamsDialog::setInsetParamsWidget(InsetParamsWidget * widget)
102 {
103 	d->widget_ = widget;
104 	stackedWidget->addWidget(widget);
105 	stackedWidget->setCurrentWidget(widget);
106 	connect(d->widget_, SIGNAL(changed()), this, SLOT(onWidget_changed()));
107 }
108 
109 
on_restorePB_clicked()110 void InsetParamsDialog::on_restorePB_clicked()
111 {
112 	updateView(true);
113 	restorePB->setEnabled(false);
114 	d->changed_ = false;
115 	d->inset_ = inset(d->widget_->insetCode());
116 }
117 
118 
on_okPB_clicked()119 void InsetParamsDialog::on_okPB_clicked()
120 {
121 	Inset const * i = inset(d->widget_->insetCode());
122 	if (i)
123 		applyView();
124 	else
125 		newInset();
126 	hide();
127 }
128 
129 
newInset()130 void InsetParamsDialog::newInset()
131 {
132 	docstring const argument = d->widget_->dialogToParams();
133 	dispatch(FuncRequest(d->widget_->creationCode(), argument));
134 }
135 
136 
on_newPB_clicked()137 void InsetParamsDialog::on_newPB_clicked()
138 {
139 	newInset();
140 }
141 
142 
on_applyPB_clicked()143 void InsetParamsDialog::on_applyPB_clicked()
144 {
145 	applyView();
146 }
147 
148 
on_closePB_clicked()149 void InsetParamsDialog::on_closePB_clicked()
150 {
151 	hide();
152 }
153 
154 
on_immediateApplyCB_stateChanged(int state)155 void InsetParamsDialog::on_immediateApplyCB_stateChanged(int state)
156 {
157 	checkWidgets(state == Qt::Checked);
158 }
159 
160 
on_synchronizedCB_stateChanged(int)161 void InsetParamsDialog::on_synchronizedCB_stateChanged(int)
162 {
163 	checkWidgets(false);
164 }
165 
166 
checkWidgets(bool immediate)167 docstring InsetParamsDialog::checkWidgets(bool immediate)
168 {
169 	bool const read_only = buffer().isReadonly();
170 	bool const widget_ok = d->widget_->checkWidgets(read_only);
171 	Inset const * ins = inset(d->widget_->insetCode());
172 	docstring const argument = d->widget_->dialogToParams();
173 	bool valid_argument = !argument.empty();
174 	if (ins)
175 		valid_argument &= ins->validateModifyArgument(argument);
176 	FuncCode const code = immediate
177 		? d->widget_->creationCode() : LFUN_INSET_MODIFY;
178 	bool const lfun_ok = lyx::getStatus(FuncRequest(code, argument)).enabled();
179 
180 	okPB->setEnabled(!immediate && widget_ok && !read_only && valid_argument);
181 	bool const can_be_restored = !immediate && !read_only
182 			&& ins && (ins != d->inset_ || d->changed_);
183 	restorePB->setEnabled(can_be_restored);
184 	applyPB->setEnabled(ins && !immediate && lfun_ok && widget_ok && !read_only && valid_argument);
185 	newPB->setEnabled(widget_ok && !read_only && valid_argument);
186 	synchronizedCB->setEnabled(!immediate);
187 	return argument;
188 }
189 
190 
onWidget_changed()191 void InsetParamsDialog::onWidget_changed()
192 {
193 	d->changed_ = true;
194 	docstring const argument = checkWidgets(immediateApplyCB->isChecked());
195 	if (immediateApplyCB->isChecked()
196 	    && d->widget_->checkWidgets(buffer().isReadonly()))
197 		dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
198 }
199 
200 
applyView()201 void InsetParamsDialog::applyView()
202 {
203 	docstring const argument = checkWidgets(immediateApplyCB->isChecked());
204 	dispatch(FuncRequest(LFUN_INSET_MODIFY, argument));
205 	d->changed_ = false;
206 	d->inset_ = inset(d->widget_->insetCode());
207 	updateView(true);
208 }
209 
210 
updateView(bool update_widget)211 void InsetParamsDialog::updateView(bool update_widget)
212 {
213 	if (update_widget) {
214 		Inset const * i = inset(d->widget_->insetCode());
215 		if (i) {
216 			d->widget_->blockSignals(true);
217 			d->widget_->paramsToDialog(i);
218 			d->widget_->blockSignals(false);
219 		}
220 	}
221 	checkWidgets(immediateApplyCB->isChecked());
222 }
223 
224 
updateView()225 void InsetParamsDialog::updateView()
226 {
227 	bool const update_widget =
228 		(synchronizedCB->isChecked() || immediateApplyCB->isChecked());
229 	updateView(update_widget);
230 }
231 
232 
createDialog(GuiView & lv,InsetCode code)233 Dialog * createDialog(GuiView & lv, InsetCode code)
234 {
235 	InsetParamsWidget * widget;
236 	switch (code) {
237 	case ERT_CODE:
238 		widget = new GuiERT;
239 		break;
240 	case FLOAT_CODE:
241 		widget = new FloatPlacement(true);
242 		break;
243 	case BIBITEM_CODE:
244 		widget = new GuiBibitem;
245 		break;
246 	case BRANCH_CODE:
247 		widget = new GuiBranch;
248 		break;
249 	case BOX_CODE:
250 		widget = new GuiBox;
251 		break;
252 	case HYPERLINK_CODE:
253 		widget = new GuiHyperlink;
254 		break;
255 	case INFO_CODE:
256 		widget = new GuiInfo;
257 		break;
258 	case LABEL_CODE:
259 		widget = new GuiLabel;
260 		break;
261 	case LINE_CODE:
262 		widget = new GuiLine;
263 		break;
264 	case MATH_SPACE_CODE:
265 		widget = new GuiHSpace(true);
266 		break;
267 	case NOMENCL_CODE:
268 		widget = new GuiNomenclature;
269 		break;
270 	case NOMENCL_PRINT_CODE:
271 		widget = new GuiPrintNomencl;
272 		break;
273 	case SPACE_CODE:
274 		widget = new GuiHSpace(false);
275 		break;
276 	case TABULAR_CODE:
277 		widget = new GuiTabular;
278 		break;
279 	case VSPACE_CODE:
280 		widget = new GuiVSpace;
281 		break;
282 	default: return 0;
283 	}
284 	InsetParamsDialog * dialog = new InsetParamsDialog(lv, widget);
285 	return dialog;
286 }
287 
288 
createDialog(GuiView & lv,string const & name)289 Dialog * createDialog(GuiView & lv, string const & name)
290 {
291 	return createDialog(lv, insetCode(name));
292 }
293 
294 } // namespace frontend
295 } // namespace lyx
296 
297 #include "moc_InsetParamsDialog.cpp"
298