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