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