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
96 static string param_cache = string();
97 static docstring msg_cache = docstring();
99 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
102 string params = fromqstr(listingsED->toPlainText());
103 if (params != param_cache) {
104 param_cache = params;
105 msg_cache = InsetListingsParams(params).validate();
111 void GuiInclude::setListingsMsg()
113 static bool isOK = true;
114 docstring msg = validate_listings_params();
119 listingsTB->setPlainText(
120 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
123 listingsTB->setPlainText(toqstr(msg));
128 void GuiInclude::typeChanged(int v)
133 visiblespaceCB->setEnabled(false);
134 visiblespaceCB->setChecked(false);
135 previewCB->setEnabled(false);
136 previewCB->setChecked(false);
137 listingsGB->setEnabled(false);
141 visiblespaceCB->setEnabled(false);
142 visiblespaceCB->setChecked(false);
143 previewCB->setEnabled(true);
144 listingsGB->setEnabled(false);
148 visiblespaceCB->setEnabled(false);
149 visiblespaceCB->setChecked(false);
150 previewCB->setEnabled(false);
151 previewCB->setChecked(false);
152 listingsGB->setEnabled(true);
156 visiblespaceCB->setEnabled(true);
157 previewCB->setEnabled(false);
158 previewCB->setChecked(false);
159 listingsGB->setEnabled(false);
163 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
164 //for the reason this is here.
165 okPB->setDefault(true);
169 void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
171 filenameED->setText(toqstr(params_["filename"]));
173 visiblespaceCB->setChecked(false);
174 visiblespaceCB->setEnabled(false);
175 previewCB->setChecked(false);
176 previewCB->setEnabled(false);
177 listingsGB->setEnabled(false);
181 listingsTB->setPlainText(
182 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
184 string cmdname = params_.getCmdName();
185 if (cmdname != "include" &&
186 cmdname != "verbatiminput" &&
187 cmdname != "verbatiminput*" &&
188 cmdname != "lstinputlisting")
191 if (cmdname == "include") {
192 typeCO->setCurrentIndex(0);
194 } else if (cmdname == "input") {
195 typeCO->setCurrentIndex(1);
196 previewCB->setEnabled(true);
197 previewCB->setChecked(params_.preview());
199 } else if (cmdname == "verbatiminput*") {
200 typeCO->setCurrentIndex(2);
201 visiblespaceCB->setEnabled(true);
202 visiblespaceCB->setChecked(true);
204 } else if (cmdname == "verbatiminput") {
205 typeCO->setCurrentIndex(2);
206 visiblespaceCB->setEnabled(true);
208 } else if (cmdname == "lstinputlisting") {
209 typeCO->setCurrentIndex(3);
210 listingsGB->setEnabled(true);
211 listingsED->setEnabled(true);
212 InsetListingsParams par(to_utf8(params_["lstparams"]));
213 // extract caption and label and put them into their respective editboxes
214 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
215 for (vector<string>::iterator it = pars.begin();
216 it != pars.end(); ++it) {
217 if (prefixIs(*it, "caption=")) {
218 string cap = it->substr(8);
219 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
220 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
223 } else if (prefixIs(*it, "label=")) {
224 string lbl = it->substr(6);
225 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
226 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
231 // the rest is put to the extra edit box.
232 string extra = getStringFromVector(pars);
233 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
236 // Make sure that the bc is in the INITIAL state
237 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
242 void GuiInclude::applyView()
244 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
245 params_.preview(previewCB->isChecked());
247 int const item = typeCO->currentIndex();
249 params_.setCmdName("include");
250 } else if (item == 1) {
251 params_.setCmdName("input");
252 } else if (item == 3) {
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");
273 void GuiInclude::browse()
277 int const item = typeCO->currentIndex();
287 QString name = browse(filenameED->text(), type);
289 filenameED->setText(name);
293 void GuiInclude::edit()
297 string const file = fromqstr(filenameED->text());
298 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
303 dispatch(FuncRequest(LFUN_INSET_EDIT));
307 bool GuiInclude::isValid()
309 return !filenameED->text().isEmpty() && validate_listings_params().empty();
313 QString GuiInclude::browse(QString const & in_name, Type in_type) const
315 QString const title = qt_("Select document to include");
317 // input TeX, verbatim, or LyX file ?
322 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
326 filters = fileFilters(QString());
330 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
332 return browseRelToParent(in_name, docpath, title, filters, false,
333 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
337 bool GuiInclude::initialiseParams(std::string const & data)
339 InsetCommand::string2params(data, params_);
340 paramsToDialog(params_);
345 void GuiInclude::dispatchParams()
347 std::string const lfun = InsetCommand::params2string(params_);
348 dispatch(FuncRequest(getLfun(), lfun));
352 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
355 } // namespace frontend
358 #include "moc_GuiInclude.cpp"