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