3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "GuiInclude.h"
18 #include "BufferParams.h"
19 #include "FuncRequest.h"
22 #include "qt_helpers.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/FileName.h"
29 #include "support/filetools.h"
31 #include "insets/InsetListingsParams.h"
32 #include "insets/InsetInclude.h"
36 #include <QPushButton>
41 using namespace lyx::support;
42 using namespace lyx::support::os;
48 GuiInclude::GuiInclude(GuiView & lv)
49 : GuiDialog(lv, "include", qt_("Child Document")),
50 params_(insetCode("include"))
54 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
55 this, SLOT(slotButtonBox(QAbstractButton *)));
57 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
58 connect(filenameED, SIGNAL(textChanged(const QString &)),
59 this, SLOT(change_adaptor()));
60 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
61 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
62 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
63 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
64 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
65 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
66 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
67 connect(literalCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
68 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
69 connect(listingsED, SIGNAL(textChanged()), this, SLOT(setListingsMsg()));
70 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71 connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
73 setFocusProxy(filenameED);
75 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
76 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
77 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
78 bc().addReadOnly(filenameED);
79 bc().addReadOnly(browsePB);
80 bc().addReadOnly(visiblespaceCB);
81 bc().addReadOnly(typeCO);
82 bc().addReadOnly(listingsED);
84 bc().addCheckedLineEdit(filenameED, filenameLA);
88 void GuiInclude::change_adaptor()
94 docstring GuiInclude::validate_listings_params()
96 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
98 string params = fromqstr(listingsED->toPlainText());
99 InsetListingsParams lstparams(params);
100 lstparams.setMinted(buffer().params().use_minted);
101 return lstparams.validate();
105 void GuiInclude::setListingsMsg()
108 static bool isOK = true;
109 docstring msg = validate_listings_params();
114 listingsTB->setPlainText(
115 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
118 listingsTB->setPlainText(toqstr(msg));
123 void GuiInclude::typeChanged(int v)
128 visiblespaceCB->setEnabled(false);
129 visiblespaceCB->setChecked(false);
130 previewCB->setEnabled(false);
131 previewCB->setChecked(false);
132 listingsGB->setEnabled(false);
136 visiblespaceCB->setEnabled(false);
137 visiblespaceCB->setChecked(false);
138 previewCB->setEnabled(true);
139 listingsGB->setEnabled(false);
143 visiblespaceCB->setEnabled(false);
144 visiblespaceCB->setChecked(false);
145 previewCB->setEnabled(false);
146 previewCB->setChecked(false);
147 listingsGB->setEnabled(true);
151 visiblespaceCB->setEnabled(true);
152 previewCB->setEnabled(false);
153 previewCB->setChecked(false);
154 listingsGB->setEnabled(false);
160 void GuiInclude::paramsToDialog(InsetCommandParams const & icp)
162 filenameED->setText(toqstr(icp["filename"]));
164 visiblespaceCB->setChecked(false);
165 visiblespaceCB->setEnabled(false);
166 previewCB->setChecked(false);
167 previewCB->setEnabled(false);
168 listingsGB->setEnabled(false);
172 listingsTB->setPlainText(
173 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
175 string cmdname = icp.getCmdName();
176 if (cmdname != "include" &&
177 cmdname != "verbatiminput" &&
178 cmdname != "verbatiminput*" &&
179 cmdname != "lstinputlisting" &&
180 cmdname != "inputminted")
183 if (cmdname == "include") {
184 typeCO->setCurrentIndex(0);
186 } else if (cmdname == "input") {
187 typeCO->setCurrentIndex(1);
188 previewCB->setEnabled(true);
189 previewCB->setChecked(icp.preview());
191 } else if (cmdname == "verbatiminput*") {
192 typeCO->setCurrentIndex(2);
193 visiblespaceCB->setEnabled(true);
194 visiblespaceCB->setChecked(true);
196 } else if (cmdname == "verbatiminput") {
197 typeCO->setCurrentIndex(2);
198 visiblespaceCB->setEnabled(true);
200 } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
201 typeCO->setCurrentIndex(3);
202 listingsGB->setEnabled(true);
203 listingsED->setEnabled(true);
204 InsetListingsParams par(to_utf8(icp["lstparams"]));
205 // extract caption and label and put them into their respective editboxes
206 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
207 for (vector<string>::iterator it = pars.begin();
208 it != pars.end(); ++it) {
209 if (prefixIs(*it, "caption=")) {
210 string cap = it->substr(8);
211 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
212 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
215 } else if (prefixIs(*it, "label=")) {
216 string lbl = it->substr(6);
217 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
218 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
223 // the rest is put to the extra edit box.
224 string extra = getStringFromVector(pars);
225 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
227 literalCB->setChecked(icp["literal"] == "true");
229 // Make sure that the bc is in the INITIAL state
230 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
235 void GuiInclude::applyView()
237 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
238 params_.preview(previewCB->isChecked());
240 int const item = typeCO->currentIndex();
242 params_.setCmdName("include");
243 } else if (item == 1) {
244 params_.setCmdName("input");
245 } else if (item == 3) {
246 if (buffer().params().use_minted)
247 params_.setCmdName("inputminted");
249 params_.setCmdName("lstinputlisting");
250 // the parameter string should have passed validation
251 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
252 string caption = fromqstr(captionLE->text());
253 string label = fromqstr(labelLE->text());
254 if (!caption.empty())
255 par.addParam("caption", "{" + caption + "}");
257 par.addParam("label", "{" + label + "}");
258 string const listparams = par.params();
259 params_["lstparams"] = from_utf8(listparams);
261 if (visiblespaceCB->isChecked())
262 params_.setCmdName("verbatiminput*");
264 params_.setCmdName("verbatiminput");
266 params_["literal"] = literalCB->isChecked()
267 ? from_ascii("true") : from_ascii("false");
271 void GuiInclude::browse()
275 int const item = typeCO->currentIndex();
285 QString name = browse(filenameED->text(), type);
287 filenameED->setText(name);
291 void GuiInclude::edit()
295 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
300 dispatch(FuncRequest(LFUN_INSET_EDIT));
304 bool GuiInclude::isValid()
306 return !filenameED->text().isEmpty() && validate_listings_params().empty();
310 QString GuiInclude::browse(QString const & in_name, Type in_type) const
312 QString const title = qt_("Select document to include");
314 // input TeX, verbatim, or LyX file ?
319 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
323 filters = fileFilters(QString());
327 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
329 return browseRelToParent(in_name, docpath, title, filters, false,
330 qt_("D&ocuments"), toqstr(lyxrc.document_path));
334 bool GuiInclude::initialiseParams(std::string const & sdata)
336 InsetCommand::string2params(sdata, params_);
337 paramsToDialog(params_);
342 void GuiInclude::dispatchParams()
344 std::string const lfun = InsetCommand::params2string(params_);
345 dispatch(FuncRequest(getLfun(), lfun));
349 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
352 } // namespace frontend
355 #include "moc_GuiInclude.cpp"