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 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
96 string params = fromqstr(listingsED->toPlainText());
97 return InsetListingsParams(params).validate();
101 void GuiInclude::setListingsMsg()
104 static bool isOK = true;
105 docstring msg = validate_listings_params();
110 listingsTB->setPlainText(
111 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
114 listingsTB->setPlainText(toqstr(msg));
119 void GuiInclude::typeChanged(int v)
124 visiblespaceCB->setEnabled(false);
125 visiblespaceCB->setChecked(false);
126 previewCB->setEnabled(false);
127 previewCB->setChecked(false);
128 listingsGB->setEnabled(false);
132 visiblespaceCB->setEnabled(false);
133 visiblespaceCB->setChecked(false);
134 previewCB->setEnabled(true);
135 listingsGB->setEnabled(false);
139 visiblespaceCB->setEnabled(false);
140 visiblespaceCB->setChecked(false);
141 previewCB->setEnabled(false);
142 previewCB->setChecked(false);
143 listingsGB->setEnabled(true);
147 visiblespaceCB->setEnabled(true);
148 previewCB->setEnabled(false);
149 previewCB->setChecked(false);
150 listingsGB->setEnabled(false);
154 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
155 //for the reason this is here.
156 okPB->setDefault(true);
160 void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
162 filenameED->setText(toqstr(params_["filename"]));
164 visiblespaceCB->setChecked(false);
165 visiblespaceCB->setEnabled(false);
166 previewCB->setChecked(false);
167 previewCB->setEnabled(false);
168 listingsGB->setEnabled(false);
172 listingsTB->setPlainText(
173 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
175 string cmdname = params_.getCmdName();
176 if (cmdname != "include" &&
177 cmdname != "verbatiminput" &&
178 cmdname != "verbatiminput*" &&
179 cmdname != "lstinputlisting")
182 if (cmdname == "include") {
183 typeCO->setCurrentIndex(0);
185 } else if (cmdname == "input") {
186 typeCO->setCurrentIndex(1);
187 previewCB->setEnabled(true);
188 previewCB->setChecked(params_.preview());
190 } else if (cmdname == "verbatiminput*") {
191 typeCO->setCurrentIndex(2);
192 visiblespaceCB->setEnabled(true);
193 visiblespaceCB->setChecked(true);
195 } else if (cmdname == "verbatiminput") {
196 typeCO->setCurrentIndex(2);
197 visiblespaceCB->setEnabled(true);
199 } else if (cmdname == "lstinputlisting") {
200 typeCO->setCurrentIndex(3);
201 listingsGB->setEnabled(true);
202 listingsED->setEnabled(true);
203 InsetListingsParams par(to_utf8(params_["lstparams"]));
204 // extract caption and label and put them into their respective editboxes
205 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
206 for (vector<string>::iterator it = pars.begin();
207 it != pars.end(); ++it) {
208 if (prefixIs(*it, "caption=")) {
209 string cap = it->substr(8);
210 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
211 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
214 } else if (prefixIs(*it, "label=")) {
215 string lbl = it->substr(6);
216 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
217 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
222 // the rest is put to the extra edit box.
223 string extra = getStringFromVector(pars);
224 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
227 // Make sure that the bc is in the INITIAL state
228 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
233 void GuiInclude::applyView()
235 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
236 params_.preview(previewCB->isChecked());
238 int const item = typeCO->currentIndex();
240 params_.setCmdName("include");
241 } else if (item == 1) {
242 params_.setCmdName("input");
243 } else if (item == 3) {
244 params_.setCmdName("lstinputlisting");
245 // the parameter string should have passed validation
246 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
247 string caption = fromqstr(captionLE->text());
248 string label = fromqstr(labelLE->text());
249 if (!caption.empty())
250 par.addParam("caption", "{" + caption + "}");
252 par.addParam("label", "{" + label + "}");
253 string const listparams = par.params();
254 params_["lstparams"] = from_utf8(listparams);
256 if (visiblespaceCB->isChecked())
257 params_.setCmdName("verbatiminput*");
259 params_.setCmdName("verbatiminput");
264 void GuiInclude::browse()
268 int const item = typeCO->currentIndex();
278 QString name = browse(filenameED->text(), type);
280 filenameED->setText(name);
284 void GuiInclude::edit()
288 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
293 dispatch(FuncRequest(LFUN_INSET_EDIT));
297 bool GuiInclude::isValid()
299 return !filenameED->text().isEmpty() && validate_listings_params().empty();
303 QString GuiInclude::browse(QString const & in_name, Type in_type) const
305 QString const title = qt_("Select document to include");
307 // input TeX, verbatim, or LyX file ?
312 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
316 filters = fileFilters(QString());
320 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
322 return browseRelToParent(in_name, docpath, title, filters, false,
323 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
327 bool GuiInclude::initialiseParams(std::string const & data)
329 InsetCommand::string2params(data, params_);
330 paramsToDialog(params_);
335 void GuiInclude::dispatchParams()
337 std::string const lfun = InsetCommand::params2string(params_);
338 dispatch(FuncRequest(getLfun(), lfun));
342 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
345 } // namespace frontend
348 #include "moc_GuiInclude.cpp"