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