source: trunk/src/gui/painting/qprinterinfo_mac.cpp@ 203

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

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qprinterinfo.h"
43
44#include "private/qt_mac_p.h"
45
46QT_BEGIN_NAMESPACE
47
48#ifndef QT_NO_PRINTER
49
50class QPrinterInfoPrivate
51{
52Q_DECLARE_PUBLIC(QPrinterInfo)
53public:
54 ~QPrinterInfoPrivate();
55 QPrinterInfoPrivate();
56 QPrinterInfoPrivate(const QString& name);
57
58private:
59 QPrinterInfo* q_ptr;
60
61 QString m_name;
62 bool m_default;
63 bool m_isNull;
64};
65
66static QPrinterInfoPrivate nullQPrinterInfoPrivate;
67
68extern QPrinter::PaperSize qSizeFTopaperSize(const QSizeF& size);
69
70/////////////////////////////////////////////////////////////////////////////
71/////////////////////////////////////////////////////////////////////////////
72
73QList<QPrinterInfo> QPrinterInfo::availablePrinters()
74{
75 QList<QPrinterInfo> printers;
76
77 OSStatus status = noErr;
78 QCFType<CFArrayRef> printerList;
79 status = PMServerCreatePrinterList(kPMServerLocal, &printerList);
80 if (status == noErr) {
81 CFIndex count = CFArrayGetCount(printerList);
82 for (CFIndex i=0; i<count; ++i) {
83 PMPrinter printer = static_cast<PMPrinter>(const_cast<void *>(CFArrayGetValueAtIndex(printerList, i)));
84 QString name = QCFString::toQString(PMPrinterGetName(printer));
85 printers.append(QPrinterInfo(name));
86 if (PMPrinterIsDefault(printer)) {
87 printers[i].d_ptr->m_default = true;
88 }
89 }
90 }
91
92 return printers;
93}
94
95QPrinterInfo QPrinterInfo::defaultPrinter(){
96 QList<QPrinterInfo> printers = availablePrinters();
97 for (int c = 0; c < printers.size(); ++c) {
98 if (printers[c].isDefault()) {
99 return printers[c];
100 }
101 }
102 return QPrinterInfo();
103}
104
105/////////////////////////////////////////////////////////////////////////////
106/////////////////////////////////////////////////////////////////////////////
107
108QPrinterInfo::QPrinterInfo(const QPrinter& prn)
109{
110 d_ptr = &nullQPrinterInfoPrivate;
111 QList<QPrinterInfo> list = availablePrinters();
112 for (int c = 0; c < list.size(); ++c) {
113 if (prn.printerName() == list[c].printerName()) {
114 *this = list[c];
115 return;
116 }
117 }
118
119 *this = QPrinterInfo();
120}
121
122QPrinterInfo::~QPrinterInfo()
123{
124 if (d_ptr != &nullQPrinterInfoPrivate)
125 delete d_ptr;
126}
127
128QPrinterInfo::QPrinterInfo()
129{
130 d_ptr = &nullQPrinterInfoPrivate;
131}
132
133QPrinterInfo::QPrinterInfo(const QString& name)
134{
135 d_ptr = new QPrinterInfoPrivate(name);
136 d_ptr->q_ptr = this;
137}
138
139QPrinterInfo::QPrinterInfo(const QPrinterInfo& src)
140{
141 d_ptr = &nullQPrinterInfoPrivate;
142 *this = src;
143}
144
145QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src)
146{
147 Q_ASSERT(d_ptr);
148 if (d_ptr != &nullQPrinterInfoPrivate)
149 delete d_ptr;
150 d_ptr = new QPrinterInfoPrivate(*src.d_ptr);
151 d_ptr->q_ptr = this;
152 return *this;
153}
154
155QString QPrinterInfo::printerName() const
156{
157 const Q_D(QPrinterInfo);
158 return d->m_name;
159}
160
161bool QPrinterInfo::isNull() const
162{
163 const Q_D(QPrinterInfo);
164 return d->m_isNull;
165}
166
167bool QPrinterInfo::isDefault() const
168{
169 const Q_D(QPrinterInfo);
170 return d->m_default;
171}
172
173QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
174{
175#if (MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4)
176 return QList<QPrinter::PaperSize>();
177#else
178 if (QSysInfo::MacintoshVersion <= QSysInfo::MV_10_4)
179 return QList<QPrinter::PaperSize>();
180
181 const Q_D(QPrinterInfo);
182
183 PMPrinter cfPrn = PMPrinterCreateFromPrinterID(
184 QCFString::toCFStringRef(d->m_name));
185
186 if (!cfPrn) return QList<QPrinter::PaperSize>();
187
188 CFArrayRef array;
189 OSStatus status = PMPrinterGetPaperList(cfPrn, &array);
190
191 if (status != 0) {
192 PMRelease(cfPrn);
193 return QList<QPrinter::PaperSize>();
194 }
195
196 QList<QPrinter::PaperSize> paperList;
197 int count = CFArrayGetCount(array);
198 for (int c = 0; c < count; c++) {
199 PMPaper paper = static_cast<PMPaper>(
200 const_cast<void*>(
201 CFArrayGetValueAtIndex(array, c)));
202 double width, height;
203 status = PMPaperGetWidth(paper, &width);
204 status |= PMPaperGetHeight(paper, &height);
205 if (status != 0) continue;
206
207 QSizeF size(width * 0.3527, height * 0.3527);
208 paperList.append(qSizeFTopaperSize(size));
209 }
210
211 PMRelease(cfPrn);
212
213 return paperList;
214#endif // MAC_OS_X_VERSION_MAX_ALLOWED
215}
216
217/////////////////////////////////////////////////////////////////////////////
218/////////////////////////////////////////////////////////////////////////////
219
220QPrinterInfoPrivate::QPrinterInfoPrivate() :
221 q_ptr(NULL),
222 m_default(false),
223 m_isNull(true)
224{
225}
226
227QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) :
228 q_ptr(NULL),
229 m_name(name),
230 m_default(false),
231 m_isNull(false)
232{
233}
234
235QPrinterInfoPrivate::~QPrinterInfoPrivate()
236{
237}
238
239#endif // QT_NO_PRINTER
240
241QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.