3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
17 #include "qt_helpers.h"
18 #include "PrinterParams.h"
21 #include "BufferParams.h"
22 #include "FuncRequest.h"
25 #include "support/convert.h"
26 #include "support/filetools.h"
27 #include "support/os.h"
31 #include <QRadioButton>
33 #include <QPushButton>
36 using namespace lyx::support;
42 GuiPrint::GuiPrint(GuiView & lv)
43 : GuiDialog(lv, "print", qt_("Print Document"))
47 connect(printPB, SIGNAL(clicked()), this, SLOT(slotOK()));
48 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
50 connect(copiesSB, SIGNAL(valueChanged(int)), this, SLOT(copiesChanged(int)));
51 connect(printerED, SIGNAL(textChanged(QString)),
52 this, SLOT(printerChanged()));
53 connect(fileED, SIGNAL(textChanged(QString)),
54 this, SLOT(fileChanged() ));
55 connect(browsePB, SIGNAL(clicked()), this, SLOT(browseClicked()));
56 connect(allRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
57 connect(reverseCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
58 connect(collateCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
59 connect(fromED, SIGNAL(textChanged(QString)),
60 this, SLOT(pagerangeChanged()));
61 connect(fromED, SIGNAL(textChanged(QString)),
62 this, SLOT(change_adaptor()));
63 connect(toED, SIGNAL(textChanged(QString)),
64 this, SLOT(pagerangeChanged()));
65 connect(toED, SIGNAL(textChanged(QString)),
66 this, SLOT(change_adaptor()));
67 connect(fileRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
68 connect(printerRB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
69 connect(rangeRB, SIGNAL(toggled(bool)), fromED, SLOT(setEnabled(bool)));
70 connect(rangeRB, SIGNAL(toggled(bool)), toED, SLOT(setEnabled(bool)));
72 bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
74 bc().setCancel(closePB);
78 void GuiPrint::change_adaptor()
84 void GuiPrint::browseClicked()
87 browseRelFile(QString(), bufferFilepath(), qt_("Print to file"),
88 QStringList(qt_("PostScript files (*.ps)")), true);
89 if (!file.isEmpty()) {
90 fileED->setText(file);
96 void GuiPrint::fileChanged()
98 if (!fileED->text().isEmpty())
99 fileRB->setChecked(true);
104 void GuiPrint::copiesChanged(int i)
106 collateCB->setEnabled(i != 1);
111 void GuiPrint::printerChanged()
113 printerRB->setChecked(true);
118 void GuiPrint::pagerangeChanged()
124 void GuiPrint::updateContents()
126 // only reset params if a different buffer
127 if (!params_.file_name.empty()
128 && params_.file_name == fromqstr(fileED->text()))
131 printerED->setText(toqstr(params_.printer_name));
132 fileED->setText(toqstr(params_.file_name));
134 printerRB->setChecked(true);
135 if (params_.target == PrinterParams::FILE)
136 fileRB->setChecked(true);
138 reverseCB->setChecked(params_.reverse_order);
140 copiesSB->setValue(params_.count_copies);
142 oddCB->setChecked(params_.odd_pages);
143 evenCB->setChecked(params_.even_pages);
145 collateCB->setChecked(params_.sorted_copies);
147 if (params_.all_pages)
148 allRB->setChecked(true);
150 rangeRB->setChecked(true);
151 fromED->setText(QString::number(params_.from_page));
152 toED->setText(QString::number(params_.to_page));
157 void GuiPrint::applyView()
159 params_.target = fileRB->isChecked()
160 ? PrinterParams::FILE : PrinterParams::PRINTER;
161 params_.printer_name = fromqstr(printerED->text());
162 params_.file_name = os::internal_path(fromqstr(fileED->text()));
163 params_.all_pages = allRB->isChecked();
164 params_.from_page = fromED->text().toUInt();
165 params_.to_page = toED->text().toUInt();
166 params_.odd_pages = oddCB->isChecked();
167 params_.even_pages = evenCB->isChecked();
168 params_.count_copies = copiesSB->text().toUInt();
169 params_.sorted_copies = collateCB->isChecked();
170 params_.reverse_order = reverseCB->isChecked();
174 bool GuiPrint::initialiseParams(string const &)
176 /// get global printer parameters
177 params_ = PrinterParams();
178 params_.file_name = support::changeExtension(buffer().absFileName(),
179 lyxrc.print_file_extension);
181 setButtonsValid(true); // so that the user can press Ok
186 void GuiPrint::clearParams()
188 params_ = PrinterParams();
192 /// print the current buffer
193 void GuiPrint::dispatchParams()
195 string command = lyxrc.print_command + ' ';
197 if (params_.target == PrinterParams::PRINTER
198 && lyxrc.print_adapt_output // dvips wants a printer name
199 && !params_.printer_name.empty()) {// printer name given
200 command += lyxrc.print_to_printer + params_.printer_name + ' ';
203 if (!params_.all_pages && params_.from_page) {
204 command += lyxrc.print_pagerange_flag + ' ';
205 command += convert<string>(params_.from_page);
206 if (params_.to_page) {
207 // we have a range "from-to"
208 command += '-' + convert<string>(params_.to_page);
213 // If both are, or both are not selected, then skip the odd/even printing
214 if (params_.odd_pages != params_.even_pages) {
215 if (params_.odd_pages)
216 command += lyxrc.print_oddpage_flag + ' ';
217 else if (params_.even_pages)
218 command += lyxrc.print_evenpage_flag + ' ';
221 if (params_.count_copies > 1) {
222 if (params_.sorted_copies)
223 command += lyxrc.print_collcopies_flag;
225 command += lyxrc.print_copies_flag;
226 command += ' ' + convert<string>(params_.count_copies) + ' ';
229 if (params_.reverse_order)
230 command += lyxrc.print_reverse_flag + ' ';
232 if (!lyxrc.print_extra_options.empty())
233 command += lyxrc.print_extra_options + ' ';
235 command += buffer().params().dvips_options();
237 string const target = (params_.target == PrinterParams::PRINTER) ?
240 string const target_name = (params_.target == PrinterParams::PRINTER) ?
241 (params_.printer_name.empty() ? "default" : params_.printer_name) :
244 string const data = target + " \"" + target_name + "\" \"" + command + '"';
245 dispatch(FuncRequest(getLfun(), data));
249 Dialog * createGuiPrint(GuiView & lv) { return new GuiPrint(lv); }
252 } // namespace frontend
255 #include "moc_GuiPrint.cpp"