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