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