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>
46 using support::FileFilterList;
47 using support::FileName;
48 using support::makeAbsPath;
49 using support::onlyPath;
50 using support::os::internal_path;
51 using support::prefixIs;
52 using support::getStringFromVector;
53 using support::getVectorFromString;
56 GuiInclude::GuiInclude(GuiView & lv)
57 : GuiCommand(lv, "include")
60 setViewTitle(_("Child Document"));
62 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
63 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
65 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
66 connect(filenameED, SIGNAL(textChanged(const QString &)),
67 this, SLOT(change_adaptor()));
68 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
69 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
70 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
71 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
72 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
73 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
74 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
75 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
76 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
77 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
78 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
80 setFocusProxy(filenameED);
82 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
84 bc().setCancel(closePB);
85 bc().addReadOnly(filenameED);
86 bc().addReadOnly(browsePB);
87 bc().addReadOnly(visiblespaceCB);
88 bc().addReadOnly(typeCO);
89 bc().addReadOnly(listingsED);
91 bc().addCheckedLineEdit(filenameED, filenameLA);
95 void GuiInclude::change_adaptor()
101 docstring GuiInclude::validate_listings_params()
103 // use a cache here to avoid repeated validation
104 // of the same parameters
105 static string param_cache = string();
106 static docstring msg_cache = docstring();
108 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
111 string params = fromqstr(listingsED->toPlainText());
112 if (params != param_cache) {
113 param_cache = params;
114 msg_cache = InsetListingsParams(params).validate();
120 void GuiInclude::set_listings_msg()
122 static bool isOK = true;
123 docstring msg = validate_listings_params();
128 listingsTB->setPlainText(
129 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
132 listingsTB->setPlainText(toqstr(msg));
137 void GuiInclude::closeEvent(QCloseEvent * e)
144 void GuiInclude::typeChanged(int v)
149 visiblespaceCB->setEnabled(false);
150 visiblespaceCB->setChecked(false);
151 previewCB->setEnabled(false);
152 previewCB->setChecked(false);
153 listingsGB->setEnabled(false);
157 visiblespaceCB->setEnabled(false);
158 visiblespaceCB->setChecked(false);
159 previewCB->setEnabled(true);
160 listingsGB->setEnabled(false);
164 visiblespaceCB->setEnabled(false);
165 visiblespaceCB->setChecked(false);
166 previewCB->setEnabled(false);
167 previewCB->setChecked(false);
168 listingsGB->setEnabled(true);
172 visiblespaceCB->setEnabled(true);
173 previewCB->setEnabled(false);
174 previewCB->setChecked(false);
175 listingsGB->setEnabled(false);
179 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
180 //for the reason this is here.
181 okPB->setDefault(true);
185 void GuiInclude::updateContents()
187 filenameED->setText(toqstr(params_["filename"]));
189 visiblespaceCB->setChecked(false);
190 visiblespaceCB->setEnabled(false);
191 previewCB->setChecked(false);
192 previewCB->setEnabled(false);
193 listingsGB->setEnabled(false);
197 listingsTB->setPlainText(
198 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
200 string cmdname = params_.getCmdName();
201 if (cmdname != "include" &&
202 cmdname != "verbatiminput" &&
203 cmdname != "verbatiminput*" &&
204 cmdname != "lstinputlisting")
207 if (cmdname == "include") {
208 typeCO->setCurrentIndex(0);
210 } else if (cmdname == "input") {
211 typeCO->setCurrentIndex(1);
212 previewCB->setEnabled(true);
213 previewCB->setChecked(params_.preview());
215 } else if (cmdname == "verbatiminput*") {
216 typeCO->setCurrentIndex(2);
217 visiblespaceCB->setEnabled(true);
218 visiblespaceCB->setChecked(true);
220 } else if (cmdname == "verbatiminput") {
221 typeCO->setCurrentIndex(2);
222 visiblespaceCB->setEnabled(true);
224 } else if (cmdname == "lstinputlisting") {
225 typeCO->setCurrentIndex(3);
226 listingsGB->setEnabled(true);
227 listingsED->setEnabled(true);
228 InsetListingsParams par(to_utf8(params_["lstparams"]));
229 // extract caption and label and put them into their respective editboxes
230 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
231 for (vector<string>::iterator it = pars.begin();
232 it != pars.end(); ++it) {
233 if (prefixIs(*it, "caption=")) {
234 string cap = it->substr(8);
235 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
236 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
239 } else if (prefixIs(*it, "label=")) {
240 string lbl = it->substr(6);
241 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
242 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
247 // the rest is put to the extra edit box.
248 string extra = getStringFromVector(pars);
249 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
254 void GuiInclude::applyView()
256 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
257 params_.preview(previewCB->isChecked());
259 int const item = typeCO->currentIndex();
261 params_.setCmdName("include");
262 } else if (item == 1) {
263 params_.setCmdName("input");
264 } else if (item == 3) {
265 params_.setCmdName("lstinputlisting");
266 // the parameter string should have passed validation
267 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
268 string caption = fromqstr(captionLE->text());
269 string label = fromqstr(labelLE->text());
270 if (!caption.empty())
271 par.addParam("caption", "{" + caption + "}");
273 par.addParam("label", "{" + label + "}");
274 string const listparams = par.params();
275 params_["lstparams"] = from_ascii(listparams);
277 if (visiblespaceCB->isChecked())
278 params_.setCmdName("verbatiminput*");
280 params_.setCmdName("verbatiminput");
285 void GuiInclude::browse()
289 int const item = typeCO->currentIndex();
299 docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
301 filenameED->setText(toqstr(name));
305 void GuiInclude::edit()
308 string const file = fromqstr(filenameED->text());
315 bool GuiInclude::isValid()
317 return !filenameED->text().isEmpty() && validate_listings_params().empty();
321 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
323 docstring const title = _("Select document to include");
325 // input TeX, verbatim, or LyX file ?
326 FileFilterList filters;
330 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
337 docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
339 return browseRelFile(in_name, docpath, title, filters, false,
340 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
344 void GuiInclude::edit(string const & file)
346 string const ext = support::getExtension(file);
348 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
350 // tex file or other text file in verbatim mode
351 formats.edit(buffer(),
352 makeAbsPath(file, onlyPath(buffer().absFileName())),
357 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
360 } // namespace frontend
363 #include "GuiInclude_moc.cpp"