]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
Fix a crash following the input of an invalid paragraph separation value in the docum...
[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 "Format.h"
19 #include "FuncRequest.h"
20 #include "support/gettext.h"
21 #include "LyXRC.h"
22
23 #include "qt_helpers.h"
24 #include "LyXRC.h"
25
26 #include "support/os.h"
27 #include "support/lstrings.h"
28 #include "support/FileFilterList.h"
29 #include "support/filetools.h"
30
31 #include "insets/InsetListingsParams.h"
32 #include "insets/InsetInclude.h"
33
34 #include <QPushButton>
35 #include <QCheckBox>
36 #include <QCloseEvent>
37 #include <QLineEdit>
38
39 #include <utility>
40
41 using namespace std;
42 using namespace lyx::support;
43 using namespace lyx::support::os;
44
45 namespace lyx {
46 namespace frontend {
47
48
49 GuiInclude::GuiInclude(GuiView & lv)
50         : GuiCommand(lv, "include", qt_("Child Document"))
51 {
52         setupUi(this);
53
54         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
55         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
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(embedCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
63         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
64         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
65         connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
66         connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
67         connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
68         connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
69         connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
70         connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
71         connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
72
73         setFocusProxy(filenameED);
74
75         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
76         bc().setOK(okPB);
77         bc().setCancel(closePB);
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         // use a cache here to avoid repeated validation
97         // of the same parameters
98         static string param_cache = string();
99         static docstring msg_cache = docstring();
100         
101         if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
102                 return docstring();
103
104         string params = fromqstr(listingsED->toPlainText());
105         if (params != param_cache) {
106                 param_cache = params;
107                 msg_cache = InsetListingsParams(params).validate();
108         }
109         return msg_cache;
110 }
111
112
113 void GuiInclude::set_listings_msg()
114 {
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::closeEvent(QCloseEvent * e)
131 {
132         slotClose();
133         e->accept();
134 }
135
136
137 void GuiInclude::typeChanged(int v)
138 {
139         switch (v) {
140                 //case Include
141                 case 0:
142                         visiblespaceCB->setEnabled(false);
143                         visiblespaceCB->setChecked(false);
144                         previewCB->setEnabled(false);
145                         previewCB->setChecked(false);
146                         listingsGB->setEnabled(false);
147                         break;
148                 //case Input
149                 case 1:
150                         visiblespaceCB->setEnabled(false);
151                         visiblespaceCB->setChecked(false);
152                         previewCB->setEnabled(true);
153                         listingsGB->setEnabled(false);
154                         break;
155                 //case listings
156                 case 3:
157                         visiblespaceCB->setEnabled(false);
158                         visiblespaceCB->setChecked(false);
159                         previewCB->setEnabled(false);
160                         previewCB->setChecked(false);
161                         listingsGB->setEnabled(true);
162                         break;
163                 //case Verbatim
164                 default:
165                         visiblespaceCB->setEnabled(true);
166                         previewCB->setEnabled(false);
167                         previewCB->setChecked(false);
168                         listingsGB->setEnabled(false);
169                         break;
170         }
171         //see this thread 
172         //  http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
173         //for the reason this is here.
174         okPB->setDefault(true);
175 }
176
177
178 void GuiInclude::updateContents()
179 {
180         filenameED->setText(toqstr(params_["filename"]));
181         embedCB->setCheckState(params_["embed"].empty() ?  Qt::Unchecked : Qt::Checked);
182
183         visiblespaceCB->setChecked(false);
184         visiblespaceCB->setEnabled(false);
185         previewCB->setChecked(false);
186         previewCB->setEnabled(false);
187         listingsGB->setEnabled(false);
188         captionLE->clear();
189         labelLE->clear();
190         listingsED->clear();
191         listingsTB->setPlainText(
192                 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
193
194         string cmdname = params_.getCmdName();
195         if (cmdname != "include" &&
196             cmdname != "verbatiminput" &&
197             cmdname != "verbatiminput*" &&
198                 cmdname != "lstinputlisting")
199                 cmdname = "input";
200
201         if (cmdname == "include") {
202                 typeCO->setCurrentIndex(0);
203
204         } else if (cmdname == "input") {
205                 typeCO->setCurrentIndex(1);
206                 previewCB->setEnabled(true);
207                 previewCB->setChecked(params_.preview());
208
209         } else if (cmdname == "verbatiminput*") {
210                 typeCO->setCurrentIndex(2);
211                 visiblespaceCB->setEnabled(true);
212                 visiblespaceCB->setChecked(true);
213
214         } else if (cmdname == "verbatiminput") {
215                 typeCO->setCurrentIndex(2);
216                 visiblespaceCB->setEnabled(true);
217
218         } else if (cmdname == "lstinputlisting") {
219                 typeCO->setCurrentIndex(3);
220                 listingsGB->setEnabled(true);
221                 listingsED->setEnabled(true);
222                 InsetListingsParams par(to_utf8(params_["lstparams"]));
223                 // extract caption and label and put them into their respective editboxes
224                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
225                 for (vector<string>::iterator it = pars.begin();
226                         it != pars.end(); ++it) {
227                         if (prefixIs(*it, "caption=")) {
228                                 string cap = it->substr(8);
229                                 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
230                                         captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
231                                         *it = "";
232                                 } 
233                         } else if (prefixIs(*it, "label=")) {
234                                 string lbl = it->substr(6);
235                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
236                                         labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
237                                         *it = "";
238                                 }
239                         }
240                 }
241                 // the rest is put to the extra edit box.
242                 string extra = getStringFromVector(pars);
243                 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
244         }
245 }
246
247
248 void GuiInclude::applyView()
249 {
250         params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
251         params_["embed"] = embedCB->checkState() == Qt::Checked ? _("true") : _("false");
252         params_.preview(previewCB->isChecked());
253
254         int const item = typeCO->currentIndex();
255         if (item == 0) {
256                 params_.setCmdName("include");
257         } else if (item == 1) {
258                 params_.setCmdName("input");
259         } else if (item == 3) {
260                 params_.setCmdName("lstinputlisting");
261                 // the parameter string should have passed validation
262                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
263                 string caption = fromqstr(captionLE->text());
264                 string label = fromqstr(labelLE->text());
265                 if (!caption.empty())
266                         par.addParam("caption", "{" + caption + "}");
267                 if (!label.empty())
268                         par.addParam("label", "{" + label + "}");
269                 string const listparams = par.params();
270                 params_["lstparams"] = from_ascii(listparams);
271         } else {
272                 if (visiblespaceCB->isChecked())
273                         params_.setCmdName("verbatiminput*");
274                 else
275                         params_.setCmdName("verbatiminput");
276         }
277 }
278
279
280 void GuiInclude::browse()
281 {
282         Type type;
283
284         int const item = typeCO->currentIndex();
285         if (item == 0)
286                 type = INCLUDE;
287         else if (item == 1)
288                 type = INPUT;
289         else if (item == 2)
290                 type = VERBATIM;
291         else
292                 type = LISTINGS;
293
294         docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
295         if (!name.empty())
296                 filenameED->setText(toqstr(name));
297 }
298
299
300 void GuiInclude::edit()
301 {
302         if (isValid()) {
303                 string const file = fromqstr(filenameED->text());
304                 slotOK();
305                 edit(file);
306         }
307 }
308
309
310 bool GuiInclude::isValid()
311 {
312         return !filenameED->text().isEmpty() && validate_listings_params().empty();
313 }
314
315
316 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
317 {
318         docstring const title = _("Select document to include");
319
320         // input TeX, verbatim, or LyX file ?
321         FileFilterList filters;
322         switch (in_type) {
323         case INCLUDE:
324         case INPUT:
325                 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
326                 break;
327         case VERBATIM:
328         case LISTINGS:
329                 break;
330         }
331
332         docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
333
334         return browseRelFile(in_name, docpath, title, filters, false, 
335                 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
336 }
337
338
339 void GuiInclude::edit(string const & file)
340 {
341         string const ext = support::getExtension(file);
342         if (ext == "lyx")
343                 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
344         else
345                 // tex file or other text file in verbatim mode
346                 formats.edit(buffer(), 
347                         makeAbsPath(file, onlyPath(buffer().absFileName())),
348                         "text");
349 }
350
351
352 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
353
354
355 } // namespace frontend
356 } // namespace lyx
357
358 #include "GuiInclude_moc.cpp"