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