]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
fcd8ede71ca19952dcfd38227f40fe378a5d2b75
[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::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::updateContents()
172 {
173         filenameED->setText(toqstr(params_["filename"]));
174         embedCB->setCheckState(params_["embed"].empty() ?  Qt::Unchecked : Qt::Checked);
175
176         visiblespaceCB->setChecked(false);
177         visiblespaceCB->setEnabled(false);
178         previewCB->setChecked(false);
179         previewCB->setEnabled(false);
180         listingsGB->setEnabled(false);
181         captionLE->clear();
182         labelLE->clear();
183         listingsED->clear();
184         listingsTB->setPlainText(
185                 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
186
187         string cmdname = params_.getCmdName();
188         if (cmdname != "include" &&
189             cmdname != "verbatiminput" &&
190             cmdname != "verbatiminput*" &&
191                 cmdname != "lstinputlisting")
192                 cmdname = "input";
193
194         if (cmdname == "include") {
195                 typeCO->setCurrentIndex(0);
196
197         } else if (cmdname == "input") {
198                 typeCO->setCurrentIndex(1);
199                 previewCB->setEnabled(true);
200                 previewCB->setChecked(params_.preview());
201
202         } else if (cmdname == "verbatiminput*") {
203                 typeCO->setCurrentIndex(2);
204                 visiblespaceCB->setEnabled(true);
205                 visiblespaceCB->setChecked(true);
206
207         } else if (cmdname == "verbatiminput") {
208                 typeCO->setCurrentIndex(2);
209                 visiblespaceCB->setEnabled(true);
210
211         } else if (cmdname == "lstinputlisting") {
212                 typeCO->setCurrentIndex(3);
213                 listingsGB->setEnabled(true);
214                 listingsED->setEnabled(true);
215                 InsetListingsParams par(to_utf8(params_["lstparams"]));
216                 // extract caption and label and put them into their respective editboxes
217                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
218                 for (vector<string>::iterator it = pars.begin();
219                         it != pars.end(); ++it) {
220                         if (prefixIs(*it, "caption=")) {
221                                 string cap = it->substr(8);
222                                 if (cap[0] == '{' && cap[cap.size() - 1] == '}') {
223                                         captionLE->setText(toqstr(cap.substr(1, cap.size() - 2)));
224                                         *it = "";
225                                 } 
226                         } else if (prefixIs(*it, "label=")) {
227                                 string lbl = it->substr(6);
228                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}') {
229                                         labelLE->setText(toqstr(lbl.substr(1, lbl.size() - 2)));
230                                         *it = "";
231                                 }
232                         }
233                 }
234                 // the rest is put to the extra edit box.
235                 string extra = getStringFromVector(pars);
236                 listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
237         }
238 }
239
240
241 void GuiInclude::applyView()
242 {
243         params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
244         params_["embed"] = embedCB->checkState() == Qt::Checked ? _("true") : _("false");
245         params_.preview(previewCB->isChecked());
246
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_ascii(listparams);
264         } else {
265                 if (visiblespaceCB->isChecked())
266                         params_.setCmdName("verbatiminput*");
267                 else
268                         params_.setCmdName("verbatiminput");
269         }
270 }
271
272
273 void GuiInclude::browse()
274 {
275         Type type;
276
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;
286
287         docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
288         if (!name.empty())
289                 filenameED->setText(toqstr(name));
290 }
291
292
293 void GuiInclude::edit()
294 {
295         if (isValid()) {
296                 string const file = fromqstr(filenameED->text());
297                 slotOK();
298                 edit(file);
299         }
300 }
301
302
303 bool GuiInclude::isValid()
304 {
305         return !filenameED->text().isEmpty() && validate_listings_params().empty();
306 }
307
308
309 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
310 {
311         docstring const title = _("Select document to include");
312
313         // input TeX, verbatim, or LyX file ?
314         FileFilterList filters;
315         switch (in_type) {
316         case INCLUDE:
317         case INPUT:
318                 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
319                 break;
320         case VERBATIM:
321         case LISTINGS:
322                 break;
323         }
324
325         docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
326
327         return browseRelFile(in_name, docpath, title, filters, false, 
328                 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
329 }
330
331
332 void GuiInclude::edit(string const & file)
333 {
334         string const ext = support::getExtension(file);
335         if (ext == "lyx")
336                 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
337         else
338                 // tex file or other text file in verbatim mode
339                 formats.edit(buffer(), 
340                         makeAbsPath(file, onlyPath(buffer().absFileName())),
341                         "text");
342 }
343
344
345 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
346
347
348 } // namespace frontend
349 } // namespace lyx
350
351 #include "GuiInclude_moc.cpp"