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 bc().addCheckedLineEdit(filenameED, filenameLA);
91 void GuiInclude::change_adaptor()
97 docstring GuiInclude::validate_listings_params()
99 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
101 string params = fromqstr(listingsED->toPlainText());
102 InsetListingsParams lstparams(params);
103 lstparams.setMinted(buffer().params().use_minted);
104 return lstparams.validate();
108 void GuiInclude::setListingsMsg()
111 static bool isOK = true;
112 docstring msg = validate_listings_params();
117 listingsTB->setPlainText(
118 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
121 listingsTB->setPlainText(toqstr(msg));
126 void GuiInclude::typeChanged(int v)
131 visiblespaceCB->setEnabled(false);
132 visiblespaceCB->setChecked(false);
133 previewCB->setEnabled(false);
134 previewCB->setChecked(false);
135 listingsGB->setEnabled(false);
139 visiblespaceCB->setEnabled(false);
140 visiblespaceCB->setChecked(false);
141 previewCB->setEnabled(true);
142 listingsGB->setEnabled(false);
146 visiblespaceCB->setEnabled(false);
147 visiblespaceCB->setChecked(false);
148 previewCB->setEnabled(false);
149 previewCB->setChecked(false);
150 listingsGB->setEnabled(true);
154 visiblespaceCB->setEnabled(true);
155 previewCB->setEnabled(false);
156 previewCB->setChecked(false);
157 listingsGB->setEnabled(false);
163 void GuiInclude::paramsToDialog(InsetCommandParams const & icp)
165 filenameED->setText(toqstr(icp["filename"]));
167 visiblespaceCB->setChecked(false);
168 visiblespaceCB->setEnabled(false);
169 previewCB->setChecked(false);
170 previewCB->setEnabled(false);
171 listingsGB->setEnabled(false);
175 listingsTB->setPlainText(
176 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
178 string cmdname = icp.getCmdName();
179 if (cmdname != "include" &&
180 cmdname != "verbatiminput" &&
181 cmdname != "verbatiminput*" &&
182 cmdname != "lstinputlisting" &&
183 cmdname != "inputminted")
186 if (cmdname == "include") {
187 typeCO->setCurrentIndex(0);
189 } else if (cmdname == "input") {
190 typeCO->setCurrentIndex(1);
191 previewCB->setEnabled(true);
192 previewCB->setChecked(icp.preview());
194 } else if (cmdname == "verbatiminput*") {
195 typeCO->setCurrentIndex(2);
196 visiblespaceCB->setEnabled(true);
197 visiblespaceCB->setChecked(true);
199 } else if (cmdname == "verbatiminput") {
200 typeCO->setCurrentIndex(2);
201 visiblespaceCB->setEnabled(true);
203 } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
204 typeCO->setCurrentIndex(3);
205 listingsGB->setEnabled(true);
206 listingsED->setEnabled(true);
207 InsetListingsParams par(to_utf8(icp["lstparams"]));
208 // extract caption and label and put them into their respective editboxes
209 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
210 for (vector<string>::iterator it = pars.begin();
211 it != pars.end(); ++it) {
212 if (prefixIs(*it, "caption=")) {
213 string cap = it->substr(8);
214 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
215 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
218 } else if (prefixIs(*it, "label=")) {
219 string lbl = it->substr(6);
220 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
221 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
226 // the rest is put to the extra edit box.
227 string extra = getStringFromVector(pars);
228 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
230 literalCB->setChecked(icp["literal"] == "true");
232 // Make sure that the bc is in the INITIAL state
233 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
238 void GuiInclude::applyView()
240 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
241 params_.preview(previewCB->isChecked());
243 int const item = typeCO->currentIndex();
245 params_.setCmdName("include");
246 } else if (item == 1) {
247 params_.setCmdName("input");
248 } else if (item == 3) {
249 if (buffer().params().use_minted)
250 params_.setCmdName("inputminted");
252 params_.setCmdName("lstinputlisting");
253 // the parameter string should have passed validation
254 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
255 string caption = fromqstr(captionLE->text());
256 string label = fromqstr(labelLE->text());
257 if (!caption.empty())
258 par.addParam("caption", "{" + caption + "}");
260 par.addParam("label", "{" + label + "}");
261 string const listparams = par.params();
262 params_["lstparams"] = from_utf8(listparams);
264 if (visiblespaceCB->isChecked())
265 params_.setCmdName("verbatiminput*");
267 params_.setCmdName("verbatiminput");
269 params_["literal"] = literalCB->isChecked()
270 ? from_ascii("true") : from_ascii("false");
272 // Do we need to create a LyX file?
273 if (item == 0 || item == 1) {
274 QString fname = filenameED->text();
275 string const mypath = buffer().absFileName();
276 string const bpath = buffer().filePath();
277 QString absfname = makeAbsPath(fname, toqstr(bpath));
278 if (!QFile::exists(absfname)) {
279 dispatch(FuncRequest(LFUN_BUFFER_NEW, fromqstr(absfname)));
280 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
281 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, mypath));
287 void GuiInclude::edit()
291 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
296 QString const fname = filenameED->text();
297 string const bpath = buffer().filePath();
298 string const absfname = support::makeAbsPath(fromqstr(fname), bpath).absFileName();
299 // The button is enabled only if the document is already open.
300 // If something goes wrong and it is not, we'll get an error
301 // message from the dispatch. So no need for one here.
302 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, absfname));
306 bool GuiInclude::isValid()
308 QString fname = filenameED->text();
309 if (fname.isEmpty() || !validate_listings_params().empty()) {
310 editPB->setEnabled(false);
314 QPushButton * okbutton = buttonBox->button(QDialogButtonBox::Ok);
315 int const item = typeCO->currentIndex();
316 // Are we inputting or including a LyX file?
317 if (item != 0 && item != 1) {
318 okbutton->setText(qt_("&OK"));
322 string const bpath = buffer().filePath();
323 // Path might be relative to current Buffer, so make absolute
324 FileName const absfname = support::makeAbsPath(fromqstr(fname), bpath);
326 // Do we have a LyX filename?
327 if (!isLyXFileName(fromqstr(fname))) {
328 okbutton->setText(qt_("&OK"));
329 return absfname.exists();
332 // Set OK button text according to whether file already exists
333 okbutton->setText(absfname.exists() ? qt_("&OK") : qt_("&Create"));
334 // enable edit button iff file is open in some Buffer
335 editPB->setEnabled(theBufferList().getBuffer(absfname));
340 void GuiInclude::browse()
344 int const item = typeCO->currentIndex();
354 QString name = browse(filenameED->text(), type);
356 filenameED->setText(name);
360 QString GuiInclude::browse(QString const & in_name, Type in_type) const
362 QString const title = qt_("Select document to include");
364 // input TeX, verbatim, or LyX file ?
369 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
373 filters = fileFilters(QString());
377 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
379 return browseRelToParent(in_name, docpath, title, filters, false,
380 qt_("D&ocuments"), toqstr(lyxrc.document_path));
384 bool GuiInclude::initialiseParams(std::string const & sdata)
386 InsetCommand::string2params(sdata, params_);
387 paramsToDialog(params_);
392 void GuiInclude::dispatchParams()
394 std::string const lfun = InsetCommand::params2string(params_);
395 dispatch(FuncRequest(getLfun(), lfun));
399 } // namespace frontend
402 #include "moc_GuiInclude.cpp"