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>
50 using support::FileFilterList;
51 using support::FileName;
52 using support::makeAbsPath;
53 using support::onlyPath;
54 using support::os::internal_path;
55 using support::prefixIs;
56 using support::getStringFromVector;
57 using support::getVectorFromString;
60 GuiInclude::GuiInclude(GuiView & lv)
61 : GuiCommand(lv, "include")
64 setViewTitle(_("Child Document"));
66 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
67 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
69 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70 connect(filenameED, SIGNAL(textChanged(const QString &)),
71 this, SLOT(change_adaptor()));
72 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
73 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
74 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
75 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
76 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
77 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
78 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
79 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
80 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
81 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
82 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
84 setFocusProxy(filenameED);
86 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
88 bc().setCancel(closePB);
89 bc().addReadOnly(filenameED);
90 bc().addReadOnly(browsePB);
91 bc().addReadOnly(visiblespaceCB);
92 bc().addReadOnly(typeCO);
93 bc().addReadOnly(listingsED);
95 bc().addCheckedLineEdit(filenameED, filenameLA);
99 void GuiInclude::change_adaptor()
105 docstring GuiInclude::validate_listings_params()
107 // use a cache here to avoid repeated validation
108 // of the same parameters
109 static string param_cache = string();
110 static docstring msg_cache = docstring();
112 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
115 string params = fromqstr(listingsED->toPlainText());
116 if (params != param_cache) {
117 param_cache = params;
118 msg_cache = InsetListingsParams(params).validate();
124 void GuiInclude::set_listings_msg()
126 static bool isOK = true;
127 docstring msg = validate_listings_params();
132 listingsTB->setPlainText(
133 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
136 listingsTB->setPlainText(toqstr(msg));
141 void GuiInclude::closeEvent(QCloseEvent * e)
148 void GuiInclude::typeChanged(int v)
153 visiblespaceCB->setEnabled(false);
154 visiblespaceCB->setChecked(false);
155 previewCB->setEnabled(false);
156 previewCB->setChecked(false);
157 listingsGB->setEnabled(false);
161 visiblespaceCB->setEnabled(false);
162 visiblespaceCB->setChecked(false);
163 previewCB->setEnabled(true);
164 listingsGB->setEnabled(false);
168 visiblespaceCB->setEnabled(false);
169 visiblespaceCB->setChecked(false);
170 previewCB->setEnabled(false);
171 previewCB->setChecked(false);
172 listingsGB->setEnabled(true);
176 visiblespaceCB->setEnabled(true);
177 previewCB->setEnabled(false);
178 previewCB->setChecked(false);
179 listingsGB->setEnabled(false);
183 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
184 //for the reason this is here.
185 okPB->setDefault(true);
189 void GuiInclude::updateContents()
191 filenameED->setText(toqstr(params_["filename"]));
193 visiblespaceCB->setChecked(false);
194 visiblespaceCB->setEnabled(false);
195 previewCB->setChecked(false);
196 previewCB->setEnabled(false);
197 listingsGB->setEnabled(false);
201 listingsTB->setPlainText(
202 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
204 string cmdname = params_.getCmdName();
205 if (cmdname != "include" &&
206 cmdname != "verbatiminput" &&
207 cmdname != "verbatiminput*" &&
208 cmdname != "lstinputlisting")
211 if (cmdname == "include") {
212 typeCO->setCurrentIndex(0);
214 } else if (cmdname == "input") {
215 typeCO->setCurrentIndex(1);
216 previewCB->setEnabled(true);
217 previewCB->setChecked(params_.preview());
219 } else if (cmdname == "verbatiminput*") {
220 typeCO->setCurrentIndex(2);
221 visiblespaceCB->setEnabled(true);
222 visiblespaceCB->setChecked(true);
224 } else if (cmdname == "verbatiminput") {
225 typeCO->setCurrentIndex(2);
226 visiblespaceCB->setEnabled(true);
228 } else if (cmdname == "lstinputlisting") {
229 typeCO->setCurrentIndex(3);
230 listingsGB->setEnabled(true);
231 listingsED->setEnabled(true);
232 InsetListingsParams par(to_utf8(params_["lstparams"]));
233 // extract caption and label and put them into their respective editboxes
234 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
235 for (vector<string>::iterator it = pars.begin();
236 it != pars.end(); ++it) {
237 if (prefixIs(*it, "caption=")) {
238 string cap = it->substr(8);
239 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
240 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
243 } else if (prefixIs(*it, "label=")) {
244 string lbl = it->substr(6);
245 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
246 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
251 // the rest is put to the extra edit box.
252 string extra = getStringFromVector(pars);
253 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
258 void GuiInclude::applyView()
260 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
261 params_.preview(previewCB->isChecked());
263 int const item = typeCO->currentIndex();
265 params_.setCmdName("include");
266 } else if (item == 1) {
267 params_.setCmdName("input");
268 } else if (item == 3) {
269 params_.setCmdName("lstinputlisting");
270 // the parameter string should have passed validation
271 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
272 string caption = fromqstr(captionLE->text());
273 string label = fromqstr(labelLE->text());
274 if (!caption.empty())
275 par.addParam("caption", "{" + caption + "}");
277 par.addParam("label", "{" + label + "}");
278 string const listparams = par.params();
279 params_["lstparams"] = from_ascii(listparams);
281 if (visiblespaceCB->isChecked())
282 params_.setCmdName("verbatiminput*");
284 params_.setCmdName("verbatiminput");
289 void GuiInclude::browse()
293 int const item = typeCO->currentIndex();
303 docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
305 filenameED->setText(toqstr(name));
309 void GuiInclude::edit()
312 string const file = fromqstr(filenameED->text());
319 bool GuiInclude::isValid()
321 return !filenameED->text().isEmpty() && validate_listings_params().empty();
325 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
327 docstring const title = _("Select document to include");
329 // input TeX, verbatim, or LyX file ?
330 FileFilterList filters;
334 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
341 docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
343 return browseRelFile(in_name, docpath, title, filters, false,
344 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
348 void GuiInclude::edit(string const & file)
350 string const ext = support::getExtension(file);
352 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
354 // tex file or other text file in verbatim mode
355 formats.edit(buffer(),
356 makeAbsPath(file, onlyPath(buffer().absFileName())),
361 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
364 } // namespace frontend
367 #include "GuiInclude_moc.cpp"