1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "nsPrintSettingsQt.h"
11 #include "nsCRTGlue.h"
13 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsQt
,
17 nsPrintSettingsQt::nsPrintSettingsQt():
18 mQPrinter(new QPrinter())
22 nsPrintSettingsQt::~nsPrintSettingsQt()
24 //smart pointer should take care of cleanup
27 nsPrintSettingsQt::nsPrintSettingsQt(const nsPrintSettingsQt
& aPS
):
28 mQPrinter(aPS
.mQPrinter
)
33 nsPrintSettingsQt::operator=(const nsPrintSettingsQt
& rhs
)
39 nsPrintSettings::operator=(rhs
);
40 mQPrinter
= rhs
.mQPrinter
;
45 nsPrintSettingsQt::_Clone(nsIPrintSettings
** _retval
)
47 NS_ENSURE_ARG_POINTER(_retval
);
49 nsPrintSettingsQt
* newSettings
= new nsPrintSettingsQt(*this);
50 *_retval
= newSettings
;
56 nsPrintSettingsQt::_Assign(nsIPrintSettings
* aPS
)
58 nsPrintSettingsQt
* printSettingsQt
= static_cast<nsPrintSettingsQt
*>(aPS
);
60 return NS_ERROR_UNEXPECTED
;
61 *this = *printSettingsQt
;
66 nsPrintSettingsQt::GetPrintRange(int16_t* aPrintRange
)
68 NS_ENSURE_ARG_POINTER(aPrintRange
);
70 QPrinter::PrintRange range
= mQPrinter
->printRange();
71 if (range
== QPrinter::PageRange
) {
72 *aPrintRange
= kRangeSpecifiedPageRange
;
73 } else if (range
== QPrinter::Selection
) {
74 *aPrintRange
= kRangeSelection
;
76 *aPrintRange
= kRangeAllPages
;
83 nsPrintSettingsQt::SetPrintRange(int16_t aPrintRange
)
85 if (aPrintRange
== kRangeSelection
) {
86 mQPrinter
->setPrintRange(QPrinter::Selection
);
87 } else if (aPrintRange
== kRangeSpecifiedPageRange
) {
88 mQPrinter
->setPrintRange(QPrinter::PageRange
);
90 mQPrinter
->setPrintRange(QPrinter::AllPages
);
96 nsPrintSettingsQt::GetStartPageRange(int32_t* aStartPageRange
)
98 NS_ENSURE_ARG_POINTER(aStartPageRange
);
99 int32_t start
= mQPrinter
->fromPage();
100 *aStartPageRange
= start
;
105 nsPrintSettingsQt::SetStartPageRange(int32_t aStartPageRange
)
107 int32_t endRange
= mQPrinter
->toPage();
108 mQPrinter
->setFromTo(aStartPageRange
, endRange
);
113 nsPrintSettingsQt::GetEndPageRange(int32_t* aEndPageRange
)
115 NS_ENSURE_ARG_POINTER(aEndPageRange
);
116 int32_t end
= mQPrinter
->toPage();
117 *aEndPageRange
= end
;
122 nsPrintSettingsQt::SetEndPageRange(int32_t aEndPageRange
)
124 int32_t startRange
= mQPrinter
->fromPage();
125 mQPrinter
->setFromTo(startRange
, aEndPageRange
);
130 nsPrintSettingsQt::GetPrintReversed(bool* aPrintReversed
)
132 NS_ENSURE_ARG_POINTER(aPrintReversed
);
133 if (mQPrinter
->pageOrder() == QPrinter::LastPageFirst
) {
134 *aPrintReversed
= true;
136 *aPrintReversed
= false;
142 nsPrintSettingsQt::SetPrintReversed(bool aPrintReversed
)
144 if (aPrintReversed
) {
145 mQPrinter
->setPageOrder(QPrinter::LastPageFirst
);
147 mQPrinter
->setPageOrder(QPrinter::FirstPageFirst
);
153 nsPrintSettingsQt::GetPrintInColor(bool* aPrintInColor
)
155 NS_ENSURE_ARG_POINTER(aPrintInColor
);
156 if (mQPrinter
->colorMode() == QPrinter::Color
) {
157 *aPrintInColor
= true;
159 *aPrintInColor
= false;
164 nsPrintSettingsQt::SetPrintInColor(bool aPrintInColor
)
167 mQPrinter
->setColorMode(QPrinter::Color
);
169 mQPrinter
->setColorMode(QPrinter::GrayScale
);
175 nsPrintSettingsQt::GetOrientation(int32_t* aOrientation
)
177 NS_ENSURE_ARG_POINTER(aOrientation
);
178 QPrinter::Orientation orientation
= mQPrinter
->orientation();
179 if (orientation
== QPrinter::Landscape
) {
180 *aOrientation
= kLandscapeOrientation
;
182 *aOrientation
= kPortraitOrientation
;
188 nsPrintSettingsQt::SetOrientation(int32_t aOrientation
)
190 if (aOrientation
== kLandscapeOrientation
) {
191 mQPrinter
->setOrientation(QPrinter::Landscape
);
193 mQPrinter
->setOrientation(QPrinter::Portrait
);
199 nsPrintSettingsQt::GetToFileName(PRUnichar
** aToFileName
)
201 NS_ENSURE_ARG_POINTER(aToFileName
);
203 filename
= mQPrinter
->outputFileName();
204 *aToFileName
= ToNewUnicode(
205 nsDependentString((PRUnichar
*)filename
.data()));
210 nsPrintSettingsQt::SetToFileName(const PRUnichar
* aToFileName
)
212 nsCOMPtr
<nsIFile
> file
;
213 nsresult rv
= NS_NewLocalFile(nsDependentString(aToFileName
), true,
214 getter_AddRefs(file
));
215 NS_ENSURE_SUCCESS(rv
, rv
);
217 QString
filename((const QChar
*)aToFileName
, NS_strlen(aToFileName
));
218 mQPrinter
->setOutputFileName(filename
);
224 nsPrintSettingsQt::GetPrinterName(PRUnichar
** aPrinter
)
226 NS_ENSURE_ARG_POINTER(aPrinter
);
227 *aPrinter
= ToNewUnicode(nsDependentString(
228 (const PRUnichar
*)mQPrinter
->printerName().constData()));
233 nsPrintSettingsQt::SetPrinterName(const PRUnichar
* aPrinter
)
235 QString
printername((const QChar
*)aPrinter
, NS_strlen(aPrinter
));
236 mQPrinter
->setPrinterName(printername
);
241 nsPrintSettingsQt::GetNumCopies(int32_t* aNumCopies
)
243 NS_ENSURE_ARG_POINTER(aNumCopies
);
244 *aNumCopies
= mQPrinter
->numCopies();
249 nsPrintSettingsQt::SetNumCopies(int32_t aNumCopies
)
251 mQPrinter
->setNumCopies(aNumCopies
);
256 nsPrintSettingsQt::GetScaling(double* aScaling
)
258 NS_ENSURE_ARG_POINTER(aScaling
);
259 qDebug()<<Q_FUNC_INFO
;
260 qDebug()<<"Scaling not implemented in Qt port";
261 *aScaling
= 1.0; //FIXME
262 return NS_ERROR_NOT_IMPLEMENTED
;
266 nsPrintSettingsQt::SetScaling(double aScaling
)
268 qDebug()<<Q_FUNC_INFO
;
269 qDebug()<<"Scaling not implemented in Qt port"; //FIXME
270 return NS_ERROR_NOT_IMPLEMENTED
;
273 static const char* const indexToPaperName
[] =
274 { "A4", "B5", "Letter", "Legal", "Executive",
275 "A0", "A1", "A2", "A3", "A5", "A6", "A7", "A8", "A9",
276 "B0", "B1", "B10", "B2", "B3", "B4", "B6", "B7", "B8", "B9",
277 "C5E", "Comm10E", "DLE", "Folio", "Ledger", "Tabloid"
280 static const QPrinter::PageSize indexToQtPaperEnum
[] =
282 QPrinter::A4
, QPrinter::B5
, QPrinter::Letter
, QPrinter::Legal
,
283 QPrinter::Executive
, QPrinter::A0
, QPrinter::A1
, QPrinter::A2
, QPrinter::A3
,
284 QPrinter::A5
, QPrinter::A6
, QPrinter::A7
, QPrinter::A8
, QPrinter::A9
,
285 QPrinter::B0
, QPrinter::B1
, QPrinter::B10
, QPrinter::B2
, QPrinter::B3
,
286 QPrinter::B4
, QPrinter::B6
, QPrinter::B7
, QPrinter::B8
, QPrinter::B9
,
287 QPrinter::C5E
, QPrinter::Comm10E
, QPrinter::DLE
, QPrinter::Folio
,
288 QPrinter::Ledger
, QPrinter::Tabloid
292 nsPrintSettingsQt::GetPaperName(PRUnichar
** aPaperName
)
294 PR_STATIC_ASSERT(sizeof(indexToPaperName
)/
295 sizeof(char*) == QPrinter::NPageSize
);
296 PR_STATIC_ASSERT(sizeof(indexToQtPaperEnum
)/
297 sizeof(QPrinter::PageSize
) == QPrinter::NPageSize
);
299 QPrinter::PaperSize size
= mQPrinter
->paperSize();
300 QString
name(indexToPaperName
[size
]);
301 *aPaperName
= ToNewUnicode(nsDependentString
302 ((const PRUnichar
*)name
.constData()));
307 nsPrintSettingsQt::SetPaperName(const PRUnichar
* aPaperName
)
309 QString
ref((QChar
*)aPaperName
, NS_strlen(aPaperName
));
310 for (uint32_t i
= 0; i
< QPrinter::NPageSize
; i
++)
312 if (ref
== QString(indexToPaperName
[i
])) {
313 mQPrinter
->setPageSize(indexToQtPaperEnum
[i
]);
317 return NS_ERROR_FAILURE
;
320 QPrinter::Unit
GetQtUnit(int16_t aGeckoUnit
)
322 if (aGeckoUnit
== nsIPrintSettings::kPaperSizeMillimeters
) {
323 return QPrinter::Millimeter
;
325 return QPrinter::Inch
;
329 #define SETUNWRITEABLEMARGIN\
330 mQPrinter->setPageMargins(\
331 NS_TWIPS_TO_INCHES(mUnwriteableMargin.left),\
332 NS_TWIPS_TO_INCHES(mUnwriteableMargin.top),\
333 NS_TWIPS_TO_INCHES(mUnwriteableMargin.right),\
334 NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom),\
338 nsPrintSettingsQt::SetUnwriteableMarginInTwips(nsIntMargin
& aUnwriteableMargin
)
340 nsPrintSettings::SetUnwriteableMarginInTwips(aUnwriteableMargin
);
346 nsPrintSettingsQt::SetUnwriteableMarginTop(double aUnwriteableMarginTop
)
348 nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop
);
354 nsPrintSettingsQt::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft
)
356 nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft
);
362 nsPrintSettingsQt::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom
)
364 nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom
);
370 nsPrintSettingsQt::SetUnwriteableMarginRight(double aUnwriteableMarginRight
)
372 nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight
);
378 nsPrintSettingsQt::GetPaperWidth(double* aPaperWidth
)
380 NS_ENSURE_ARG_POINTER(aPaperWidth
);
381 QSizeF papersize
= mQPrinter
->paperSize(GetQtUnit(mPaperSizeUnit
));
382 *aPaperWidth
= papersize
.width();
387 nsPrintSettingsQt::SetPaperWidth(double aPaperWidth
)
389 QSizeF papersize
= mQPrinter
->paperSize(GetQtUnit(mPaperSizeUnit
));
390 papersize
.setWidth(aPaperWidth
);
391 mQPrinter
->setPaperSize(papersize
, GetQtUnit(mPaperSizeUnit
));
396 nsPrintSettingsQt::GetPaperHeight(double* aPaperHeight
)
398 NS_ENSURE_ARG_POINTER(aPaperHeight
);
399 QSizeF papersize
= mQPrinter
->paperSize(GetQtUnit(mPaperSizeUnit
));
400 *aPaperHeight
= papersize
.height();
405 nsPrintSettingsQt::SetPaperHeight(double aPaperHeight
)
407 QSizeF papersize
= mQPrinter
->paperSize(GetQtUnit(mPaperSizeUnit
));
408 papersize
.setHeight(aPaperHeight
);
409 mQPrinter
->setPaperSize(papersize
, GetQtUnit(mPaperSizeUnit
));
414 nsPrintSettingsQt::SetPaperSizeUnit(int16_t aPaperSizeUnit
)
416 mPaperSizeUnit
= aPaperSizeUnit
;
421 nsPrintSettingsQt::GetEffectivePageSize(double* aWidth
, double* aHeight
)
423 QSizeF papersize
= mQPrinter
->paperSize(QPrinter::Inch
);
424 if (mQPrinter
->orientation() == QPrinter::Landscape
) {
425 *aWidth
= NS_INCHES_TO_INT_TWIPS(papersize
.height());
426 *aHeight
= NS_INCHES_TO_INT_TWIPS(papersize
.width());
428 *aWidth
= NS_INCHES_TO_INT_TWIPS(papersize
.width());
429 *aHeight
= NS_INCHES_TO_INT_TWIPS(papersize
.height());