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 "BufferList.h"
19 #include "BufferParams.h"
20 #include "FuncRequest.h"
23 #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 "frontends/alert.h"
33 #include "insets/InsetListingsParams.h"
34 #include "insets/InsetInclude.h"
39 #include <QPushButton>
44 using namespace lyx::support;
45 using namespace lyx::support::os;
51 GuiInclude::GuiInclude(GuiView & lv)
52 : GuiDialog(lv, "include", qt_("Child Document")),
53 params_(insetCode("include"))
57 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
58 this, SLOT(slotButtonBox(QAbstractButton *)));
60 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
61 connect(filenameED, SIGNAL(textChanged(const QString &)),
62 this, SLOT(change_adaptor()));
63 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
64 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
65 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
66 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
67 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
68 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
69 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
70 connect(literalCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
72 connect(listingsED, SIGNAL(textChanged()), this, SLOT(setListingsMsg()));
73 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
74 connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
76 setFocusProxy(filenameED);
78 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
79 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
80 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
81 bc().addReadOnly(filenameED);
82 bc().addReadOnly(browsePB);
83 bc().addReadOnly(visiblespaceCB);
84 bc().addReadOnly(typeCO);
85 bc().addReadOnly(listingsED);
87 // FIXME does not make sense, as we do not have a validator
89 //bc().addCheckedLineEdit(filenameED, filenameLA);
93 void GuiInclude::change_adaptor()
99 docstring GuiInclude::validate_listings_params()
101 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
103 string params = fromqstr(listingsED->toPlainText());
104 InsetListingsParams lstparams(params);
105 lstparams.setMinted(buffer().params().use_minted);
106 return lstparams.validate();
110 void GuiInclude::setListingsMsg()
113 static bool isOK = true;
114 docstring msg = validate_listings_params();
119 listingsTB->setPlainText(
120 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
123 listingsTB->setPlainText(toqstr(msg));
128 void GuiInclude::typeChanged(int v)
133 visiblespaceCB->setEnabled(false);
134 visiblespaceCB->setChecked(false);
135 previewCB->setEnabled(false);
136 previewCB->setChecked(false);
137 listingsGB->setEnabled(false);
141 visiblespaceCB->setEnabled(false);
142 visiblespaceCB->setChecked(false);
143 previewCB->setEnabled(true);
144 listingsGB->setEnabled(false);
148 visiblespaceCB->setEnabled(false);
149 visiblespaceCB->setChecked(false);
150 previewCB->setEnabled(false);
151 previewCB->setChecked(false);
152 listingsGB->setEnabled(true);
156 visiblespaceCB->setEnabled(true);
157 previewCB->setEnabled(false);
158 previewCB->setChecked(false);
159 listingsGB->setEnabled(false);
165 void GuiInclude::paramsToDialog(InsetCommandParams const & icp)
167 filenameED->setText(toqstr(icp["filename"]));
169 visiblespaceCB->setChecked(false);
170 visiblespaceCB->setEnabled(false);
171 previewCB->setChecked(false);
172 previewCB->setEnabled(false);
173 listingsGB->setEnabled(false);
177 listingsTB->setPlainText(
178 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
180 string cmdname = icp.getCmdName();
181 if (cmdname != "include" &&
182 cmdname != "verbatiminput" &&
183 cmdname != "verbatiminput*" &&
184 cmdname != "lstinputlisting" &&
185 cmdname != "inputminted")
188 if (cmdname == "include") {
189 typeCO->setCurrentIndex(0);
191 } else if (cmdname == "input") {
192 typeCO->setCurrentIndex(1);
193 previewCB->setEnabled(true);
194 previewCB->setChecked(icp.preview());
196 } else if (cmdname == "verbatiminput*") {
197 typeCO->setCurrentIndex(2);
198 visiblespaceCB->setEnabled(true);
199 visiblespaceCB->setChecked(true);
201 } else if (cmdname == "verbatiminput") {
202 typeCO->setCurrentIndex(2);
203 visiblespaceCB->setEnabled(true);
205 } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
206 typeCO->setCurrentIndex(3);
207 listingsGB->setEnabled(true);
208 listingsED->setEnabled(true);
209 InsetListingsParams par(to_utf8(icp["lstparams"]));
210 // extract caption and label and put them into their respective editboxes
211 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
212 for (vector<string>::iterator it = pars.begin();
213 it != pars.end(); ++it) {
214 if (prefixIs(*it, "caption=")) {
215 string cap = it->substr(8);
216 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
217 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
220 } else if (prefixIs(*it, "label=")) {
221 string lbl = it->substr(6);
222 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
223 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
228 // the rest is put to the extra edit box.
229 string extra = getStringFromVector(pars);
230 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
232 literalCB->setChecked(icp["literal"] == "true");
234 // Make sure that the bc is in the INITIAL state
235 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
240 void GuiInclude::applyView()
242 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
243 params_.preview(previewCB->isChecked());
245 int const item = typeCO->currentIndex();
247 params_.setCmdName("include");
248 } else if (item == 1) {
249 params_.setCmdName("input");
250 } else if (item == 3) {
251 if (buffer().params().use_minted)
252 params_.setCmdName("inputminted");
254 params_.setCmdName("lstinputlisting");
255 // the parameter string should have passed validation
256 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
257 string caption = fromqstr(captionLE->text());
258 string label = fromqstr(labelLE->text());
259 if (!caption.empty())
260 par.addParam("caption", "{" + caption + "}");
262 par.addParam("label", "{" + label + "}");
263 string const listparams = par.params();
264 params_["lstparams"] = from_utf8(listparams);
266 if (visiblespaceCB->isChecked())
267 params_.setCmdName("verbatiminput*");
269 params_.setCmdName("verbatiminput");
271 params_["literal"] = literalCB->isChecked()
272 ? from_ascii("true") : from_ascii("false");
274 // Do we need to create a LyX file?
275 if (item == 0 || item == 1) {
276 QString fname = filenameED->text();
277 string const mypath = buffer().absFileName();
278 string const bpath = buffer().filePath();
279 QString absfname = makeAbsPath(fname, toqstr(bpath));
280 if (!QFile::exists(absfname)) {
281 dispatch(FuncRequest(LFUN_BUFFER_NEW, fromqstr(absfname)));
282 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
283 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, mypath));
289 void GuiInclude::edit()
293 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
298 QString const fname = filenameED->text();
299 string const bpath = buffer().filePath();
300 string const absfname = support::makeAbsPath(fromqstr(fname), bpath).absFileName();
301 // The button is enabled only if the document is already open.
302 // If something goes wrong and it is not, we'll get an error
303 // message from the dispatch. So no need for one here.
304 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, absfname));
308 bool GuiInclude::isValid()
310 QString fname = filenameED->text();
311 if (fname.isEmpty() || !validate_listings_params().empty()) {
312 editPB->setEnabled(false);
316 QPushButton * okbutton = buttonBox->button(QDialogButtonBox::Ok);
317 int const item = typeCO->currentIndex();
318 // Are we inputting or including a LyX file?
319 if (item != 0 && item != 1) {
320 okbutton->setText(qt_("&OK"));
324 string const bpath = buffer().filePath();
325 // Path might be relative to current Buffer, so make absolute
326 FileName const absfname = support::makeAbsPath(fromqstr(fname), bpath);
328 // Do we have a LyX filename?
329 if (!isLyXFileName(fromqstr(fname))) {
330 okbutton->setText(qt_("&OK"));
331 return absfname.exists();
334 // Set OK button text according to whether file already exists
335 okbutton->setText(absfname.exists() ? qt_("&OK") : qt_("&Create"));
336 // enable edit button iff file is open in some Buffer
337 editPB->setEnabled(theBufferList().getBuffer(absfname));
342 void GuiInclude::browse()
346 int const item = typeCO->currentIndex();
356 QString name = browse(filenameED->text(), type);
358 filenameED->setText(name);
362 QString GuiInclude::browse(QString const & in_name, Type in_type) const
364 QString const title = qt_("Select document to include");
366 // input TeX, verbatim, or LyX file ?
371 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
375 filters = fileFilters(QString());
379 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
381 return browseRelToParent(in_name, docpath, title, filters, false,
382 qt_("D&ocuments"), toqstr(lyxrc.document_path));
386 bool GuiInclude::initialiseParams(std::string const & sdata)
388 InsetCommand::string2params(sdata, params_);
389 paramsToDialog(params_);
394 void GuiInclude::dispatchParams()
396 std::string const lfun = InsetCommand::params2string(params_);
397 dispatch(FuncRequest(getLfun(), lfun));
401 } // namespace frontend
404 #include "moc_GuiInclude.cpp"