3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "GuiInclude.h"
14 #include "ControlInclude.h"
16 #include "support/os.h"
17 #include "support/lstrings.h"
19 #include "qt_helpers.h"
22 #include "insets/InsetListingsParams.h"
24 #include <QPushButton>
26 #include <QCloseEvent>
32 using lyx::support::os::internal_path;
33 using lyx::support::prefixIs;
34 using lyx::support::getStringFromVector;
35 using lyx::support::getVectorFromString;
41 GuiIncludeDialog::GuiIncludeDialog(LyXView & lv)
42 : GuiDialog(lv, "include")
45 setViewTitle(_("Child Document"));
46 setController(new ControlInclude(*this));
48 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
49 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
51 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
52 connect(filenameED, SIGNAL(textChanged(const QString &)),
53 this, SLOT(change_adaptor()));
54 connect(editPB, SIGNAL(clicked()), this, SLOT(editClicked()));
55 connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
56 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
57 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
58 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
59 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
60 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
61 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
62 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
63 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
64 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
66 setFocusProxy(filenameED);
68 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
70 bc().setCancel(closePB);
71 bc().addReadOnly(filenameED);
72 bc().addReadOnly(browsePB);
73 bc().addReadOnly(visiblespaceCB);
74 bc().addReadOnly(typeCO);
75 bc().addReadOnly(listingsED);
77 bc().addCheckedLineEdit(filenameED, filenameLA);
81 ControlInclude & GuiIncludeDialog::controller() const
83 return static_cast<ControlInclude &>(Dialog::controller());
87 void GuiIncludeDialog::showView()
93 void GuiIncludeDialog::change_adaptor()
99 docstring GuiIncludeDialog::validate_listings_params()
101 // use a cache here to avoid repeated validation
102 // of the same parameters
103 static string param_cache = string();
104 static docstring msg_cache = docstring();
106 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
109 string params = fromqstr(listingsED->toPlainText());
110 if (params != param_cache) {
111 param_cache = params;
112 msg_cache = InsetListingsParams(params).validate();
118 void GuiIncludeDialog::set_listings_msg()
120 static bool isOK = true;
121 docstring msg = validate_listings_params();
126 listingsTB->setPlainText(
127 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
130 listingsTB->setPlainText(toqstr(msg));
135 void GuiIncludeDialog::closeEvent(QCloseEvent * e)
142 void GuiIncludeDialog::typeChanged(int v)
147 visiblespaceCB->setEnabled(false);
148 visiblespaceCB->setChecked(false);
149 previewCB->setEnabled(false);
150 previewCB->setChecked(false);
151 listingsGB->setEnabled(false);
155 visiblespaceCB->setEnabled(false);
156 visiblespaceCB->setChecked(false);
157 previewCB->setEnabled(true);
158 listingsGB->setEnabled(false);
162 visiblespaceCB->setEnabled(false);
163 visiblespaceCB->setChecked(false);
164 previewCB->setEnabled(false);
165 previewCB->setChecked(false);
166 listingsGB->setEnabled(true);
170 visiblespaceCB->setEnabled(true);
171 previewCB->setEnabled(false);
172 previewCB->setChecked(false);
173 listingsGB->setEnabled(false);
177 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
178 //for the reason this is here.
179 okPB->setDefault(true);
183 void GuiIncludeDialog::editClicked()
189 void GuiIncludeDialog::browseClicked()
195 void GuiIncludeDialog::update_contents()
197 InsetCommandParams const & params = controller().params();
199 filenameED->setText(toqstr(params["filename"]));
201 visiblespaceCB->setChecked(false);
202 visiblespaceCB->setEnabled(false);
203 previewCB->setChecked(false);
204 previewCB->setEnabled(false);
205 listingsGB->setEnabled(false);
209 listingsTB->setPlainText(
210 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
212 string cmdname = controller().params().getCmdName();
213 if (cmdname != "include" &&
214 cmdname != "verbatiminput" &&
215 cmdname != "verbatiminput*" &&
216 cmdname != "lstinputlisting")
219 if (cmdname == "include") {
220 typeCO->setCurrentIndex(0);
222 } else if (cmdname == "input") {
223 typeCO->setCurrentIndex(1);
224 previewCB->setEnabled(true);
225 previewCB->setChecked(params.preview());
227 } else if (cmdname == "verbatiminput*") {
228 typeCO->setCurrentIndex(2);
229 visiblespaceCB->setEnabled(true);
230 visiblespaceCB->setChecked(true);
232 } else if (cmdname == "verbatiminput") {
233 typeCO->setCurrentIndex(2);
234 visiblespaceCB->setEnabled(true);
236 } else if (cmdname == "lstinputlisting") {
237 typeCO->setCurrentIndex(3);
238 listingsGB->setEnabled(true);
239 listingsED->setEnabled(true);
240 InsetListingsParams par(params.getOptions());
241 // extract caption and label and put them into their respective editboxes
242 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
243 for (vector<string>::iterator it = pars.begin();
244 it != pars.end(); ++it) {
245 if (prefixIs(*it, "caption=")) {
246 string cap = it->substr(8);
247 if (cap[0] == '{' && cap[cap.size()-1] == '}') {
248 captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
251 } else if (prefixIs(*it, "label=")) {
252 string lbl = it->substr(6);
253 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
254 labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
259 // the rest is put to the extra edit box.
260 string extra = getStringFromVector(pars);
261 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
266 void GuiIncludeDialog::applyView()
268 InsetCommandParams params = controller().params();
270 params["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
271 params.preview(previewCB->isChecked());
273 int const item = typeCO->currentIndex();
275 params.setCmdName("include");
276 } else if (item == 1) {
277 params.setCmdName("input");
278 } else if (item == 3) {
279 params.setCmdName("lstinputlisting");
280 // the parameter string should have passed validation
281 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
282 string caption = fromqstr(captionLE->text());
283 string label = fromqstr(labelLE->text());
284 if (!caption.empty())
285 par.addParam("caption", "{" + caption + "}");
287 par.addParam("label", "{" + label + "}");
288 params.setOptions(par.params());
290 if (visiblespaceCB->isChecked())
291 params.setCmdName("verbatiminput*");
293 params.setCmdName("verbatiminput");
295 controller().setParams(params);
299 void GuiIncludeDialog::browse()
301 ControlInclude::Type type;
303 int const item = typeCO->currentIndex();
305 type = ControlInclude::INCLUDE;
307 type = ControlInclude::INPUT;
309 type = ControlInclude::VERBATIM;
311 type = ControlInclude::LISTINGS;
313 docstring const & name =
314 controller().browse(qstring_to_ucs4(filenameED->text()), type);
316 filenameED->setText(toqstr(name));
320 void GuiIncludeDialog::edit()
323 string const file = fromqstr(filenameED->text());
325 controller().edit(file);
330 bool GuiIncludeDialog::isValid()
332 return !filenameED->text().isEmpty() &&
333 validate_listings_params().empty();
336 } // namespace frontend
339 #include "GuiInclude_moc.cpp"