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);
187 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
191 void QInclude::update_contents()
193 PathValidator * path_validator = getPathValidator(dialog_->filenameED);
195 path_validator->setChecker(kernel().docType(), lyxrc);
197 InsetCommandParams const & params = controller().params();
199 dialog_->filenameED->setText(toqstr(params["filename"]));
201 dialog_->visiblespaceCB->setChecked(false);
202 dialog_->visiblespaceCB->setEnabled(false);
203 dialog_->previewCB->setChecked(false);
204 dialog_->previewCB->setEnabled(false);
205 dialog_->listingsGB->setEnabled(false);
206 dialog_->captionLE->clear();
207 dialog_->labelLE->clear();
208 dialog_->listingsED->clear();
209 dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
211 string cmdname = controller().params().getCmdName();
212 if (cmdname != "include" &&
213 cmdname != "verbatiminput" &&
214 cmdname != "verbatiminput*" &&
215 cmdname != "lstinputlisting")
218 if (cmdname == "include") {
219 dialog_->typeCO->setCurrentIndex(0);
221 } else if (cmdname == "input") {
222 dialog_->typeCO->setCurrentIndex(1);
223 dialog_->previewCB->setEnabled(true);
224 dialog_->previewCB->setChecked(params.preview());
226 } else if (cmdname == "verbatiminput*") {
227 dialog_->typeCO->setCurrentIndex(2);
228 dialog_->visiblespaceCB->setEnabled(true);
229 dialog_->visiblespaceCB->setChecked(true);
231 } else if (cmdname == "verbatiminput") {
232 dialog_->typeCO->setCurrentIndex(2);
233 dialog_->visiblespaceCB->setEnabled(true);
235 } else if (cmdname == "lstinputlisting") {
236 dialog_->typeCO->setCurrentIndex(3);
237 dialog_->listingsGB->setEnabled(true);
238 dialog_->listingsED->setEnabled(true);
239 InsetListingsParams par(params.getOptions());
240 // extract caption and label and put them into their respective editboxes
241 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
242 for (vector<string>::iterator it = pars.begin();
243 it != pars.end(); ++it) {
244 if (prefixIs(*it, "caption=")) {
245 string cap = it->substr(8);
246 if (cap[0] == '{' && cap[cap.size()-1] == '}')
247 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
249 throw invalidParam("caption parameter is not quoted with braces");
251 } else if (prefixIs(*it, "label=")) {
252 string lbl = it->substr(6);
253 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
254 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
256 throw invalidParam("label parameter is not quoted with braces");
260 // the rest is put to the extra edit box.
261 string extra = getStringFromVector(pars);
262 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
267 void QInclude::apply()
269 InsetCommandParams params = controller().params();
271 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
272 params.preview(dialog_->previewCB->isChecked());
274 int const item = dialog_->typeCO->currentIndex();
276 params.setCmdName("include");
277 params.setOptions(string());
278 } else if (item == 1) {
279 params.setCmdName("input");
280 params.setOptions(string());
281 } else if (item == 3) {
282 params.setCmdName("lstinputlisting");
283 // the parameter string should have passed validation
284 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
285 string caption = fromqstr(dialog_->captionLE->text());
286 string label = fromqstr(dialog_->labelLE->text());
287 if (!caption.empty())
288 par.addParam("caption", "{" + caption + "}");
290 par.addParam("label", "{" + label + "}");
291 params.setOptions(par.params());
293 if (dialog_->visiblespaceCB->isChecked())
294 params.setCmdName("verbatiminput*");
296 params.setCmdName("verbatiminput");
297 params.setOptions(string());
299 controller().setParams(params);
303 void QInclude::browse()
305 ControlInclude::Type type;
307 int const item = dialog_->typeCO->currentIndex();
309 type = ControlInclude::INCLUDE;
311 type = ControlInclude::INPUT;
313 type = ControlInclude::VERBATIM;
315 type = ControlInclude::LISTINGS;
317 docstring const & name =
318 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
320 dialog_->filenameED->setText(toqstr(name));
324 void QInclude::load()
327 string const file = fromqstr(dialog_->filenameED->text());
329 controller().load(file);
334 bool QInclude::isValid()
336 return !dialog_->filenameED->text().isEmpty();
339 } // namespace frontend
342 #include "QInclude_moc.cpp"