]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSendto.cpp
Fix bug #7714 by sorting the formats.
[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 #include "qt_helpers.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "Format.h"
20 #include "FuncRequest.h"
21
22 #include "support/qstring_helpers.h"
23 #include "support/filetools.h"
24
25 #include <algorithm>
26
27 #include <QLineEdit>
28 #include <QListWidget>
29 #include <QPushButton>
30
31 using namespace std;
32 using namespace lyx::support;
33
34 namespace lyx {
35 namespace frontend {
36
37
38 GuiSendTo::GuiSendTo(GuiView & lv)
39         : GuiDialog(lv, "sendto", qt_("Export or Send Document"))
40 {
41         setupUi(this);
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(formatLW, SIGNAL(itemSelectionChanged()),
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 namespace {
71 bool formatSorter(Format const * lhs, Format const * rhs) {
72         return lhs->prettyname() < rhs->prettyname();
73 }
74 } // end namespace
75
76 void GuiSendTo::updateContents()
77 {
78         all_formats_ = buffer().params().exportableFormats(false);
79         
80         sort(all_formats_.begin(), all_formats_.end(), formatSorter);
81
82         // Save the current selection if any
83         Format const * current_format = 0;
84         int const line = formatLW->currentRow();
85         if (line >= 0 && line <= formatLW->count()
86             && formatLW->selectedItems().size() > 0)
87                 current_format = all_formats_[line];
88
89         // Check whether the current contents of the browser will be
90         // changed by loading the contents of formats
91         vector<string> keys;
92         keys.resize(all_formats_.size());
93
94         vector<string>::iterator result = keys.begin();
95         vector<Format const *>::const_iterator it  = all_formats_.begin();
96         vector<Format const *>::const_iterator end = all_formats_.end();
97
98         int current_line = -1;
99         for (int ln = 0; it != end; ++it, ++result, ++ln) {
100                 *result = (*it)->prettyname();
101                 if (current_format 
102                     && (*it)->prettyname() == current_format->prettyname())
103                         current_line = ln;
104         }
105
106         // Reload the browser
107         formatLW->clear();
108
109         for (vector<string>::const_iterator it = keys.begin();
110              it != keys.end(); ++it) {
111                 formatLW->addItem(qt_(*it));
112         }
113
114         // Restore the selection
115         if (current_line > -1)
116                 formatLW->setCurrentItem(formatLW->item(current_line));
117 }
118
119
120 void GuiSendTo::applyView()
121 {
122         int const line = formatLW->currentRow();
123         QString const command = commandCO->currentText().trimmed();
124
125         if (commandCO->findText(command) == -1)
126                 commandCO->insertItem(0, command);
127
128         if (line < 0 || line > formatLW->count())
129                 return;
130
131         format_ = all_formats_[line];
132         command_ = command;
133 }
134
135
136 bool GuiSendTo::isValid()
137 {
138         int const line = formatLW->currentRow();
139
140         if (line < 0 || line > int(formatLW->count()))
141                 return false;
142
143         return (formatLW->selectedItems().size() > 0
144                 && formatLW->count() != 0);
145 }
146
147
148 bool GuiSendTo::initialiseParams(string const &)
149 {
150         format_ = 0;
151         paramsToDialog(format_, command_);
152         return true;
153 }
154
155
156 void GuiSendTo::paramsToDialog(Format const * /*format*/, QString const & command)
157 {
158         if (!command.isEmpty())
159                 commandCO->addItem(command);
160
161         bc().setValid(isValid());
162 }
163
164
165 void GuiSendTo::dispatchParams()
166 {
167         if (!format_ || format_->name().empty())
168                 return;
169
170         string data = format_->name();
171         if (!command_.isEmpty())
172                 data += " " + fromqstr(command_);
173
174         FuncCode const lfun = command_.isEmpty() ?
175                 LFUN_BUFFER_EXPORT : getLfun();
176
177         dispatch(FuncRequest(lfun, data));
178 }
179
180 Dialog * createGuiSendTo(GuiView & lv) { return new GuiSendTo(lv); }
181
182
183 } // namespace frontend
184 } // namespace lyx
185
186 #include "moc_GuiSendto.cpp"