]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QInclude.cpp
31aaa115e6f689d9d9086abeba0a284369722fad
[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(
95                                 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
96                         okPB->setEnabled(true);
97                 }
98         } catch (invalidParam & e) {
99                 isOK = false;
100                 listingsTB->setPlainText(toqstr(e.what()));
101                 okPB->setEnabled(false);
102         }
103 }
104
105
106 void QIncludeDialog::closeEvent(QCloseEvent * e)
107 {
108         form_->slotWMHide();
109         e->accept();
110 }
111
112
113 void QIncludeDialog::typeChanged(int v)
114 {
115         switch (v) {
116                 //case Include
117                 case 0:
118                         visiblespaceCB->setEnabled(false);
119                         visiblespaceCB->setChecked(false);
120                         previewCB->setEnabled(false);
121                         previewCB->setChecked(false);
122                         listingsGB->setEnabled(false);
123                         break;
124                 //case Input
125                 case 1:
126                         visiblespaceCB->setEnabled(false);
127                         visiblespaceCB->setChecked(false);
128                         previewCB->setEnabled(true);
129                         listingsGB->setEnabled(false);
130                         break;
131                 //case listings
132                 case 3:
133                         visiblespaceCB->setEnabled(false);
134                         visiblespaceCB->setChecked(false);
135                         previewCB->setEnabled(false);
136                         previewCB->setChecked(false);
137                         listingsGB->setEnabled(true);
138                         break;
139                 //case Verbatim
140                 default:
141                         visiblespaceCB->setEnabled(true);
142                         previewCB->setEnabled(false);
143                         previewCB->setChecked(false);
144                         listingsGB->setEnabled(false);
145                         break;
146         }
147         //see this thread 
148         //  http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg118471.html
149         //for the reason this is here.
150         okPB->setDefault(true);
151 }
152
153
154 void QIncludeDialog::loadClicked()
155 {
156         form_->load();
157 }
158
159
160 void QIncludeDialog::browseClicked()
161 {
162         form_->browse();
163 }
164
165
166 /////////////////////////////////////////////////////////////////////
167 //
168 // QInclude
169 //
170 /////////////////////////////////////////////////////////////////////
171
172
173 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
174
175
176 QInclude::QInclude(Dialog & parent)
177         : IncludeBase(parent, _("Child Document"))
178 {}
179
180
181 void QInclude::build_dialog()
182 {
183         dialog_.reset(new QIncludeDialog(this));
184
185         bcview().setOK(dialog_->okPB);
186         bcview().setCancel(dialog_->closePB);
187         bcview().addReadOnly(dialog_->filenameED);
188         bcview().addReadOnly(dialog_->browsePB);
189         bcview().addReadOnly(dialog_->visiblespaceCB);
190         bcview().addReadOnly(dialog_->typeCO);
191         bcview().addReadOnly(dialog_->listingsED);
192
193         addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
194 }
195
196
197 void QInclude::update_contents()
198 {
199         PathValidator * path_validator = getPathValidator(dialog_->filenameED);
200         if (path_validator)
201                 path_validator->setChecker(kernel().docType(), lyxrc);
202
203         InsetCommandParams const & params = controller().params();
204
205         dialog_->filenameED->setText(toqstr(params["filename"]));
206
207         dialog_->visiblespaceCB->setChecked(false);
208         dialog_->visiblespaceCB->setEnabled(false);
209         dialog_->previewCB->setChecked(false);
210         dialog_->previewCB->setEnabled(false);
211         dialog_->listingsGB->setEnabled(false);
212         dialog_->captionLE->clear();
213         dialog_->labelLE->clear();
214         dialog_->listingsED->clear();
215         dialog_->listingsTB->setPlainText(
216                 qt_("Input listing parameters on the right. Enter ? for a list of parameters."));
217
218         string cmdname = controller().params().getCmdName();
219         if (cmdname != "include" &&
220             cmdname != "verbatiminput" &&
221             cmdname != "verbatiminput*" &&
222                 cmdname != "lstinputlisting")
223                 cmdname = "input";
224
225         if (cmdname == "include") {
226                 dialog_->typeCO->setCurrentIndex(0);
227
228         } else if (cmdname == "input") {
229                 dialog_->typeCO->setCurrentIndex(1);
230                 dialog_->previewCB->setEnabled(true);
231                 dialog_->previewCB->setChecked(params.preview());
232
233         } else if (cmdname == "verbatiminput*") {
234                 dialog_->typeCO->setCurrentIndex(2);
235                 dialog_->visiblespaceCB->setEnabled(true);
236                 dialog_->visiblespaceCB->setChecked(true);
237
238         } else if (cmdname == "verbatiminput") {
239                 dialog_->typeCO->setCurrentIndex(2);
240                 dialog_->visiblespaceCB->setEnabled(true);
241
242         } else if (cmdname == "lstinputlisting") {
243                 dialog_->typeCO->setCurrentIndex(3);
244                 dialog_->listingsGB->setEnabled(true);
245                 dialog_->listingsED->setEnabled(true);
246                 InsetListingsParams par(params.getOptions());
247                 // extract caption and label and put them into their respective editboxes
248                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
249                 for (vector<string>::iterator it = pars.begin();
250                         it != pars.end(); ++it) {
251                         if (prefixIs(*it, "caption=")) {
252                                 string cap = it->substr(8);
253                                 if (cap[0] == '{' && cap[cap.size()-1] == '}')
254                                         dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
255                                 else
256                                         throw invalidParam(_("caption parameter is not quoted with braces"));
257                                 *it = "";
258                         } else if (prefixIs(*it, "label=")) {
259                                 string lbl = it->substr(6);
260                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
261                                         dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
262                                 else
263                                         throw invalidParam(_("label parameter is not quoted with braces"));
264                                 *it = "";
265                         }
266                 }
267                 // the rest is put to the extra edit box.
268                 string extra = getStringFromVector(pars);
269                 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
270         }
271 }
272
273
274 void QInclude::apply()
275 {
276         InsetCommandParams params = controller().params();
277
278         params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
279         params.preview(dialog_->previewCB->isChecked());
280
281         int const item = dialog_->typeCO->currentIndex();
282         if (item == 0) {
283                 params.setCmdName("include");
284         } else if (item == 1) {
285                 params.setCmdName("input");
286         } else if (item == 3) {
287                 params.setCmdName("lstinputlisting");
288                 // the parameter string should have passed validation
289                 InsetListingsParams par(fromqstr(dialog_->listingsED->toPlainText()));
290                 string caption = fromqstr(dialog_->captionLE->text());
291                 string label = fromqstr(dialog_->labelLE->text());
292                 if (!caption.empty())
293                         par.addParam("caption", "{" + caption + "}");
294                 if (!label.empty())
295                         par.addParam("label", "{" + label + "}");
296                 params.setOptions(par.params());
297         } else {
298                 if (dialog_->visiblespaceCB->isChecked())
299                         params.setCmdName("verbatiminput*");
300                 else
301                         params.setCmdName("verbatiminput");
302         }
303         controller().setParams(params);
304 }
305
306
307 void QInclude::browse()
308 {
309         ControlInclude::Type type;
310
311         int const item = dialog_->typeCO->currentIndex();
312         if (item == 0)
313                 type = ControlInclude::INCLUDE;
314         else if (item == 1)
315                 type = ControlInclude::INPUT;
316         else if (item == 2)
317                 type = ControlInclude::VERBATIM;
318         else
319                 type = ControlInclude::LISTINGS;
320
321         docstring const & name =
322                 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
323         if (!name.empty())
324                 dialog_->filenameED->setText(toqstr(name));
325 }
326
327
328 void QInclude::load()
329 {
330         if (isValid()) {
331                 string const file = fromqstr(dialog_->filenameED->text());
332                 slotOK();
333                 controller().load(file);
334         }
335 }
336
337
338 bool QInclude::isValid()
339 {
340         return !dialog_->filenameED->text().isEmpty();
341 }
342
343 } // namespace frontend
344 } // namespace lyx
345
346 #include "QInclude_moc.cpp"