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