]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSendto.cpp
* fix spelling in comments to please John.
[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 "Format.h"
19 #include "FuncRequest.h"
20
21 #include "support/qstring_helpers.h"
22 #include "support/filetools.h"
23
24 #include <QLineEdit>
25 #include <QListWidget>
26 #include <QPushButton>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32 namespace frontend {
33
34
35 GuiSendTo::GuiSendTo(GuiView & lv)
36         : GuiDialog(lv, "sendto", qt_("Send Document to Command"))
37 {
38         setupUi(this);
39
40         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
41         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
42         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
43
44         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
45                 this, SLOT(slotFormatHighlighted(QListWidgetItem *)));
46         connect(formatLW, SIGNAL(itemActivated(QListWidgetItem *)),
47                 this, SLOT(slotFormatSelected(QListWidgetItem *)));
48         connect(formatLW, SIGNAL(itemClicked(QListWidgetItem *)),
49                 this, SLOT(changed_adaptor()));
50         connect(formatLW, SIGNAL(itemSelectionChanged()),
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::updateContents()
69 {
70         all_formats_ = buffer().exportableFormats(false);
71
72         // Save the current selection if any
73         Format const * current_format = 0;
74         int const line = formatLW->currentRow();
75         if (line >= 0 && line <= formatLW->count()
76             && formatLW->selectedItems().size() > 0)
77                 current_format = all_formats_[line];
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
88         int current_line = -1;
89         for (int ln = 0; it != end; ++it, ++result, ++ln) {
90                 *result = (*it)->prettyname();
91                 if (current_format 
92                     && (*it)->prettyname() == current_format->prettyname())
93                         current_line = ln;
94         }
95
96         // Reload the browser
97         formatLW->clear();
98
99         for (vector<string>::const_iterator it = keys.begin();
100              it != keys.end(); ++it) {
101                 formatLW->addItem(qt_(*it));
102         }
103
104         // Restore the selection
105         if (current_line > -1)
106                 formatLW->setCurrentItem(formatLW->item(current_line));
107 }
108
109
110 void GuiSendTo::applyView()
111 {
112         int const line = formatLW->currentRow();
113         QString const command = commandCO->currentText().trimmed();
114
115         if (commandCO->findText(command) == -1)
116                 commandCO->insertItem(0, command);
117
118         if (line < 0 || line > formatLW->count())
119                 return;
120
121         format_ = all_formats_[line];
122         command_ = command;
123 }
124
125
126 bool GuiSendTo::isValid()
127 {
128         int const line = formatLW->currentRow();
129
130         if (line < 0 || line > int(formatLW->count()))
131                 return false;
132
133         return (formatLW->selectedItems().size() > 0
134                 && formatLW->count() != 0
135                 && !commandCO->currentText().isEmpty());
136 }
137
138
139 bool GuiSendTo::initialiseParams(string const &)
140 {
141         format_ = 0;
142         paramsToDialog(format_, command_);
143         return true;
144 }
145
146
147 void GuiSendTo::paramsToDialog(Format const * /*format*/, QString const & command)
148 {
149         if (!command.isEmpty())
150                 commandCO->addItem(command);
151
152         bc().setValid(isValid());
153 }
154
155
156 void GuiSendTo::dispatchParams()
157 {
158         if (command_.isEmpty() || !format_ || format_->name().empty())
159                 return;
160
161         string const data = format_->name() + " " + fromqstr(command_);
162         dispatch(FuncRequest(getLfun(), data));
163 }
164
165 Dialog * createGuiSendTo(GuiView & lv) { return new GuiSendTo(lv); }
166
167
168 } // namespace frontend
169 } // namespace lyx
170
171 #include "moc_GuiSendto.cpp"