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