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