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