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(set_listings_msg()));
68 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
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::set_listings_msg()
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::updateContents()
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()));
238 void GuiInclude::applyView()
240 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
241 params_.preview(previewCB->isChecked());
243 int const item = typeCO->currentIndex();
245 params_.setCmdName("include");
246 } else if (item == 1) {
247 params_.setCmdName("input");
248 } else if (item == 3) {
249 params_.setCmdName("lstinputlisting");
250 // the parameter string should have passed validation
251 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
252 string caption = fromqstr(captionLE->text());
253 string label = fromqstr(labelLE->text());
254 if (!caption.empty())
255 par.addParam("caption", "{" + caption + "}");
257 par.addParam("label", "{" + label + "}");
258 string const listparams = par.params();
259 params_["lstparams"] = from_ascii(listparams);
261 if (visiblespaceCB->isChecked())
262 params_.setCmdName("verbatiminput*");
264 params_.setCmdName("verbatiminput");
269 void GuiInclude::browse()
273 int const item = typeCO->currentIndex();
283 QString name = browse(filenameED->text(), type);
285 filenameED->setText(name);
289 void GuiInclude::edit()
293 string const file = fromqstr(filenameED->text());
296 dispatch(FuncRequest(LFUN_INSET_EDIT));
300 bool GuiInclude::isValid()
302 return !filenameED->text().isEmpty() && validate_listings_params().empty();
306 QString GuiInclude::browse(QString const & in_name, Type in_type) const
308 QString const title = qt_("Select document to include");
310 // input TeX, verbatim, or LyX file ?
315 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
322 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
324 return browseRelFile(in_name, docpath, title, filters, false,
325 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
329 bool GuiInclude::initialiseParams(std::string const & data)
331 InsetCommand::string2params("include", data, params_);
336 void GuiInclude::dispatchParams()
338 std::string const lfun = InsetCommand::params2string("include", params_);
339 dispatch(FuncRequest(getLfun(), lfun));
343 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
346 } // namespace frontend
349 #include "GuiInclude_moc.cpp"