]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiInclude.cpp
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.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiInclude.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FuncRequest.h"
20 #include "LyXRC.h"
21
22 #include "qt_helpers.h"
23 #include "LyXRC.h"
24
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/FileName.h"
29 #include "support/filetools.h"
30
31 #include "insets/InsetListingsParams.h"
32 #include "insets/InsetInclude.h"
33
34 #include <QCheckBox>
35 #include <QLineEdit>
36 #include <QPushButton>
37
38 #include <utility>
39
40 using namespace std;
41 using namespace lyx::support;
42 using namespace lyx::support::os;
43
44 namespace lyx {
45 namespace frontend {
46
47
48 GuiInclude::GuiInclude(GuiView & lv)
49         : GuiDialog(lv, "include", qt_("Child Document")),
50           params_(insetCode("include"))
51 {
52         setupUi(this);
53
54         connect(buttonBox, SIGNAL(clicked(QAbstractButton *)),
55                 this, SLOT(slotButtonBox(QAbstractButton *)));
56
57         connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
58         connect(filenameED, SIGNAL(textChanged(const QString &)),
59                 this, SLOT(change_adaptor()));
60         connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
61         connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
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(literalCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
68         connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
69         connect(listingsED, SIGNAL(textChanged()), this, SLOT(setListingsMsg()));
70         connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71         connect(bypassCB, SIGNAL(clicked()), this, SLOT(setListingsMsg()));
72
73         setFocusProxy(filenameED);
74
75         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
76         bc().setOK(buttonBox->button(QDialogButtonBox::Ok));
77         bc().setCancel(buttonBox->button(QDialogButtonBox::Cancel));
78         bc().addReadOnly(filenameED);
79         bc().addReadOnly(browsePB);
80         bc().addReadOnly(visiblespaceCB);
81         bc().addReadOnly(typeCO);
82         bc().addReadOnly(listingsED);
83
84         bc().addCheckedLineEdit(filenameED, filenameLA);
85 }
86
87
88 void GuiInclude::change_adaptor()
89 {
90         changed();
91 }
92
93
94 docstring GuiInclude::validate_listings_params()
95 {
96         if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
97                 return docstring();
98         string params = fromqstr(listingsED->toPlainText());
99         InsetListingsParams lstparams(params);
100         lstparams.setMinted(buffer().params().use_minted);
101         return lstparams.validate();
102 }
103
104
105 void GuiInclude::setListingsMsg()
106 {
107         // FIXME THREAD
108         static bool isOK = true;
109         docstring msg = validate_listings_params();
110         if (msg.empty()) {
111                 if (isOK)
112                         return;
113                 isOK = true;
114                 listingsTB->setPlainText(
115                         qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
116         } else {
117                 isOK = false;
118                 listingsTB->setPlainText(toqstr(msg));
119         }
120 }
121
122
123 void GuiInclude::typeChanged(int v)
124 {
125         switch (v) {
126                 //case Include
127                 case 0:
128                         visiblespaceCB->setEnabled(false);
129                         visiblespaceCB->setChecked(false);
130                         previewCB->setEnabled(false);
131                         previewCB->setChecked(false);
132                         listingsGB->setEnabled(false);
133                         break;
134                 //case Input
135                 case 1:
136                         visiblespaceCB->setEnabled(false);
137                         visiblespaceCB->setChecked(false);
138                         previewCB->setEnabled(true);
139                         listingsGB->setEnabled(false);
140                         break;
141                 //case listings
142                 case 3:
143                         visiblespaceCB->setEnabled(false);
144                         visiblespaceCB->setChecked(false);
145                         previewCB->setEnabled(false);
146                         previewCB->setChecked(false);
147                         listingsGB->setEnabled(true);
148                         break;
149                 //case Verbatim
150                 default:
151                         visiblespaceCB->setEnabled(true);
152                         previewCB->setEnabled(false);
153                         previewCB->setChecked(false);
154                         listingsGB->setEnabled(false);
155                         break;
156         }
157 }
158
159
160 void GuiInclude::paramsToDialog(InsetCommandParams const & icp)
161 {
162         filenameED->setText(toqstr(icp["filename"]));
163
164         visiblespaceCB->setChecked(false);
165         visiblespaceCB->setEnabled(false);
166         previewCB->setChecked(false);
167         previewCB->setEnabled(false);
168         listingsGB->setEnabled(false);
169         captionLE->clear();
170         labelLE->clear();
171         listingsED->clear();
172         listingsTB->setPlainText(
173                 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
174
175         string cmdname = icp.getCmdName();
176         if (cmdname != "include" &&
177             cmdname != "verbatiminput" &&
178             cmdname != "verbatiminput*" &&
179             cmdname != "lstinputlisting" &&
180             cmdname != "inputminted")
181                 cmdname = "input";
182
183         if (cmdname == "include") {
184                 typeCO->setCurrentIndex(0);
185
186         } else if (cmdname == "input") {
187                 typeCO->setCurrentIndex(1);
188                 previewCB->setEnabled(true);
189                 previewCB->setChecked(icp.preview());
190
191         } else if (cmdname == "verbatiminput*") {
192                 typeCO->setCurrentIndex(2);
193                 visiblespaceCB->setEnabled(true);
194                 visiblespaceCB->setChecked(true);
195
196         } else if (cmdname == "verbatiminput") {
197                 typeCO->setCurrentIndex(2);
198                 visiblespaceCB->setEnabled(true);
199
200         } else if (cmdname == "lstinputlisting" || cmdname == "inputminted") {
201                 typeCO->setCurrentIndex(3);
202                 listingsGB->setEnabled(true);
203                 listingsED->setEnabled(true);
204                 InsetListingsParams par(to_utf8(icp["lstparams"]));
205                 // extract caption and label and put them into their respective editboxes
206                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
207                 for (vector<string>::iterator it = pars.begin();
208                         it != pars.end(); ++it) {
209                         if (prefixIs(*it, "caption=")) {
210                                 string cap = it->substr(8);
211                                 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
212                                         captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
213                                         *it = "";
214                                 }
215                         } else if (prefixIs(*it, "label=")) {
216                                 string lbl = it->substr(6);
217                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
218                                         labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
219                                         *it = "";
220                                 }
221                         }
222                 }
223                 // the rest is put to the extra edit box.
224                 string extra = getStringFromVector(pars);
225                 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
226         }
227         literalCB->setChecked(icp["literal"] == "true");
228
229         // Make sure that the bc is in the INITIAL state
230         if (bc().policy().buttonStatus(ButtonPolicy::OKAY))
231                 bc().restore();
232 }
233
234
235 void GuiInclude::applyView()
236 {
237         params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
238         params_.preview(previewCB->isChecked());
239
240         int const item = typeCO->currentIndex();
241         if (item == 0) {
242                 params_.setCmdName("include");
243         } else if (item == 1) {
244                 params_.setCmdName("input");
245         } else if (item == 3) {
246                 if (buffer().params().use_minted)
247                         params_.setCmdName("inputminted");
248                 else
249                         params_.setCmdName("lstinputlisting");
250                 // the parameter string should have passed validation
251                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
252                 string caption = fromqstr(captionLE->text());
253                 string label = fromqstr(labelLE->text());
254                 if (!caption.empty())
255                         par.addParam("caption", "{" + caption + "}");
256                 if (!label.empty())
257                         par.addParam("label", "{" + label + "}");
258                 string const listparams = par.params();
259                 params_["lstparams"] = from_utf8(listparams);
260         } else {
261                 if (visiblespaceCB->isChecked())
262                         params_.setCmdName("verbatiminput*");
263                 else
264                         params_.setCmdName("verbatiminput");
265         }
266         params_["literal"] = literalCB->isChecked()
267                         ? from_ascii("true") : from_ascii("false");
268 }
269
270
271 void GuiInclude::browse()
272 {
273         Type type;
274
275         int const item = typeCO->currentIndex();
276         if (item == 0)
277                 type = INCLUDE;
278         else if (item == 1)
279                 type = INPUT;
280         else if (item == 2)
281                 type = VERBATIM;
282         else
283                 type = LISTINGS;
284
285         QString name = browse(filenameED->text(), type);
286         if (!name.isEmpty())
287                 filenameED->setText(name);
288 }
289
290
291 void GuiInclude::edit()
292 {
293         if (!isValid())
294                 return;
295         if (bc().policy().buttonStatus(ButtonPolicy::OKAY)) {
296                 slotOK();
297                 applyView();
298         } else
299                 hideView();
300         dispatch(FuncRequest(LFUN_INSET_EDIT));
301 }
302
303
304 bool GuiInclude::isValid()
305 {
306         return !filenameED->text().isEmpty() && validate_listings_params().empty();
307 }
308
309
310 QString GuiInclude::browse(QString const & in_name, Type in_type) const
311 {
312         QString const title = qt_("Select document to include");
313
314         // input TeX, verbatim, or LyX file ?
315         QStringList filters;
316         switch (in_type) {
317         case INCLUDE:
318         case INPUT:
319                 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
320                 break;
321         case VERBATIM:
322         case LISTINGS:
323                 filters = fileFilters(QString());
324                 break;
325         }
326
327         QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
328
329         return browseRelToParent(in_name, docpath, title, filters, false,
330                 qt_("D&ocuments"), toqstr(lyxrc.document_path));
331 }
332
333
334 bool GuiInclude::initialiseParams(std::string const & sdata)
335 {
336         InsetCommand::string2params(sdata, params_);
337         paramsToDialog(params_);
338         return true;
339 }
340
341
342 void GuiInclude::dispatchParams()
343 {
344         std::string const lfun = InsetCommand::params2string(params_);
345         dispatch(FuncRequest(getLfun(), lfun));
346 }
347
348
349 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
350
351
352 } // namespace frontend
353 } // namespace lyx
354
355 #include "moc_GuiInclude.cpp"