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