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