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(loadPB, SIGNAL(clicked()), this, SLOT(loadClicked()));
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(validate_listings_params()));
68 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(bypassCB, SIGNAL(clicked()), this, SLOT(validate_listings_params()));
71 setFocusProxy(filenameED);
75 void QIncludeDialog::show()
81 void QIncludeDialog::change_adaptor()
87 void QIncludeDialog::validate_listings_params()
89 static bool isOK = true;
90 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
92 if (!bypassCB->isChecked())
98 listingsTB->setPlainText(
99 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
100 okPB->setEnabled(true);
103 listingsTB->setPlainText(toqstr(msg));
104 okPB->setEnabled(false);
109 void QIncludeDialog::closeEvent(QCloseEvent * e)
116 void QIncludeDialog::typeChanged(int v)
121 visiblespaceCB->setEnabled(false);
122 visiblespaceCB->setChecked(false);
123 previewCB->setEnabled(false);
124 previewCB->setChecked(false);
125 listingsGB->setEnabled(false);
129 visiblespaceCB->setEnabled(false);
130 visiblespaceCB->setChecked(false);
131 previewCB->setEnabled(true);
132 listingsGB->setEnabled(false);
136 visiblespaceCB->setEnabled(false);
137 visiblespaceCB->setChecked(false);
138 previewCB->setEnabled(false);
139 previewCB->setChecked(false);
140 listingsGB->setEnabled(true);
144 visiblespaceCB->setEnabled(true);
145 previewCB->setEnabled(false);
146 previewCB->setChecked(false);
147 listingsGB->setEnabled(false);
151 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
152 //for the reason this is here.
153 okPB->setDefault(true);
157 void QIncludeDialog::loadClicked()
163 void QIncludeDialog::browseClicked()
169 /////////////////////////////////////////////////////////////////////
173 /////////////////////////////////////////////////////////////////////
176 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
179 QInclude::QInclude(Dialog & parent)
180 : IncludeBase(parent, _("Child Document"))
184 void QInclude::build_dialog()
186 dialog_.reset(new QIncludeDialog(this));
188 bcview().setOK(dialog_->okPB);
189 bcview().setCancel(dialog_->closePB);
190 bcview().addReadOnly(dialog_->filenameED);
191 bcview().addReadOnly(dialog_->browsePB);
192 bcview().addReadOnly(dialog_->visiblespaceCB);
193 bcview().addReadOnly(dialog_->typeCO);
194 bcview().addReadOnly(dialog_->listingsED);
196 addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
200 void QInclude::update_contents()
202 InsetCommandParams const & params = controller().params();
204 dialog_->filenameED->setText(toqstr(params["filename"]));
206 dialog_->visiblespaceCB->setChecked(false);
207 dialog_->visiblespaceCB->setEnabled(false);
208 dialog_->previewCB->setChecked(false);
209 dialog_->previewCB->setEnabled(false);
210 dialog_->listingsGB->setEnabled(false);
211 dialog_->captionLE->clear();
212 dialog_->labelLE->clear();
213 dialog_->listingsED->clear();
214 dialog_->listingsTB->setPlainText(
215 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
217 string cmdname = controller().params().getCmdName();
218 if (cmdname != "include" &&
219 cmdname != "verbatiminput" &&
220 cmdname != "verbatiminput*" &&
221 cmdname != "lstinputlisting")
224 if (cmdname == "include") {
225 dialog_->typeCO->setCurrentIndex(0);
227 } else if (cmdname == "input") {
228 dialog_->typeCO->setCurrentIndex(1);
229 dialog_->previewCB->setEnabled(true);
230 dialog_->previewCB->setChecked(params.preview());
232 } else if (cmdname == "verbatiminput*") {
233 dialog_->typeCO->setCurrentIndex(2);
234 dialog_->visiblespaceCB->setEnabled(true);
235 dialog_->visiblespaceCB->setChecked(true);
237 } else if (cmdname == "verbatiminput") {
238 dialog_->typeCO->setCurrentIndex(2);
239 dialog_->visiblespaceCB->setEnabled(true);
241 } else if (cmdname == "lstinputlisting") {
242 dialog_->typeCO->setCurrentIndex(3);
243 dialog_->listingsGB->setEnabled(true);
244 dialog_->listingsED->setEnabled(true);
245 InsetListingsParams par(params.getOptions());
246 // extract caption and label and put them into their respective editboxes
247 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
248 for (vector<string>::iterator it = pars.begin();
249 it != pars.end(); ++it) {
250 if (prefixIs(*it, "caption=")) {
251 string cap = it->substr(8);
252 if (cap[0] == '{' && cap[cap.size()-1] == '}') {
253 dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
256 } else if (prefixIs(*it, "label=")) {
257 string lbl = it->substr(6);
258 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
259 dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
264 // the rest is put to the extra edit box.
265 string extra = getStringFromVector(pars);
266 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
271 void QInclude::apply()
273 InsetCommandParams params = controller().params();
275 params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
276 params.preview(dialog_->previewCB->isChecked());
278 int const item = dialog_->typeCO->currentIndex();
280 params.setCmdName("include");
281 } else if (item == 1) {
282 params.setCmdName("input");
283 } else if (item == 3) {
284 params.setCmdName("lstinputlisting");
285 // the parameter string should have passed validation
286 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
287 string caption = fromqstr(dialog_->captionLE->text());
288 string label = fromqstr(dialog_->labelLE->text());
289 if (!caption.empty())
290 par.addParam("caption", "{" + caption + "}");
292 par.addParam("label", "{" + label + "}");
293 params.setOptions(par.params());
295 if (dialog_->visiblespaceCB->isChecked())
296 params.setCmdName("verbatiminput*");
298 params.setCmdName("verbatiminput");
300 controller().setParams(params);
304 void QInclude::browse()
306 ControlInclude::Type type;
308 int const item = dialog_->typeCO->currentIndex();
310 type = ControlInclude::INCLUDE;
312 type = ControlInclude::INPUT;
314 type = ControlInclude::VERBATIM;
316 type = ControlInclude::LISTINGS;
318 docstring const & name =
319 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
321 dialog_->filenameED->setText(toqstr(name));
325 void QInclude::load()
328 string const file = fromqstr(dialog_->filenameED->text());
330 controller().load(file);
335 bool QInclude::isValid()
337 return !dialog_->filenameED->text().isEmpty();
340 } // namespace frontend
343 #include "QInclude_moc.cpp"