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"
24 #include "insets/InsetListingsParams.h"
25 #include "controllers/ControlInclude.h"
27 #include <QPushButton>
29 #include <QCloseEvent>
35 using lyx::support::os::internal_path;
36 using lyx::support::prefixIs;
37 using lyx::support::getStringFromVector;
38 using lyx::support::getVectorFromString;
43 /////////////////////////////////////////////////////////////////////
47 /////////////////////////////////////////////////////////////////////
49 QIncludeDialog::QIncludeDialog(QInclude * form)
53 connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
56 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
57 connect(filenameED, SIGNAL(textChanged(const QString &)),
58 this, SLOT(change_adaptor()));
59 connect(editPB, SIGNAL(clicked()), this, SLOT(editClicked()));
60 connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
61 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
62 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
63 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
64 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
65 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
66 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
67 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
68 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
71 setFocusProxy(filenameED);
75 void QIncludeDialog::show()
81 void QIncludeDialog::change_adaptor()
87 docstring QIncludeDialog::validate_listings_params()
89 // use a cache here to avoid repeated validation
90 // of the same parameters
91 static string param_cache = string();
92 static docstring msg_cache = docstring();
94 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
97 string params = fromqstr(listingsED->toPlainText());
98 if (params != param_cache) {
100 msg_cache = InsetListingsParams(params).validate();
106 void QIncludeDialog::set_listings_msg()
108 static bool isOK = true;
109 docstring msg = validate_listings_params();
114 listingsTB->setPlainText(
115 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
118 listingsTB->setPlainText(toqstr(msg));
123 void QIncludeDialog::closeEvent(QCloseEvent * e)
130 void QIncludeDialog::typeChanged(int v)
135 visiblespaceCB->setEnabled(false);
136 visiblespaceCB->setChecked(false);
137 previewCB->setEnabled(false);
138 previewCB->setChecked(false);
139 listingsGB->setEnabled(false);
143 visiblespaceCB->setEnabled(false);
144 visiblespaceCB->setChecked(false);
145 previewCB->setEnabled(true);
146 listingsGB->setEnabled(false);
150 visiblespaceCB->setEnabled(false);
151 visiblespaceCB->setChecked(false);
152 previewCB->setEnabled(false);
153 previewCB->setChecked(false);
154 listingsGB->setEnabled(true);
158 visiblespaceCB->setEnabled(true);
159 previewCB->setEnabled(false);
160 previewCB->setChecked(false);
161 listingsGB->setEnabled(false);
165 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
166 //for the reason this is here.
167 okPB->setDefault(true);
171 void QIncludeDialog::editClicked()
177 void QIncludeDialog::browseClicked()
183 /////////////////////////////////////////////////////////////////////
187 /////////////////////////////////////////////////////////////////////
190 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
193 QInclude::QInclude(Dialog & parent)
194 : IncludeBase(parent, _("Child Document"))
198 void QInclude::build_dialog()
200 dialog_.reset(new QIncludeDialog(this));
202 bcview().setOK(dialog_->okPB);
203 bcview().setCancel(dialog_->closePB);
204 bcview().addReadOnly(dialog_->filenameED);
205 bcview().addReadOnly(dialog_->browsePB);
206 bcview().addReadOnly(dialog_->visiblespaceCB);
207 bcview().addReadOnly(dialog_->typeCO);
208 bcview().addReadOnly(dialog_->listingsED);
210 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
214 void QInclude::update_contents()
216 InsetCommandParams const & params = controller().params();
218 dialog_->filenameED->setText(toqstr(params["filename"]));
220 dialog_->visiblespaceCB->setChecked(false);
221 dialog_->visiblespaceCB->setEnabled(false);
222 dialog_->previewCB->setChecked(false);
223 dialog_->previewCB->setEnabled(false);
224 dialog_->listingsGB->setEnabled(false);
225 dialog_->captionLE->clear();
226 dialog_->labelLE->clear();
227 dialog_->listingsED->clear();
228 dialog_->listingsTB->setPlainText(
229 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
231 string cmdname = controller().params().getCmdName();
232 if (cmdname != "include" &&
233 cmdname != "verbatiminput" &&
234 cmdname != "verbatiminput*" &&
235 cmdname != "lstinputlisting")
238 if (cmdname == "include") {
239 dialog_->typeCO->setCurrentIndex(0);
241 } else if (cmdname == "input") {
242 dialog_->typeCO->setCurrentIndex(1);
243 dialog_->previewCB->setEnabled(true);
244 dialog_->previewCB->setChecked(params.preview());
246 } else if (cmdname == "verbatiminput*") {
247 dialog_->typeCO->setCurrentIndex(2);
248 dialog_->visiblespaceCB->setEnabled(true);
249 dialog_->visiblespaceCB->setChecked(true);
251 } else if (cmdname == "verbatiminput") {
252 dialog_->typeCO->setCurrentIndex(2);
253 dialog_->visiblespaceCB->setEnabled(true);
255 } else if (cmdname == "lstinputlisting") {
256 dialog_->typeCO->setCurrentIndex(3);
257 dialog_->listingsGB->setEnabled(true);
258 dialog_->listingsED->setEnabled(true);
259 InsetListingsParams par(params.getOptions());
260 // extract caption and label and put them into their respective editboxes
261 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
262 for (vector<string>::iterator it = pars.begin();
263 it != pars.end(); ++it) {
264 if (prefixIs(*it, "caption=")) {
265 string cap = it->substr(8);
266 if (cap[0] == '{' && cap[cap.size()-1] == '}') {
267 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
270 } else if (prefixIs(*it, "label=")) {
271 string lbl = it->substr(6);
272 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
273 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
278 // the rest is put to the extra edit box.
279 string extra = getStringFromVector(pars);
280 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
285 void QInclude::apply()
287 InsetCommandParams params = controller().params();
289 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
290 params.preview(dialog_->previewCB->isChecked());
292 int const item = dialog_->typeCO->currentIndex();
294 params.setCmdName("include");
295 } else if (item == 1) {
296 params.setCmdName("input");
297 } else if (item == 3) {
298 params.setCmdName("lstinputlisting");
299 // the parameter string should have passed validation
300 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
301 string caption = fromqstr(dialog_->captionLE->text());
302 string label = fromqstr(dialog_->labelLE->text());
303 if (!caption.empty())
304 par.addParam("caption", "{" + caption + "}");
306 par.addParam("label", "{" + label + "}");
307 params.setOptions(par.params());
309 if (dialog_->visiblespaceCB->isChecked())
310 params.setCmdName("verbatiminput*");
312 params.setCmdName("verbatiminput");
314 controller().setParams(params);
318 void QInclude::browse()
320 ControlInclude::Type type;
322 int const item = dialog_->typeCO->currentIndex();
324 type = ControlInclude::INCLUDE;
326 type = ControlInclude::INPUT;
328 type = ControlInclude::VERBATIM;
330 type = ControlInclude::LISTINGS;
332 docstring const & name =
333 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
335 dialog_->filenameED->setText(toqstr(name));
339 void QInclude::edit()
342 string const file = fromqstr(dialog_->filenameED->text());
344 controller().edit(file);
349 bool QInclude::isValid()
351 return !dialog_->filenameED->text().isEmpty() &&
352 dialog_->validate_listings_params().empty();
355 } // namespace frontend
358 #include "QInclude_moc.cpp"