]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrint.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / GuiPrint.cpp
1 /**
2  * \file GuiPrint.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Edwin Leuven
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiPrint.h"
16
17 #include "qt_helpers.h"
18 #include "PrinterParams.h"
19
20 #include "Buffer.h"
21 #include "BufferParams.h"
22 #include "FuncRequest.h"
23 #include "support/gettext.h"
24
25 #include "support/convert.h"
26 #include "support/FileFilterList.h"
27 #include "support/filetools.h"
28 #include "support/os.h"
29
30 #include <QLineEdit>
31 #include <QCheckBox>
32 #include <QRadioButton>
33 #include <QSpinBox>
34 #include <QPushButton>
35
36 using namespace std;
37 using namespace lyx::support;
38
39 namespace lyx {
40 namespace frontend {
41
42
43 GuiPrint::GuiPrint(GuiView & lv)
44         : GuiDialog(lv, "print", qt_("Print Document"))
45 {
46         setupUi(this);
47
48         connect(printPB, SIGNAL(clicked()), this, SLOT(slotOK()));
49         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
50
51         connect(copiesSB, SIGNAL(valueChanged(int)), this, SLOT(copiesChanged(int)));
52         connect(printerED, SIGNAL(textChanged(QString)),
53                 this, SLOT(printerChanged()));
54         connect(fileED, SIGNAL(textChanged(QString)),
55                 this, SLOT(fileChanged() ));
56         connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
57         connect(allRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
58         connect(reverseCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
59         connect(collateCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
60         connect(fromED, SIGNAL(textChanged(const QString&)),
61                 this, SLOT(pagerangeChanged()));
62         connect(fromED, SIGNAL(textChanged(const QString&)),
63                 this, SLOT(change_adaptor()));
64         connect(toED, SIGNAL(textChanged(const QString&)),
65                 this, SLOT(pagerangeChanged()));
66         connect(toED, SIGNAL(textChanged(const QString&)),
67                 this, SLOT(change_adaptor()));
68         connect(fileRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69         connect(printerRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
70         connect(rangeRB, SIGNAL(toggled(bool)), fromED, SLOT(setEnabled(bool)));
71         connect(rangeRB, SIGNAL(toggled(bool)), toED, SLOT(setEnabled(bool)));
72
73         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
74         bc().setOK(printPB);
75         bc().setCancel(closePB);
76 }
77
78
79 void GuiPrint::change_adaptor()
80 {
81         changed();
82 }
83
84
85 void GuiPrint::browseClicked()
86 {
87         docstring name =
88                 browseRelFile(docstring(), from_utf8(buffer().filePath()),
89                              _("Print to file"),
90                              FileFilterList(_("PostScript files (*.ps)")),
91                              true);
92         QString file = toqstr(name);
93         if (!file.isNull()) {
94                 fileED->setText(file);
95                 changed();
96         }
97 }
98
99
100 void GuiPrint::fileChanged()
101 {
102         if (!fileED->text().isEmpty())
103                 fileRB->setChecked(true);
104         changed();
105 }
106
107
108 void GuiPrint::copiesChanged(int i)
109 {
110         collateCB->setEnabled(i != 1);
111         changed();
112 }
113
114
115 void GuiPrint::printerChanged()
116 {
117         printerRB->setChecked(true);
118         changed();
119 }
120
121
122 void GuiPrint::pagerangeChanged()
123 {
124         changed();
125 }
126
127
128 void GuiPrint::updateContents()
129 {
130         // only reset params if a different buffer
131         if (!params_.file_name.empty()
132                         && params_.file_name == fromqstr(fileED->text()))
133                 return;
134
135         printerED->setText(toqstr(params_.printer_name));
136         fileED->setText(toqstr(params_.file_name));
137
138         printerRB->setChecked(true);
139         if (params_.target == PrinterParams::FILE)
140                 fileRB->setChecked(true);
141
142         reverseCB->setChecked(params_.reverse_order);
143
144         copiesSB->setValue(params_.count_copies);
145
146         oddCB->setChecked(params_.odd_pages);
147         evenCB->setChecked(params_.even_pages);
148
149         collateCB->setChecked(params_.sorted_copies);
150
151         if (params_.all_pages) {
152                 allRB->setChecked(true);
153         } else {
154                 rangeRB->setChecked(true);
155                 fromED->setText(QString::number(params_.from_page));
156                 toED->setText(QString::number(params_.to_page));
157         }
158 }
159
160
161 void GuiPrint::applyView()
162 {
163         PrinterParams::Target t = PrinterParams::PRINTER;
164         if (fileRB->isChecked())
165                 t = PrinterParams::FILE;
166
167         params_ = PrinterParams(t,
168                 fromqstr(printerED->text()),
169                 os::internal_path(fromqstr(fileED->text())),
170                 allRB->isChecked(),
171                 fromED->text().toUInt(),
172                 toED->text().toUInt(),
173                 oddCB->isChecked(),
174                 evenCB->isChecked(),
175                 copiesSB->text().toUInt(),
176                 collateCB->isChecked(),
177                 reverseCB->isChecked()
178         );
179 }
180
181
182 bool GuiPrint::initialiseParams(string const &)
183 {
184         /// get global printer parameters
185         string const name = changeExtension(buffer().absFileName(),
186                                         lyxrc.print_file_extension);
187         params_ = PrinterParams(PrinterParams::PRINTER, lyxrc.printer, name);
188
189         setButtonsValid(true); // so that the user can press Ok
190         return true;
191 }
192
193
194 void GuiPrint::clearParams()
195 {
196         params_ = PrinterParams();
197 }
198
199
200 /// print the current buffer
201 void GuiPrint::dispatchParams()
202 {
203         string command = lyxrc.print_command + ' ';
204
205         if (params_.target == PrinterParams::PRINTER
206             && lyxrc.print_adapt_output  // dvips wants a printer name
207             && !params_.printer_name.empty()) {// printer name given
208                 command += lyxrc.print_to_printer + params_.printer_name + ' ';
209         }
210
211         if (!params_.all_pages && params_.from_page) {
212                 command += lyxrc.print_pagerange_flag + ' ';
213                 command += convert<string>(params_.from_page);
214                 if (params_.to_page) {
215                         // we have a range "from-to"
216                         command += '-' + convert<string>(params_.to_page);
217                 }
218                 command += ' ';
219         }
220
221         // If both are, or both are not selected, then skip the odd/even printing
222         if (params_.odd_pages != params_.even_pages) {
223                 if (params_.odd_pages)
224                         command += lyxrc.print_oddpage_flag + ' ';
225                 else if (params_.even_pages)
226                         command += lyxrc.print_evenpage_flag + ' ';
227         }
228
229         if (params_.count_copies > 1) {
230                 if (params_.sorted_copies)
231                         command += lyxrc.print_collcopies_flag;
232                 else
233                         command += lyxrc.print_copies_flag;
234                 command += ' ' + convert<string>(params_.count_copies) + ' ';
235         }
236
237         if (params_.reverse_order)
238                 command += lyxrc.print_reverse_flag + ' ';
239
240         if (!lyxrc.print_extra_options.empty())
241                 command += lyxrc.print_extra_options + ' ';
242
243         command += buffer().params().dvips_options();
244
245         string const target = (params_.target == PrinterParams::PRINTER) ?
246                 "printer" : "file";
247
248         string const target_name = (params_.target == PrinterParams::PRINTER) ?
249                 (params_.printer_name.empty() ? "default" : params_.printer_name) :
250                 params_.file_name;
251
252         string const data = target + " \"" + target_name + "\" \"" + command + '"';
253         dispatch(FuncRequest(getLfun(), data));
254 }
255
256
257 Dialog * createGuiPrint(GuiView & lv) { return new GuiPrint(lv); }
258
259
260 } // namespace frontend
261 } // namespace lyx
262
263 #include "GuiPrint_moc.cpp"