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