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