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>
41 using namespace lyx::support;
42 using namespace lyx::support::os;
48 GuiInclude::GuiInclude(GuiView & lv)
49 : GuiCommand(lv, "include", qt_("Child Document"))
53 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
56 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
57 connect(filenameED, SIGNAL(textChanged(const QString &)),
58 this, SLOT(change_adaptor()));
59 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
60 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
61 connect(embedCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
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"]));
173 embedCB->setCheckState(params_["embed"].empty() ? Qt::Unchecked : Qt::Checked);
175 visiblespaceCB->setChecked(false);
176 visiblespaceCB->setEnabled(false);
177 previewCB->setChecked(false);
178 previewCB->setEnabled(false);
179 listingsGB->setEnabled(false);
183 listingsTB->setPlainText(
184 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
186 string cmdname = params_.getCmdName();
187 if (cmdname != "include" &&
188 cmdname != "verbatiminput" &&
189 cmdname != "verbatiminput*" &&
190 cmdname != "lstinputlisting")
193 if (cmdname == "include") {
194 typeCO->setCurrentIndex(0);
196 } else if (cmdname == "input") {
197 typeCO->setCurrentIndex(1);
198 previewCB->setEnabled(true);
199 previewCB->setChecked(params_.preview());
201 } else if (cmdname == "verbatiminput*") {
202 typeCO->setCurrentIndex(2);
203 visiblespaceCB->setEnabled(true);
204 visiblespaceCB->setChecked(true);
206 } else if (cmdname == "verbatiminput") {
207 typeCO->setCurrentIndex(2);
208 visiblespaceCB->setEnabled(true);
210 } else if (cmdname == "lstinputlisting") {
211 typeCO->setCurrentIndex(3);
212 listingsGB->setEnabled(true);
213 listingsED->setEnabled(true);
214 InsetListingsParams par(to_utf8(params_["lstparams"]));
215 // extract caption and label and put them into their respective editboxes
216 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
217 for (vector<string>::iterator it = pars.begin();
218 it != pars.end(); ++it) {
219 if (prefixIs(*it, "caption=")) {
220 string cap = it->substr(8);
221 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
222 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
225 } else if (prefixIs(*it, "label=")) {
226 string lbl = it->substr(6);
227 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
228 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
233 // the rest is put to the extra edit box.
234 string extra = getStringFromVector(pars);
235 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
240 void GuiInclude::applyView()
242 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
243 params_["embed"] = embedCB->checkState() == Qt::Checked ? _("true") : _("false");
244 params_.preview(previewCB->isChecked());
246 int const item = typeCO->currentIndex();
248 params_.setCmdName("include");
249 } else if (item == 1) {
250 params_.setCmdName("input");
251 } else if (item == 3) {
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_ascii(listparams);
264 if (visiblespaceCB->isChecked())
265 params_.setCmdName("verbatiminput*");
267 params_.setCmdName("verbatiminput");
272 void GuiInclude::browse()
276 int const item = typeCO->currentIndex();
286 QString name = browse(filenameED->text(), type);
288 filenameED->setText(name);
292 void GuiInclude::edit()
295 string const file = fromqstr(filenameED->text());
302 bool GuiInclude::isValid()
304 return !filenameED->text().isEmpty() && validate_listings_params().empty();
308 QString GuiInclude::browse(QString const & in_name, Type in_type) const
310 QString const title = qt_("Select document to include");
312 // input TeX, verbatim, or LyX file ?
313 FileFilterList filters;
317 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
324 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
326 return browseRelFile(in_name, docpath, title, filters, false,
327 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
331 void GuiInclude::edit(string const & file)
333 string const ext = support::getExtension(file);
335 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
337 // tex file or other text file in verbatim mode
338 formats.edit(buffer(),
339 makeAbsPath(file, support::onlyPath(buffer().absFileName())),
344 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
347 } // namespace frontend
350 #include "GuiInclude_moc.cpp"