]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QInclude.cpp
Add support for listings package. Two listings command \lstinline, \lstinputlisting...
[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
15 #include "QInclude.h"
16
17 #include "CheckedLineEdit.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21
22 #include "LyXRC.h"
23
24 #include "insets/InsetListingsParams.h"
25 #include "controllers/ControlInclude.h"
26
27 #include <QPushButton>
28 #include <QCheckBox>
29 #include <QCloseEvent>
30 #include <QLineEdit>
31
32 using std::string;
33
34 using lyx::support::os::internal_path;
35
36
37 namespace lyx {
38 namespace frontend {
39
40 /////////////////////////////////////////////////////////////////////
41 //
42 // QIncludeDialog
43 //
44 /////////////////////////////////////////////////////////////////////
45
46 QIncludeDialog::QIncludeDialog(QInclude * form)
47         : form_(form)
48 {
49         setupUi(this);
50         connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
51         connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
52
53         connect(visiblespaceCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
54         connect(filenameED, SIGNAL(textChanged(const QString &)),
55                 this, SLOT(change_adaptor()));
56         connect(loadPB, SIGNAL(clicked()), this, SLOT(loadClicked()));
57         connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
58         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
59         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
60         connect(previewCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
61         connect(listingsED, SIGNAL(textChanged()), this, SLOT(change_adaptor()));
62         connect(listingsED, SIGNAL(textChanged()), this, SLOT(validate_listings_params()));
63
64         filenameED->setValidator(new PathValidator(true, filenameED));
65         setFocusProxy(filenameED);
66 }
67
68
69 void QIncludeDialog::show()
70 {
71         QDialog::show();
72 }
73
74
75 void QIncludeDialog::change_adaptor()
76 {
77         form_->changed();
78 }
79
80
81 void QIncludeDialog::validate_listings_params()
82 {
83         static bool isOK = true;
84         try {
85                 InsetListingsParams par(fromqstr(listingsED->toPlainText()));
86                 if (!isOK) {
87                         isOK = true;
88                         // listingsTB->setTextColor("black");
89                         listingsTB->setPlainText("Input listings parameters below. Enter ? for a list of parameters.");
90                         okPB->setEnabled(true);
91                 }
92         } catch (invalidParam & e) {
93                 isOK = false;
94                 // listingsTB->setTextColor("red");
95                 listingsTB->setPlainText(e.what());
96                 okPB->setEnabled(false);
97         }
98 }
99
100
101 void QIncludeDialog::closeEvent(QCloseEvent * e)
102 {
103         form_->slotWMHide();
104         e->accept();
105 }
106
107
108 void QIncludeDialog::typeChanged(int v)
109 {
110         switch (v) {
111                 //case Include
112                 case 0:
113                         visiblespaceCB->setEnabled(false);
114                         visiblespaceCB->setChecked(false);
115                         previewCB->setEnabled(false);
116                         previewCB->setChecked(false);
117                         listingsGB->setEnabled(false);
118                         listingsED->setEnabled(false);
119                         break;
120                 //case Input
121                 case 1:
122                         visiblespaceCB->setEnabled(false);
123                         visiblespaceCB->setChecked(false);
124                         previewCB->setEnabled(true);
125                         listingsGB->setEnabled(false);
126                         listingsED->setEnabled(false);
127                         break;
128                 //case listings
129                 case 3:
130                         visiblespaceCB->setEnabled(false);
131                         visiblespaceCB->setChecked(false);
132                         previewCB->setEnabled(false);
133                         listingsGB->setEnabled(true);
134                         listingsED->setEnabled(true);
135                         break;
136                 //case Verbatim
137                 default:
138                         visiblespaceCB->setEnabled(true);
139                         previewCB->setEnabled(false);
140                         previewCB->setChecked(false);
141                         listingsGB->setEnabled(false);
142                         listingsED->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 below. 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                 dialog_->listingsED->setText(toqstr(par.separatedParams()));
239         }       
240 }
241
242
243 void QInclude::apply()
244 {
245         InsetCommandParams params = controller().params();
246
247         params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
248         params.preview(dialog_->previewCB->isChecked());
249
250         int const item = dialog_->typeCO->currentIndex();
251         if (item == 0) {
252                 params.setCmdName("include");
253                 params.setOptions(string());
254         } else if (item == 1) {
255                 params.setCmdName("input");
256                 params.setOptions(string());
257         } else if (item == 3) {
258                 params.setCmdName("lstinputlisting");
259                 // the parameter string should have passed validation
260                 params.setOptions(InsetListingsParams(fromqstr(dialog_->listingsED->toPlainText())).params());
261         } else {
262                 if (dialog_->visiblespaceCB->isChecked())
263                         params.setCmdName("verbatiminput*");
264                 else
265                         params.setCmdName("verbatiminput");
266                 params.setOptions(string());
267         }
268         controller().setParams(params);
269 }
270
271
272 void QInclude::browse()
273 {
274         ControlInclude::Type type;
275
276         int const item = dialog_->typeCO->currentIndex();
277         if (item == 0)
278                 type = ControlInclude::INCLUDE;
279         else if (item == 1)
280                 type = ControlInclude::INPUT;
281         else if (item == 2)
282                 type = ControlInclude::VERBATIM;
283         else
284                 type = ControlInclude::LISTINGS;
285
286         docstring const & name =
287                 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
288         if (!name.empty())
289                 dialog_->filenameED->setText(toqstr(name));
290 }
291
292
293 void QInclude::load()
294 {
295         if (isValid()) {
296                 string const file = fromqstr(dialog_->filenameED->text());
297                 slotOK();
298                 controller().load(file);
299         }
300 }
301
302
303 bool QInclude::isValid()
304 {
305         return !dialog_->filenameED->text().isEmpty();
306 }
307
308 } // namespace frontend
309 } // namespace lyx
310
311 #include "QInclude_moc.cpp"