]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QInclude.cpp
* LyX::addFileToLoad(): new method for double-clicked file icons on MacOS.
[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
15 #include "QInclude.h"
16
17 #include "CheckedLineEdit.h"
18 #include "Qt2BC.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21
22 #include "LyXRC.h"
23
24 #include "controllers/ControlInclude.h"
25
26 #include <QPushButton>
27 #include <QCheckBox>
28 #include <QCloseEvent>
29 #include <QLineEdit>
30
31 using std::string;
32
33 using lyx::support::os::internal_path;
34
35
36 namespace lyx {
37 namespace frontend {
38
39 /////////////////////////////////////////////////////////////////////
40 //
41 // QIncludeDialog
42 //
43 /////////////////////////////////////////////////////////////////////
44
45 QIncludeDialog::QIncludeDialog(QInclude * 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(loadPB, SIGNAL(clicked()), this, SLOT(loadClicked()));
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
61         filenameED->setValidator(new PathValidator(true, filenameED));
62         setFocusProxy(filenameED);
63 }
64
65
66 void QIncludeDialog::show()
67 {
68         QDialog::show();
69 }
70
71
72 void QIncludeDialog::change_adaptor()
73 {
74         form_->changed();
75 }
76
77
78 void QIncludeDialog::closeEvent(QCloseEvent * e)
79 {
80         form_->slotWMHide();
81         e->accept();
82 }
83
84
85 void QIncludeDialog::typeChanged(int v)
86 {
87         switch (v) {
88                 //case Include
89                 case 0:
90                         visiblespaceCB->setEnabled(false);
91                         visiblespaceCB->setChecked(false);
92                         previewCB->setEnabled(false);
93                         previewCB->setChecked(false);
94                         break;
95                 //case Input
96                 case 1:
97                         visiblespaceCB->setEnabled(false);
98                         visiblespaceCB->setChecked(false);
99                         previewCB->setEnabled(true);
100                         break;
101                 //case Verbatim
102                 default:
103                         visiblespaceCB->setEnabled(true);
104                         previewCB->setEnabled(false);
105                         previewCB->setChecked(false);
106                         break;
107         }
108 }
109
110
111 void QIncludeDialog::loadClicked()
112 {
113         form_->load();
114 }
115
116
117 void QIncludeDialog::browseClicked()
118 {
119         form_->browse();
120 }
121
122
123 /////////////////////////////////////////////////////////////////////
124 //
125 // QInclude
126 //
127 /////////////////////////////////////////////////////////////////////
128
129
130 typedef QController<ControlInclude, QView<QIncludeDialog> > IncludeBase;
131
132
133 QInclude::QInclude(Dialog & parent)
134         : IncludeBase(parent, _("Child Document"))
135 {}
136
137
138 void QInclude::build_dialog()
139 {
140         dialog_.reset(new QIncludeDialog(this));
141
142         bcview().setOK(dialog_->okPB);
143         bcview().setCancel(dialog_->closePB);
144         bcview().addReadOnly(dialog_->filenameED);
145         bcview().addReadOnly(dialog_->browsePB);
146         bcview().addReadOnly(dialog_->visiblespaceCB);
147         bcview().addReadOnly(dialog_->typeCO);
148
149         addCheckedLineEdit(bcview(), dialog_->filenameED, dialog_->filenameLA);
150 }
151
152
153 void QInclude::update_contents()
154 {
155         PathValidator * path_validator = getPathValidator(dialog_->filenameED);
156         if (path_validator)
157                 path_validator->setChecker(kernel().docType(), lyxrc);
158
159         InsetCommandParams const & params = controller().params();
160
161         dialog_->filenameED->setText(toqstr(params["filename"]));
162
163         dialog_->visiblespaceCB->setChecked(false);
164         dialog_->visiblespaceCB->setEnabled(false);
165         dialog_->previewCB->setChecked(false);
166         dialog_->previewCB->setEnabled(false);
167
168         string cmdname = controller().params().getCmdName();
169         if (cmdname != "include" &&
170             cmdname != "verbatiminput" &&
171             cmdname != "verbatiminput*")
172                 cmdname = "input";
173
174         if (cmdname == "include") {
175                 dialog_->typeCO->setCurrentIndex(0);
176
177         } else if (cmdname == "input") {
178                 dialog_->typeCO->setCurrentIndex(1);
179                 dialog_->previewCB->setEnabled(true);
180                 dialog_->previewCB->setChecked(params.preview());
181
182         } else if (cmdname == "verbatiminput*") {
183                 dialog_->typeCO->setCurrentIndex(2);
184                 dialog_->visiblespaceCB->setEnabled(true);
185                 dialog_->visiblespaceCB->setChecked(true);
186
187         } else if (cmdname == "verbatiminput") {
188                 dialog_->typeCO->setCurrentIndex(2);
189                 dialog_->visiblespaceCB->setEnabled(true);
190         }
191 }
192
193
194 void QInclude::apply()
195 {
196         InsetCommandParams params = controller().params();
197
198         params["filename"] = from_utf8(internal_path(fromqstr(dialog_->filenameED->text())));
199         params.preview(dialog_->previewCB->isChecked());
200
201         int const item = dialog_->typeCO->currentIndex();
202         if (item == 0)
203                 params.setCmdName("include");
204         else if (item == 1)
205                 params.setCmdName("input");
206         else {
207                 if (dialog_->visiblespaceCB->isChecked())
208                         params.setCmdName("verbatiminput*");
209                 else
210                         params.setCmdName("verbatiminput");
211         }
212         controller().setParams(params);
213 }
214
215
216 void QInclude::browse()
217 {
218         ControlInclude::Type type;
219
220         int const item = dialog_->typeCO->currentIndex();
221         if (item == 0)
222                 type = ControlInclude::INCLUDE;
223         else if (item == 1)
224                 type = ControlInclude::INPUT;
225         else
226                 type = ControlInclude::VERBATIM;
227
228         docstring const & name =
229                 controller().browse(qstring_to_ucs4(dialog_->filenameED->text()), type);
230         if (!name.empty())
231                 dialog_->filenameED->setText(toqstr(name));
232 }
233
234
235 void QInclude::load()
236 {
237         if (isValid()) {
238                 string const file = fromqstr(dialog_->filenameED->text());
239                 slotOK();
240                 controller().load(file);
241         }
242 }
243
244
245 bool QInclude::isValid()
246 {
247         return !dialog_->filenameED->text().isEmpty();
248 }
249
250 } // namespace frontend
251 } // namespace lyx
252
253 #include "QInclude_moc.cpp"