]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPrint.cpp
* fix spelling in comments to please John.
[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/filetools.h"
27 #include "support/os.h"
28
29 #include <QLineEdit>
30 #include <QCheckBox>
31 #include <QRadioButton>
32 #include <QSpinBox>
33 #include <QPushButton>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace frontend {
40
41
42 GuiPrint::GuiPrint(GuiView & lv)
43         : GuiDialog(lv, "print", qt_("Print Document"))
44 {
45         setupUi(this);
46
47         connect(printPB, SIGNAL(clicked()), this, SLOT(slotOK()));
48         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
49
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)));
71
72         bc().setPolicy(ButtonPolicy::OkApplyCancelPolicy);
73         bc().setOK(printPB);
74         bc().setCancel(closePB);
75 }
76
77
78 void GuiPrint::change_adaptor()
79 {
80         changed();
81 }
82
83
84 void GuiPrint::browseClicked()
85 {
86         QString file =
87                 browseRelFile(QString(), bufferFilepath(), qt_("Print to file"),
88                              QStringList(qt_("PostScript files (*.ps)")), true);
89         if (!file.isEmpty()) {
90                 fileED->setText(file);
91                 changed();
92         }
93 }
94
95
96 void GuiPrint::fileChanged()
97 {
98         if (!fileED->text().isEmpty())
99                 fileRB->setChecked(true);
100         changed();
101 }
102
103
104 void GuiPrint::copiesChanged(int i)
105 {
106         collateCB->setEnabled(i != 1);
107         changed();
108 }
109
110
111 void GuiPrint::printerChanged()
112 {
113         printerRB->setChecked(true);
114         changed();
115 }
116
117
118 void GuiPrint::pagerangeChanged()
119 {
120         changed();
121 }
122
123
124 void GuiPrint::updateContents()
125 {
126         // only reset params if a different buffer
127         if (!params_.file_name.empty()
128                         && params_.file_name == fromqstr(fileED->text()))
129                 return;
130
131         printerED->setText(toqstr(params_.printer_name));
132         fileED->setText(toqstr(params_.file_name));
133
134         printerRB->setChecked(true);
135         if (params_.target == PrinterParams::FILE)
136                 fileRB->setChecked(true);
137
138         reverseCB->setChecked(params_.reverse_order);
139
140         copiesSB->setValue(params_.count_copies);
141
142         oddCB->setChecked(params_.odd_pages);
143         evenCB->setChecked(params_.even_pages);
144
145         collateCB->setChecked(params_.sorted_copies);
146
147         if (params_.all_pages)
148                 allRB->setChecked(true);
149         else {
150                 rangeRB->setChecked(true);
151                 fromED->setText(QString::number(params_.from_page));
152                 toED->setText(QString::number(params_.to_page));
153         }
154 }
155
156
157 void GuiPrint::applyView()
158 {
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();
171 }
172
173
174 bool GuiPrint::initialiseParams(string const &)
175 {
176         /// get global printer parameters
177         params_ = PrinterParams();
178         params_.file_name = support::changeExtension(buffer().absFileName(),
179                                         lyxrc.print_file_extension);
180
181         setButtonsValid(true); // so that the user can press Ok
182         return true;
183 }
184
185
186 void GuiPrint::clearParams()
187 {
188         params_ = PrinterParams();
189 }
190
191
192 /// print the current buffer
193 void GuiPrint::dispatchParams()
194 {
195         string command = lyxrc.print_command + ' ';
196
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 + ' ';
201         }
202
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);
209                 }
210                 command += ' ';
211         }
212
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 + ' ';
219         }
220
221         if (params_.count_copies > 1) {
222                 if (params_.sorted_copies)
223                         command += lyxrc.print_collcopies_flag;
224                 else
225                         command += lyxrc.print_copies_flag;
226                 command += ' ' + convert<string>(params_.count_copies) + ' ';
227         }
228
229         if (params_.reverse_order)
230                 command += lyxrc.print_reverse_flag + ' ';
231
232         if (!lyxrc.print_extra_options.empty())
233                 command += lyxrc.print_extra_options + ' ';
234
235         command += buffer().params().dvips_options();
236
237         string const target = (params_.target == PrinterParams::PRINTER) ?
238                 "printer" : "file";
239
240         string const target_name = (params_.target == PrinterParams::PRINTER) ?
241                 (params_.printer_name.empty() ? "default" : params_.printer_name) :
242                 params_.file_name;
243
244         string const data = target + " \"" + target_name + "\" \"" + command + '"';
245         dispatch(FuncRequest(getLfun(), data));
246 }
247
248
249 Dialog * createGuiPrint(GuiView & lv) { return new GuiPrint(lv); }
250
251
252 } // namespace frontend
253 } // namespace lyx
254
255 #include "moc_GuiPrint.cpp"