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