author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 8 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtGui module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qplatformdefs.h" |
|
43 |
||
44 |
#ifndef QT_NO_PRINTDIALOG |
|
45 |
||
46 |
#include "private/qabstractprintdialog_p.h" |
|
47 |
#include <QtGui/qmessagebox.h> |
|
48 |
#include "qprintdialog.h" |
|
49 |
#include "qfiledialog.h" |
|
50 |
#include <QtCore/qdir.h> |
|
51 |
#include <QtGui/qevent.h> |
|
52 |
#include <QtGui/qfilesystemmodel.h> |
|
53 |
#include <QtGui/qstyleditemdelegate.h> |
|
54 |
#include <QtGui/qprinter.h> |
|
55 |
||
56 |
#include <QtGui/qdialogbuttonbox.h> |
|
57 |
||
58 |
#include "qfscompleter_p.h" |
|
59 |
#include "ui_qprintpropertieswidget.h" |
|
60 |
#include "ui_qprintsettingsoutput.h" |
|
61 |
#include "ui_qprintwidget.h" |
|
62 |
||
63 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
64 |
# include <private/qcups_p.h> |
|
65 |
# include <cups/cups.h> |
|
66 |
# include <private/qpdf_p.h> |
|
67 |
#else |
|
68 |
# include <QtCore/qlibrary.h> |
|
69 |
#endif |
|
70 |
||
71 |
#include <private/qprinterinfo_unix_p.h> |
|
72 |
||
73 |
QT_BEGIN_NAMESPACE |
|
74 |
||
75 |
extern int qt_printerRealNumCopies(QPaintEngine *); |
|
76 |
||
77 |
class QOptionTreeItem; |
|
78 |
class QPPDOptionsModel; |
|
79 |
||
80 |
class QPrintPropertiesDialog : public QDialog |
|
81 |
{ |
|
82 |
Q_OBJECT |
|
83 |
public: |
|
84 |
QPrintPropertiesDialog(QAbstractPrintDialog *parent = 0); |
|
85 |
~QPrintPropertiesDialog(); |
|
86 |
||
87 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
88 |
void setCups(QCUPSSupport *cups) { m_cups = cups; } |
|
89 |
void addItemToOptions(QOptionTreeItem *parent, QList<const ppd_option_t*>& options, QList<const char*>& markedOptions) const; |
|
90 |
#endif |
|
91 |
||
92 |
void selectPrinter(); |
|
93 |
void selectPdfPsPrinter(const QPrinter *p); |
|
94 |
||
95 |
/// copy printer properties to the widget |
|
96 |
void applyPrinterProperties(QPrinter *p); |
|
97 |
void setupPrinter() const; |
|
98 |
||
99 |
protected: |
|
100 |
void showEvent(QShowEvent* event); |
|
101 |
||
102 |
private: |
|
103 |
Ui::QPrintPropertiesWidget widget; |
|
104 |
QDialogButtonBox *m_buttons; |
|
105 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
106 |
QCUPSSupport *m_cups; |
|
107 |
QPPDOptionsModel *m_cupsOptionsModel; |
|
108 |
#endif |
|
109 |
}; |
|
110 |
||
111 |
class QPrintDialogPrivate : public QAbstractPrintDialogPrivate |
|
112 |
{ |
|
113 |
Q_DECLARE_PUBLIC(QPrintDialog) |
|
114 |
Q_DECLARE_TR_FUNCTIONS(QPrintDialog) |
|
115 |
public: |
|
116 |
QPrintDialogPrivate(); |
|
117 |
~QPrintDialogPrivate(); |
|
118 |
||
119 |
void init(); |
|
120 |
/// copy printer properties to the widget |
|
121 |
void applyPrinterProperties(QPrinter *p); |
|
122 |
||
123 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
124 |
void selectPrinter(QCUPSSupport *cups); |
|
125 |
#endif |
|
126 |
||
127 |
void _q_chbPrintLastFirstToggled(bool); |
|
128 |
#ifndef QT_NO_MESSAGEBOX |
|
129 |
void _q_checkFields(); |
|
130 |
#endif |
|
131 |
void _q_collapseOrExpandDialog(); |
|
132 |
||
133 |
void setupPrinter(); |
|
134 |
void updateWidgets(); |
|
135 |
||
136 |
virtual void setTabs(const QList<QWidget*> &tabs); |
|
137 |
||
138 |
Ui::QPrintSettingsOutput options; |
|
139 |
QUnixPrintWidget *top; |
|
140 |
QWidget *bottom; |
|
141 |
QDialogButtonBox *buttons; |
|
142 |
QPushButton *collapseButton; |
|
143 |
}; |
|
144 |
||
145 |
#if defined (Q_OS_UNIX) |
|
146 |
class QUnixPrintWidgetPrivate |
|
147 |
{ |
|
148 |
public: |
|
149 |
QUnixPrintWidgetPrivate(QUnixPrintWidget *q); |
|
150 |
~QUnixPrintWidgetPrivate(); |
|
151 |
||
152 |
/// copy printer properties to the widget |
|
153 |
void applyPrinterProperties(QPrinter *p); |
|
154 |
bool checkFields(); |
|
155 |
void setupPrinter(); |
|
156 |
void setOptionsPane(QPrintDialogPrivate *pane); |
|
157 |
||
158 |
// slots |
|
159 |
void _q_printerChanged(int index); |
|
160 |
void _q_btnPropertiesClicked(); |
|
161 |
void _q_btnBrowseClicked(); |
|
162 |
||
163 |
QUnixPrintWidget * const parent; |
|
164 |
QPrintPropertiesDialog *propertiesDialog; |
|
165 |
Ui::QPrintWidget widget; |
|
166 |
QAbstractPrintDialog * q; |
|
167 |
QPrinter *printer; |
|
168 |
QList<QPrinterDescription> lprPrinters; |
|
169 |
void updateWidget(); |
|
170 |
||
171 |
private: |
|
172 |
QPrintDialogPrivate *optionsPane; |
|
173 |
bool filePrintersAdded; |
|
174 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
175 |
QCUPSSupport* cups; |
|
176 |
int cupsPrinterCount; |
|
177 |
const cups_dest_t* cupsPrinters; |
|
178 |
const ppd_file_t* cupsPPD; |
|
179 |
#endif |
|
180 |
}; |
|
181 |
#endif |
|
182 |
||
183 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
184 |
class QOptionTreeItem |
|
185 |
{ |
|
186 |
public: |
|
187 |
enum ItemType { Root, Group, Option, Choice }; |
|
188 |
||
189 |
QOptionTreeItem(ItemType t, int i, const void* p, const char* desc, QOptionTreeItem* pi) |
|
190 |
: type(t), |
|
191 |
index(i), |
|
192 |
ptr(p), |
|
193 |
description(desc), |
|
194 |
selected(-1), |
|
195 |
selDescription(0), |
|
196 |
parentItem(pi) {} |
|
197 |
||
198 |
~QOptionTreeItem() { |
|
199 |
while (!childItems.isEmpty()) |
|
200 |
delete childItems.takeFirst(); |
|
201 |
} |
|
202 |
||
203 |
ItemType type; |
|
204 |
int index; |
|
205 |
const void* ptr; |
|
206 |
const char* description; |
|
207 |
int selected; |
|
208 |
const char* selDescription; |
|
209 |
QOptionTreeItem* parentItem; |
|
210 |
QList<QOptionTreeItem*> childItems; |
|
211 |
}; |
|
212 |
||
213 |
class QPPDOptionsModel : public QAbstractItemModel |
|
214 |
{ |
|
215 |
friend class QPPDOptionsEditor; |
|
216 |
public: |
|
217 |
QPPDOptionsModel(QCUPSSupport *cups, QObject *parent = 0); |
|
218 |
~QPPDOptionsModel(); |
|
219 |
||
220 |
int columnCount(const QModelIndex& parent = QModelIndex()) const; |
|
221 |
int rowCount(const QModelIndex& parent = QModelIndex()) const; |
|
222 |
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; |
|
223 |
QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const; |
|
224 |
QModelIndex parent(const QModelIndex& index) const; |
|
225 |
Qt::ItemFlags flags(const QModelIndex& index) const; |
|
226 |
QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; |
|
227 |
||
228 |
QOptionTreeItem* rootItem; |
|
229 |
QCUPSSupport *cups; |
|
230 |
const ppd_file_t* ppd; |
|
231 |
void parseItems(); |
|
232 |
void parseGroups(QOptionTreeItem* parent); |
|
233 |
void parseOptions(QOptionTreeItem* parent); |
|
234 |
void parseChoices(QOptionTreeItem* parent); |
|
235 |
}; |
|
236 |
||
237 |
class QPPDOptionsEditor : public QStyledItemDelegate |
|
238 |
{ |
|
239 |
Q_OBJECT |
|
240 |
public: |
|
241 |
QPPDOptionsEditor(QObject* parent = 0) : QStyledItemDelegate(parent) {} |
|
242 |
~QPPDOptionsEditor() {} |
|
243 |
||
244 |
QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const; |
|
245 |
void setEditorData(QWidget* editor, const QModelIndex& index) const; |
|
246 |
void setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const; |
|
247 |
||
248 |
private slots: |
|
249 |
void cbChanged(int index); |
|
250 |
||
251 |
}; |
|
252 |
||
253 |
#endif |
|
254 |
||
255 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
256 |
||
257 |
QPrintPropertiesDialog::QPrintPropertiesDialog(QAbstractPrintDialog *parent) |
|
258 |
: QDialog(parent) |
|
259 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
260 |
, m_cups(0), m_cupsOptionsModel(0) |
|
261 |
#endif |
|
262 |
{ |
|
263 |
QVBoxLayout *lay = new QVBoxLayout(this); |
|
264 |
this->setLayout(lay); |
|
265 |
QWidget *content = new QWidget(this); |
|
266 |
widget.setupUi(content); |
|
267 |
m_buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); |
|
268 |
lay->addWidget(content); |
|
269 |
lay->addWidget(m_buttons); |
|
270 |
||
271 |
connect(m_buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(accept())); |
|
272 |
connect(m_buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(reject())); |
|
273 |
} |
|
274 |
||
275 |
QPrintPropertiesDialog::~QPrintPropertiesDialog() |
|
276 |
{ |
|
277 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
278 |
delete m_cupsOptionsModel; |
|
279 |
#else |
|
280 |
delete widget.cupsPropertiesPage; |
|
281 |
#endif |
|
282 |
} |
|
283 |
||
284 |
void QPrintPropertiesDialog::applyPrinterProperties(QPrinter *p) |
|
285 |
{ |
|
286 |
widget.pageSetup->setPrinter(p); |
|
287 |
} |
|
288 |
||
289 |
void QPrintPropertiesDialog::setupPrinter() const |
|
290 |
{ |
|
291 |
widget.pageSetup->setupPrinter(); |
|
292 |
||
293 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
294 |
QPPDOptionsModel* model = static_cast<QPPDOptionsModel*>(widget.treeView->model()); |
|
295 |
if (model) { |
|
296 |
QOptionTreeItem* rootItem = model->rootItem; |
|
297 |
QList<const ppd_option_t*> options; |
|
298 |
QList<const char*> markedOptions; |
|
299 |
||
300 |
addItemToOptions(rootItem, options, markedOptions); |
|
301 |
model->cups->saveOptions(options, markedOptions); |
|
302 |
} |
|
303 |
#endif |
|
304 |
} |
|
305 |
||
306 |
void QPrintPropertiesDialog::selectPrinter() |
|
307 |
{ |
|
308 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
309 |
widget.pageSetup->selectPrinter(m_cups); |
|
310 |
widget.treeView->setModel(0); |
|
311 |
if (m_cups && QCUPSSupport::isAvailable()) { |
|
312 |
||
313 |
if (m_cupsOptionsModel == 0) { |
|
314 |
m_cupsOptionsModel = new QPPDOptionsModel(m_cups); |
|
315 |
||
316 |
widget.treeView->setItemDelegate(new QPPDOptionsEditor(this)); |
|
317 |
} else { |
|
318 |
// update the model |
|
319 |
m_cupsOptionsModel->parseItems(); |
|
320 |
} |
|
321 |
||
322 |
if (m_cupsOptionsModel->rowCount() > 0) { |
|
323 |
widget.treeView->setModel(m_cupsOptionsModel); |
|
324 |
||
325 |
for (int i = 0; i < m_cupsOptionsModel->rowCount(); ++i) |
|
326 |
widget.treeView->expand(m_cupsOptionsModel->index(i,0)); |
|
327 |
||
328 |
widget.tabs->setTabEnabled(1, true); // enable the advanced tab |
|
329 |
} else { |
|
330 |
widget.tabs->setTabEnabled(1, false); |
|
331 |
} |
|
332 |
||
333 |
} else |
|
334 |
#endif |
|
335 |
{ |
|
336 |
widget.cupsPropertiesPage->setEnabled(false); |
|
337 |
widget.pageSetup->selectPrinter(0); |
|
338 |
} |
|
339 |
} |
|
340 |
||
341 |
void QPrintPropertiesDialog::selectPdfPsPrinter(const QPrinter *p) |
|
342 |
{ |
|
343 |
widget.treeView->setModel(0); |
|
344 |
widget.pageSetup->selectPdfPsPrinter(p); |
|
345 |
widget.tabs->setTabEnabled(1, false); // disable the advanced tab |
|
346 |
} |
|
347 |
||
348 |
void QPrintPropertiesDialog::showEvent(QShowEvent* event) |
|
349 |
{ |
|
350 |
widget.treeView->resizeColumnToContents(0); |
|
351 |
event->accept(); |
|
352 |
} |
|
353 |
||
354 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
355 |
void QPrintPropertiesDialog::addItemToOptions(QOptionTreeItem *parent, QList<const ppd_option_t*>& options, QList<const char*>& markedOptions) const |
|
356 |
{ |
|
357 |
for (int i = 0; i < parent->childItems.count(); ++i) { |
|
358 |
QOptionTreeItem *itm = parent->childItems.at(i); |
|
359 |
if (itm->type == QOptionTreeItem::Option) { |
|
360 |
const ppd_option_t* opt = reinterpret_cast<const ppd_option_t*>(itm->ptr); |
|
361 |
options << opt; |
|
362 |
if (qstrcmp(opt->defchoice, opt->choices[itm->selected].choice) != 0) { |
|
363 |
markedOptions << opt->keyword << opt->choices[itm->selected].choice; |
|
364 |
} |
|
365 |
} else { |
|
366 |
addItemToOptions(itm, options, markedOptions); |
|
367 |
} |
|
368 |
} |
|
369 |
} |
|
370 |
#endif |
|
371 |
||
372 |
QPrintDialogPrivate::QPrintDialogPrivate() |
|
373 |
: top(0), bottom(0), buttons(0), collapseButton(0) |
|
374 |
{ |
|
375 |
} |
|
376 |
||
377 |
QPrintDialogPrivate::~QPrintDialogPrivate() |
|
378 |
{ |
|
379 |
} |
|
380 |
||
381 |
void QPrintDialogPrivate::init() |
|
382 |
{ |
|
383 |
Q_Q(QPrintDialog); |
|
384 |
||
385 |
top = new QUnixPrintWidget(0, q); |
|
386 |
bottom = new QWidget(q); |
|
387 |
options.setupUi(bottom); |
|
388 |
options.color->setIconSize(QSize(32, 32)); |
|
389 |
options.color->setIcon(QIcon(QLatin1String(":/trolltech/dialogs/qprintdialog/images/status-color.png"))); |
|
390 |
options.grayscale->setIconSize(QSize(32, 32)); |
|
391 |
options.grayscale->setIcon(QIcon(QLatin1String(":/trolltech/dialogs/qprintdialog/images/status-gray-scale.png"))); |
|
392 |
top->d->setOptionsPane(this); |
|
393 |
||
394 |
buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, q); |
|
395 |
collapseButton = new QPushButton(QPrintDialog::tr("&Options >>"), buttons); |
|
396 |
buttons->addButton(collapseButton, QDialogButtonBox::ResetRole); |
|
397 |
bottom->setVisible(false); |
|
398 |
||
399 |
QPushButton *printButton = buttons->button(QDialogButtonBox::Ok); |
|
400 |
printButton->setText(QPrintDialog::tr("&Print")); |
|
401 |
printButton->setDefault(true); |
|
402 |
||
403 |
QVBoxLayout *lay = new QVBoxLayout(q); |
|
404 |
q->setLayout(lay); |
|
405 |
lay->addWidget(top); |
|
406 |
lay->addWidget(bottom); |
|
407 |
lay->addWidget(buttons); |
|
408 |
||
409 |
QPrinter* p = q->printer(); |
|
410 |
||
411 |
applyPrinterProperties(p); |
|
412 |
||
413 |
#ifdef QT_NO_MESSAGEBOX |
|
414 |
QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(accept())); |
|
415 |
#else |
|
416 |
QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(_q_checkFields())); |
|
417 |
#endif |
|
418 |
QObject::connect(buttons, SIGNAL(rejected()), q, SLOT(reject())); |
|
419 |
||
420 |
QObject::connect(options.reverse, SIGNAL(toggled(bool)), |
|
421 |
q, SLOT(_q_chbPrintLastFirstToggled(bool))); |
|
422 |
||
423 |
QObject::connect(collapseButton, SIGNAL(released()), q, SLOT(_q_collapseOrExpandDialog())); |
|
424 |
} |
|
425 |
||
426 |
void QPrintDialogPrivate::applyPrinterProperties(QPrinter *p) |
|
427 |
{ |
|
428 |
if (p->colorMode() == QPrinter::Color) |
|
429 |
options.color->setChecked(true); |
|
430 |
else |
|
431 |
options.grayscale->setChecked(true); |
|
432 |
||
433 |
switch(p->duplex()) { |
|
434 |
case QPrinter::DuplexNone: |
|
435 |
options.noDuplex->setChecked(true); break; |
|
436 |
case QPrinter::DuplexLongSide: |
|
437 |
case QPrinter::DuplexAuto: |
|
438 |
options.duplexLong->setChecked(true); break; |
|
439 |
case QPrinter::DuplexShortSide: |
|
440 |
options.duplexShort->setChecked(true); break; |
|
441 |
} |
|
442 |
options.copies->setValue(qt_printerRealNumCopies(p->paintEngine())); |
|
443 |
options.collate->setChecked(p->collateCopies()); |
|
444 |
options.reverse->setChecked(p->pageOrder() == QPrinter::LastPageFirst); |
|
445 |
top->d->applyPrinterProperties(p); |
|
446 |
} |
|
447 |
||
448 |
void QPrintDialogPrivate::_q_chbPrintLastFirstToggled(bool checked) |
|
449 |
{ |
|
450 |
Q_Q(QPrintDialog); |
|
451 |
if (checked) |
|
452 |
q->printer()->setPageOrder(QPrinter::LastPageFirst); |
|
453 |
else |
|
454 |
q->printer()->setPageOrder(QPrinter::FirstPageFirst); |
|
455 |
} |
|
456 |
||
457 |
void QPrintDialogPrivate::_q_collapseOrExpandDialog() |
|
458 |
{ |
|
459 |
int collapseHeight = 0; |
|
460 |
Q_Q(QPrintDialog); |
|
461 |
QWidget *widgetToHide = bottom; |
|
462 |
if (widgetToHide->isVisible()) { |
|
463 |
collapseButton->setText(QPrintDialog::tr("&Options >>")); |
|
464 |
collapseHeight = widgetToHide->y() + widgetToHide->height() - (top->y() + top->height()); |
|
465 |
} |
|
466 |
else |
|
467 |
collapseButton->setText(QPrintDialog::tr("&Options <<")); |
|
468 |
widgetToHide->setVisible(! widgetToHide->isVisible()); |
|
469 |
if (! widgetToHide->isVisible()) { // make it shrink |
|
470 |
q->layout()->activate(); |
|
471 |
q->resize( QSize(q->width(), q->height() - collapseHeight) ); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
#ifndef QT_NO_MESSAGEBOX |
|
476 |
void QPrintDialogPrivate::_q_checkFields() |
|
477 |
{ |
|
478 |
Q_Q(QPrintDialog); |
|
479 |
if (top->d->checkFields()) |
|
480 |
q->accept(); |
|
481 |
} |
|
482 |
#endif // QT_NO_MESSAGEBOX |
|
483 |
||
484 |
void QPrintDialogPrivate::setupPrinter() |
|
485 |
{ |
|
486 |
Q_Q(QPrintDialog); |
|
487 |
QPrinter* p = q->printer(); |
|
488 |
||
489 |
if (options.duplex->isEnabled()) { |
|
490 |
if (options.noDuplex->isChecked()) |
|
491 |
p->setDuplex(QPrinter::DuplexNone); |
|
492 |
else if (options.duplexLong->isChecked()) |
|
493 |
p->setDuplex(QPrinter::DuplexLongSide); |
|
494 |
else |
|
495 |
p->setDuplex(QPrinter::DuplexShortSide); |
|
496 |
} |
|
497 |
||
498 |
p->setColorMode( options.color->isChecked() ? QPrinter::Color : QPrinter::GrayScale ); |
|
499 |
||
500 |
// print range |
|
501 |
if (options.printAll->isChecked()) { |
|
502 |
p->setPrintRange(QPrinter::AllPages); |
|
503 |
p->setFromTo(0,0); |
|
504 |
} else if (options.printSelection->isChecked()) { |
|
505 |
p->setPrintRange(QPrinter::Selection); |
|
506 |
p->setFromTo(0,0); |
|
507 |
} else if (options.printRange->isChecked()) { |
|
508 |
p->setPrintRange(QPrinter::PageRange); |
|
509 |
p->setFromTo(options.from->value(), qMax(options.from->value(), options.to->value())); |
|
510 |
} |
|
511 |
||
512 |
// copies |
|
513 |
p->setNumCopies(options.copies->value()); |
|
514 |
p->setCollateCopies(options.collate->isChecked()); |
|
515 |
||
516 |
top->d->setupPrinter(); |
|
517 |
} |
|
518 |
||
519 |
void QPrintDialogPrivate::updateWidgets() |
|
520 |
{ |
|
521 |
Q_Q(QPrintDialog); |
|
522 |
options.gbPrintRange->setVisible(q->isOptionEnabled(QPrintDialog::PrintPageRange) || |
|
523 |
q->isOptionEnabled(QPrintDialog::PrintSelection)); |
|
524 |
||
525 |
options.printRange->setEnabled(q->isOptionEnabled(QPrintDialog::PrintPageRange)); |
|
526 |
options.printSelection->setVisible(q->isOptionEnabled(QPrintDialog::PrintSelection)); |
|
527 |
options.collate->setVisible(q->isOptionEnabled(QPrintDialog::PrintCollateCopies)); |
|
528 |
||
529 |
switch (q->printRange()) { |
|
530 |
case QPrintDialog::AllPages: |
|
531 |
options.printAll->setChecked(true); |
|
532 |
break; |
|
533 |
case QPrintDialog::Selection: |
|
534 |
options.printSelection->setChecked(true); |
|
535 |
break; |
|
536 |
case QPrintDialog::PageRange: |
|
537 |
options.printRange->setChecked(true); |
|
538 |
break; |
|
539 |
default: |
|
540 |
break; |
|
541 |
} |
|
542 |
const int minPage = qMax(1, qMin(q->minPage() , q->maxPage())); |
|
543 |
const int maxPage = qMax(1, q->maxPage() == INT_MAX ? 9999 : q->maxPage()); |
|
544 |
||
545 |
options.from->setMinimum(minPage); |
|
546 |
options.to->setMinimum(minPage); |
|
547 |
options.from->setMaximum(maxPage); |
|
548 |
options.to->setMaximum(maxPage); |
|
549 |
||
550 |
options.from->setValue(q->fromPage()); |
|
551 |
options.to->setValue(q->toPage()); |
|
552 |
top->d->updateWidget(); |
|
553 |
} |
|
554 |
||
555 |
void QPrintDialogPrivate::setTabs(const QList<QWidget*> &tabWidgets) |
|
556 |
{ |
|
557 |
while(options.tabs->count() > 2) |
|
558 |
delete options.tabs->widget(2); |
|
559 |
||
560 |
QList<QWidget*>::ConstIterator iter = tabWidgets.begin(); |
|
561 |
while(iter != tabWidgets.constEnd()) { |
|
562 |
QWidget *tab = *iter; |
|
563 |
options.tabs->addTab(tab, tab->windowTitle()); |
|
564 |
++iter; |
|
565 |
} |
|
566 |
} |
|
567 |
||
568 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
569 |
void QPrintDialogPrivate::selectPrinter(QCUPSSupport *cups) |
|
570 |
{ |
|
571 |
options.duplex->setEnabled(cups && cups->ppdOption("Duplex")); |
|
572 |
} |
|
573 |
#endif |
|
574 |
||
575 |
//////////////////////////////////////////////////////////////////////////////// |
|
576 |
||
577 |
QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent) |
|
578 |
: QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, parent) |
|
579 |
{ |
|
580 |
Q_D(QPrintDialog); |
|
581 |
d->init(); |
|
582 |
} |
|
583 |
||
584 |
/*! |
|
585 |
Constructs a print dialog with the given \a parent. |
|
586 |
*/ |
|
587 |
QPrintDialog::QPrintDialog(QWidget *parent) |
|
588 |
: QAbstractPrintDialog(*(new QPrintDialogPrivate), 0, parent) |
|
589 |
{ |
|
590 |
Q_D(QPrintDialog); |
|
591 |
d->init(); |
|
592 |
} |
|
593 |
||
594 |
QPrintDialog::~QPrintDialog() |
|
595 |
{ |
|
596 |
} |
|
597 |
||
598 |
void QPrintDialog::setVisible(bool visible) |
|
599 |
{ |
|
600 |
Q_D(QPrintDialog); |
|
601 |
||
602 |
if (visible) |
|
603 |
d->updateWidgets(); |
|
604 |
||
605 |
QAbstractPrintDialog::setVisible(visible); |
|
606 |
} |
|
607 |
||
608 |
int QPrintDialog::exec() |
|
609 |
{ |
|
610 |
return QDialog::exec(); |
|
611 |
} |
|
612 |
||
613 |
void QPrintDialog::accept() |
|
614 |
{ |
|
615 |
Q_D(QPrintDialog); |
|
616 |
d->setupPrinter(); |
|
617 |
QDialog::accept(); |
|
618 |
} |
|
619 |
||
620 |
#ifdef QT3_SUPPORT |
|
621 |
QPrinter *QPrintDialog::printer() const |
|
622 |
{ |
|
623 |
Q_D(const QPrintDialog); |
|
624 |
return d->printer; |
|
625 |
} |
|
626 |
||
627 |
void QPrintDialog::setPrinter(QPrinter *printer, bool pickupSettings) |
|
628 |
{ |
|
629 |
if (!printer) |
|
630 |
return; |
|
631 |
||
632 |
Q_D(QPrintDialog); |
|
633 |
d->printer = printer; |
|
634 |
||
635 |
if (pickupSettings) |
|
636 |
d->applyPrinterProperties(printer); |
|
637 |
} |
|
638 |
||
639 |
void QPrintDialog::addButton(QPushButton *button) |
|
640 |
{ |
|
641 |
Q_D(QPrintDialog); |
|
642 |
d->buttons->addButton(button, QDialogButtonBox::HelpRole); |
|
643 |
} |
|
644 |
#endif // QT3_SUPPORT |
|
645 |
||
646 |
#if defined (Q_OS_UNIX) |
|
647 |
||
648 |
/*! \internal |
|
649 |
*/ |
|
650 |
QUnixPrintWidgetPrivate::QUnixPrintWidgetPrivate(QUnixPrintWidget *p) |
|
651 |
: parent(p), propertiesDialog(0), printer(0), optionsPane(0), filePrintersAdded(false) |
|
652 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
653 |
, cups(0), cupsPrinterCount(0), cupsPrinters(0), cupsPPD(0) |
|
654 |
#endif |
|
655 |
{ |
|
656 |
q = 0; |
|
657 |
if (parent) |
|
658 |
q = qobject_cast<QAbstractPrintDialog*> (parent->parent()); |
|
659 |
||
660 |
widget.setupUi(parent); |
|
661 |
||
662 |
int currentPrinterIndex = 0; |
|
663 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
664 |
cups = new QCUPSSupport; |
|
665 |
if (QCUPSSupport::isAvailable()) { |
|
666 |
cupsPPD = cups->currentPPD(); |
|
667 |
cupsPrinterCount = cups->availablePrintersCount(); |
|
668 |
cupsPrinters = cups->availablePrinters(); |
|
669 |
||
670 |
for (int i = 0; i < cupsPrinterCount; ++i) { |
|
671 |
QString printerName(QString::fromLocal8Bit(cupsPrinters[i].name)); |
|
672 |
if (cupsPrinters[i].instance) |
|
673 |
printerName += QLatin1Char('/') + QString::fromLocal8Bit(cupsPrinters[i].instance); |
|
674 |
||
675 |
widget.printers->addItem(printerName); |
|
676 |
if (cupsPrinters[i].is_default) |
|
677 |
widget.printers->setCurrentIndex(i); |
|
678 |
} |
|
679 |
// the model depends on valid ppd. so before enabling the |
|
680 |
// properties button we make sure the ppd is in fact valid. |
|
681 |
if (cupsPrinterCount && cups->currentPPD()) { |
|
682 |
widget.properties->setEnabled(true); |
|
683 |
} |
|
684 |
currentPrinterIndex = cups->currentPrinterIndex(); |
|
685 |
} else { |
|
686 |
#endif |
|
687 |
currentPrinterIndex = qt_getLprPrinters(lprPrinters); |
|
688 |
// populating printer combo |
|
689 |
QList<QPrinterDescription>::const_iterator i = lprPrinters.constBegin(); |
|
690 |
for(; i != lprPrinters.constEnd(); ++i) |
|
691 |
widget.printers->addItem((*i).name); |
|
692 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
693 |
} |
|
694 |
#endif |
|
695 |
||
696 |
#ifndef QT_NO_FILESYSTEMMODEL |
|
697 |
QFileSystemModel *fsm = new QFileSystemModel(widget.filename); |
|
698 |
fsm->setRootPath(QDir::homePath()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
699 |
#if !defined(QT_NO_FSCOMPLETER) && !defined(QT_NO_FILEDIALOG) |
0 | 700 |
widget.filename->setCompleter(new QFSCompleter(fsm, widget.filename)); |
701 |
#endif |
|
702 |
#endif |
|
703 |
_q_printerChanged(currentPrinterIndex); |
|
704 |
||
705 |
QObject::connect(widget.printers, SIGNAL(currentIndexChanged(int)), |
|
706 |
parent, SLOT(_q_printerChanged(int))); |
|
707 |
QObject::connect(widget.fileBrowser, SIGNAL(clicked()), parent, SLOT(_q_btnBrowseClicked())); |
|
708 |
QObject::connect(widget.properties, SIGNAL(clicked()), parent, SLOT(_q_btnPropertiesClicked())); |
|
709 |
||
710 |
// disable features that QPrinter does not yet support. |
|
711 |
widget.preview->setVisible(false); |
|
712 |
} |
|
713 |
||
714 |
void QUnixPrintWidgetPrivate::updateWidget() |
|
715 |
{ |
|
716 |
const bool printToFile = q == 0 || q->isOptionEnabled(QPrintDialog::PrintToFile); |
|
717 |
if (printToFile && !filePrintersAdded) { |
|
718 |
if (widget.printers->count()) |
|
719 |
widget.printers->insertSeparator(widget.printers->count()); |
|
720 |
widget.printers->addItem(QPrintDialog::tr("Print to File (PDF)")); |
|
721 |
widget.printers->addItem(QPrintDialog::tr("Print to File (Postscript)")); |
|
722 |
filePrintersAdded = true; |
|
723 |
} |
|
724 |
if (!printToFile && filePrintersAdded) { |
|
725 |
widget.printers->removeItem(widget.printers->count()-1); |
|
726 |
widget.printers->removeItem(widget.printers->count()-1); |
|
727 |
if (widget.printers->count()) |
|
728 |
widget.printers->removeItem(widget.printers->count()-1); // remove separator |
|
729 |
filePrintersAdded = false; |
|
730 |
} |
|
731 |
if (printer && filePrintersAdded && (printer->outputFormat() != QPrinter::NativeFormat |
|
732 |
|| printer->printerName().isEmpty())) |
|
733 |
{ |
|
734 |
if (printer->outputFormat() == QPrinter::PdfFormat) |
|
735 |
widget.printers->setCurrentIndex(widget.printers->count() - 2); |
|
736 |
else if (printer->outputFormat() == QPrinter::PostScriptFormat) |
|
737 |
widget.printers->setCurrentIndex(widget.printers->count() - 1); |
|
738 |
widget.filename->setEnabled(true); |
|
739 |
widget.lOutput->setEnabled(true); |
|
740 |
} |
|
741 |
||
742 |
widget.filename->setVisible(printToFile); |
|
743 |
widget.lOutput->setVisible(printToFile); |
|
744 |
widget.fileBrowser->setVisible(printToFile); |
|
745 |
||
746 |
widget.properties->setVisible(q->isOptionEnabled(QAbstractPrintDialog::PrintShowPageSize)); |
|
747 |
} |
|
748 |
||
749 |
QUnixPrintWidgetPrivate::~QUnixPrintWidgetPrivate() |
|
750 |
{ |
|
751 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
752 |
delete cups; |
|
753 |
#endif |
|
754 |
} |
|
755 |
||
756 |
void QUnixPrintWidgetPrivate::_q_printerChanged(int index) |
|
757 |
{ |
|
758 |
if (index < 0) |
|
759 |
return; |
|
760 |
const int printerCount = widget.printers->count(); |
|
761 |
widget.filename->setEnabled(false); |
|
762 |
widget.lOutput->setEnabled(false); |
|
763 |
||
764 |
if (filePrintersAdded) { |
|
765 |
Q_ASSERT(index != printerCount - 3); // separator |
|
766 |
if (index > printerCount - 3) { // PDF or postscript |
|
767 |
bool pdfPrinter = (index == printerCount - 2); |
|
768 |
widget.location->setText(QPrintDialog::tr("Local file")); |
|
769 |
widget.type->setText(QPrintDialog::tr("Write %1 file").arg(pdfPrinter ? QString::fromLatin1("PDF") |
|
770 |
: QString::fromLatin1("PostScript"))); |
|
771 |
widget.properties->setEnabled(true); |
|
772 |
widget.filename->setEnabled(true); |
|
773 |
QString filename = widget.filename->text(); |
|
774 |
QString suffix = QFileInfo(filename).suffix(); |
|
775 |
if (pdfPrinter && suffix == QLatin1String("ps")) |
|
776 |
filename = filename.replace(QLatin1String(".ps"), QLatin1String(".pdf")); |
|
777 |
if (!pdfPrinter && suffix == QLatin1String("pdf")) |
|
778 |
filename = filename.replace(QLatin1String(".pdf"), QLatin1String(".ps")); |
|
779 |
widget.filename->setText(filename); |
|
780 |
widget.lOutput->setEnabled(true); |
|
781 |
if (propertiesDialog) |
|
782 |
propertiesDialog->selectPdfPsPrinter(printer); |
|
783 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
784 |
if (optionsPane) |
|
785 |
optionsPane->selectPrinter(0); |
|
786 |
#endif |
|
787 |
return; |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
widget.location->setText(QString()); |
|
792 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
793 |
if (QCUPSSupport::isAvailable()) { |
|
794 |
cups->setCurrentPrinter(index); |
|
795 |
||
796 |
const cups_option_t *opt = cups->printerOption(QString::fromLatin1("printer-location")); |
|
797 |
QString location; |
|
798 |
if (opt) |
|
799 |
location = QString::fromLocal8Bit(opt->value); |
|
800 |
widget.location->setText(location); |
|
801 |
||
802 |
cupsPPD = cups->currentPPD(); |
|
803 |
// set printer type line |
|
804 |
QString type; |
|
805 |
if (cupsPPD) |
|
806 |
type = QString::fromLocal8Bit(cupsPPD->manufacturer) + QLatin1String(" - ") + QString::fromLocal8Bit(cupsPPD->modelname); |
|
807 |
widget.type->setText(type); |
|
808 |
if (propertiesDialog) |
|
809 |
propertiesDialog->selectPrinter(); |
|
810 |
if (optionsPane) |
|
811 |
optionsPane->selectPrinter(cups); |
|
812 |
} else { |
|
813 |
if (optionsPane) |
|
814 |
optionsPane->selectPrinter(0); |
|
815 |
#endif |
|
816 |
if (lprPrinters.count() > 0) { |
|
817 |
QString type = lprPrinters.at(index).name + QLatin1Char('@') + lprPrinters.at(index).host; |
|
818 |
if (!lprPrinters.at(index).comment.isEmpty()) |
|
819 |
type += QLatin1String(", ") + lprPrinters.at(index).comment; |
|
820 |
widget.type->setText(type); |
|
821 |
if (propertiesDialog) |
|
822 |
propertiesDialog->selectPrinter(); |
|
823 |
} |
|
824 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
825 |
} |
|
826 |
#endif |
|
827 |
} |
|
828 |
||
829 |
void QUnixPrintWidgetPrivate::setOptionsPane(QPrintDialogPrivate *pane) |
|
830 |
{ |
|
831 |
optionsPane = pane; |
|
832 |
if (optionsPane) |
|
833 |
_q_printerChanged(widget.printers->currentIndex()); |
|
834 |
} |
|
835 |
||
836 |
void QUnixPrintWidgetPrivate::_q_btnBrowseClicked() |
|
837 |
{ |
|
838 |
QString filename = widget.filename->text(); |
|
839 |
#ifndef QT_NO_FILEDIALOG |
|
840 |
filename = QFileDialog::getSaveFileName(parent, QPrintDialog::tr("Print To File ..."), filename, |
|
841 |
QString(), 0, QFileDialog::DontConfirmOverwrite); |
|
842 |
#else |
|
843 |
filename.clear(); |
|
844 |
#endif |
|
845 |
if (!filename.isEmpty()) { |
|
846 |
widget.filename->setText(filename); |
|
847 |
if (filename.endsWith(QString::fromLatin1(".ps"), Qt::CaseInsensitive)) |
|
848 |
widget.printers->setCurrentIndex(widget.printers->count() - 1); // the postscript one |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
849 |
else if (filename.endsWith(QString::fromLatin1(".pdf"), Qt::CaseInsensitive)) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
850 |
widget.printers->setCurrentIndex(widget.printers->count() - 2); // the pdf one |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
851 |
else if (widget.printers->currentIndex() != widget.printers->count() - 1) // if ps is not selected, pdf is default |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
852 |
widget.printers->setCurrentIndex(widget.printers->count() - 2); // the pdf one |
0 | 853 |
} |
854 |
} |
|
855 |
||
856 |
void QUnixPrintWidgetPrivate::applyPrinterProperties(QPrinter *p) |
|
857 |
{ |
|
858 |
if (p == 0) |
|
859 |
return; |
|
860 |
printer = p; |
|
861 |
if (p->outputFileName().isEmpty()) { |
|
862 |
QString home = QString::fromLocal8Bit(qgetenv("HOME").constData()); |
|
863 |
QString cur = QDir::currentPath(); |
|
864 |
if (home.at(home.length()-1) != QLatin1Char('/')) |
|
865 |
home += QLatin1Char('/'); |
|
866 |
if (cur.at(cur.length()-1) != QLatin1Char('/')) |
|
867 |
cur += QLatin1Char('/'); |
|
868 |
if (cur.left(home.length()) != home) |
|
869 |
cur = home; |
|
870 |
#ifdef Q_WS_X11 |
|
871 |
if (p->docName().isEmpty()) { |
|
872 |
if (p->outputFormat() == QPrinter::PostScriptFormat) |
|
873 |
cur += QLatin1String("print.ps"); |
|
874 |
else |
|
875 |
cur += QLatin1String("print.pdf"); |
|
876 |
} else { |
|
877 |
QRegExp re(QString::fromLatin1("(.*)\\.\\S+")); |
|
878 |
if (re.exactMatch(p->docName())) |
|
879 |
cur += re.cap(1); |
|
880 |
else |
|
881 |
cur += p->docName(); |
|
882 |
if (p->outputFormat() == QPrinter::PostScriptFormat) |
|
883 |
cur += QLatin1String(".ps"); |
|
884 |
else |
|
885 |
cur += QLatin1String(".pdf"); |
|
886 |
} |
|
887 |
#endif |
|
888 |
widget.filename->setText(cur); |
|
889 |
} |
|
890 |
else |
|
891 |
widget.filename->setText( p->outputFileName() ); |
|
892 |
QString printer = p->printerName(); |
|
893 |
if (!printer.isEmpty()) { |
|
894 |
for (int i = 0; i < widget.printers->count(); ++i) { |
|
895 |
if (widget.printers->itemText(i) == printer) { |
|
896 |
widget.printers->setCurrentIndex(i); |
|
897 |
break; |
|
898 |
} |
|
899 |
} |
|
900 |
} |
|
901 |
// PDF and PS printers are not added to the dialog yet, we'll handle those cases in QUnixPrintWidgetPrivate::updateWidget |
|
902 |
||
903 |
if (propertiesDialog) |
|
904 |
propertiesDialog->applyPrinterProperties(p); |
|
905 |
} |
|
906 |
||
907 |
#ifndef QT_NO_MESSAGEBOX |
|
908 |
bool QUnixPrintWidgetPrivate::checkFields() |
|
909 |
{ |
|
910 |
if (widget.filename->isEnabled()) { |
|
911 |
QString file = widget.filename->text(); |
|
912 |
QFile f(file); |
|
913 |
QFileInfo fi(f); |
|
914 |
bool exists = fi.exists(); |
|
915 |
bool opened = false; |
|
916 |
if (exists && fi.isDir()) { |
|
917 |
QMessageBox::warning(q, q->windowTitle(), |
|
918 |
QPrintDialog::tr("%1 is a directory.\nPlease choose a different file name.").arg(file)); |
|
919 |
return false; |
|
920 |
} else if ((exists && !fi.isWritable()) || !(opened = f.open(QFile::Append))) { |
|
921 |
QMessageBox::warning(q, q->windowTitle(), |
|
922 |
QPrintDialog::tr("File %1 is not writable.\nPlease choose a different file name.").arg(file)); |
|
923 |
return false; |
|
924 |
} else if (exists) { |
|
925 |
int ret = QMessageBox::question(q, q->windowTitle(), |
|
926 |
QPrintDialog::tr("%1 already exists.\nDo you want to overwrite it?").arg(file), |
|
927 |
QMessageBox::Yes|QMessageBox::No, QMessageBox::No); |
|
928 |
if (ret == QMessageBox::No) |
|
929 |
return false; |
|
930 |
} |
|
931 |
if (opened) { |
|
932 |
f.close(); |
|
933 |
if (!exists) |
|
934 |
f.remove(); |
|
935 |
} |
|
936 |
} |
|
937 |
||
938 |
// Every test passed. Accept the dialog. |
|
939 |
return true; |
|
940 |
} |
|
941 |
#endif // QT_NO_MESSAGEBOX |
|
942 |
||
943 |
void QUnixPrintWidgetPrivate::_q_btnPropertiesClicked() |
|
944 |
{ |
|
945 |
if (propertiesDialog == 0) { |
|
946 |
propertiesDialog = new QPrintPropertiesDialog(q); |
|
947 |
propertiesDialog->setResult(QDialog::Rejected); |
|
948 |
} |
|
949 |
||
950 |
if (propertiesDialog->result() == QDialog::Rejected) { |
|
951 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
952 |
propertiesDialog->setCups(cups); |
|
953 |
#endif |
|
954 |
propertiesDialog->applyPrinterProperties(q->printer()); |
|
955 |
||
956 |
if (q->isOptionEnabled(QPrintDialog::PrintToFile) |
|
957 |
&& (widget.printers->currentIndex() > widget.printers->count() - 3)) // PDF or postscript |
|
958 |
propertiesDialog->selectPdfPsPrinter(q->printer()); |
|
959 |
else |
|
960 |
propertiesDialog->selectPrinter(); |
|
961 |
} |
|
962 |
propertiesDialog->exec(); |
|
963 |
} |
|
964 |
||
965 |
void QUnixPrintWidgetPrivate::setupPrinter() |
|
966 |
{ |
|
967 |
const int printerCount = widget.printers->count(); |
|
968 |
const int index = widget.printers->currentIndex(); |
|
969 |
||
970 |
if (filePrintersAdded && index > printerCount - 3) { // PDF or postscript |
|
971 |
printer->setPrinterName(QString()); |
|
972 |
Q_ASSERT(index != printerCount - 3); // separator |
|
973 |
if (index == printerCount - 2) |
|
974 |
printer->setOutputFormat(QPrinter::PdfFormat); |
|
975 |
else |
|
976 |
printer->setOutputFormat(QPrinter::PostScriptFormat); |
|
977 |
QString path = widget.filename->text(); |
|
978 |
if (QDir::isRelativePath(path)) |
|
979 |
path = QDir::homePath() + QDir::separator() + path; |
|
980 |
printer->setOutputFileName(path); |
|
981 |
} |
|
982 |
else { |
|
983 |
printer->setPrinterName(widget.printers->currentText()); |
|
984 |
printer->setOutputFileName(QString()); |
|
985 |
} |
|
986 |
||
987 |
if (propertiesDialog && propertiesDialog->result() == QDialog::Accepted) |
|
988 |
propertiesDialog->setupPrinter(); |
|
989 |
} |
|
990 |
||
991 |
||
992 |
/*! \internal |
|
993 |
*/ |
|
994 |
QUnixPrintWidget::QUnixPrintWidget(QPrinter *printer, QWidget *parent) |
|
995 |
: QWidget(parent), d(new QUnixPrintWidgetPrivate(this)) |
|
996 |
{ |
|
997 |
d->applyPrinterProperties(printer); |
|
998 |
} |
|
999 |
||
1000 |
/*! \internal |
|
1001 |
*/ |
|
1002 |
QUnixPrintWidget::~QUnixPrintWidget() |
|
1003 |
{ |
|
1004 |
delete d; |
|
1005 |
} |
|
1006 |
||
1007 |
/*! \internal |
|
1008 |
||
1009 |
Updates the printer with the states held in the QUnixPrintWidget. |
|
1010 |
*/ |
|
1011 |
void QUnixPrintWidget::updatePrinter() |
|
1012 |
{ |
|
1013 |
d->setupPrinter(); |
|
1014 |
} |
|
1015 |
||
1016 |
#endif |
|
1017 |
||
1018 |
//////////////////////////////////////////////////////////////////////////////// |
|
1019 |
#if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY) |
|
1020 |
||
1021 |
QPPDOptionsModel::QPPDOptionsModel(QCUPSSupport *c, QObject *parent) |
|
1022 |
: QAbstractItemModel(parent), rootItem(0), cups(c), ppd(c->currentPPD()) |
|
1023 |
{ |
|
1024 |
parseItems(); |
|
1025 |
} |
|
1026 |
||
1027 |
QPPDOptionsModel::~QPPDOptionsModel() |
|
1028 |
{ |
|
1029 |
} |
|
1030 |
||
1031 |
int QPPDOptionsModel::columnCount(const QModelIndex&) const |
|
1032 |
{ |
|
1033 |
return 2; |
|
1034 |
} |
|
1035 |
||
1036 |
int QPPDOptionsModel::rowCount(const QModelIndex& parent) const |
|
1037 |
{ |
|
1038 |
QOptionTreeItem* itm; |
|
1039 |
if (!parent.isValid()) |
|
1040 |
itm = rootItem; |
|
1041 |
else |
|
1042 |
itm = reinterpret_cast<QOptionTreeItem*>(parent.internalPointer()); |
|
1043 |
||
1044 |
if (itm->type == QOptionTreeItem::Option) |
|
1045 |
return 0; |
|
1046 |
||
1047 |
return itm->childItems.count(); |
|
1048 |
} |
|
1049 |
||
1050 |
QVariant QPPDOptionsModel::data(const QModelIndex& index, int role) const |
|
1051 |
{ |
|
1052 |
switch(role) { |
|
1053 |
case Qt::FontRole: { |
|
1054 |
QOptionTreeItem* itm = reinterpret_cast<QOptionTreeItem*>(index.internalPointer()); |
|
1055 |
if (itm && itm->type == QOptionTreeItem::Group){ |
|
1056 |
QFont font = QApplication::font(); |
|
1057 |
font.setBold(true); |
|
1058 |
return QVariant(font); |
|
1059 |
} |
|
1060 |
return QVariant(); |
|
1061 |
} |
|
1062 |
break; |
|
1063 |
||
1064 |
case Qt::DisplayRole: { |
|
1065 |
QOptionTreeItem* itm; |
|
1066 |
if (!index.isValid()) |
|
1067 |
itm = rootItem; |
|
1068 |
else |
|
1069 |
itm = reinterpret_cast<QOptionTreeItem*>(index.internalPointer()); |
|
1070 |
||
1071 |
if (index.column() == 0) |
|
1072 |
return cups->unicodeString(itm->description); |
|
1073 |
else if (itm->type == QOptionTreeItem::Option && itm->selected > -1) |
|
1074 |
return cups->unicodeString(itm->selDescription); |
|
1075 |
else |
|
1076 |
return QVariant(); |
|
1077 |
} |
|
1078 |
break; |
|
1079 |
||
1080 |
default: |
|
1081 |
return QVariant(); |
|
1082 |
} |
|
1083 |
if (role != Qt::DisplayRole) |
|
1084 |
return QVariant(); |
|
1085 |
} |
|
1086 |
||
1087 |
QModelIndex QPPDOptionsModel::index(int row, int column, const QModelIndex& parent) const |
|
1088 |
{ |
|
1089 |
QOptionTreeItem* itm; |
|
1090 |
if (!parent.isValid()) |
|
1091 |
itm = rootItem; |
|
1092 |
else |
|
1093 |
itm = reinterpret_cast<QOptionTreeItem*>(parent.internalPointer()); |
|
1094 |
||
1095 |
return createIndex(row, column, itm->childItems.at(row)); |
|
1096 |
} |
|
1097 |
||
1098 |
||
1099 |
QModelIndex QPPDOptionsModel::parent(const QModelIndex& index) const |
|
1100 |
{ |
|
1101 |
if (!index.isValid()) |
|
1102 |
return QModelIndex(); |
|
1103 |
||
1104 |
QOptionTreeItem* itm = reinterpret_cast<QOptionTreeItem*>(index.internalPointer()); |
|
1105 |
||
1106 |
if (itm->parentItem && itm->parentItem != rootItem) |
|
1107 |
return createIndex(itm->parentItem->index, 0, itm->parentItem); |
|
1108 |
else |
|
1109 |
return QModelIndex(); |
|
1110 |
} |
|
1111 |
||
1112 |
Qt::ItemFlags QPPDOptionsModel::flags(const QModelIndex& index) const |
|
1113 |
{ |
|
1114 |
if (!index.isValid() || reinterpret_cast<QOptionTreeItem*>(index.internalPointer())->type == QOptionTreeItem::Group) |
|
1115 |
return Qt::ItemIsEnabled; |
|
1116 |
||
1117 |
if (index.column() == 1) |
|
1118 |
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; |
|
1119 |
||
1120 |
return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
|
1121 |
} |
|
1122 |
||
1123 |
void QPPDOptionsModel::parseItems() |
|
1124 |
{ |
|
1125 |
emit layoutAboutToBeChanged(); |
|
1126 |
ppd = cups->currentPPD(); |
|
1127 |
delete rootItem; |
|
1128 |
rootItem = new QOptionTreeItem(QOptionTreeItem::Root, 0, ppd, "Root Item", 0); |
|
1129 |
parseGroups(rootItem); |
|
1130 |
emit layoutChanged(); |
|
1131 |
} |
|
1132 |
||
1133 |
void QPPDOptionsModel::parseGroups(QOptionTreeItem* parent) |
|
1134 |
{ |
|
1135 |
if (parent->type == QOptionTreeItem::Root) { |
|
1136 |
||
1137 |
const ppd_file_t* ppdFile = reinterpret_cast<const ppd_file_t*>(parent->ptr); |
|
1138 |
||
1139 |
if (ppdFile) { |
|
1140 |
for (int i = 0; i < ppdFile->num_groups; ++i) { |
|
1141 |
QOptionTreeItem* group = new QOptionTreeItem(QOptionTreeItem::Group, i, &ppdFile->groups[i], ppdFile->groups[i].text, parent); |
|
1142 |
parent->childItems.append(group); |
|
1143 |
parseGroups(group); // parse possible subgroups |
|
1144 |
parseOptions(group); // parse options |
|
1145 |
} |
|
1146 |
} |
|
1147 |
} else if (parent->type == QOptionTreeItem::Group) { |
|
1148 |
||
1149 |
const ppd_group_t* group = reinterpret_cast<const ppd_group_t*>(parent->ptr); |
|
1150 |
||
1151 |
if (group) { |
|
1152 |
for (int i = 0; i < group->num_subgroups; ++i) { |
|
1153 |
QOptionTreeItem* subgroup = new QOptionTreeItem(QOptionTreeItem::Group, i, &group->subgroups[i], group->subgroups[i].text, parent); |
|
1154 |
parent->childItems.append(subgroup); |
|
1155 |
parseGroups(subgroup); // parse possible subgroups |
|
1156 |
parseOptions(subgroup); // parse options |
|
1157 |
} |
|
1158 |
} |
|
1159 |
} |
|
1160 |
} |
|
1161 |
||
1162 |
void QPPDOptionsModel::parseOptions(QOptionTreeItem* parent) |
|
1163 |
{ |
|
1164 |
const ppd_group_t* group = reinterpret_cast<const ppd_group_t*>(parent->ptr); |
|
1165 |
for (int i = 0; i < group->num_options; ++i) { |
|
1166 |
QOptionTreeItem* opt = new QOptionTreeItem(QOptionTreeItem::Option, i, &group->options[i], group->options[i].text, parent); |
|
1167 |
parent->childItems.append(opt); |
|
1168 |
parseChoices(opt); |
|
1169 |
} |
|
1170 |
} |
|
1171 |
||
1172 |
void QPPDOptionsModel::parseChoices(QOptionTreeItem* parent) |
|
1173 |
{ |
|
1174 |
const ppd_option_t* option = reinterpret_cast<const ppd_option_t*>(parent->ptr); |
|
1175 |
bool marked = false; |
|
1176 |
for (int i = 0; i < option->num_choices; ++i) { |
|
1177 |
QOptionTreeItem* choice = new QOptionTreeItem(QOptionTreeItem::Choice, i, &option->choices[i], option->choices[i].text, parent); |
|
1178 |
if (static_cast<int>(option->choices[i].marked) == 1) { |
|
1179 |
parent->selected = i; |
|
1180 |
parent->selDescription = option->choices[i].text; |
|
1181 |
marked = true; |
|
1182 |
} else if (!marked && qstrcmp(option->choices[i].choice, option->defchoice) == 0) { |
|
1183 |
parent->selected = i; |
|
1184 |
parent->selDescription = option->choices[i].text; |
|
1185 |
} |
|
1186 |
parent->childItems.append(choice); |
|
1187 |
} |
|
1188 |
} |
|
1189 |
||
1190 |
QVariant QPPDOptionsModel::headerData(int section, Qt::Orientation, int role) const |
|
1191 |
{ |
|
1192 |
if (role != Qt::DisplayRole) |
|
1193 |
return QVariant(); |
|
1194 |
||
1195 |
switch(section){ |
|
1196 |
case 0: |
|
1197 |
return QVariant(QApplication::translate("QPPDOptionsModel", "Name")); |
|
1198 |
case 1: |
|
1199 |
return QVariant(QApplication::translate("QPPDOptionsModel", "Value")); |
|
1200 |
default: |
|
1201 |
return QVariant(); |
|
1202 |
} |
|
1203 |
} |
|
1204 |
||
1205 |
//////////////////////////////////////////////////////////////////////////////// |
|
1206 |
||
1207 |
QWidget* QPPDOptionsEditor::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const |
|
1208 |
{ |
|
1209 |
if (index.column() == 1 && reinterpret_cast<QOptionTreeItem*>(index.internalPointer())->type == QOptionTreeItem::Option) |
|
1210 |
return new QComboBox(parent); |
|
1211 |
else |
|
1212 |
return 0; |
|
1213 |
} |
|
1214 |
||
1215 |
void QPPDOptionsEditor::setEditorData(QWidget* editor, const QModelIndex& index) const |
|
1216 |
{ |
|
1217 |
if (index.column() != 1) |
|
1218 |
return; |
|
1219 |
||
1220 |
QComboBox* cb = static_cast<QComboBox*>(editor); |
|
1221 |
QOptionTreeItem* itm = reinterpret_cast<QOptionTreeItem*>(index.internalPointer()); |
|
1222 |
||
1223 |
if (itm->selected == -1) |
|
1224 |
cb->addItem(QString()); |
|
1225 |
||
1226 |
for (int i = 0; i < itm->childItems.count(); ++i) |
|
1227 |
cb->addItem(QString::fromLocal8Bit(itm->childItems.at(i)->description)); |
|
1228 |
||
1229 |
if (itm->selected > -1) |
|
1230 |
cb->setCurrentIndex(itm->selected); |
|
1231 |
||
1232 |
connect(cb, SIGNAL(currentIndexChanged(int)), this, SLOT(cbChanged(int))); |
|
1233 |
} |
|
1234 |
||
1235 |
void QPPDOptionsEditor::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const |
|
1236 |
{ |
|
1237 |
QComboBox* cb = static_cast<QComboBox*>(editor); |
|
1238 |
QOptionTreeItem* itm = reinterpret_cast<QOptionTreeItem*>(index.internalPointer()); |
|
1239 |
||
1240 |
if (itm->selected == cb->currentIndex()) |
|
1241 |
return; |
|
1242 |
||
1243 |
const ppd_option_t* opt = reinterpret_cast<const ppd_option_t*>(itm->ptr); |
|
1244 |
QPPDOptionsModel* m = static_cast<QPPDOptionsModel*>(model); |
|
1245 |
||
1246 |
if (m->cups->markOption(opt->keyword, opt->choices[cb->currentIndex()].choice) == 0) { |
|
1247 |
itm->selected = cb->currentIndex(); |
|
1248 |
itm->selDescription = reinterpret_cast<const ppd_option_t*>(itm->ptr)->choices[itm->selected].text; |
|
1249 |
} |
|
1250 |
} |
|
1251 |
||
1252 |
void QPPDOptionsEditor::cbChanged(int) |
|
1253 |
{ |
|
1254 |
/* |
|
1255 |
emit commitData(static_cast<QWidget*>(sender())); |
|
1256 |
*/ |
|
1257 |
} |
|
1258 |
||
1259 |
#endif |
|
1260 |
||
1261 |
QT_END_NAMESPACE |
|
1262 |
||
1263 |
#include "moc_qprintdialog.cpp" |
|
1264 |
#include "qprintdialog_unix.moc" |
|
1265 |
#include "qrc_qprintdialog.cpp" |
|
1266 |
||
1267 |
#endif // QT_NO_PRINTDIALOG |
|
1268 |