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 listing parameters on the right. Enter ? for a list of parameters."));
96 okPB->setEnabled(true);
98 } catch (invalidParam & e) {
100 listingsTB->setPlainText(toqstr(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 previewCB->setChecked(false);
137 listingsGB->setEnabled(true);
141 visiblespaceCB->setEnabled(true);
142 previewCB->setEnabled(false);
143 previewCB->setChecked(false);
144 listingsGB->setEnabled(false);
148 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
149 //for the reason this is here.
150 okPB->setDefault(true);
154 void QIncludeDialog::loadClicked()
160 void QIncludeDialog::browseClicked()
166 /////////////////////////////////////////////////////////////////////
170 /////////////////////////////////////////////////////////////////////
173 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
176 QInclude::QInclude(Dialog & parent)
177 : IncludeBase(parent, _("Child Document"))
181 void QInclude::build_dialog()
183 dialog_.reset(new QIncludeDialog(this));
185 bcview().setOK(dialog_->okPB);
186 bcview().setCancel(dialog_->closePB);
187 bcview().addReadOnly(dialog_->filenameED);
188 bcview().addReadOnly(dialog_->browsePB);
189 bcview().addReadOnly(dialog_->visiblespaceCB);
190 bcview().addReadOnly(dialog_->typeCO);
191 bcview().addReadOnly(dialog_->listingsED);
193 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
197 void QInclude::update_contents()
199 PathValidator * path_validator = getPathValidator(dialog_->filenameED);
201 path_validator->setChecker(kernel().docType(), lyxrc);
203 InsetCommandParams const & params = controller().params();
205 dialog_->filenameED->setText(toqstr(params["filename"]));
207 dialog_->visiblespaceCB->setChecked(false);
208 dialog_->visiblespaceCB->setEnabled(false);
209 dialog_->previewCB->setChecked(false);
210 dialog_->previewCB->setEnabled(false);
211 dialog_->listingsGB->setEnabled(false);
212 dialog_->captionLE->clear();
213 dialog_->labelLE->clear();
214 dialog_->listingsED->clear();
215 dialog_->listingsTB->setPlainText(
216 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
218 string cmdname = controller().params().getCmdName();
219 if (cmdname != "include" &&
220 cmdname != "verbatiminput" &&
221 cmdname != "verbatiminput*" &&
222 cmdname != "lstinputlisting")
225 if (cmdname == "include") {
226 dialog_->typeCO->setCurrentIndex(0);
228 } else if (cmdname == "input") {
229 dialog_->typeCO->setCurrentIndex(1);
230 dialog_->previewCB->setEnabled(true);
231 dialog_->previewCB->setChecked(params.preview());
233 } else if (cmdname == "verbatiminput*") {
234 dialog_->typeCO->setCurrentIndex(2);
235 dialog_->visiblespaceCB->setEnabled(true);
236 dialog_->visiblespaceCB->setChecked(true);
238 } else if (cmdname == "verbatiminput") {
239 dialog_->typeCO->setCurrentIndex(2);
240 dialog_->visiblespaceCB->setEnabled(true);
242 } else if (cmdname == "lstinputlisting") {
243 dialog_->typeCO->setCurrentIndex(3);
244 dialog_->listingsGB->setEnabled(true);
245 dialog_->listingsED->setEnabled(true);
246 InsetListingsParams par(params.getOptions());
247 // extract caption and label and put them into their respective editboxes
248 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
249 for (vector<string>::iterator it = pars.begin();
250 it != pars.end(); ++it) {
251 if (prefixIs(*it, "caption=")) {
252 string cap = it->substr(8);
253 if (cap[0] == '{' && cap[cap.size()-1] == '}')
254 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
256 throw invalidParam(_("caption parameter is not quoted with braces"));
258 } else if (prefixIs(*it, "label=")) {
259 string lbl = it->substr(6);
260 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
261 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
263 throw invalidParam(_("label parameter is not quoted with braces"));
267 // the rest is put to the extra edit box.
268 string extra = getStringFromVector(pars);
269 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
274 void QInclude::apply()
276 InsetCommandParams params = controller().params();
278 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
279 params.preview(dialog_->previewCB->isChecked());
281 int const item = dialog_->typeCO->currentIndex();
283 params.setCmdName("include");
284 } else if (item == 1) {
285 params.setCmdName("input");
286 } else if (item == 3) {
287 params.setCmdName("lstinputlisting");
288 // the parameter string should have passed validation
289 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
290 string caption = fromqstr(dialog_->captionLE->text());
291 string label = fromqstr(dialog_->labelLE->text());
292 if (!caption.empty())
293 par.addParam("caption", "{" + caption + "}");
295 par.addParam("label", "{" + label + "}");
296 params.setOptions(par.params());
298 if (dialog_->visiblespaceCB->isChecked())
299 params.setCmdName("verbatiminput*");
301 params.setCmdName("verbatiminput");
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"