3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "support/os.h"
14 #include "support/lstrings.h"
18 #include "CheckedLineEdit.h"
20 #include "qt_helpers.h"
21 #include "Validator.h"
25 #include "insets/InsetListingsParams.h"
26 #include "controllers/ControlInclude.h"
28 #include <QPushButton>
30 #include <QCloseEvent>
36 using lyx::support::os::internal_path;
37 using lyx::support::prefixIs;
38 using lyx::support::getStringFromVector;
39 using lyx::support::getVectorFromString;
44 /////////////////////////////////////////////////////////////////////
48 /////////////////////////////////////////////////////////////////////
50 QIncludeDialog::QIncludeDialog(QInclude * form)
54 connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
55 connect(closePB, SIGNAL(clicked()), form, 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(loadPB, SIGNAL(clicked()), this, SLOT(loadClicked()));
61 connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
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(validate_listings_params()));
70 filenameED->setValidator(new PathValidator(true, filenameED));
71 setFocusProxy(filenameED);
75 void QIncludeDialog::show()
81 void QIncludeDialog::change_adaptor()
87 void QIncludeDialog::validate_listings_params()
89 static bool isOK = true;
91 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
94 listingsTB->setPlainText(
95 qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
96 okPB->setEnabled(true);
98 } catch (invalidParam & e) {
100 listingsTB->setPlainText(e.what());
101 okPB->setEnabled(false);
106 void QIncludeDialog::closeEvent(QCloseEvent * e)
113 void QIncludeDialog::typeChanged(int v)
118 visiblespaceCB->setEnabled(false);
119 visiblespaceCB->setChecked(false);
120 previewCB->setEnabled(false);
121 previewCB->setChecked(false);
122 listingsGB->setEnabled(false);
126 visiblespaceCB->setEnabled(false);
127 visiblespaceCB->setChecked(false);
128 previewCB->setEnabled(true);
129 listingsGB->setEnabled(false);
133 visiblespaceCB->setEnabled(false);
134 visiblespaceCB->setChecked(false);
135 previewCB->setEnabled(false);
136 listingsGB->setEnabled(true);
140 visiblespaceCB->setEnabled(true);
141 previewCB->setEnabled(false);
142 previewCB->setChecked(false);
143 listingsGB->setEnabled(false);
149 void QIncludeDialog::loadClicked()
155 void QIncludeDialog::browseClicked()
161 /////////////////////////////////////////////////////////////////////
165 /////////////////////////////////////////////////////////////////////
168 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
171 QInclude::QInclude(Dialog & parent)
172 : IncludeBase(parent, _("Child Document"))
176 void QInclude::build_dialog()
178 dialog_.reset(new QIncludeDialog(this));
180 bcview().setOK(dialog_->okPB);
181 bcview().setCancel(dialog_->closePB);
182 bcview().addReadOnly(dialog_->filenameED);
183 bcview().addReadOnly(dialog_->browsePB);
184 bcview().addReadOnly(dialog_->visiblespaceCB);
185 bcview().addReadOnly(dialog_->typeCO);
186 bcview().addReadOnly(dialog_->listingsED);
188 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
192 void QInclude::update_contents()
194 PathValidator * path_validator = getPathValidator(dialog_->filenameED);
196 path_validator->setChecker(kernel().docType(), lyxrc);
198 InsetCommandParams const & params = controller().params();
200 dialog_->filenameED->setText(toqstr(params["filename"]));
202 dialog_->visiblespaceCB->setChecked(false);
203 dialog_->visiblespaceCB->setEnabled(false);
204 dialog_->previewCB->setChecked(false);
205 dialog_->previewCB->setEnabled(false);
206 dialog_->listingsGB->setEnabled(false);
207 dialog_->captionLE->clear();
208 dialog_->labelLE->clear();
209 dialog_->listingsED->clear();
210 dialog_->listingsTB->setPlainText(
211 qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
213 string cmdname = controller().params().getCmdName();
214 if (cmdname != "include" &&
215 cmdname != "verbatiminput" &&
216 cmdname != "verbatiminput*" &&
217 cmdname != "lstinputlisting")
220 if (cmdname == "include") {
221 dialog_->typeCO->setCurrentIndex(0);
223 } else if (cmdname == "input") {
224 dialog_->typeCO->setCurrentIndex(1);
225 dialog_->previewCB->setEnabled(true);
226 dialog_->previewCB->setChecked(params.preview());
228 } else if (cmdname == "verbatiminput*") {
229 dialog_->typeCO->setCurrentIndex(2);
230 dialog_->visiblespaceCB->setEnabled(true);
231 dialog_->visiblespaceCB->setChecked(true);
233 } else if (cmdname == "verbatiminput") {
234 dialog_->typeCO->setCurrentIndex(2);
235 dialog_->visiblespaceCB->setEnabled(true);
237 } else if (cmdname == "lstinputlisting") {
238 dialog_->typeCO->setCurrentIndex(3);
239 dialog_->listingsGB->setEnabled(true);
240 dialog_->listingsED->setEnabled(true);
241 InsetListingsParams par(params.getOptions());
242 // extract caption and label and put them into their respective editboxes
243 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
244 for (vector<string>::iterator it = pars.begin();
245 it != pars.end(); ++it) {
246 if (prefixIs(*it, "caption=")) {
247 string cap = it->substr(8);
248 if (cap[0] == '{' && cap[cap.size()-1] == '}')
249 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
251 // FIXME: make this translatable!
252 throw invalidParam("caption parameter is not quoted with braces");
254 } else if (prefixIs(*it, "label=")) {
255 string lbl = it->substr(6);
256 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
257 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
259 // FIXME: make this translatable!
260 throw invalidParam("label parameter is not quoted with braces");
264 // the rest is put to the extra edit box.
265 string extra = getStringFromVector(pars);
266 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
271 void QInclude::apply()
273 InsetCommandParams params = controller().params();
275 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
276 params.preview(dialog_->previewCB->isChecked());
278 int const item = dialog_->typeCO->currentIndex();
280 params.setCmdName("include");
281 params.setOptions(string());
282 } else if (item == 1) {
283 params.setCmdName("input");
284 params.setOptions(string());
285 } else if (item == 3) {
286 params.setCmdName("lstinputlisting");
287 // the parameter string should have passed validation
288 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
289 string caption = fromqstr(dialog_->captionLE->text());
290 string label = fromqstr(dialog_->labelLE->text());
291 if (!caption.empty())
292 par.addParam("caption", "{" + caption + "}");
294 par.addParam("label", "{" + label + "}");
295 params.setOptions(par.params());
297 if (dialog_->visiblespaceCB->isChecked())
298 params.setCmdName("verbatiminput*");
300 params.setCmdName("verbatiminput");
301 params.setOptions(string());
303 controller().setParams(params);
307 void QInclude::browse()
309 ControlInclude::Type type;
311 int const item = dialog_->typeCO->currentIndex();
313 type = ControlInclude::INCLUDE;
315 type = ControlInclude::INPUT;
317 type = ControlInclude::VERBATIM;
319 type = ControlInclude::LISTINGS;
321 docstring const & name =
322 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
324 dialog_->filenameED->setText(toqstr(name));
328 void QInclude::load()
331 string const file = fromqstr(dialog_->filenameED->text());
333 controller().load(file);
338 bool QInclude::isValid()
340 return !dialog_->filenameED->text().isEmpty();
343 } // namespace frontend
346 #include "QInclude_moc.cpp"