]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSendto.cpp
start XeTeX support.
[lyx.git] / src / frontends / qt4 / GuiSendto.cpp
1 /**
2  * \file GuiSendto.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiSendto.h"
15 #include "qt_helpers.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Converter.h"
20 #include "Format.h"
21 #include "FuncRequest.h"
22 #include "LyXRC.h"
23
24 #include "support/qstring_helpers.h"
25 #include "support/filetools.h"
26
27 #include <QLineEdit>
28 #include <QListWidget>
29 #include <QPushButton>
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35 namespace frontend {
36
37
38 GuiSendTo::GuiSendTo(GuiView & lv)
39         : GuiDialog(lv, "sendto", qt_("Send Document to Command"))
40 {
41         setupUi(this);
42
43         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
44         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
45         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
46
47         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
48                 this, SLOT(slotFormatHighlighted(QListWidgetItem *)));
49         connect(formatLW, SIGNAL(itemActivated(QListWidgetItem *)),
50                 this, SLOT(slotFormatSelected(QListWidgetItem *)));
51         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
52                 this, SLOT(changed_adaptor()));
53         connect(formatLW, SIGNAL(itemSelectionChanged()),
54                 this, SLOT(changed_adaptor()));
55         connect(commandCO, SIGNAL(textChanged(QString)),
56                 this, SLOT(changed_adaptor()));
57
58         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
59         bc().setOK(okPB);
60         bc().setApply(applyPB);
61         bc().setCancel(closePB);
62 }
63
64
65 void GuiSendTo::changed_adaptor()
66 {
67         changed();
68 }
69
70
71 void GuiSendTo::updateContents()
72 {
73         all_formats_ = allFormats();
74
75         // Save the current selection if any
76         Format const * current_format = 0;
77         int const line = formatLW->currentRow();
78         if (line >= 0 && line <= formatLW->count()
79             && formatLW->selectedItems().size() > 0)
80                 current_format = all_formats_[line];
81
82         // Check whether the current contents of the browser will be
83         // changed by loading the contents of formats
84         vector<string> keys;
85         keys.resize(all_formats_.size());
86
87         vector<string>::iterator result = keys.begin();
88         vector<Format const *>::const_iterator it  = all_formats_.begin();
89         vector<Format const *>::const_iterator end = all_formats_.end();
90
91         int current_line = -1;
92         for (int ln = 0; it != end; ++it, ++result, ++ln) {
93                 *result = (*it)->prettyname();
94                 if (current_format 
95                     && (*it)->prettyname() == current_format->prettyname())
96                         current_line = ln;
97         }
98
99         // Reload the browser
100         formatLW->clear();
101
102         for (vector<string>::const_iterator it = keys.begin();
103              it != keys.end(); ++it) {
104                 formatLW->addItem(qt_(*it));
105         }
106
107         // Restore the selection
108         if (current_line > -1)
109                 formatLW->setCurrentItem(formatLW->item(current_line));
110 }
111
112
113 void GuiSendTo::applyView()
114 {
115         int const line = formatLW->currentRow();
116         QString const command = commandCO->currentText().trimmed();
117
118         if (commandCO->findText(command) == -1)
119                 commandCO->insertItem(0, command);
120
121         if (line < 0 || line > formatLW->count())
122                 return;
123
124         format_ = all_formats_[line];
125         command_ = command;
126 }
127
128
129 bool GuiSendTo::isValid()
130 {
131         int const line = formatLW->currentRow();
132
133         if (line < 0 || line > int(formatLW->count()))
134                 return false;
135
136         return (formatLW->selectedItems().size() > 0
137                 && formatLW->count() != 0
138                 && !commandCO->currentText().isEmpty());
139 }
140
141
142 bool GuiSendTo::initialiseParams(string const &)
143 {
144         format_ = 0;
145         command_ = toqstr(lyxrc.custom_export_command);
146         paramsToDialog(format_, command_);
147         return true;
148 }
149
150
151 void GuiSendTo::paramsToDialog(Format const * /*format*/, QString const & command)
152 {
153         if (!command.isEmpty())
154                 commandCO->addItem(command);
155
156         bc().setValid(isValid());
157 }
158
159
160 void GuiSendTo::dispatchParams()
161 {
162         if (command_.isEmpty() || !format_ || format_->name().empty())
163                 return;
164
165         string const data = format_->name() + " " + fromqstr(command_);
166         dispatch(FuncRequest(getLfun(), data));
167 }
168
169 // FIXME: Move to Converters?
170 vector<Format const *> GuiSendTo::allFormats() const
171 {
172         // What formats can we output natively?
173         vector<string> exports;
174         exports.push_back("lyx");
175         exports.push_back("text");
176
177         if (buffer().isLatex()) {
178                 if (buffer().params().useXetex)
179                         exports.push_back("xetex");
180                 else {
181                         exports.push_back("latex");
182                         exports.push_back("pdflatex");
183                 }
184         }
185         else if (buffer().isDocBook())
186                 exports.push_back("docbook");
187         else if (buffer().isLiterate())
188                 exports.push_back("literate");
189
190         // Loop over these native formats and ascertain what formats we
191         // can convert to
192         vector<Format const *> to;
193
194         vector<string>::const_iterator ex_it  = exports.begin();
195         vector<string>::const_iterator ex_end = exports.end();
196         for (; ex_it != ex_end; ++ex_it) {
197                 // Start off with the native export format.
198                 // "formats" is LyX's list of recognised formats
199                 to.push_back(formats.getFormat(*ex_it));
200
201                 Formats::const_iterator fo_it  = formats.begin();
202                 Formats::const_iterator fo_end = formats.end();
203                 for (; fo_it != fo_end; ++fo_it) {
204                         // we need to hide the default graphic export formats
205                         // from the external menu, because we need them only
206                         // for the internal lyx-view and external latex run
207                         string const name = fo_it->name();
208                         if (name != "eps" && name != "xpm" && name != "png" &&
209                             theConverters().isReachable(*ex_it, name))
210                                 to.push_back(&(*fo_it));
211                 }
212         }
213
214         // Remove repeated formats.
215         sort(to.begin(), to.end());
216         to.erase(unique(to.begin(), to.end()), to.end());
217
218         return to;
219 }
220
221
222 Dialog * createGuiSendTo(GuiView & lv) { return new GuiSendTo(lv); }
223
224
225 } // namespace frontend
226 } // namespace lyx
227
228 #include "moc_GuiSendto.cpp"