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