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