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"
19 #include "FuncRequest.h"
20 #include "support/gettext.h"
23 #include "qt_helpers.h"
26 #include "support/os.h"
27 #include "support/lstrings.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h"
31 #include "insets/InsetListingsParams.h"
32 #include "insets/InsetInclude.h"
34 #include <QPushButton>
36 #include <QCloseEvent>
42 using namespace lyx::support;
43 using namespace lyx::support::os;
49 GuiInclude::GuiInclude(GuiView & lv)
50 : GuiCommand(lv, "include")
53 setViewTitle(_("Child Document"));
55 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
56 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
58 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
59 connect(filenameED, SIGNAL(textChanged(const QString &)),
60 this, SLOT(change_adaptor()));
61 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
62 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
63 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
64 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
65 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
66 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
67 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
68 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
69 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
70 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
73 setFocusProxy(filenameED);
75 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
77 bc().setCancel(closePB);
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 // use a cache here to avoid repeated validation
97 // of the same parameters
98 static string param_cache = string();
99 static docstring msg_cache = docstring();
101 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
104 string params = fromqstr(listingsED->toPlainText());
105 if (params != param_cache) {
106 param_cache = params;
107 msg_cache = InsetListingsParams(params).validate();
113 void GuiInclude::set_listings_msg()
115 static bool isOK = true;
116 docstring msg = validate_listings_params();
121 listingsTB->setPlainText(
122 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
125 listingsTB->setPlainText(toqstr(msg));
130 void GuiInclude::closeEvent(QCloseEvent * e)
137 void GuiInclude::typeChanged(int v)
142 visiblespaceCB->setEnabled(false);
143 visiblespaceCB->setChecked(false);
144 previewCB->setEnabled(false);
145 previewCB->setChecked(false);
146 listingsGB->setEnabled(false);
150 visiblespaceCB->setEnabled(false);
151 visiblespaceCB->setChecked(false);
152 previewCB->setEnabled(true);
153 listingsGB->setEnabled(false);
157 visiblespaceCB->setEnabled(false);
158 visiblespaceCB->setChecked(false);
159 previewCB->setEnabled(false);
160 previewCB->setChecked(false);
161 listingsGB->setEnabled(true);
165 visiblespaceCB->setEnabled(true);
166 previewCB->setEnabled(false);
167 previewCB->setChecked(false);
168 listingsGB->setEnabled(false);
172 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
173 //for the reason this is here.
174 okPB->setDefault(true);
178 void GuiInclude::updateContents()
180 filenameED->setText(toqstr(params_["filename"]));
182 visiblespaceCB->setChecked(false);
183 visiblespaceCB->setEnabled(false);
184 previewCB->setChecked(false);
185 previewCB->setEnabled(false);
186 listingsGB->setEnabled(false);
190 listingsTB->setPlainText(
191 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
193 string cmdname = params_.getCmdName();
194 if (cmdname != "include" &&
195 cmdname != "verbatiminput" &&
196 cmdname != "verbatiminput*" &&
197 cmdname != "lstinputlisting")
200 if (cmdname == "include") {
201 typeCO->setCurrentIndex(0);
203 } else if (cmdname == "input") {
204 typeCO->setCurrentIndex(1);
205 previewCB->setEnabled(true);
206 previewCB->setChecked(params_.preview());
208 } else if (cmdname == "verbatiminput*") {
209 typeCO->setCurrentIndex(2);
210 visiblespaceCB->setEnabled(true);
211 visiblespaceCB->setChecked(true);
213 } else if (cmdname == "verbatiminput") {
214 typeCO->setCurrentIndex(2);
215 visiblespaceCB->setEnabled(true);
217 } else if (cmdname == "lstinputlisting") {
218 typeCO->setCurrentIndex(3);
219 listingsGB->setEnabled(true);
220 listingsED->setEnabled(true);
221 InsetListingsParams par(to_utf8(params_["lstparams"]));
222 // extract caption and label and put them into their respective editboxes
223 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
224 for (vector<string>::iterator it = pars.begin();
225 it != pars.end(); ++it) {
226 if (prefixIs(*it, "caption=")) {
227 string cap = it->substr(8);
228 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
229 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
232 } else if (prefixIs(*it, "label=")) {
233 string lbl = it->substr(6);
234 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
235 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
240 // the rest is put to the extra edit box.
241 string extra = getStringFromVector(pars);
242 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
247 void GuiInclude::applyView()
249 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
250 params_.preview(previewCB->isChecked());
252 int const item = typeCO->currentIndex();
254 params_.setCmdName("include");
255 } else if (item == 1) {
256 params_.setCmdName("input");
257 } else if (item == 3) {
258 params_.setCmdName("lstinputlisting");
259 // the parameter string should have passed validation
260 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
261 string caption = fromqstr(captionLE->text());
262 string label = fromqstr(labelLE->text());
263 if (!caption.empty())
264 par.addParam("caption", "{" + caption + "}");
266 par.addParam("label", "{" + label + "}");
267 string const listparams = par.params();
268 params_["lstparams"] = from_ascii(listparams);
270 if (visiblespaceCB->isChecked())
271 params_.setCmdName("verbatiminput*");
273 params_.setCmdName("verbatiminput");
278 void GuiInclude::browse()
282 int const item = typeCO->currentIndex();
292 docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
294 filenameED->setText(toqstr(name));
298 void GuiInclude::edit()
301 string const file = fromqstr(filenameED->text());
308 bool GuiInclude::isValid()
310 return !filenameED->text().isEmpty() && validate_listings_params().empty();
314 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
316 docstring const title = _("Select document to include");
318 // input TeX, verbatim, or LyX file ?
319 FileFilterList filters;
323 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
330 docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
332 return browseRelFile(in_name, docpath, title, filters, false,
333 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
337 void GuiInclude::edit(string const & file)
339 string const ext = support::getExtension(file);
341 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
343 // tex file or other text file in verbatim mode
344 formats.edit(buffer(),
345 makeAbsPath(file, onlyPath(buffer().absFileName())),
350 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
353 } // namespace frontend
356 #include "GuiInclude_moc.cpp"