]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiInclude.cpp
Compil fix.
[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(set_listings_msg()));
68         connect(bypassCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69         connect(bypassCB, SIGNAL(clicked()), this, SLOT(set_listings_msg()));
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::set_listings_msg()
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::updateContents()
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
237
238 void GuiInclude::applyView()
239 {
240         params_["filename"] = from_utf8(internal_path(fromqstr(filenameED->text())));
241         params_.preview(previewCB->isChecked());
242
243         int const item = typeCO->currentIndex();
244         if (item == 0) {
245                 params_.setCmdName("include");
246         } else if (item == 1) {
247                 params_.setCmdName("input");
248         } else if (item == 3) {
249                 params_.setCmdName("lstinputlisting");
250                 // the parameter string should have passed validation
251                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
252                 string caption = fromqstr(captionLE->text());
253                 string label = fromqstr(labelLE->text());
254                 if (!caption.empty())
255                         par.addParam("caption", "{" + caption + "}");
256                 if (!label.empty())
257                         par.addParam("label", "{" + label + "}");
258                 string const listparams = par.params();
259                 params_["lstparams"] = from_ascii(listparams);
260         } else {
261                 if (visiblespaceCB->isChecked())
262                         params_.setCmdName("verbatiminput*");
263                 else
264                         params_.setCmdName("verbatiminput");
265         }
266 }
267
268
269 void GuiInclude::browse()
270 {
271         Type type;
272
273         int const item = typeCO->currentIndex();
274         if (item == 0)
275                 type = INCLUDE;
276         else if (item == 1)
277                 type = INPUT;
278         else if (item == 2)
279                 type = VERBATIM;
280         else
281                 type = LISTINGS;
282
283         QString name = browse(filenameED->text(), type);
284         if (!name.isEmpty())
285                 filenameED->setText(name);
286 }
287
288
289 void GuiInclude::edit()
290 {
291         if (!isValid())
292                 return;
293         string const file = fromqstr(filenameED->text());
294         slotOK();
295         applyView();
296         dispatch(FuncRequest(LFUN_INSET_EDIT));
297 }
298
299
300 bool GuiInclude::isValid()
301 {
302         return !filenameED->text().isEmpty() && validate_listings_params().empty();
303 }
304
305
306 QString GuiInclude::browse(QString const & in_name, Type in_type) const
307 {
308         QString const title = qt_("Select document to include");
309
310         // input TeX, verbatim, or LyX file ?
311         QStringList filters;
312         switch (in_type) {
313         case INCLUDE:
314         case INPUT:
315                 filters = fileFilters(qt_("LaTeX/LyX Documents (*.tex *.lyx)"));
316                 break;
317         case VERBATIM:
318         case LISTINGS:
319                 break;
320         }
321
322         QString const docpath = toqstr(support::onlyPath(buffer().absFileName()));
323
324         return browseRelFile(in_name, docpath, title, filters, false, 
325                 qt_("Documents|#o#O"), toqstr(lyxrc.document_path));
326 }
327
328
329 bool GuiInclude::initialiseParams(std::string const & data)
330 {
331         InsetCommand::string2params("include", data, params_);
332         return true;
333 }
334
335
336 void GuiInclude::dispatchParams()
337 {
338         std::string const lfun = InsetCommand::params2string("include", params_);
339         dispatch(FuncRequest(getLfun(), lfun));
340 }
341
342
343 Dialog * createGuiInclude(GuiView & lv) { return new GuiInclude(lv); }
344
345
346 } // namespace frontend
347 } // namespace lyx
348
349 #include "GuiInclude_moc.cpp"