]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
7caf50fb188c850f262995896abc80b60ffd1cdf
[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/FileName.h"
30 #include "support/filetools.h"
31
32 #include "insets/InsetListingsParams.h"
33 #include "insets/InsetInclude.h"
34
35 #include <QPushButton>
36 #include <QCheckBox>
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         : GuiDialog(lv, "include", qt_("Child Document")),
51           params_(insetCode("include"))
52 {
53         setupUi(this);
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(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
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
239
240 void GuiInclude::applyView()
241 {
242         params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
243         params_.preview(previewCB->isChecked());
244
245         int const item = typeCO->currentIndex();
246         if (item == 0) {
247                 params_.setCmdName("include");
248         } else if (item == 1) {
249                 params_.setCmdName("input");
250         } else if (item == 3) {
251                 params_.setCmdName("lstinputlisting");
252                 // the parameter string should have passed validation
253                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
254                 string caption = fromqstr(captionLE->text());
255                 string label = fromqstr(labelLE->text());
256                 if (!caption.empty())
257                         par.addParam("caption", "{" + caption + "}");
258                 if (!label.empty())
259                         par.addParam("label", "{" + label + "}");
260                 string const listparams = par.params();
261                 params_["lstparams"] = from_ascii(listparams);
262         } else {
263                 if (visiblespaceCB->isChecked())
264                         params_.setCmdName("verbatiminput*");
265                 else
266                         params_.setCmdName("verbatiminput");
267         }
268 }
269
270
271 void GuiInclude::browse()
272 {
273         Type type;
274
275         int const item = typeCO->currentIndex();
276         if (item == 0)
277                 type = INCLUDE;
278         else if (item == 1)
279                 type = INPUT;
280         else if (item == 2)
281                 type = VERBATIM;
282         else
283                 type = LISTINGS;
284
285         QString name = browse(filenameED->text(), type);
286         if (!name.isEmpty())
287                 filenameED->setText(name);
288 }
289
290
291 void GuiInclude::edit()
292 {
293         if (isValid()) {
294                 string const file = fromqstr(filenameED->text());
295                 slotOK();
296                 edit(file);
297         }
298 }
299
300
301 bool GuiInclude::isValid()
302 {
303         return !filenameED->text().isEmpty() && validate_listings_params().empty();
304 }
305
306
307 QString GuiInclude::browse(QString const & in_name, Type in_type) const
308 {
309         QString const title = qt_("Select document to include");
310
311         // input TeX, verbatim, or LyX file ?
312         FileFilterList filters;
313         switch (in_type) {
314         case INCLUDE:
315         case INPUT:
316                 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
317                 break;
318         case VERBATIM:
319         case LISTINGS:
320                 break;
321         }
322
323         QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
324
325         return browseRelFile(in_name, docpath, title, filters, false, 
326                 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
327 }
328
329
330 void GuiInclude::edit(string const & file)
331 {
332         string const ext = support::getExtension(file);
333         if (ext == "lyx")
334                 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
335         else
336                 // tex file or other text file in verbatim mode
337                 formats.edit(buffer(), 
338                         makeAbsPath(file, support::onlyPath(buffer().absFileName())),
339                         "text");
340 }
341
342
343 bool GuiInclude::initialiseParams(std::string const & data)
344 {
345         InsetCommand::string2params("include", data, params_);
346         return true;
347 }
348
349
350 void GuiInclude::dispatchParams()
351 {
352         std::string const lfun = InsetCommand::params2string("include", params_);
353         dispatch(FuncRequest(getLfun(), lfun));
354 }
355
356
357 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
358
359
360 } // namespace frontend
361 } // namespace lyx
362
363 #include "GuiInclude_moc.cpp"