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 "GuiInclude.h"
14 #include "ControlInclude.h"
16 #include "support/os.h"
17 #include "support/lstrings.h"
19 #include "qt_helpers.h"
22 #include "insets/InsetListingsParams.h"
24 #include <QPushButton>
26 #include <QCloseEvent>
32 using lyx::support::os::internal_path;
33 using lyx::support::prefixIs;
34 using lyx::support::getStringFromVector;
35 using lyx::support::getVectorFromString;
41 GuiIncludeDialog::GuiIncludeDialog(LyXView & lv)
42 : GuiDialog(lv, "include")
45 setViewTitle(_("Child Document"));
46 setController(new ControlInclude(*this));
48 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
49 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
51 connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
52 connect(filenameED, SIGNAL(textChanged(const QString &)),
53 this, SLOT(change_adaptor()));
54 connect(editPB, SIGNAL(clicked()), this, SLOT(editClicked()));
55 connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
56 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
57 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
58 connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
59 connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
60 connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
61 connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
62 connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
63 connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
64 connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
66 setFocusProxy(filenameED);
68 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
70 bc().setCancel(closePB);
71 bc().addReadOnly(filenameED);
72 bc().addReadOnly(browsePB);
73 bc().addReadOnly(visiblespaceCB);
74 bc().addReadOnly(typeCO);
75 bc().addReadOnly(listingsED);
77 bc().addCheckedLineEdit(filenameED, filenameLA);
81 ControlInclude & GuiIncludeDialog::controller()
83 return static_cast<ControlInclude &>(GuiDialog::controller());
87 void GuiIncludeDialog::change_adaptor()
93 docstring GuiIncludeDialog::validate_listings_params()
95 // use a cache here to avoid repeated validation
96 // of the same parameters
97 static string param_cache = string();
98 static docstring msg_cache = docstring();
100 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
103 string params = fromqstr(listingsED->toPlainText());
104 if (params != param_cache) {
105 param_cache = params;
106 msg_cache = InsetListingsParams(params).validate();
112 void GuiIncludeDialog::set_listings_msg()
114 static bool isOK = true;
115 docstring msg = validate_listings_params();
120 listingsTB->setPlainText(
121 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
124 listingsTB->setPlainText(toqstr(msg));
129 void GuiIncludeDialog::closeEvent(QCloseEvent * e)
136 void GuiIncludeDialog::typeChanged(int v)
141 visiblespaceCB->setEnabled(false);
142 visiblespaceCB->setChecked(false);
143 previewCB->setEnabled(false);
144 previewCB->setChecked(false);
145 listingsGB->setEnabled(false);
149 visiblespaceCB->setEnabled(false);
150 visiblespaceCB->setChecked(false);
151 previewCB->setEnabled(true);
152 listingsGB->setEnabled(false);
156 visiblespaceCB->setEnabled(false);
157 visiblespaceCB->setChecked(false);
158 previewCB->setEnabled(false);
159 previewCB->setChecked(false);
160 listingsGB->setEnabled(true);
164 visiblespaceCB->setEnabled(true);
165 previewCB->setEnabled(false);
166 previewCB->setChecked(false);
167 listingsGB->setEnabled(false);
171 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
172 //for the reason this is here.
173 okPB->setDefault(true);
177 void GuiIncludeDialog::editClicked()
183 void GuiIncludeDialog::browseClicked()
189 void GuiIncludeDialog::updateContents()
191 InsetCommandParams const & params = controller().params();
193 filenameED->setText(toqstr(params["filename"]));
195 visiblespaceCB->setChecked(false);
196 visiblespaceCB->setEnabled(false);
197 previewCB->setChecked(false);
198 previewCB->setEnabled(false);
199 listingsGB->setEnabled(false);
203 listingsTB->setPlainText(
204 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
206 string cmdname = controller().params().getCmdName();
207 if (cmdname != "include" &&
208 cmdname != "verbatiminput" &&
209 cmdname != "verbatiminput*" &&
210 cmdname != "lstinputlisting")
213 if (cmdname == "include") {
214 typeCO->setCurrentIndex(0);
216 } else if (cmdname == "input") {
217 typeCO->setCurrentIndex(1);
218 previewCB->setEnabled(true);
219 previewCB->setChecked(params.preview());
221 } else if (cmdname == "verbatiminput*") {
222 typeCO->setCurrentIndex(2);
223 visiblespaceCB->setEnabled(true);
224 visiblespaceCB->setChecked(true);
226 } else if (cmdname == "verbatiminput") {
227 typeCO->setCurrentIndex(2);
228 visiblespaceCB->setEnabled(true);
230 } else if (cmdname == "lstinputlisting") {
231 typeCO->setCurrentIndex(3);
232 listingsGB->setEnabled(true);
233 listingsED->setEnabled(true);
234 InsetListingsParams par(params.getOptions());
235 // extract caption and label and put them into their respective editboxes
236 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
237 for (vector<string>::iterator it = pars.begin();
238 it != pars.end(); ++it) {
239 if (prefixIs(*it, "caption=")) {
240 string cap = it->substr(8);
241 if (cap[0] == '{' && cap[cap.size()-1] == '}') {
242 captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
245 } else if (prefixIs(*it, "label=")) {
246 string lbl = it->substr(6);
247 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
248 labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
253 // the rest is put to the extra edit box.
254 string extra = getStringFromVector(pars);
255 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
260 void GuiIncludeDialog::applyView()
262 InsetCommandParams params = controller().params();
264 params["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
265 params.preview(previewCB->isChecked());
267 int const item = typeCO->currentIndex();
269 params.setCmdName("include");
270 } else if (item == 1) {
271 params.setCmdName("input");
272 } else if (item == 3) {
273 params.setCmdName("lstinputlisting");
274 // the parameter string should have passed validation
275 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
276 string caption = fromqstr(captionLE->text());
277 string label = fromqstr(labelLE->text());
278 if (!caption.empty())
279 par.addParam("caption", "{" + caption + "}");
281 par.addParam("label", "{" + label + "}");
282 params.setOptions(par.params());
284 if (visiblespaceCB->isChecked())
285 params.setCmdName("verbatiminput*");
287 params.setCmdName("verbatiminput");
289 controller().setParams(params);
293 void GuiIncludeDialog::browse()
295 ControlInclude::Type type;
297 int const item = typeCO->currentIndex();
299 type = ControlInclude::INCLUDE;
301 type = ControlInclude::INPUT;
303 type = ControlInclude::VERBATIM;
305 type = ControlInclude::LISTINGS;
307 docstring const & name =
308 controller().browse(qstring_to_ucs4(filenameED->text()), type);
310 filenameED->setText(toqstr(name));
314 void GuiIncludeDialog::edit()
317 string const file = fromqstr(filenameED->text());
319 controller().edit(file);
324 bool GuiIncludeDialog::isValid()
326 return !filenameED->text().isEmpty() &&
327 validate_listings_params().empty();
330 } // namespace frontend
333 #include "GuiInclude_moc.cpp"