]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
do what the FIXME suggested
[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 <QLineEdit>
37
38 #include <utility>
39
40 using namespace std;
41 using namespace lyx::support;
42 using namespace lyx::support::os;
43
44 namespace lyx {
45 namespace frontend {
46
47
48 GuiInclude::GuiInclude(GuiView & lv)
49         : GuiCommand(lv, "include", qt_("Child Document"))
50 {
51         setupUi(this);
52
53         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
54         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
55
56         connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
57         connect(filenameED, SIGNAL(textChanged(const QString &)),
58                 this, SLOT(change_adaptor()));
59         connect(editPB, SIGNAL(clicked()), this, SLOT(edit()));
60         connect(browsePB, SIGNAL(clicked()), this, SLOT(browse()));
61         connect(embedCB, SIGNAL(toggled(bool)), this, SLOT(change_adaptor()));
62         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
63         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
64         connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
65         connect(captionLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
66         connect(labelLE, SIGNAL(textChanged(const QString&)), this, SLOT(change_adaptor()));
67         connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
68         connect(listingsED, SIGNAL(textChanged()), this, SLOT(set_listings_msg()));
69         connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70         connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
71
72         setFocusProxy(filenameED);
73
74         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
75         bc().setOK(okPB);
76         bc().setCancel(closePB);
77         bc().addReadOnly(filenameED);
78         bc().addReadOnly(browsePB);
79         bc().addReadOnly(visiblespaceCB);
80         bc().addReadOnly(typeCO);
81         bc().addReadOnly(listingsED);
82
83         bc().addCheckedLineEdit(filenameED, filenameLA);
84 }
85
86
87 void GuiInclude::change_adaptor()
88 {
89         changed();
90 }
91
92
93 docstring GuiInclude::validate_listings_params()
94 {
95         // use a cache here to avoid repeated validation
96         // of the same parameters
97         static string param_cache = string();
98         static docstring msg_cache = docstring();
99         
100         if (typeCO->currentIndex() != 3 || bypassCB->isChecked())
101                 return docstring();
102
103         string params = fromqstr(listingsED->toPlainText());
104         if (params != param_cache) {
105                 param_cache = params;
106                 msg_cache = InsetListingsParams(params).validate();
107         }
108         return msg_cache;
109 }
110
111
112 void GuiInclude::set_listings_msg()
113 {
114         static bool isOK = true;
115         docstring msg = validate_listings_params();
116         if (msg.empty()) {
117                 if (isOK)
118                         return;
119                 isOK = true;
120                 listingsTB->setPlainText(
121                         qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
122         } else {
123                 isOK = false;
124                 listingsTB->setPlainText(toqstr(msg));
125         }
126 }
127
128
129 void GuiInclude::typeChanged(int v)
130 {
131         switch (v) {
132                 //case Include
133                 case 0:
134                         visiblespaceCB->setEnabled(false);
135                         visiblespaceCB->setChecked(false);
136                         previewCB->setEnabled(false);
137                         previewCB->setChecked(false);
138                         listingsGB->setEnabled(false);
139                         break;
140                 //case Input
141                 case 1:
142                         visiblespaceCB->setEnabled(false);
143                         visiblespaceCB->setChecked(false);
144                         previewCB->setEnabled(true);
145                         listingsGB->setEnabled(false);
146                         break;
147                 //case listings
148                 case 3:
149                         visiblespaceCB->setEnabled(false);
150                         visiblespaceCB->setChecked(false);
151                         previewCB->setEnabled(false);
152                         previewCB->setChecked(false);
153                         listingsGB->setEnabled(true);
154                         break;
155                 //case Verbatim
156                 default:
157                         visiblespaceCB->setEnabled(true);
158                         previewCB->setEnabled(false);
159                         previewCB->setChecked(false);
160                         listingsGB->setEnabled(false);
161                         break;
162         }
163         //see this thread 
164         //  http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
165         //for the reason this is here.
166         okPB->setDefault(true);
167 }
168
169
170 void GuiInclude::updateContents()
171 {
172         filenameED->setText(toqstr(params_["filename"]));
173         embedCB->setCheckState(params_["embed"].empty() ?  Qt::Unchecked : Qt::Checked);
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_["embed"] = embedCB->checkState() == Qt::Checked ? _("true") : _("false");
244         params_.preview(previewCB->isChecked());
245
246         int const item = typeCO->currentIndex();
247         if (item == 0) {
248                 params_.setCmdName("include");
249         } else if (item == 1) {
250                 params_.setCmdName("input");
251         } else if (item == 3) {
252                 params_.setCmdName("lstinputlisting");
253                 // the parameter string should have passed validation
254                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
255                 string caption = fromqstr(captionLE->text());
256                 string label = fromqstr(labelLE->text());
257                 if (!caption.empty())
258                         par.addParam("caption", "{" + caption + "}");
259                 if (!label.empty())
260                         par.addParam("label", "{" + label + "}");
261                 string const listparams = par.params();
262                 params_["lstparams"] = from_ascii(listparams);
263         } else {
264                 if (visiblespaceCB->isChecked())
265                         params_.setCmdName("verbatiminput*");
266                 else
267                         params_.setCmdName("verbatiminput");
268         }
269 }
270
271
272 void GuiInclude::browse()
273 {
274         Type type;
275
276         int const item = typeCO->currentIndex();
277         if (item == 0)
278                 type = INCLUDE;
279         else if (item == 1)
280                 type = INPUT;
281         else if (item == 2)
282                 type = VERBATIM;
283         else
284                 type = LISTINGS;
285
286         docstring const & name = browse(qstring_to_ucs4(filenameED->text()), type);
287         if (!name.empty())
288                 filenameED->setText(toqstr(name));
289 }
290
291
292 void GuiInclude::edit()
293 {
294         if (isValid()) {
295                 string const file = fromqstr(filenameED->text());
296                 slotOK();
297                 edit(file);
298         }
299 }
300
301
302 bool GuiInclude::isValid()
303 {
304         return !filenameED->text().isEmpty() && validate_listings_params().empty();
305 }
306
307
308 docstring GuiInclude::browse(docstring const & in_name, Type in_type) const
309 {
310         docstring const title = _("Select document to include");
311
312         // input TeX, verbatim, or LyX file ?
313         FileFilterList filters;
314         switch (in_type) {
315         case INCLUDE:
316         case INPUT:
317                 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
318                 break;
319         case VERBATIM:
320         case LISTINGS:
321                 break;
322         }
323
324         docstring const docpath = from_utf8(onlyPath(buffer().absFileName()));
325
326         return browseRelFile(in_name, docpath, title, filters, false, 
327                 _("Documents|#o#O"), from_utf8(lyxrc.document_path));
328 }
329
330
331 void GuiInclude::edit(string const & file)
332 {
333         string const ext = support::getExtension(file);
334         if (ext == "lyx")
335                 dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
336         else
337                 // tex file or other text file in verbatim mode
338                 formats.edit(buffer(), 
339                         makeAbsPath(file, onlyPath(buffer().absFileName())),
340                         "text");
341 }
342
343
344 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
345
346
347 } // namespace frontend
348 } // namespace lyx
349
350 #include "GuiInclude_moc.cpp"