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/FileName.h"
30 #include "support/filetools.h"
32 #include "insets/InsetListingsParams.h"
33 #include "insets/InsetInclude.h"
35 #include <QPushButton>
42 using namespace lyx::support;
43 using namespace lyx::support::os;
49 GuiInclude::GuiInclude(GuiView & lv)
50 : GuiCommand(lv, "include", qt_("Child Document"))
54 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
55 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
57 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
58 connect(filenameED, SIGNAL(textChanged(const QString &)),
59 this, SLOT(change_adaptor()));
60 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
61 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
62 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
63 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
64 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
65 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
66 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
67 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
68 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
72 setFocusProxy(filenameED);
74 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
76 bc().setCancel(closePB);
77 bc().addReadOnly(filenameED);
78 bc().addReadOnly(browsePB);
79 bc().addReadOnly(visiblespaceCB);
80 bc().addReadOnly(typeCO);
81 bc().addReadOnly(listingsED);
83 bc().addCheckedLineEdit(filenameED, filenameLA);
87 void GuiInclude::change_adaptor()
93 docstring GuiInclude::validate_listings_params()
95 // use a cache here to avoid repeated validation
96 // of the same parameters
97 static string param_cache = string();
98 static docstring msg_cache = docstring();
100 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
103 string params = fromqstr(listingsED->toPlainText());
104 if (params != param_cache) {
105 param_cache = params;
106 msg_cache = InsetListingsParams(params).validate();
112 void GuiInclude::set_listings_msg()
114 static bool isOK = true;
115 docstring msg = validate_listings_params();
120 listingsTB->setPlainText(
121 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
124 listingsTB->setPlainText(toqstr(msg));
129 void GuiInclude::typeChanged(int v)
134 visiblespaceCB->setEnabled(false);
135 visiblespaceCB->setChecked(false);
136 previewCB->setEnabled(false);
137 previewCB->setChecked(false);
138 listingsGB->setEnabled(false);
142 visiblespaceCB->setEnabled(false);
143 visiblespaceCB->setChecked(false);
144 previewCB->setEnabled(true);
145 listingsGB->setEnabled(false);
149 visiblespaceCB->setEnabled(false);
150 visiblespaceCB->setChecked(false);
151 previewCB->setEnabled(false);
152 previewCB->setChecked(false);
153 listingsGB->setEnabled(true);
157 visiblespaceCB->setEnabled(true);
158 previewCB->setEnabled(false);
159 previewCB->setChecked(false);
160 listingsGB->setEnabled(false);
164 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
165 //for the reason this is here.
166 okPB->setDefault(true);
170 void GuiInclude::updateContents()
172 filenameED->setText(toqstr(params_["filename"]));
174 visiblespaceCB->setChecked(false);
175 visiblespaceCB->setEnabled(false);
176 previewCB->setChecked(false);
177 previewCB->setEnabled(false);
178 listingsGB->setEnabled(false);
182 listingsTB->setPlainText(
183 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
185 string cmdname = params_.getCmdName();
186 if (cmdname != "include" &&
187 cmdname != "verbatiminput" &&
188 cmdname != "verbatiminput*" &&
189 cmdname != "lstinputlisting")
192 if (cmdname == "include") {
193 typeCO->setCurrentIndex(0);
195 } else if (cmdname == "input") {
196 typeCO->setCurrentIndex(1);
197 previewCB->setEnabled(true);
198 previewCB->setChecked(params_.preview());
200 } else if (cmdname == "verbatiminput*") {
201 typeCO->setCurrentIndex(2);
202 visiblespaceCB->setEnabled(true);
203 visiblespaceCB->setChecked(true);
205 } else if (cmdname == "verbatiminput") {
206 typeCO->setCurrentIndex(2);
207 visiblespaceCB->setEnabled(true);
209 } else if (cmdname == "lstinputlisting") {
210 typeCO->setCurrentIndex(3);
211 listingsGB->setEnabled(true);
212 listingsED->setEnabled(true);
213 InsetListingsParams par(to_utf8(params_["lstparams"]));
214 // extract caption and label and put them into their respective editboxes
215 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
216 for (vector<string>::iterator it = pars.begin();
217 it != pars.end(); ++it) {
218 if (prefixIs(*it, "caption=")) {
219 string cap = it->substr(8);
220 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
221 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
224 } else if (prefixIs(*it, "label=")) {
225 string lbl = it->substr(6);
226 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
227 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
232 // the rest is put to the extra edit box.
233 string extra = getStringFromVector(pars);
234 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
239 void GuiInclude::applyView()
241 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
242 params_.preview(previewCB->isChecked());
244 int const item = typeCO->currentIndex();
246 params_.setCmdName("include");
247 } else if (item == 1) {
248 params_.setCmdName("input");
249 } else if (item == 3) {
250 params_.setCmdName("lstinputlisting");
251 // the parameter string should have passed validation
252 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
253 string caption = fromqstr(captionLE->text());
254 string label = fromqstr(labelLE->text());
255 if (!caption.empty())
256 par.addParam("caption", "{" + caption + "}");
258 par.addParam("label", "{" + label + "}");
259 string const listparams = par.params();
260 params_["lstparams"] = from_ascii(listparams);
262 if (visiblespaceCB->isChecked())
263 params_.setCmdName("verbatiminput*");
265 params_.setCmdName("verbatiminput");
270 void GuiInclude::browse()
274 int const item = typeCO->currentIndex();
284 QString name = browse(filenameED->text(), type);
286 filenameED->setText(name);
290 void GuiInclude::edit()
293 string const file = fromqstr(filenameED->text());
300 bool GuiInclude::isValid()
302 return !filenameED->text().isEmpty() && validate_listings_params().empty();
306 QString GuiInclude::browse(QString const & in_name, Type in_type) const
308 QString const title = qt_("Select document to include");
310 // input TeX, verbatim, or LyX file ?
311 FileFilterList filters;
315 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
322 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
324 return browseRelFile(in_name, docpath, title, filters, false,
325 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
329 void GuiInclude::edit(string const & file)
331 string const ext = support::getExtension(file);
333 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
335 // tex file or other text file in verbatim mode
336 formats.edit(buffer(),
337 makeAbsPath(file, support::onlyPath(buffer().absFileName())),
342 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
345 } // namespace frontend
348 #include "GuiInclude_moc.cpp"