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