]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSendto.cpp
fix memory leaks
[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(GuiView & lv)
40         : GuiDialog(lv, "sendto")
41 {
42         setupUi(this);
43         setViewTitle(_("Send Document to Command"));
44
45         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
46         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
47         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
48
49         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
50                 this, SLOT(slotFormatHighlighted(QListWidgetItem *)));
51         connect(formatLW, SIGNAL(itemActivated(QListWidgetItem *)),
52                 this, SLOT(slotFormatSelected(QListWidgetItem *)));
53         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
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::closeEvent(QCloseEvent * e)
72 {
73         slotClose();
74         e->accept();
75 }
76
77
78 void GuiSendTo::updateContents()
79 {
80         all_formats_ = allFormats();
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         for (; it != end; ++it, ++result)
91                 *result = (*it)->prettyname();
92
93         // Reload the browser
94         formatLW->clear();
95
96         for (vector<string>::const_iterator it = keys.begin();
97              it != keys.end(); ++it) {
98                 formatLW->addItem(toqstr(*it));
99         }
100
101         commandCO->addItem(toqstr(command_));
102 }
103
104
105 void GuiSendTo::applyView()
106 {
107         int const line = formatLW->currentRow();
108
109         if (line < 0 || line > int(formatLW->count()))
110                 return;
111
112         format_ = all_formats_[line];
113         command_ = trim(fromqstr(commandCO->currentText()));
114 }
115
116
117 bool GuiSendTo::isValid()
118 {
119         int const line = formatLW->currentRow();
120
121         if (line < 0 || line > int(formatLW->count()))
122                 return false;
123
124         return formatLW->count() != 0 &&
125                 !commandCO->currentText().isEmpty();
126 }
127
128
129 bool GuiSendTo::initialiseParams(std::string const &)
130 {
131         format_ = 0;
132         command_ = lyxrc.custom_export_command;
133         return true;
134 }
135
136
137 void GuiSendTo::dispatchParams()
138 {
139         if (command_.empty() || !format_ || format_->name().empty())
140                 return;
141
142         string const data = format_->name() + " " + command_;
143         dispatch(FuncRequest(getLfun(), data));
144 }
145
146 // FIXME: Move to Converters?
147 vector<Format const *> GuiSendTo::allFormats() const
148 {
149         // What formats can we output natively?
150         vector<string> exports;
151         exports.push_back("lyx");
152         exports.push_back("text");
153
154         if (buffer().isLatex())
155                 exports.push_back("latex");
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         std::sort(to.begin(), to.end());
187         to.erase(std::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"