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"
18 #include "EmbeddedFiles.h"
20 #include "FuncRequest.h"
21 #include "support/gettext.h"
24 #include "qt_helpers.h"
27 #include "frontends/alert.h"
29 #include "support/os.h"
30 #include "support/lstrings.h"
31 #include "support/ExceptionMessage.h"
32 #include "support/FileFilterList.h"
33 #include "support/filetools.h"
35 #include "insets/InsetListingsParams.h"
36 #include "insets/InsetInclude.h"
38 #include <QPushButton>
45 using namespace lyx::support;
46 using namespace lyx::support::os;
52 GuiInclude::GuiInclude(GuiView & lv)
53 : GuiCommand(lv, "include", qt_("Child Document"))
57 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
58 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
60 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
61 connect(filenameED, SIGNAL(textChanged(const QString &)),
62 this, SLOT(change_adaptor()));
63 connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
64 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
65 connect(embedCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
66 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
67 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
68 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
70 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
71 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
72 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
73 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
74 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
76 setFocusProxy(filenameED);
78 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
80 bc().setCancel(closePB);
81 bc().addReadOnly(filenameED);
82 bc().addReadOnly(browsePB);
83 bc().addReadOnly(visiblespaceCB);
84 bc().addReadOnly(typeCO);
85 bc().addReadOnly(listingsED);
87 bc().addCheckedLineEdit(filenameED, filenameLA);
91 void GuiInclude::change_adaptor()
97 docstring GuiInclude::validate_listings_params()
99 // use a cache here to avoid repeated validation
100 // of the same parameters
101 static string param_cache = string();
102 static docstring msg_cache = docstring();
104 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
107 string params = fromqstr(listingsED->toPlainText());
108 if (params != param_cache) {
109 param_cache = params;
110 msg_cache = InsetListingsParams(params).validate();
116 void GuiInclude::set_listings_msg()
118 static bool isOK = true;
119 docstring msg = validate_listings_params();
124 listingsTB->setPlainText(
125 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
128 listingsTB->setPlainText(toqstr(msg));
133 void GuiInclude::typeChanged(int v)
138 visiblespaceCB->setEnabled(false);
139 visiblespaceCB->setChecked(false);
140 previewCB->setEnabled(false);
141 previewCB->setChecked(false);
142 listingsGB->setEnabled(false);
146 visiblespaceCB->setEnabled(false);
147 visiblespaceCB->setChecked(false);
148 previewCB->setEnabled(true);
149 listingsGB->setEnabled(false);
153 visiblespaceCB->setEnabled(false);
154 visiblespaceCB->setChecked(false);
155 previewCB->setEnabled(false);
156 previewCB->setChecked(false);
157 listingsGB->setEnabled(true);
161 visiblespaceCB->setEnabled(true);
162 previewCB->setEnabled(false);
163 previewCB->setChecked(false);
164 listingsGB->setEnabled(false);
168 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
169 //for the reason this is here.
170 okPB->setDefault(true);
174 void GuiInclude::updateContents()
176 filenameED->setText(toqstr(params_["filename"]));
177 embedCB->setCheckState(params_["embed"].empty() ? Qt::Unchecked : Qt::Checked);
179 visiblespaceCB->setChecked(false);
180 visiblespaceCB->setEnabled(false);
181 previewCB->setChecked(false);
182 previewCB->setEnabled(false);
183 listingsGB->setEnabled(false);
187 listingsTB->setPlainText(
188 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
190 string cmdname = params_.getCmdName();
191 if (cmdname != "include" &&
192 cmdname != "verbatiminput" &&
193 cmdname != "verbatiminput*" &&
194 cmdname != "lstinputlisting")
197 if (cmdname == "include") {
198 typeCO->setCurrentIndex(0);
200 } else if (cmdname == "input") {
201 typeCO->setCurrentIndex(1);
202 previewCB->setEnabled(true);
203 previewCB->setChecked(params_.preview());
205 } else if (cmdname == "verbatiminput*") {
206 typeCO->setCurrentIndex(2);
207 visiblespaceCB->setEnabled(true);
208 visiblespaceCB->setChecked(true);
210 } else if (cmdname == "verbatiminput") {
211 typeCO->setCurrentIndex(2);
212 visiblespaceCB->setEnabled(true);
214 } else if (cmdname == "lstinputlisting") {
215 typeCO->setCurrentIndex(3);
216 listingsGB->setEnabled(true);
217 listingsED->setEnabled(true);
218 InsetListingsParams par(to_utf8(params_["lstparams"]));
219 // extract caption and label and put them into their respective editboxes
220 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
221 for (vector<string>::iterator it = pars.begin();
222 it != pars.end(); ++it) {
223 if (prefixIs(*it, "caption=")) {
224 string cap = it->substr(8);
225 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
226 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
229 } else if (prefixIs(*it, "label=")) {
230 string lbl = it->substr(6);
231 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
232 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
237 // the rest is put to the extra edit box.
238 string extra = getStringFromVector(pars);
239 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
244 void GuiInclude::applyView()
246 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
247 params_["embed"].clear();
249 Buffer & buf = buffer();
250 EmbeddedFile file(to_utf8(params_["filename"]), buf.filePath());
251 file.setEmbed(embedCB->checkState() == Qt::Checked);
252 // move file around if needed, an exception may be raised.
253 file.enable(buf.embedded(), &buf, true);
254 // if things are OK..., set params_["embed"]
255 params_["embed"] = file.embedded() ? from_utf8(file.inzipName()) : docstring();
256 } catch (ExceptionMessage const & message) {
257 Alert::error(message.title_, message.details_);
258 // params_["embed"] will be empty if a file is failed to embed
260 params_.preview(previewCB->isChecked());
262 int const item = typeCO->currentIndex();
264 params_.setCmdName("include");
265 } else if (item == 1) {
266 params_.setCmdName("input");
267 } else if (item == 3) {
268 params_.setCmdName("lstinputlisting");
269 // the parameter string should have passed validation
270 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
271 string caption = fromqstr(captionLE->text());
272 string label = fromqstr(labelLE->text());
273 if (!caption.empty())
274 par.addParam("caption", "{" + caption + "}");
276 par.addParam("label", "{" + label + "}");
277 string const listparams = par.params();
278 params_["lstparams"] = from_ascii(listparams);
280 if (visiblespaceCB->isChecked())
281 params_.setCmdName("verbatiminput*");
283 params_.setCmdName("verbatiminput");
288 void GuiInclude::browse()
292 int const item = typeCO->currentIndex();
302 QString name = browse(filenameED->text(), type);
304 filenameED->setText(name);
308 void GuiInclude::edit()
311 string const file = fromqstr(filenameED->text());
318 bool GuiInclude::isValid()
320 return !filenameED->text().isEmpty() && validate_listings_params().empty();
324 QString GuiInclude::browse(QString const & in_name, Type in_type) const
326 QString const title = qt_("Select document to include");
328 // input TeX, verbatim, or LyX file ?
329 FileFilterList filters;
333 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
340 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
342 return browseRelFile(in_name, docpath, title, filters, false,
343 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
347 void GuiInclude::edit(string const & file)
349 string const ext = support::getExtension(file);
351 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
353 // tex file or other text file in verbatim mode
354 formats.edit(buffer(),
355 makeAbsPath(file, support::onlyPath(buffer().absFileName())),
360 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
363 } // namespace frontend
366 #include "GuiInclude_moc.cpp"