]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSendto.cpp
0afd45723dbc80f010847a07b896d6892a0124b4
[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
16 #include "Buffer.h"
17 #include "Converter.h"
18 #include "Format.h"
19 #include "FuncRequest.h"
20 #include "LyXRC.h"
21 #include "qt_helpers.h"
22
23 #include "support/filetools.h"
24 #include "support/lstrings.h"
25
26 #include <QListWidget>
27 #include <QPushButton>
28 #include <QCloseEvent>
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(commandCO, SIGNAL(textChanged(QString)),
53                 this, SLOT(changed_adaptor()));
54
55         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
56         bc().setOK(okPB);
57         bc().setApply(applyPB);
58         bc().setCancel(closePB);
59 }
60
61
62 void GuiSendTo::changed_adaptor()
63 {
64         changed();
65 }
66
67
68 void GuiSendTo::updateContents()
69 {
70         all_formats_ = allFormats();
71
72         // Check whether the current contents of the browser will be
73         // changed by loading the contents of formats
74         vector<string> keys;
75         keys.resize(all_formats_.size());
76
77         vector<string>::iterator result = keys.begin();
78         vector<Format const *>::const_iterator it  = all_formats_.begin();
79         vector<Format const *>::const_iterator end = all_formats_.end();
80         for (; it != end; ++it, ++result)
81                 *result = (*it)->prettyname();
82
83         // Reload the browser
84         formatLW->clear();
85
86         for (vector<string>::const_iterator it = keys.begin();
87              it != keys.end(); ++it) {
88                 formatLW->addItem(toqstr(*it));
89         }
90
91         commandCO->addItem(toqstr(command_));
92 }
93
94
95 void GuiSendTo::applyView()
96 {
97         int const line = formatLW->currentRow();
98
99         if (line < 0 || line > int(formatLW->count()))
100                 return;
101
102         format_ = all_formats_[line];
103         command_ = trim(fromqstr(commandCO->currentText()));
104 }
105
106
107 bool GuiSendTo::isValid()
108 {
109         int const line = formatLW->currentRow();
110
111         if (line < 0 || line > int(formatLW->count()))
112                 return false;
113
114         return formatLW->count() != 0 &&
115                 !commandCO->currentText().isEmpty();
116 }
117
118
119 bool GuiSendTo::initialiseParams(string const &)
120 {
121         format_ = 0;
122         command_ = lyxrc.custom_export_command;
123         return true;
124 }
125
126
127 void GuiSendTo::dispatchParams()
128 {
129         if (command_.empty() || !format_ || format_->name().empty())
130                 return;
131
132         string const data = format_->name() + " " + command_;
133         dispatch(FuncRequest(getLfun(), data));
134 }
135
136 // FIXME: Move to Converters?
137 vector<Format const *> GuiSendTo::allFormats() const
138 {
139         // What formats can we output natively?
140         vector<string> exports;
141         exports.push_back("lyx");
142         exports.push_back("text");
143
144         if (buffer().isLatex()) {
145                 exports.push_back("latex");
146                 exports.push_back("pdflatex");
147         }
148         else if (buffer().isDocBook())
149                 exports.push_back("docbook");
150         else if (buffer().isLiterate())
151                 exports.push_back("literate");
152
153         // Loop over these native formats and ascertain what formats we
154         // can convert to
155         vector<Format const *> to;
156
157         vector<string>::const_iterator ex_it  = exports.begin();
158         vector<string>::const_iterator ex_end = exports.end();
159         for (; ex_it != ex_end; ++ex_it) {
160                 // Start off with the native export format.
161                 // "formats" is LyX's list of recognised formats
162                 to.push_back(formats.getFormat(*ex_it));
163
164                 Formats::const_iterator fo_it  = formats.begin();
165                 Formats::const_iterator fo_end = formats.end();
166                 for (; fo_it != fo_end; ++fo_it) {
167                         // we need to hide the default graphic export formats
168                         // from the external menu, because we need them only
169                         // for the internal lyx-view and external latex run
170                         string const name = fo_it->name();
171                         if (name != "eps" && name != "xpm" && name != "png" &&
172                             theConverters().isReachable(*ex_it, name))
173                                 to.push_back(&(*fo_it));
174                 }
175         }
176
177         // Remove repeated formats.
178         sort(to.begin(), to.end());
179         to.erase(unique(to.begin(), to.end()), to.end());
180
181         return to;
182 }
183
184
185 Dialog * createGuiSendTo(GuiView & lv) { return new GuiSendTo(lv); }
186
187
188 } // namespace frontend
189 } // namespace lyx
190
191 #include "GuiSendto_moc.cpp"