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