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 "FuncRequest.h"
21 #include "qt_helpers.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
30 #include "insets/InsetListingsParams.h"
31 #include "insets/InsetInclude.h"
33 #include <QPushButton>
40 using namespace lyx::support;
41 using namespace lyx::support::os;
47 GuiInclude::GuiInclude(GuiView & lv)
48 : GuiDialog(lv, "include", qt_("Child Document")),
49 params_(insetCode("include"))
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(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
62 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
63 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
64 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
65 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
66 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
67 connect(listingsED, SIGNAL(textChanged()), this, SLOT(setListingsMsg()));
68 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
71 setFocusProxy(filenameED);
73 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
75 bc().setCancel(closePB);
76 bc().addReadOnly(filenameED);
77 bc().addReadOnly(browsePB);
78 bc().addReadOnly(visiblespaceCB);
79 bc().addReadOnly(typeCO);
80 bc().addReadOnly(listingsED);
82 bc().addCheckedLineEdit(filenameED, filenameLA);
86 void GuiInclude::change_adaptor()
92 docstring GuiInclude::validate_listings_params()
94 // use a cache here to avoid repeated validation
95 // 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::setListingsMsg()
115 static bool isOK = true;
116 docstring msg = validate_listings_params();
121 listingsTB->setPlainText(
122 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
125 listingsTB->setPlainText(toqstr(msg));
130 void GuiInclude::typeChanged(int v)
135 visiblespaceCB->setEnabled(false);
136 visiblespaceCB->setChecked(false);
137 previewCB->setEnabled(false);
138 previewCB->setChecked(false);
139 listingsGB->setEnabled(false);
143 visiblespaceCB->setEnabled(false);
144 visiblespaceCB->setChecked(false);
145 previewCB->setEnabled(true);
146 listingsGB->setEnabled(false);
150 visiblespaceCB->setEnabled(false);
151 visiblespaceCB->setChecked(false);
152 previewCB->setEnabled(false);
153 previewCB->setChecked(false);
154 listingsGB->setEnabled(true);
158 visiblespaceCB->setEnabled(true);
159 previewCB->setEnabled(false);
160 previewCB->setChecked(false);
161 listingsGB->setEnabled(false);
165 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
166 //for the reason this is here.
167 okPB->setDefault(true);
171 void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
173 filenameED->setText(toqstr(params_["filename"]));
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()));
238 // Make sure that the bc is in the INITIAL state
239 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
244 void GuiInclude::applyView()
246 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
247 params_.preview(previewCB->isChecked());
249 int const item = typeCO->currentIndex();
251 params_.setCmdName("include");
252 } else if (item == 1) {
253 params_.setCmdName("input");
254 } else if (item == 3) {
255 params_.setCmdName("lstinputlisting");
256 // the parameter string should have passed validation
257 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
258 string caption = fromqstr(captionLE->text());
259 string label = fromqstr(labelLE->text());
260 if (!caption.empty())
261 par.addParam("caption", "{" + caption + "}");
263 par.addParam("label", "{" + label + "}");
264 string const listparams = par.params();
265 params_["lstparams"] = from_utf8(listparams);
267 if (visiblespaceCB->isChecked())
268 params_.setCmdName("verbatiminput*");
270 params_.setCmdName("verbatiminput");
275 void GuiInclude::browse()
279 int const item = typeCO->currentIndex();
289 QString name = browse(filenameED->text(), type);
291 filenameED->setText(name);
295 void GuiInclude::edit()
299 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
304 dispatch(FuncRequest(LFUN_INSET_EDIT));
308 bool GuiInclude::isValid()
310 return !filenameED->text().isEmpty() && validate_listings_params().empty();
314 QString GuiInclude::browse(QString const & in_name, Type in_type) const
316 QString const title = qt_("Select document to include");
318 // input TeX, verbatim, or LyX file ?
323 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
327 filters = fileFilters(QString());
331 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
333 return browseRelToParent(in_name, docpath, title, filters, false,
334 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
338 bool GuiInclude::initialiseParams(std::string const & data)
340 InsetCommand::string2params(data, params_);
341 paramsToDialog(params_);
346 void GuiInclude::dispatchParams()
348 std::string const lfun = InsetCommand::params2string(params_);
349 dispatch(FuncRequest(getLfun(), lfun));
353 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
356 } // namespace frontend
359 #include "moc_GuiInclude.cpp"