]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QExternal.C
Joao latest bits
[lyx.git] / src / frontends / qt2 / QExternal.C
1 /**
2  * \file QExternal.C
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 "debug.h"
14 #include "ControlExternal.h"
15 #include "qt_helpers.h"
16
17 #include "insets/ExternalTemplate.h"
18 #include "insets/insetexternal.h"
19
20 #include "support/lstrings.h"
21 #include "support/tostr.h"
22
23 #include <qlineedit.h>
24 #include <qpushbutton.h>
25 #include <qcheckbox.h>
26 #include <qcombobox.h>
27 #include <qtextview.h>
28
29 #include "QExternalDialog.h"
30 #include "QExternal.h"
31 #include "Qt2BC.h"
32
33 using lyx::support::strToInt;
34 using lyx::support::trim;
35
36 using std::string;
37
38
39 typedef QController<ControlExternal, QView<QExternalDialog> > base_class;
40
41 QExternal::QExternal(Dialog & parent)
42         : base_class(parent, _("LyX: External Material"))
43 {
44 }
45
46
47 void QExternal::build_dialog()
48 {
49         dialog_.reset(new QExternalDialog(this));
50
51         bcview().setOK(dialog_->okPB);
52         bcview().setApply(dialog_->applyPB);
53         bcview().setCancel(dialog_->closePB);
54         bcview().addReadOnly(dialog_->externalCO);
55         bcview().addReadOnly(dialog_->fileED);
56         bcview().addReadOnly(dialog_->browsePB);
57         bcview().addReadOnly(dialog_->extraED);
58
59         std::vector<string> templates(controller().getTemplates());
60
61         for (std::vector<string>::const_iterator cit = templates.begin();
62                 cit != templates.end(); ++cit) {
63                 dialog_->externalCO->insertItem(toqstr(*cit), -1);
64         }
65 }
66
67
68 void QExternal::update_contents()
69 {
70         InsetExternalParams const & params = controller().params();
71
72         string const name =
73                 params.filename.outputFilename(kernel().bufferFilepath());
74         dialog_->fileED->setText(toqstr(name));
75
76         dialog_->externalCO->setCurrentItem(controller()
77                                             .getTemplateNumber(params.templatename()));
78         updateTemplate();
79
80         int item = 0;
81         switch (params.display) {
82                 case lyx::external::DefaultDisplay: item = 0; break;
83                 case lyx::external::MonochromeDisplay: item = 1; break;
84                 case lyx::external::GrayscaleDisplay: item = 2; break;
85                 case lyx::external::ColorDisplay: item = 3; break;
86                 case lyx::external::PreviewDisplay: item = 4; break;
87                 case lyx::external::NoDisplay: item = 0; break;
88         }
89         dialog_->showCB->setCurrentItem(item);
90         dialog_->showCB->setEnabled(params.display != lyx::external::NoDisplay &&
91                                     !readOnly());
92         dialog_->displayCB->setChecked(params.display != lyx::external::NoDisplay);
93         dialog_->displayscale->setEnabled(params.display != lyx::external::NoDisplay &&
94                                           !readOnly());
95         dialog_->displayscale->setText(toqstr(tostr(params.lyxscale)));
96
97         isValid();
98 }
99
100
101 void QExternal::updateTemplate()
102 {
103         namespace external = lyx::external;
104
105         dialog_->externalTV->setText(toqstr(helpText()));
106
107         // Ascertain whether the template has any formats supporting
108         // the 'Extra' option
109         QLineEdit * const input = dialog_->extraED;
110         QComboBox * const combo = dialog_->extraFormatCB;
111
112         extra_.clear();
113         input->clear();
114         combo->clear();
115
116         external::Template templ =
117                 controller().getTemplate(dialog_->externalCO->currentItem());
118         external::Template::Formats::const_iterator it  = templ.formats.begin();
119         external::Template::Formats::const_iterator end = templ.formats.end();
120         for (; it != end; ++it) {
121                 if (it->second.option_transformers.find(external::Extra) ==
122                     it->second.option_transformers.end())
123                         continue;
124                 string const format = it->first;
125                 string const opt = controller().params().extradata.get(format);
126                 combo->insertItem(toqstr(format));
127                 extra_[format] = toqstr(opt);
128         }
129
130         bool const enabled = combo->count()  > 0;
131
132         input->setEnabled(enabled && !kernel().isBufferReadonly());
133         combo->setEnabled(enabled);
134
135         if (enabled) {
136                 combo->setCurrentItem(0);
137                 input->setText(extra_[fromqstr(combo->currentText())]);
138         }
139 }
140
141
142 string const QExternal::helpText() const
143 {
144         lyx::external::Template templ =
145                 controller().getTemplate(dialog_->externalCO->currentItem());
146         return templ.helpText;
147 }
148
149
150 void QExternal::apply()
151 {
152         InsetExternalParams params = controller().params();
153
154         params.filename.set(fromqstr(dialog_->fileED->text()),
155                             kernel().bufferFilepath());
156
157         params.settemplate(
158                 controller().getTemplate(dialog_->externalCO->currentItem()).lyxName);
159
160         switch (dialog_->showCB->currentItem()) {
161                 case 0: params.display = lyx::external::DefaultDisplay; break;
162                 case 1: params.display = lyx::external::MonochromeDisplay; break;
163                 case 2: params.display = lyx::external::GrayscaleDisplay; break;
164                 case 3: params.display = lyx::external::ColorDisplay; break;
165                 case 4: params.display = lyx::external::PreviewDisplay; break;
166                 default:;
167         }
168
169         if (!dialog_->displayCB->isChecked())
170                 params.display = lyx::external::NoDisplay;
171
172         params.lyxscale = strToInt(fromqstr(dialog_->displayscale->text()));
173
174         std::map<string, QString>::const_iterator it  = extra_.begin();
175         std::map<string, QString>::const_iterator end = extra_.end();
176         for (; it != end; ++it)
177                 params.extradata.set(it->first, trim(fromqstr(it->second)));
178
179         controller().setParams(params);
180 }