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