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