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