GuiPrintNomencl.{cpp,h}:
[lyx.git] / src / frontends / qt4 / GuiInclude.cpp
bloba179478f879a9dc2f38711d85f8f1a3681623075
1 /**
2 * \file GuiInclude.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author John Levon
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
13 #include <config.h>
15 #include "GuiInclude.h"
17 #include "Buffer.h"
18 #include "FuncRequest.h"
19 #include "LyXRC.h"
21 #include "qt_helpers.h"
22 #include "LyXRC.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/FileName.h"
28 #include "support/filetools.h"
30 #include "insets/InsetListingsParams.h"
31 #include "insets/InsetInclude.h"
33 #include <QPushButton>
34 #include <QCheckBox>
35 #include <QLineEdit>
37 #include <utility>
39 using namespace std;
40 using namespace lyx::support;
41 using namespace lyx::support::os;
43 namespace lyx {
44 namespace frontend {
47 GuiInclude::GuiInclude(GuiView & lv)
48 : GuiDialog(lv, "include", qt_("Child Document")),
49 params_(insetCode("include"))
51 setupUi(this);
53 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
54 connect(closePB, SIGNAL(clicked()), this, 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(edit()));
60 connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
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);
73 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
74 bc().setOK(okPB);
75 bc().setCancel(closePB);
76 bc().addReadOnly(filenameED);
77 bc().addReadOnly(browsePB);
78 bc().addReadOnly(visiblespaceCB);
79 bc().addReadOnly(typeCO);
80 bc().addReadOnly(listingsED);
82 bc().addCheckedLineEdit(filenameED, filenameLA);
86 void GuiInclude::change_adaptor()
88 changed();
92 docstring GuiInclude::validate_listings_params()
94 // use a cache here to avoid repeated validation
95 // of the same parameters
96 static string param_cache = string();
97 static docstring msg_cache = docstring();
99 if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
100 return docstring();
102 string params = fromqstr(listingsED->toPlainText());
103 if (params != param_cache) {
104 param_cache = params;
105 msg_cache = InsetListingsParams(params).validate();
107 return msg_cache;
111 void GuiInclude::set_listings_msg()
113 static bool isOK = true;
114 docstring msg = validate_listings_params();
115 if (msg.empty()) {
116 if (isOK)
117 return;
118 isOK = true;
119 listingsTB->setPlainText(
120 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
121 } else {
122 isOK = false;
123 listingsTB->setPlainText(toqstr(msg));
128 void GuiInclude::typeChanged(int v)
130 switch (v) {
131 //case Include
132 case 0:
133 visiblespaceCB->setEnabled(false);
134 visiblespaceCB->setChecked(false);
135 previewCB->setEnabled(false);
136 previewCB->setChecked(false);
137 listingsGB->setEnabled(false);
138 break;
139 //case Input
140 case 1:
141 visiblespaceCB->setEnabled(false);
142 visiblespaceCB->setChecked(false);
143 previewCB->setEnabled(true);
144 listingsGB->setEnabled(false);
145 break;
146 //case listings
147 case 3:
148 visiblespaceCB->setEnabled(false);
149 visiblespaceCB->setChecked(false);
150 previewCB->setEnabled(false);
151 previewCB->setChecked(false);
152 listingsGB->setEnabled(true);
153 break;
154 //case Verbatim
155 default:
156 visiblespaceCB->setEnabled(true);
157 previewCB->setEnabled(false);
158 previewCB->setChecked(false);
159 listingsGB->setEnabled(false);
160 break;
162 //see this thread
163 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
164 //for the reason this is here.
165 okPB->setDefault(true);
169 void GuiInclude::paramsToDialog(InsetCommandParams const & params_)
171 filenameED->setText(toqstr(params_["filename"]));
173 visiblespaceCB->setChecked(false);
174 visiblespaceCB->setEnabled(false);
175 previewCB->setChecked(false);
176 previewCB->setEnabled(false);
177 listingsGB->setEnabled(false);
178 captionLE->clear();
179 labelLE->clear();
180 listingsED->clear();
181 listingsTB->setPlainText(
182 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
184 string cmdname = params_.getCmdName();
185 if (cmdname != "include" &&
186 cmdname != "verbatiminput" &&
187 cmdname != "verbatiminput*" &&
188 cmdname != "lstinputlisting")
189 cmdname = "input";
191 if (cmdname == "include") {
192 typeCO->setCurrentIndex(0);
194 } else if (cmdname == "input") {
195 typeCO->setCurrentIndex(1);
196 previewCB->setEnabled(true);
197 previewCB->setChecked(params_.preview());
199 } else if (cmdname == "verbatiminput*") {
200 typeCO->setCurrentIndex(2);
201 visiblespaceCB->setEnabled(true);
202 visiblespaceCB->setChecked(true);
204 } else if (cmdname == "verbatiminput") {
205 typeCO->setCurrentIndex(2);
206 visiblespaceCB->setEnabled(true);
208 } else if (cmdname == "lstinputlisting") {
209 typeCO->setCurrentIndex(3);
210 listingsGB->setEnabled(true);
211 listingsED->setEnabled(true);
212 InsetListingsParams par(to_utf8(params_["lstparams"]));
213 // extract caption and label and put them into their respective editboxes
214 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
215 for (vector<string>::iterator it = pars.begin();
216 it != pars.end(); ++it) {
217 if (prefixIs(*it, "caption=")) {
218 string cap = it->substr(8);
219 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
220 captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
221 *it = "";
223 } else if (prefixIs(*it, "label=")) {
224 string lbl = it->substr(6);
225 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
226 labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
227 *it = "";
231 // the rest is put to the extra edit box.
232 string extra = getStringFromVector(pars);
233 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
236 // Make sure that the bc is in the INITIAL state
237 if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
238 bc().restore();
242 void GuiInclude::applyView()
244 params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
245 params_.preview(previewCB->isChecked());
247 int const item = typeCO->currentIndex();
248 if (item == 0) {
249 params_.setCmdName("include");
250 } else if (item == 1) {
251 params_.setCmdName("input");
252 } else if (item == 3) {
253 params_.setCmdName("lstinputlisting");
254 // the parameter string should have passed validation
255 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
256 string caption = fromqstr(captionLE->text());
257 string label = fromqstr(labelLE->text());
258 if (!caption.empty())
259 par.addParam("caption", "{" + caption + "}");
260 if (!label.empty())
261 par.addParam("label", "{" + label + "}");
262 string const listparams = par.params();
263 params_["lstparams"] = from_utf8(listparams);
264 } else {
265 if (visiblespaceCB->isChecked())
266 params_.setCmdName("verbatiminput*");
267 else
268 params_.setCmdName("verbatiminput");
273 void GuiInclude::browse()
275 Type type;
277 int const item = typeCO->currentIndex();
278 if (item == 0)
279 type = INCLUDE;
280 else if (item == 1)
281 type = INPUT;
282 else if (item == 2)
283 type = VERBATIM;
284 else
285 type = LISTINGS;
287 QString name = browse(filenameED->text(), type);
288 if (!name.isEmpty())
289 filenameED->setText(name);
293 void GuiInclude::edit()
295 if (!isValid())
296 return;
297 string const file = fromqstr(filenameED->text());
298 if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
299 slotOK();
300 applyView();
301 } else
302 hideView();
303 dispatch(FuncRequest(LFUN_INSET_EDIT));
307 bool GuiInclude::isValid()
309 return !filenameED->text().isEmpty() && validate_listings_params().empty();
313 QString GuiInclude::browse(QString const & in_name, Type in_type) const
315 QString const title = qt_("Select document to include");
317 // input TeX, verbatim, or LyX file ?
318 QStringList filters;
319 switch (in_type) {
320 case INCLUDE:
321 case INPUT:
322 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
323 break;
324 case VERBATIM:
325 case LISTINGS:
326 break;
329 QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
331 return browseRelFile(in_name, docpath, title, filters, false,
332 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
336 bool GuiInclude::initialiseParams(std::string const & data)
338 InsetCommand::string2params("include", data, params_);
339 paramsToDialog(params_);
340 return true;
344 void GuiInclude::dispatchParams()
346 std::string const lfun = InsetCommand::params2string("include", params_);
347 dispatch(FuncRequest(getLfun(), lfun));
351 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
354 } // namespace frontend
355 } // namespace lyx
357 #include "moc_GuiInclude.cpp"