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