source: trunk/src/gui/dialogs/qprintdialog_qws.cpp@ 575

Last change on this file since 575 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 17.1 KB
RevLine 
[556]1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qplatformdefs.h"
43
44#include <private/qabstractprintdialog_p.h>
45#include "qprintdialog.h"
46
47#ifndef QT_NO_PRINTDIALOG
48
49#include "qapplication.h"
50#include "qbuttongroup.h"
51#include "qradiobutton.h"
52#include "qcombobox.h"
53#include "qspinbox.h"
54#include "qprinter.h"
55#include "qlineedit.h"
56#include "qdir.h"
57#include "qmessagebox.h"
58#include "qinputdialog.h"
59#include "qlayout.h"
60#include "qlabel.h"
61
62#include "qlibrary.h"
63
64#ifndef QT_NO_NIS
65
66#ifndef BOOL_DEFINED
67#define BOOL_DEFINED
68#endif
69
70#include <rpcsvc/ypclnt.h>
71#include <rpcsvc/yp_prot.h>
72
73#endif //QT_NO_NIS
74
75#include <ctype.h>
76#include <stdlib.h>
77
78#include <qdebug.h>
79
80QT_BEGIN_NAMESPACE
81
82typedef void (*QPrintDialogCreator)(QPrintDialog *parent);
83Q_GUI_EXPORT QPrintDialogCreator _qt_print_dialog_creator;
84
85class QPrintDialogPrivate : public QAbstractPrintDialogPrivate
86{
87 Q_DECLARE_PUBLIC(QPrintDialog)
88public:
89 QButtonGroup *printerOrFile;
90 bool outputToFile;
91 QRadioButton *printToPrinterButton;
92 QRadioButton *printToFileButton;
93 QLineEdit *fileName;
94
95 QButtonGroup *colorMode;
96 QRadioButton *printColor;
97 QRadioButton *printGray;
98 QPrinter::ColorMode colorMode2;
99
100 QComboBox *orientationCombo, *sizeCombo;
101 QPrinter::PaperSize pageSize;
102 QPrinter::Orientation orientation;
103
104 QSpinBox *copies;
105 int numCopies;
106 QPrinter::PaperSize indexToPaperSize[QPrinter::NPaperSize];
107
108 QComboBox *rangeCombo;
109 QSpinBox *firstPage;
110 QSpinBox *lastPage;
111
112 QComboBox *pageOrderCombo;
113 QPrinter::PageOrder pageOrder2;
114
115 QString faxNum;
116
117 void init();
118
119 void _q_okClicked();
120 void _q_printerOrFileSelected(QAbstractButton *b);
121 void _q_paperSizeSelected(int);
122 void _q_orientSelected(int);
123 void _q_pageOrderSelected(int);
124 void _q_colorModeSelected(QAbstractButton *);
125 void _q_setNumCopies(int);
126 void _q_printRangeSelected(int);
127 void _q_setFirstPage(int);
128 void _q_setLastPage(int);
129 void _q_fileNameEditChanged(const QString &text);
130
131 void setupDestination();
132 void setupPrinterSettings();
133 void setupPaper();
134 void setupOptions();
135
136 void setPrinter(QPrinter *p, bool pickUpSettings);
137};
138
139static void isc(QPrintDialogPrivate *d, const QString & text,
140 QPrinter::PaperSize ps);
141
142void QPrintDialogPrivate::_q_okClicked()
143{
144 Q_Q(QPrintDialog);
145#ifndef QT_NO_MESSAGEBOX
146 if (outputToFile && fileName->isModified() && QFileInfo(fileName->text()).exists()) {
147 int confirm = QMessageBox::warning(
148 q, QPrintDialog::tr("File exists"),
149 QPrintDialog::tr("<qt>Do you want to overwrite it?</qt>"),
150 QMessageBox::Yes, QMessageBox::No);
151 if (confirm == QMessageBox::No)
152 return;
153 }
154#endif // QT_NO_MESSAGEBOX
155
156 lastPage->interpretText();
157 firstPage->interpretText();
158 copies->interpretText();
159 if (outputToFile) {
160 printer->setOutputFileName(fileName->text());
161 }
162 printer->setOrientation(orientation);
163 printer->setPaperSize(pageSize);
164 printer->setPageOrder(pageOrder2);
165 printer->setColorMode(colorMode2);
166 printer->setNumCopies(numCopies);
167
168 switch ((rangeCombo->itemData(rangeCombo->currentIndex())).toInt()){
169 case (int)QPrintDialog::AllPages:
170 q->setPrintRange(QPrintDialog::AllPages);
171 q->setFromTo(0, 0);
172 break;
173 case (int)QPrintDialog::Selection:
174 q->setPrintRange(QPrintDialog::Selection);
175 q->setFromTo(0, 0);
176 break;
177 case (int)QPrintDialog::PageRange:
178 q->setPrintRange(QPrintDialog::PageRange);
179 q->setFromTo(firstPage->value(), lastPage->value());
180 break;
181 }
182 q->accept();
183}
184
185void QPrintDialogPrivate::_q_printerOrFileSelected(QAbstractButton *b)
186{
187 outputToFile = (b == printToFileButton);
188 if (outputToFile) {
189 _q_fileNameEditChanged(fileName->text());
190 if (!fileName->isModified() && fileName->text().isEmpty()) {
191 QString file = "print.tiff";
192 fileName->setText(file);
193 fileName->setCursorPosition(file.length());
194 fileName->selectAll();
195 fileName->setModified(true); // confirm overwrite when OK clicked
196
197 }
198 fileName->setEnabled(true);
199 fileName->setFocus();
200 } else {
201 fileName->setText(QString());
202 if (fileName->isEnabled())
203 fileName->setEnabled(false);
204 }
205}
206
207void QPrintDialogPrivate::_q_paperSizeSelected(int id)
208{
209 if (id < QPrinter::NPaperSize)
210 pageSize = QPrinter::PaperSize(indexToPaperSize[id]);
211}
212
213void QPrintDialogPrivate::_q_orientSelected(int id)
214{
215 orientation = (QPrinter::Orientation)id;
216}
217
218void QPrintDialogPrivate::_q_pageOrderSelected(int id)
219{
220 pageOrder2 = (QPrinter::PageOrder)id;
221}
222
223void QPrintDialogPrivate::_q_colorModeSelected(QAbstractButton *b)
224{
225 colorMode2 = (b == printColor) ? QPrinter::Color : QPrinter::GrayScale;
226}
227
228void QPrintDialogPrivate::_q_setNumCopies(int copies)
229{
230 numCopies = copies;
231}
232
233void QPrintDialogPrivate::_q_printRangeSelected(int id)
234{
235 bool enable = (rangeCombo->itemData(id).toInt() == (int)QPrintDialog::PageRange);
236 firstPage->setEnabled(enable);
237 lastPage->setEnabled(enable);
238}
239
240void QPrintDialogPrivate::_q_setFirstPage(int fp)
241{
242 Q_Q(QPrintDialog);
243 if (printer) {
244 lastPage->setMinimum(fp);
245 lastPage->setMaximum(qMax(fp, q->maxPage()));
246 }
247}
248
249void QPrintDialogPrivate::_q_setLastPage(int lp)
250{
251 Q_Q(QPrintDialog);
252 if (printer) {
253 firstPage->setMinimum(qMin(lp, q->minPage()));
254 firstPage->setMaximum(lp);
255 }
256}
257
258void QPrintDialogPrivate::_q_fileNameEditChanged(const QString &text)
259{
260 Q_UNUSED(text);
261}
262
263void QPrintDialogPrivate::setupDestination()
264{
265 Q_Q(QPrintDialog);
266
267 // print destinations
268 printerOrFile = new QButtonGroup(q);
269 QObject::connect(printerOrFile, SIGNAL(buttonClicked(QAbstractButton*)),
270 q, SLOT(_q_printerOrFileSelected(QAbstractButton*)));
271
272 printToPrinterButton = q->findChild<QRadioButton *>("printToPrinterButton");
273 printerOrFile->addButton(printToPrinterButton);
274 printToFileButton = q->findChild<QRadioButton *>("printToFileButton");
275 printerOrFile->addButton(printToFileButton);
276
277 // file name
278 fileName = q->findChild<QLineEdit *>("fileName");
279 QObject::connect(fileName, SIGNAL(textChanged(QString)),
280 q, SLOT(_q_fileNameEditChanged(QString)));
281
282 outputToFile = false;
283}
284
285void QPrintDialogPrivate::setupPrinterSettings()
286{
287 Q_Q(QPrintDialog);
288
289 // color mode
290 colorMode = new QButtonGroup(q);
291 QObject::connect(colorMode, SIGNAL(buttonClicked(QAbstractButton*)),
292 q, SLOT(_q_colorModeSelected(QAbstractButton*)));
293
294 printColor = q->findChild<QRadioButton *>("printColor");
295 colorMode->addButton(printColor);
296 printGray = q->findChild<QRadioButton *>("printGray");
297 colorMode->addButton(printGray);
298}
299
300void isc(QPrintDialogPrivate *ptr, const QString & text, QPrinter::PaperSize ps)
301{
302 if (ptr && !text.isEmpty() && ps < QPrinter::NPaperSize) {
303 ptr->sizeCombo->addItem(text);
304 int index = ptr->sizeCombo->count()-1;
305 if (index >= 0 && index < QPrinter::NPaperSize)
306 ptr->indexToPaperSize[index] = ps;
307 }
308}
309
310void QPrintDialogPrivate::setupPaper()
311{
312 Q_Q(QPrintDialog);
313
314 pageSize = QPrinter::A4;
315
316 // paper orientation
317 orientationCombo = q->findChild<QComboBox *>("orientationCombo");
318 orientation = QPrinter::Portrait;
319 QObject::connect(orientationCombo, SIGNAL(activated(int)),
320 q, SLOT(_q_orientSelected(int)));
321
322 // paper size
323 sizeCombo = q->findChild<QComboBox *>("sizeCombo");
324
325 int n;
326 for(n=0; n<QPrinter::NPaperSize; n++)
327 indexToPaperSize[n] = QPrinter::A4;
328
329 isc(this, QPrintDialog::tr("A0 (841 x 1189 mm)"), QPrinter::A0);
330 isc(this, QPrintDialog::tr("A1 (594 x 841 mm)"), QPrinter::A1);
331 isc(this, QPrintDialog::tr("A2 (420 x 594 mm)"), QPrinter::A2);
332 isc(this, QPrintDialog::tr("A3 (297 x 420 mm)"), QPrinter::A3);
333 isc(this, QPrintDialog::tr("A4 (210 x 297 mm, 8.26 x 11.7 inches)"), QPrinter::A4);
334 isc(this, QPrintDialog::tr("A5 (148 x 210 mm)"), QPrinter::A5);
335 isc(this, QPrintDialog::tr("A6 (105 x 148 mm)"), QPrinter::A6);
336 isc(this, QPrintDialog::tr("A7 (74 x 105 mm)"), QPrinter::A7);
337 isc(this, QPrintDialog::tr("A8 (52 x 74 mm)"), QPrinter::A8);
338 isc(this, QPrintDialog::tr("A9 (37 x 52 mm)"), QPrinter::A9);
339 isc(this, QPrintDialog::tr("B0 (1000 x 1414 mm)"), QPrinter::B0);
340 isc(this, QPrintDialog::tr("B1 (707 x 1000 mm)"), QPrinter::B1);
341 isc(this, QPrintDialog::tr("B2 (500 x 707 mm)"), QPrinter::B2);
342 isc(this, QPrintDialog::tr("B3 (353 x 500 mm)"), QPrinter::B3);
343 isc(this, QPrintDialog::tr("B4 (250 x 353 mm)"), QPrinter::B4);
344 isc(this, QPrintDialog::tr("B5 (176 x 250 mm, 6.93 x 9.84 inches)"), QPrinter::B5);
345 isc(this, QPrintDialog::tr("B6 (125 x 176 mm)"), QPrinter::B6);
346 isc(this, QPrintDialog::tr("B7 (88 x 125 mm)"), QPrinter::B7);
347 isc(this, QPrintDialog::tr("B8 (62 x 88 mm)"), QPrinter::B8);
348 isc(this, QPrintDialog::tr("B9 (44 x 62 mm)"), QPrinter::B9);
349 isc(this, QPrintDialog::tr("B10 (31 x 44 mm)"), QPrinter::B10);
350 isc(this, QPrintDialog::tr("C5E (163 x 229 mm)"), QPrinter::C5E);
351 isc(this, QPrintDialog::tr("DLE (110 x 220 mm)"), QPrinter::DLE);
352 isc(this, QPrintDialog::tr("Executive (7.5 x 10 inches, 191 x 254 mm)"), QPrinter::Executive);
353 isc(this, QPrintDialog::tr("Folio (210 x 330 mm)"), QPrinter::Folio);
354 isc(this, QPrintDialog::tr("Ledger (432 x 279 mm)"), QPrinter::Ledger);
355 isc(this, QPrintDialog::tr("Legal (8.5 x 14 inches, 216 x 356 mm)"), QPrinter::Legal);
356 isc(this, QPrintDialog::tr("Letter (8.5 x 11 inches, 216 x 279 mm)"), QPrinter::Letter);
357 isc(this, QPrintDialog::tr("Tabloid (279 x 432 mm)"), QPrinter::Tabloid);
358 isc(this, QPrintDialog::tr("US Common #10 Envelope (105 x 241 mm)"), QPrinter::Comm10E);
359
360 QObject::connect(sizeCombo, SIGNAL(activated(int)),
361 q, SLOT(_q_paperSizeSelected(int)));
362}
363
364void QPrintDialogPrivate::setupOptions()
365{
366 Q_Q(QPrintDialog);
367
368 // no. of copies
369 copies = q->findChild<QSpinBox *>("copies");
370 QObject::connect(copies, SIGNAL(valueChanged(int)),
371 q, SLOT(_q_setNumCopies(int)));
372
373 // print range
374 rangeCombo = q->findChild<QComboBox *>("rangeCombo");
375 rangeCombo->addItem(QPrintDialog::tr("Print all"), QPrintDialog::AllPages);
376 rangeCombo->addItem(QPrintDialog::tr("Print selection"), QPrintDialog::Selection);
377 rangeCombo->addItem(QPrintDialog::tr("Print range"), QPrintDialog::PageRange);
378 QObject::connect(rangeCombo, SIGNAL(activated(int)),
379 q, SLOT(_q_printRangeSelected(int)));
380
381 // page range
382 firstPage = q->findChild<QSpinBox *>("firstPage");
383 firstPage->setRange(1, 9999);
384 firstPage->setValue(1);
385 QObject::connect(firstPage, SIGNAL(valueChanged(int)),
386 q, SLOT(_q_setFirstPage(int)));
387
388 lastPage = q->findChild<QSpinBox *>("lastPage");
389 lastPage->setRange(1, 9999);
390 lastPage->setValue(1);
391 QObject::connect(lastPage, SIGNAL(valueChanged(int)),
392 q, SLOT(_q_setLastPage(int)));
393
394 // print order
395 pageOrderCombo = q->findChild<QComboBox *>("pageOrderCombo");
396 QObject::connect(pageOrderCombo, SIGNAL(activated(int)),
397 q, SLOT(_q_pageOrderSelected(int)));
398}
399
400bool QPrintDialog::eventFilter(QObject *o, QEvent *e)
401{
402 Q_UNUSED(o);
403
404 Q_D(QPrintDialog);
405 switch (e->type()){
406 case QEvent::KeyPress:
407 switch (static_cast<QKeyEvent*>(e)->key()) {
408 case Qt::Key_Back:
409 d->_q_okClicked();
410 return true;
411 }
412 break;
413 default:
414 break;
415 }
416 return false;
417}
418
419QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
420 : QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, parent)
421{
422 d_func()->init();
423}
424
425QPrintDialog::QPrintDialog(QWidget *parent)
426 : QAbstractPrintDialog(*(new QPrintDialogPrivate), 0, parent)
427{
428 d_func()->init();
429}
430
431QPrintDialog::~QPrintDialog()
432{
433}
434
435void QPrintDialogPrivate::setPrinter(QPrinter *p, bool pickUpSettings)
436{
437 Q_Q(QPrintDialog);
438 printer = p;
439
440 if (p && pickUpSettings) {
441 // top to bottom in the old dialog.
442 // printer or file
443 outputToFile = !p->outputFileName().isEmpty() && q->isOptionEnabled(QPrintDialog::PrintToFile);
444 if (outputToFile)
445 printToFileButton->setChecked(true);
446 else
447 printToPrinterButton->setChecked(true);
448 fileName->setEnabled(outputToFile);
449
450 // file name
451 if (q->isOptionEnabled(QPrintDialog::PrintToFile)) {
452 fileName->setText(p->outputFileName());
453 fileName->setModified(!fileName->text().isEmpty());
454 } else {
455 printToFileButton->setEnabled(false);
456 }
457
458 // orientation
459 orientationCombo->setCurrentIndex((int)p->orientation());
460 _q_orientSelected(p->orientation());
461
462 // page size
463 int n = 0;
464 while (n < QPrinter::NPaperSize &&
465 indexToPaperSize[n] != p->pageSize())
466 n++;
467 sizeCombo->setCurrentIndex(n);
468 _q_paperSizeSelected(n);
469
470 // page order
471 pageOrder2 = p->pageOrder();
472 pageOrderCombo->setCurrentIndex((int)pageOrder2);
473
474 // color mode
475 colorMode2 = p->colorMode();
476 if (colorMode2 == QPrinter::Color)
477 printColor->setChecked(true);
478 else
479 printGray->setChecked(true);
480
481 // number of copies
482 copies->setValue(p->numCopies());
483 _q_setNumCopies(p->numCopies());
484 }
485
486 if (p) {
487 if (!q->isOptionEnabled(QPrintDialog::PrintSelection)
488 && rangeCombo->findData(QPrintDialog::Selection) > 0)
489 rangeCombo->removeItem(rangeCombo->findData(QPrintDialog::Selection));
490 if (!q->isOptionEnabled(QPrintDialog::PrintPageRange)
491 && rangeCombo->findData(QPrintDialog::PageRange) > 0)
492 rangeCombo->removeItem(rangeCombo->findData(QPrintDialog::PageRange));
493
494 switch (q->printRange()) {
495 case QPrintDialog::AllPages:
496 rangeCombo->setCurrentIndex((int)(QPrintDialog::AllPages));
497 break;
498 case QPrintDialog::Selection:
499 rangeCombo->setCurrentIndex((int)(QPrintDialog::Selection));
500 break;
501 case QPrintDialog::PageRange:
502 rangeCombo->setCurrentIndex((int)(QPrintDialog::PageRange));
503 break;
504 }
505 }
506
507 if (p && q->maxPage()) {
508 int from = q->minPage();
509 int to = q->maxPage();
510 if (q->printRange() == QPrintDialog::PageRange) {
511 from = q->fromPage();
512 to = q->toPage();
513 }
514 firstPage->setRange(q->minPage(), to);
515 lastPage->setRange(from, q->maxPage());
516 firstPage->setValue(from);
517 lastPage->setValue(to);
518 }
519}
520
521int QPrintDialog::exec()
522{
523 Q_D(QPrintDialog);
524 d->setPrinter(d->printer, true);
525 return QDialog::exec();
526}
527
528void QPrintDialogPrivate::init()
529{
530 Q_Q(QPrintDialog);
531 numCopies = 1;
532
533 if (_qt_print_dialog_creator)
534 (*_qt_print_dialog_creator)(q);
535
536 setupDestination();
537 setupPrinterSettings();
538 setupPaper();
539 setupOptions();
540
541 setPrinter(printer, true);
542
543 q->installEventFilter(q);
544}
545
546void QPrintDialog::setVisible(bool visible)
547{
548 QAbstractPrintDialog::setVisible(visible);
549}
550
551QT_END_NAMESPACE
552
553#include "moc_qprintdialog.cpp"
554#include "qrc_qprintdialog.cpp"
555
556#endif // QT_NO_PRINTDIALOG
Note: See TracBrowser for help on using the repository browser.