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