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