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