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("Input listings parameters on the right. Enter ? for a list of parameters.");
95 okPB->setEnabled(true);
97 } catch (invalidParam & e) {
99 listingsTB->setPlainText(e.what());
100 okPB->setEnabled(false);
105 void QIncludeDialog::closeEvent(QCloseEvent * e)
112 void QIncludeDialog::typeChanged(int v)
117 visiblespaceCB->setEnabled(false);
118 visiblespaceCB->setChecked(false);
119 previewCB->setEnabled(false);
120 previewCB->setChecked(false);
121 listingsGB->setEnabled(false);
125 visiblespaceCB->setEnabled(false);
126 visiblespaceCB->setChecked(false);
127 previewCB->setEnabled(true);
128 listingsGB->setEnabled(false);
132 visiblespaceCB->setEnabled(false);
133 visiblespaceCB->setChecked(false);
134 previewCB->setEnabled(false);
135 listingsGB->setEnabled(true);
139 visiblespaceCB->setEnabled(true);
140 previewCB->setEnabled(false);
141 previewCB->setChecked(false);
142 listingsGB->setEnabled(false);
148 void QIncludeDialog::loadClicked()
154 void QIncludeDialog::browseClicked()
160 /////////////////////////////////////////////////////////////////////
164 /////////////////////////////////////////////////////////////////////
167 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
170 QInclude::QInclude(Dialog & parent)
171 : IncludeBase(parent, _("Child Document"))
175 void QInclude::build_dialog()
177 dialog_.reset(new QIncludeDialog(this));
179 bcview().setOK(dialog_->okPB);
180 bcview().setCancel(dialog_->closePB);
181 bcview().addReadOnly(dialog_->filenameED);
182 bcview().addReadOnly(dialog_->browsePB);
183 bcview().addReadOnly(dialog_->visiblespaceCB);
184 bcview().addReadOnly(dialog_->typeCO);
185 bcview().addReadOnly(dialog_->listingsED);
186 dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
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_->listingsED->setEnabled(false);
209 string cmdname = controller().params().getCmdName();
210 if (cmdname != "include" &&
211 cmdname != "verbatiminput" &&
212 cmdname != "verbatiminput*" &&
213 cmdname != "lstinputlisting")
216 if (cmdname == "include") {
217 dialog_->typeCO->setCurrentIndex(0);
219 } else if (cmdname == "input") {
220 dialog_->typeCO->setCurrentIndex(1);
221 dialog_->previewCB->setEnabled(true);
222 dialog_->previewCB->setChecked(params.preview());
224 } else if (cmdname == "verbatiminput*") {
225 dialog_->typeCO->setCurrentIndex(2);
226 dialog_->visiblespaceCB->setEnabled(true);
227 dialog_->visiblespaceCB->setChecked(true);
229 } else if (cmdname == "verbatiminput") {
230 dialog_->typeCO->setCurrentIndex(2);
231 dialog_->visiblespaceCB->setEnabled(true);
233 } else if (cmdname == "lstinputlisting") {
234 dialog_->typeCO->setCurrentIndex(3);
235 dialog_->listingsGB->setEnabled(true);
236 dialog_->listingsED->setEnabled(true);
237 InsetListingsParams par(params.getOptions());
238 // extract caption and label and put them into their respective editboxes
239 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
240 for (vector<string>::iterator it = pars.begin();
241 it != pars.end(); ++it) {
242 if (prefixIs(*it, "caption=")) {
243 string cap = it->substr(8);
244 if (cap[0] == '{' && cap[cap.size()-1] == '}')
245 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
247 throw invalidParam("caption parameter is not quoted with braces");
249 } else if (prefixIs(*it, "label=")) {
250 string lbl = it->substr(6);
251 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
252 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
254 throw invalidParam("label parameter is not quoted with braces");
258 // the rest is put to the extra edit box.
259 string extra = getStringFromVector(pars);
260 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
265 void QInclude::apply()
267 InsetCommandParams params = controller().params();
269 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
270 params.preview(dialog_->previewCB->isChecked());
272 int const item = dialog_->typeCO->currentIndex();
274 params.setCmdName("include");
275 params.setOptions(string());
276 } else if (item == 1) {
277 params.setCmdName("input");
278 params.setOptions(string());
279 } else if (item == 3) {
280 params.setCmdName("lstinputlisting");
281 // the parameter string should have passed validation
282 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
283 string caption = fromqstr(dialog_->captionLE->text());
284 string label = fromqstr(dialog_->labelLE->text());
285 if (!caption.empty())
286 par.addParam("caption", "{" + caption + "}");
288 par.addParam("label", "{" + label + "}");
289 params.setOptions(par.params());
291 if (dialog_->visiblespaceCB->isChecked())
292 params.setCmdName("verbatiminput*");
294 params.setCmdName("verbatiminput");
295 params.setOptions(string());
297 controller().setParams(params);
301 void QInclude::browse()
303 ControlInclude::Type type;
305 int const item = dialog_->typeCO->currentIndex();
307 type = ControlInclude::INCLUDE;
309 type = ControlInclude::INPUT;
311 type = ControlInclude::VERBATIM;
313 type = ControlInclude::LISTINGS;
315 docstring const & name =
316 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
318 dialog_->filenameED->setText(toqstr(name));
322 void QInclude::load()
325 string const file = fromqstr(dialog_->filenameED->text());
327 controller().load(file);
332 bool QInclude::isValid()
334 return !dialog_->filenameED->text().isEmpty();
337 } // namespace frontend
340 #include "QInclude_moc.cpp"