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