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()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71 filenameED->setValidator(new PathValidator(true, filenameED));
72 setFocusProxy(filenameED);
76 void QIncludeDialog::show()
82 void QIncludeDialog::change_adaptor()
88 void QIncludeDialog::validate_listings_params()
90 static bool isOK = true;
91 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
93 if (!bypassCB->isChecked())
99 listingsTB->setPlainText(
100 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
101 okPB->setEnabled(true);
104 listingsTB->setPlainText(toqstr(msg));
105 okPB->setEnabled(false);
110 void QIncludeDialog::closeEvent(QCloseEvent * e)
117 void QIncludeDialog::typeChanged(int v)
122 visiblespaceCB->setEnabled(false);
123 visiblespaceCB->setChecked(false);
124 previewCB->setEnabled(false);
125 previewCB->setChecked(false);
126 listingsGB->setEnabled(false);
130 visiblespaceCB->setEnabled(false);
131 visiblespaceCB->setChecked(false);
132 previewCB->setEnabled(true);
133 listingsGB->setEnabled(false);
137 visiblespaceCB->setEnabled(false);
138 visiblespaceCB->setChecked(false);
139 previewCB->setEnabled(false);
140 previewCB->setChecked(false);
141 listingsGB->setEnabled(true);
145 visiblespaceCB->setEnabled(true);
146 previewCB->setEnabled(false);
147 previewCB->setChecked(false);
148 listingsGB->setEnabled(false);
152 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
153 //for the reason this is here.
154 okPB->setDefault(true);
158 void QIncludeDialog::loadClicked()
164 void QIncludeDialog::browseClicked()
170 /////////////////////////////////////////////////////////////////////
174 /////////////////////////////////////////////////////////////////////
177 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
180 QInclude::QInclude(Dialog & parent)
181 : IncludeBase(parent, _("Child Document"))
185 void QInclude::build_dialog()
187 dialog_.reset(new QIncludeDialog(this));
189 bcview().setOK(dialog_->okPB);
190 bcview().setCancel(dialog_->closePB);
191 bcview().addReadOnly(dialog_->filenameED);
192 bcview().addReadOnly(dialog_->browsePB);
193 bcview().addReadOnly(dialog_->visiblespaceCB);
194 bcview().addReadOnly(dialog_->typeCO);
195 bcview().addReadOnly(dialog_->listingsED);
197 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
201 void QInclude::update_contents()
203 PathValidator * path_validator = getPathValidator(dialog_->filenameED);
205 path_validator->setChecker(kernel().docType(), lyxrc);
207 InsetCommandParams const & params = controller().params();
209 dialog_->filenameED->setText(toqstr(params["filename"]));
211 dialog_->visiblespaceCB->setChecked(false);
212 dialog_->visiblespaceCB->setEnabled(false);
213 dialog_->previewCB->setChecked(false);
214 dialog_->previewCB->setEnabled(false);
215 dialog_->listingsGB->setEnabled(false);
216 dialog_->captionLE->clear();
217 dialog_->labelLE->clear();
218 dialog_->listingsED->clear();
219 dialog_->listingsTB->setPlainText(
220 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
222 string cmdname = controller().params().getCmdName();
223 if (cmdname != "include" &&
224 cmdname != "verbatiminput" &&
225 cmdname != "verbatiminput*" &&
226 cmdname != "lstinputlisting")
229 if (cmdname == "include") {
230 dialog_->typeCO->setCurrentIndex(0);
232 } else if (cmdname == "input") {
233 dialog_->typeCO->setCurrentIndex(1);
234 dialog_->previewCB->setEnabled(true);
235 dialog_->previewCB->setChecked(params.preview());
237 } else if (cmdname == "verbatiminput*") {
238 dialog_->typeCO->setCurrentIndex(2);
239 dialog_->visiblespaceCB->setEnabled(true);
240 dialog_->visiblespaceCB->setChecked(true);
242 } else if (cmdname == "verbatiminput") {
243 dialog_->typeCO->setCurrentIndex(2);
244 dialog_->visiblespaceCB->setEnabled(true);
246 } else if (cmdname == "lstinputlisting") {
247 dialog_->typeCO->setCurrentIndex(3);
248 dialog_->listingsGB->setEnabled(true);
249 dialog_->listingsED->setEnabled(true);
250 InsetListingsParams par(params.getOptions());
251 // extract caption and label and put them into their respective editboxes
252 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
253 for (vector<string>::iterator it = pars.begin();
254 it != pars.end(); ++it) {
255 if (prefixIs(*it, "caption=")) {
256 string cap = it->substr(8);
257 if (cap[0] == '{' && cap[cap.size()-1] == '}') {
258 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
261 } else if (prefixIs(*it, "label=")) {
262 string lbl = it->substr(6);
263 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
264 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
269 // the rest is put to the extra edit box.
270 string extra = getStringFromVector(pars);
271 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
276 void QInclude::apply()
278 InsetCommandParams params = controller().params();
280 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
281 params.preview(dialog_->previewCB->isChecked());
283 int const item = dialog_->typeCO->currentIndex();
285 params.setCmdName("include");
286 } else if (item == 1) {
287 params.setCmdName("input");
288 } else if (item == 3) {
289 params.setCmdName("lstinputlisting");
290 // the parameter string should have passed validation
291 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
292 string caption = fromqstr(dialog_->captionLE->text());
293 string label = fromqstr(dialog_->labelLE->text());
294 if (!caption.empty())
295 par.addParam("caption", "{" + caption + "}");
297 par.addParam("label", "{" + label + "}");
298 params.setOptions(par.params());
300 if (dialog_->visiblespaceCB->isChecked())
301 params.setCmdName("verbatiminput*");
303 params.setCmdName("verbatiminput");
305 controller().setParams(params);
309 void QInclude::browse()
311 ControlInclude::Type type;
313 int const item = dialog_->typeCO->currentIndex();
315 type = ControlInclude::INCLUDE;
317 type = ControlInclude::INPUT;
319 type = ControlInclude::VERBATIM;
321 type = ControlInclude::LISTINGS;
323 docstring const & name =
324 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
326 dialog_->filenameED->setText(toqstr(name));
330 void QInclude::load()
333 string const file = fromqstr(dialog_->filenameED->text());
335 controller().load(file);
340 bool QInclude::isValid()
342 return !dialog_->filenameED->text().isEmpty();
345 } // namespace frontend
348 #include "QInclude_moc.cpp"