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