]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QInclude.cpp
Fix assertion with LOF and LOT by transfering the test from TocWidget::select() to...
[lyx.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
187         addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
188 }
189
190
191 void QInclude::update_contents()
192 {
193         PathValidator * path_validator = getPathValidator(dialog_->filenameED);
194         if (path_validator)
195                 path_validator->setChecker(kernel().docType(), lyxrc);
196
197         InsetCommandParams const & params = controller().params();
198
199         dialog_->filenameED->setText(toqstr(params["filename"]));
200
201         dialog_->visiblespaceCB->setChecked(false);
202         dialog_->visiblespaceCB->setEnabled(false);
203         dialog_->previewCB->setChecked(false);
204         dialog_->previewCB->setEnabled(false);
205         dialog_->listingsGB->setEnabled(false);
206         dialog_->captionLE->clear();
207         dialog_->labelLE->clear();
208         dialog_->listingsED->clear();
209         dialog_->listingsTB->setPlainText("Input listings parameters on the right. Enter ? for a list of parameters.");
210
211         string cmdname = controller().params().getCmdName();
212         if (cmdname != "include" &&
213             cmdname != "verbatiminput" &&
214             cmdname != "verbatiminput*" &&
215                 cmdname != "lstinputlisting")
216                 cmdname = "input";
217
218         if (cmdname == "include") {
219                 dialog_->typeCO->setCurrentIndex(0);
220
221         } else if (cmdname == "input") {
222                 dialog_->typeCO->setCurrentIndex(1);
223                 dialog_->previewCB->setEnabled(true);
224                 dialog_->previewCB->setChecked(params.preview());
225
226         } else if (cmdname == "verbatiminput*") {
227                 dialog_->typeCO->setCurrentIndex(2);
228                 dialog_->visiblespaceCB->setEnabled(true);
229                 dialog_->visiblespaceCB->setChecked(true);
230
231         } else if (cmdname == "verbatiminput") {
232                 dialog_->typeCO->setCurrentIndex(2);
233                 dialog_->visiblespaceCB->setEnabled(true);
234
235         } else if (cmdname == "lstinputlisting") {
236                 dialog_->typeCO->setCurrentIndex(3);
237                 dialog_->listingsGB->setEnabled(true);
238                 dialog_->listingsED->setEnabled(true);
239                 InsetListingsParams par(params.getOptions());
240                 // extract caption and label and put them into their respective editboxes
241                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
242                 for (vector<string>::iterator it = pars.begin();
243                         it != pars.end(); ++it) {
244                         if (prefixIs(*it, "caption=")) {
245                                 string cap = it->substr(8);
246                                 if (cap[0] == '{' && cap[cap.size()-1] == '}')
247                                         dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
248                                 else
249                                         throw invalidParam("caption parameter is not quoted with braces");
250                                 *it = "";
251                         } else if (prefixIs(*it, "label=")) {
252                                 string lbl = it->substr(6);
253                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
254                                         dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
255                                 else
256                                         throw invalidParam("label parameter is not quoted with braces");
257                                 *it = "";
258                         }
259                 }
260                 // the rest is put to the extra edit box.
261                 string extra = getStringFromVector(pars);
262                 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
263         }       
264 }
265
266
267 void QInclude::apply()
268 {
269         InsetCommandParams params = controller().params();
270
271         params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
272         params.preview(dialog_->previewCB->isChecked());
273
274         int const item = dialog_->typeCO->currentIndex();
275         if (item == 0) {
276                 params.setCmdName("include");
277                 params.setOptions(string());
278         } else if (item == 1) {
279                 params.setCmdName("input");
280                 params.setOptions(string());
281         } else if (item == 3) {
282                 params.setCmdName("lstinputlisting");
283                 // the parameter string should have passed validation
284                 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
285                 string caption = fromqstr(dialog_->captionLE->text());
286                 string label = fromqstr(dialog_->labelLE->text());
287                 if (!caption.empty())
288                         par.addParam("caption", "{" + caption + "}");
289                 if (!label.empty())
290                         par.addParam("label", "{" + label + "}");
291                 params.setOptions(par.params());
292         } else {
293                 if (dialog_->visiblespaceCB->isChecked())
294                         params.setCmdName("verbatiminput*");
295                 else
296                         params.setCmdName("verbatiminput");
297                 params.setOptions(string());
298         }
299         controller().setParams(params);
300 }
301
302
303 void QInclude::browse()
304 {
305         ControlInclude::Type type;
306
307         int const item = dialog_->typeCO->currentIndex();
308         if (item == 0)
309                 type = ControlInclude::INCLUDE;
310         else if (item == 1)
311                 type = ControlInclude::INPUT;
312         else if (item == 2)
313                 type = ControlInclude::VERBATIM;
314         else
315                 type = ControlInclude::LISTINGS;
316
317         docstring const & name =
318                 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
319         if (!name.empty())
320                 dialog_->filenameED->setText(toqstr(name));
321 }
322
323
324 void QInclude::load()
325 {
326         if (isValid()) {
327                 string const file = fromqstr(dialog_->filenameED->text());
328                 slotOK();
329                 controller().load(file);
330         }
331 }
332
333
334 bool QInclude::isValid()
335 {
336         return !dialog_->filenameED->text().isEmpty();
337 }
338
339 } // namespace frontend
340 } // namespace lyx
341
342 #include "QInclude_moc.cpp"