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 "BufferParams.h"
19 #include "FuncRequest.h"
22 #include "qt_helpers.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/FileName.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 : GuiDialog(lv, "include", qt_("Child Document")),
50 params_(insetCode("include"))
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(setListingsMsg()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70 connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
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 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
97 string params = fromqstr(listingsED->toPlainText());
98 InsetListingsParams lstparams(params);
99 lstparams.setMinted(buffer().params().use_minted);
100 return lstparams.validate();
104 void GuiInclude::setListingsMsg()
107 static bool isOK = true;
108 docstring msg = validate_listings_params();
113 listingsTB->setPlainText(
114 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
117 listingsTB->setPlainText(toqstr(msg));
122 void GuiInclude::typeChanged(int v)
127 visiblespaceCB->setEnabled(false);
128 visiblespaceCB->setChecked(false);
129 previewCB->setEnabled(false);
130 previewCB->setChecked(false);
131 listingsGB->setEnabled(false);
135 visiblespaceCB->setEnabled(false);
136 visiblespaceCB->setChecked(false);
137 previewCB->setEnabled(true);
138 listingsGB->setEnabled(false);
142 visiblespaceCB->setEnabled(false);
143 visiblespaceCB->setChecked(false);
144 previewCB->setEnabled(false);
145 previewCB->setChecked(false);
146 listingsGB->setEnabled(true);
150 visiblespaceCB->setEnabled(true);
151 previewCB->setEnabled(false);
152 previewCB->setChecked(false);
153 listingsGB->setEnabled(false);
157 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
158 //for the reason this is here.
159 okPB->setDefault(true);
163 void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
165 filenameED->setText(toqstr(params_["filename"]));
167 visiblespaceCB->setChecked(false);
168 visiblespaceCB->setEnabled(false);
169 previewCB->setChecked(false);
170 previewCB->setEnabled(false);
171 listingsGB->setEnabled(false);
175 listingsTB->setPlainText(
176 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
178 string cmdname = params_.getCmdName();
179 if (cmdname != "include" &&
180 cmdname != "verbatiminput" &&
181 cmdname != "verbatiminput*" &&
182 cmdname != "lstinputlisting" &&
183 cmdname != "inputminted")
186 if (cmdname == "include") {
187 typeCO->setCurrentIndex(0);
189 } else if (cmdname == "input") {
190 typeCO->setCurrentIndex(1);
191 previewCB->setEnabled(true);
192 previewCB->setChecked(params_.preview());
194 } else if (cmdname == "verbatiminput*") {
195 typeCO->setCurrentIndex(2);
196 visiblespaceCB->setEnabled(true);
197 visiblespaceCB->setChecked(true);
199 } else if (cmdname == "verbatiminput") {
200 typeCO->setCurrentIndex(2);
201 visiblespaceCB->setEnabled(true);
203 } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
204 typeCO->setCurrentIndex(3);
205 listingsGB->setEnabled(true);
206 listingsED->setEnabled(true);
207 InsetListingsParams par(to_utf8(params_["lstparams"]));
208 // extract caption and label and put them into their respective editboxes
209 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
210 for (vector<string>::iterator it = pars.begin();
211 it != pars.end(); ++it) {
212 if (prefixIs(*it, "caption=")) {
213 string cap = it->substr(8);
214 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
215 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
218 } else if (prefixIs(*it, "label=")) {
219 string lbl = it->substr(6);
220 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
221 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
226 // the rest is put to the extra edit box.
227 string extra = getStringFromVector(pars);
228 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
231 // Make sure that the bc is in the INITIAL state
232 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
237 void GuiInclude::applyView()
239 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
240 params_.preview(previewCB->isChecked());
242 int const item = typeCO->currentIndex();
244 params_.setCmdName("include");
245 } else if (item == 1) {
246 params_.setCmdName("input");
247 } else if (item == 3) {
248 if (buffer().params().use_minted)
249 params_.setCmdName("inputminted");
251 params_.setCmdName("lstinputlisting");
252 // the parameter string should have passed validation
253 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
254 string caption = fromqstr(captionLE->text());
255 string label = fromqstr(labelLE->text());
256 if (!caption.empty())
257 par.addParam("caption", "{" + caption + "}");
259 par.addParam("label", "{" + label + "}");
260 string const listparams = par.params();
261 params_["lstparams"] = from_utf8(listparams);
263 if (visiblespaceCB->isChecked())
264 params_.setCmdName("verbatiminput*");
266 params_.setCmdName("verbatiminput");
271 void GuiInclude::browse()
275 int const item = typeCO->currentIndex();
285 QString name = browse(filenameED->text(), type);
287 filenameED->setText(name);
291 void GuiInclude::edit()
295 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
300 dispatch(FuncRequest(LFUN_INSET_EDIT));
304 bool GuiInclude::isValid()
306 return !filenameED->text().isEmpty() && validate_listings_params().empty();
310 QString GuiInclude::browse(QString const & in_name, Type in_type) const
312 QString const title = qt_("Select document to include");
314 // input TeX, verbatim, or LyX file ?
319 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
323 filters = fileFilters(QString());
327 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
329 return browseRelToParent(in_name, docpath, title, filters, false,
330 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
334 bool GuiInclude::initialiseParams(std::string const & data)
336 InsetCommand::string2params(data, params_);
337 paramsToDialog(params_);
342 void GuiInclude::dispatchParams()
344 std::string const lfun = InsetCommand::params2string(params_);
345 dispatch(FuncRequest(getLfun(), lfun));
349 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
352 } // namespace frontend
355 #include "moc_GuiInclude.cpp"