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"
36 #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(buttonBox, SIGNAL(clicked(QAbstractButton *)),
55 this, SLOT(slotButtonBox(QAbstractButton *)));
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(literalCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
68 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
69 connect(listingsED, SIGNAL(textChanged()), this, SLOT(setListingsMsg()));
70 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71 connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
73 setFocusProxy(filenameED);
75 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
76 bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
77 bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
78 bc().addReadOnly(filenameED);
79 bc().addReadOnly(browsePB);
80 bc().addReadOnly(visiblespaceCB);
81 bc().addReadOnly(typeCO);
82 bc().addReadOnly(listingsED);
84 bc().addCheckedLineEdit(filenameED, filenameLA);
88 void GuiInclude::change_adaptor()
94 docstring GuiInclude::validate_listings_params()
96 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
98 string params = fromqstr(listingsED->toPlainText());
99 InsetListingsParams lstparams(params);
100 lstparams.setMinted(buffer().params().use_minted);
101 return lstparams.validate();
105 void GuiInclude::setListingsMsg()
108 static bool isOK = true;
109 docstring msg = validate_listings_params();
114 listingsTB->setPlainText(
115 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
118 listingsTB->setPlainText(toqstr(msg));
123 void GuiInclude::typeChanged(int v)
128 visiblespaceCB->setEnabled(false);
129 visiblespaceCB->setChecked(false);
130 previewCB->setEnabled(false);
131 previewCB->setChecked(false);
132 listingsGB->setEnabled(false);
136 visiblespaceCB->setEnabled(false);
137 visiblespaceCB->setChecked(false);
138 previewCB->setEnabled(true);
139 listingsGB->setEnabled(false);
143 visiblespaceCB->setEnabled(false);
144 visiblespaceCB->setChecked(false);
145 previewCB->setEnabled(false);
146 previewCB->setChecked(false);
147 listingsGB->setEnabled(true);
151 visiblespaceCB->setEnabled(true);
152 previewCB->setEnabled(false);
153 previewCB->setChecked(false);
154 listingsGB->setEnabled(false);
158 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
159 //for the reason this is here.
160 buttonBox->button(QDialogButtonBox::Ok)->setDefault(true);
164 void GuiInclude::paramsToDialog(InsetCommandParams const & icp)
166 filenameED->setText(toqstr(icp["filename"]));
168 visiblespaceCB->setChecked(false);
169 visiblespaceCB->setEnabled(false);
170 previewCB->setChecked(false);
171 previewCB->setEnabled(false);
172 listingsGB->setEnabled(false);
176 listingsTB->setPlainText(
177 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
179 string cmdname = icp.getCmdName();
180 if (cmdname != "include" &&
181 cmdname != "verbatiminput" &&
182 cmdname != "verbatiminput*" &&
183 cmdname != "lstinputlisting" &&
184 cmdname != "inputminted")
187 if (cmdname == "include") {
188 typeCO->setCurrentIndex(0);
190 } else if (cmdname == "input") {
191 typeCO->setCurrentIndex(1);
192 previewCB->setEnabled(true);
193 previewCB->setChecked(icp.preview());
195 } else if (cmdname == "verbatiminput*") {
196 typeCO->setCurrentIndex(2);
197 visiblespaceCB->setEnabled(true);
198 visiblespaceCB->setChecked(true);
200 } else if (cmdname == "verbatiminput") {
201 typeCO->setCurrentIndex(2);
202 visiblespaceCB->setEnabled(true);
204 } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
205 typeCO->setCurrentIndex(3);
206 listingsGB->setEnabled(true);
207 listingsED->setEnabled(true);
208 InsetListingsParams par(to_utf8(icp["lstparams"]));
209 // extract caption and label and put them into their respective editboxes
210 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
211 for (vector<string>::iterator it = pars.begin();
212 it != pars.end(); ++it) {
213 if (prefixIs(*it, "caption=")) {
214 string cap = it->substr(8);
215 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
216 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
219 } else if (prefixIs(*it, "label=")) {
220 string lbl = it->substr(6);
221 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
222 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
227 // the rest is put to the extra edit box.
228 string extra = getStringFromVector(pars);
229 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
231 literalCB->setChecked(icp["literal"] == "true");
233 // Make sure that the bc is in the INITIAL state
234 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
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 if (buffer().params().use_minted)
251 params_.setCmdName("inputminted");
253 params_.setCmdName("lstinputlisting");
254 // the parameter string should have passed validation
255 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
256 string caption = fromqstr(captionLE->text());
257 string label = fromqstr(labelLE->text());
258 if (!caption.empty())
259 par.addParam("caption", "{" + caption + "}");
261 par.addParam("label", "{" + label + "}");
262 string const listparams = par.params();
263 params_["lstparams"] = from_utf8(listparams);
265 if (visiblespaceCB->isChecked())
266 params_.setCmdName("verbatiminput*");
268 params_.setCmdName("verbatiminput");
270 params_["literal"] = literalCB->isChecked()
271 ? from_ascii("true") : from_ascii("false");
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_("D&ocuments"), toqstr(lyxrc.document_path));
338 bool GuiInclude::initialiseParams(std::string const & sdata)
340 InsetCommand::string2params(sdata, 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"