]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QInclude.cpp
Whitespace cleanup
[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 listings 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                         listingsGB->setEnabled(true);
137                         break;
138                 //case Verbatim
139                 default:
140                         visiblespaceCB->setEnabled(true);
141                         previewCB->setEnabled(false);
142                         previewCB->setChecked(false);
143                         listingsGB->setEnabled(false);
144                         break;
145         }
146 }
147
148
149 void QIncludeDialog::loadClicked()
150 {
151         form_->load();
152 }
153
154
155 void QIncludeDialog::browseClicked()
156 {
157         form_->browse();
158 }
159
160
161 /////////////////////////////////////////////////////////////////////
162 //
163 // QInclude
164 //
165 /////////////////////////////////////////////////////////////////////
166
167
168 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
169
170
171 QInclude::QInclude(Dialog & parent)
172         : IncludeBase(parent, _("Child Document"))
173 {}
174
175
176 void QInclude::build_dialog()
177 {
178         dialog_.reset(new QIncludeDialog(this));
179
180         bcview().setOK(dialog_->okPB);
181         bcview().setCancel(dialog_->closePB);
182         bcview().addReadOnly(dialog_->filenameED);
183         bcview().addReadOnly(dialog_->browsePB);
184         bcview().addReadOnly(dialog_->visiblespaceCB);
185         bcview().addReadOnly(dialog_->typeCO);
186         bcview().addReadOnly(dialog_->listingsED);
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_->captionLE->clear();
208         dialog_->labelLE->clear();
209         dialog_->listingsED->clear();
210         dialog_->listingsTB->setPlainText(
211                 qt_("Input listings parameters on the right. Enter ? for a list of parameters."));
212
213         string cmdname = controller().params().getCmdName();
214         if (cmdname != "include" &&
215             cmdname != "verbatiminput" &&
216             cmdname != "verbatiminput*" &&
217                 cmdname != "lstinputlisting")
218                 cmdname = "input";
219
220         if (cmdname == "include") {
221                 dialog_->typeCO->setCurrentIndex(0);
222
223         } else if (cmdname == "input") {
224                 dialog_->typeCO->setCurrentIndex(1);
225                 dialog_->previewCB->setEnabled(true);
226                 dialog_->previewCB->setChecked(params.preview());
227
228         } else if (cmdname == "verbatiminput*") {
229                 dialog_->typeCO->setCurrentIndex(2);
230                 dialog_->visiblespaceCB->setEnabled(true);
231                 dialog_->visiblespaceCB->setChecked(true);
232
233         } else if (cmdname == "verbatiminput") {
234                 dialog_->typeCO->setCurrentIndex(2);
235                 dialog_->visiblespaceCB->setEnabled(true);
236
237         } else if (cmdname == "lstinputlisting") {
238                 dialog_->typeCO->setCurrentIndex(3);
239                 dialog_->listingsGB->setEnabled(true);
240                 dialog_->listingsED->setEnabled(true);
241                 InsetListingsParams par(params.getOptions());
242                 // extract caption and label and put them into their respective editboxes
243                 vector<string> pars = getVectorFromString(par.separatedParams(), "\n");
244                 for (vector<string>::iterator it = pars.begin();
245                         it != pars.end(); ++it) {
246                         if (prefixIs(*it, "caption=")) {
247                                 string cap = it->substr(8);
248                                 if (cap[0] == '{' && cap[cap.size()-1] == '}')
249                                         dialog_->captionLE->setText(toqstr(cap.substr(1, cap.size()-2)));
250                                 else
251                                         throw invalidParam(_("caption parameter is not quoted with braces"));
252                                 *it = "";
253                         } else if (prefixIs(*it, "label=")) {
254                                 string lbl = it->substr(6);
255                                 if (lbl[0] == '{' && lbl[lbl.size()-1] == '}')
256                                         dialog_->labelLE->setText(toqstr(lbl.substr(1, lbl.size()-2)));
257                                 else
258                                         throw invalidParam(_("label parameter is not quoted with braces"));
259                                 *it = "";
260                         }
261                 }
262                 // the rest is put to the extra edit box.
263                 string extra = getStringFromVector(pars);
264                 dialog_->listingsED->setPlainText(toqstr(InsetListingsParams(extra).separatedParams()));
265         }
266 }
267
268
269 void QInclude::apply()
270 {
271         InsetCommandParams params = controller().params();
272
273         params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
274         params.preview(dialog_->previewCB->isChecked());
275
276         int const item = dialog_->typeCO->currentIndex();
277         if (item == 0) {
278                 params.setCmdName("include");
279         } else if (item == 1) {
280                 params.setCmdName("input");
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         }
298         controller().setParams(params);
299 }
300
301
302 void QInclude::browse()
303 {
304         ControlInclude::Type type;
305
306         int const item = dialog_->typeCO->currentIndex();
307         if (item == 0)
308                 type = ControlInclude::INCLUDE;
309         else if (item == 1)
310                 type = ControlInclude::INPUT;
311         else if (item == 2)
312                 type = ControlInclude::VERBATIM;
313         else
314                 type = ControlInclude::LISTINGS;
315
316         docstring const & name =
317                 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
318         if (!name.empty())
319                 dialog_->filenameED->setText(toqstr(name));
320 }
321
322
323 void QInclude::load()
324 {
325         if (isValid()) {
326                 string const file = fromqstr(dialog_->filenameED->text());
327                 slotOK();
328                 controller().load(file);
329         }
330 }
331
332
333 bool QInclude::isValid()
334 {
335         return !dialog_->filenameED->text().isEmpty();
336 }
337
338 } // namespace frontend
339 } // namespace lyx
340
341 #include "QInclude_moc.cpp"