]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrint.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 "LyXRC.h"
24
25 #include "support/convert.h"
26 #include "support/FileFilterList.h"
27 #include "support/filetools.h"
28 #include "support/gettext.h"
29 #include "support/os.h"
30
31 #include <QLineEdit>
32 #include <QCheckBox>
33 #include <QRadioButton>
34 #include <QSpinBox>
35 #include <QPushButton>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41 namespace frontend {
42
43
44 GuiPrint::GuiPrint(GuiView & lv)
45         : GuiDialog(lv, "print", qt_("Print Document"))
46 {
47         setupUi(this);
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         QString file =
89                 browseRelFile(QString(), bufferFilepath(), qt_("Print to file"),
90                              FileFilterList(_("PostScript files (*.ps)")), true);
91         if (!file.isEmpty()) {
92                 fileED->setText(file);
93                 changed();
94         }
95 }
96
97
98 void GuiPrint::fileChanged()
99 {
100         if (!fileED->text().isEmpty())
101                 fileRB->setChecked(true);
102         changed();
103 }
104
105
106 void GuiPrint::copiesChanged(int i)
107 {
108         collateCB->setEnabled(i != 1);
109         changed();
110 }
111
112
113 void GuiPrint::printerChanged()
114 {
115         printerRB->setChecked(true);
116         changed();
117 }
118
119
120 void GuiPrint::pagerangeChanged()
121 {
122         changed();
123 }
124
125
126 void GuiPrint::updateContents()
127 {
128         // only reset params if a different buffer
129         if (!params_.file_name.empty()
130                         && params_.file_name == fromqstr(fileED->text()))
131                 return;
132
133         printerED->setText(toqstr(params_.printer_name));
134         fileED->setText(toqstr(params_.file_name));
135
136         printerRB->setChecked(true);
137         if (params_.target == PrinterParams::FILE)
138                 fileRB->setChecked(true);
139
140         reverseCB->setChecked(params_.reverse_order);
141
142         copiesSB->setValue(params_.count_copies);
143
144         oddCB->setChecked(params_.odd_pages);
145         evenCB->setChecked(params_.even_pages);
146
147         collateCB->setChecked(params_.sorted_copies);
148
149         if (params_.all_pages) {
150                 allRB->setChecked(true);
151         } else {
152                 rangeRB->setChecked(true);
153                 fromED->setText(QString::number(params_.from_page));
154                 toED->setText(QString::number(params_.to_page));
155         }
156 }
157
158
159 void GuiPrint::applyView()
160 {
161         params_.target        = fileRB->isChecked()
162                 ?  PrinterParams::FILE : PrinterParams::PRINTER;
163         params_.printer_name  = fromqstr(printerED->text());
164         params_.file_name     = os::internal_path(fromqstr(fileED->text()));
165         params_.all_pages     = allRB->isChecked();
166         params_.from_page     = fromED->text().toUInt();
167         params_.to_page       = toED->text().toUInt();
168         params_.odd_pages     = oddCB->isChecked();
169         params_.even_pages    = evenCB->isChecked();
170         params_.count_copies  = copiesSB->text().toUInt();
171         params_.sorted_copies = collateCB->isChecked();
172         params_.reverse_order = reverseCB->isChecked();
173 }
174
175
176 bool GuiPrint::initialiseParams(string const &)
177 {
178         /// get global printer parameters
179         string const name = support::changeExtension(buffer().absFileName(),
180                                         lyxrc.print_file_extension);
181         params_ = PrinterParams();
182         params_.file_name = name;
183
184         setButtonsValid(true); // so that the user can press Ok
185         return true;
186 }
187
188
189 void GuiPrint::clearParams()
190 {
191         params_ = PrinterParams();
192 }
193
194
195 /// print the current buffer
196 void GuiPrint::dispatchParams()
197 {
198         string command = lyxrc.print_command + ' ';
199
200         if (params_.target == PrinterParams::PRINTER
201             && lyxrc.print_adapt_output  // dvips wants a printer name
202             && !params_.printer_name.empty()) {// printer name given
203                 command += lyxrc.print_to_printer + params_.printer_name + ' ';
204         }
205
206         if (!params_.all_pages && params_.from_page) {
207                 command += lyxrc.print_pagerange_flag + ' ';
208                 command += convert<string>(params_.from_page);
209                 if (params_.to_page) {
210                         // we have a range "from-to"
211                         command += '-' + convert<string>(params_.to_page);
212                 }
213                 command += ' ';
214         }
215
216         // If both are, or both are not selected, then skip the odd/even printing
217         if (params_.odd_pages != params_.even_pages) {
218                 if (params_.odd_pages)
219                         command += lyxrc.print_oddpage_flag + ' ';
220                 else if (params_.even_pages)
221                         command += lyxrc.print_evenpage_flag + ' ';
222         }
223
224         if (params_.count_copies > 1) {
225                 if (params_.sorted_copies)
226                         command += lyxrc.print_collcopies_flag;
227                 else
228                         command += lyxrc.print_copies_flag;
229                 command += ' ' + convert<string>(params_.count_copies) + ' ';
230         }
231
232         if (params_.reverse_order)
233                 command += lyxrc.print_reverse_flag + ' ';
234
235         if (!lyxrc.print_extra_options.empty())
236                 command += lyxrc.print_extra_options + ' ';
237
238         command += buffer().params().dvips_options();
239
240         string const target = (params_.target == PrinterParams::PRINTER) ?
241                 "printer" : "file";
242
243         string const target_name = (params_.target == PrinterParams::PRINTER) ?
244                 (params_.printer_name.empty() ? "default" : params_.printer_name) :
245                 params_.file_name;
246
247         string const data = target + " \"" + target_name + "\" \"" + command + '"';
248         dispatch(FuncRequest(getLfun(), data));
249 }
250
251
252 Dialog * createGuiPrint(GuiView & lv) { return new GuiPrint(lv); }
253
254
255 } // namespace frontend
256 } // namespace lyx
257
258 #include "GuiPrint_moc.cpp"