Merge m-c to fx-team.
[gecko.git] / widget / qt / nsPrintSettingsQt.cpp
blob342439b88bfd7e8eb73adcb1ce1fab1c09e3bc5a
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/. */
7 #include <QPrinter>
8 #include <QDebug>
9 #include "nsPrintSettingsQt.h"
10 #include "nsIFile.h"
11 #include "nsCRTGlue.h"
13 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsQt,
14 nsPrintSettings,
15 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)
32 nsPrintSettingsQt&
33 nsPrintSettingsQt::operator=(const nsPrintSettingsQt& rhs)
35 if (this == &rhs) {
36 return *this;
39 nsPrintSettings::operator=(rhs);
40 mQPrinter = rhs.mQPrinter;
41 return *this;
44 nsresult
45 nsPrintSettingsQt::_Clone(nsIPrintSettings** _retval)
47 NS_ENSURE_ARG_POINTER(_retval);
49 nsPrintSettingsQt* newSettings = new nsPrintSettingsQt(*this);
50 *_retval = newSettings;
51 NS_ADDREF(*_retval);
52 return NS_OK;
55 NS_IMETHODIMP
56 nsPrintSettingsQt::_Assign(nsIPrintSettings* aPS)
58 nsPrintSettingsQt* printSettingsQt = static_cast<nsPrintSettingsQt*>(aPS);
59 if (!printSettingsQt)
60 return NS_ERROR_UNEXPECTED;
61 *this = *printSettingsQt;
62 return NS_OK;
65 NS_IMETHODIMP
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;
75 } else {
76 *aPrintRange = kRangeAllPages;
79 return NS_OK;
82 NS_IMETHODIMP
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);
89 } else {
90 mQPrinter->setPrintRange(QPrinter::AllPages);
92 return NS_OK;
95 NS_IMETHODIMP
96 nsPrintSettingsQt::GetStartPageRange(int32_t* aStartPageRange)
98 NS_ENSURE_ARG_POINTER(aStartPageRange);
99 int32_t start = mQPrinter->fromPage();
100 *aStartPageRange = start;
101 return NS_OK;
104 NS_IMETHODIMP
105 nsPrintSettingsQt::SetStartPageRange(int32_t aStartPageRange)
107 int32_t endRange = mQPrinter->toPage();
108 mQPrinter->setFromTo(aStartPageRange, endRange);
109 return NS_OK;
112 NS_IMETHODIMP
113 nsPrintSettingsQt::GetEndPageRange(int32_t* aEndPageRange)
115 NS_ENSURE_ARG_POINTER(aEndPageRange);
116 int32_t end = mQPrinter->toPage();
117 *aEndPageRange = end;
118 return NS_OK;
121 NS_IMETHODIMP
122 nsPrintSettingsQt::SetEndPageRange(int32_t aEndPageRange)
124 int32_t startRange = mQPrinter->fromPage();
125 mQPrinter->setFromTo(startRange, aEndPageRange);
126 return NS_OK;
129 NS_IMETHODIMP
130 nsPrintSettingsQt::GetPrintReversed(bool* aPrintReversed)
132 NS_ENSURE_ARG_POINTER(aPrintReversed);
133 if (mQPrinter->pageOrder() == QPrinter::LastPageFirst) {
134 *aPrintReversed = true;
135 } else {
136 *aPrintReversed = false;
138 return NS_OK;
141 NS_IMETHODIMP
142 nsPrintSettingsQt::SetPrintReversed(bool aPrintReversed)
144 if (aPrintReversed) {
145 mQPrinter->setPageOrder(QPrinter::LastPageFirst);
146 } else {
147 mQPrinter->setPageOrder(QPrinter::FirstPageFirst);
149 return NS_OK;
152 NS_IMETHODIMP
153 nsPrintSettingsQt::GetPrintInColor(bool* aPrintInColor)
155 NS_ENSURE_ARG_POINTER(aPrintInColor);
156 if (mQPrinter->colorMode() == QPrinter::Color) {
157 *aPrintInColor = true;
158 } else {
159 *aPrintInColor = false;
161 return NS_OK;
163 NS_IMETHODIMP
164 nsPrintSettingsQt::SetPrintInColor(bool aPrintInColor)
166 if (aPrintInColor) {
167 mQPrinter->setColorMode(QPrinter::Color);
168 } else {
169 mQPrinter->setColorMode(QPrinter::GrayScale);
171 return NS_OK;
174 NS_IMETHODIMP
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;
181 } else {
182 *aOrientation = kPortraitOrientation;
184 return NS_OK;
187 NS_IMETHODIMP
188 nsPrintSettingsQt::SetOrientation(int32_t aOrientation)
190 if (aOrientation == kLandscapeOrientation) {
191 mQPrinter->setOrientation(QPrinter::Landscape);
192 } else {
193 mQPrinter->setOrientation(QPrinter::Portrait);
195 return NS_OK;
198 NS_IMETHODIMP
199 nsPrintSettingsQt::GetToFileName(PRUnichar** aToFileName)
201 NS_ENSURE_ARG_POINTER(aToFileName);
202 QString filename;
203 filename = mQPrinter->outputFileName();
204 *aToFileName = ToNewUnicode(
205 nsDependentString((PRUnichar*)filename.data()));
206 return NS_OK;
209 NS_IMETHODIMP
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);
220 return NS_OK;
223 NS_IMETHODIMP
224 nsPrintSettingsQt::GetPrinterName(PRUnichar** aPrinter)
226 NS_ENSURE_ARG_POINTER(aPrinter);
227 *aPrinter = ToNewUnicode(nsDependentString(
228 (const PRUnichar*)mQPrinter->printerName().constData()));
229 return NS_OK;
232 NS_IMETHODIMP
233 nsPrintSettingsQt::SetPrinterName(const PRUnichar* aPrinter)
235 QString printername((const QChar*)aPrinter, NS_strlen(aPrinter));
236 mQPrinter->setPrinterName(printername);
237 return NS_OK;
240 NS_IMETHODIMP
241 nsPrintSettingsQt::GetNumCopies(int32_t* aNumCopies)
243 NS_ENSURE_ARG_POINTER(aNumCopies);
244 *aNumCopies = mQPrinter->numCopies();
245 return NS_OK;
248 NS_IMETHODIMP
249 nsPrintSettingsQt::SetNumCopies(int32_t aNumCopies)
251 mQPrinter->setNumCopies(aNumCopies);
252 return NS_OK;
255 NS_IMETHODIMP
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;
265 NS_IMETHODIMP
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
291 NS_IMETHODIMP
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()));
303 return NS_OK;
306 NS_IMETHODIMP
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]);
314 return NS_OK;
317 return NS_ERROR_FAILURE;
320 QPrinter::Unit GetQtUnit(int16_t aGeckoUnit)
322 if (aGeckoUnit == nsIPrintSettings::kPaperSizeMillimeters) {
323 return QPrinter::Millimeter;
324 } else {
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),\
335 QPrinter::Inch);
337 NS_IMETHODIMP
338 nsPrintSettingsQt::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin)
340 nsPrintSettings::SetUnwriteableMarginInTwips(aUnwriteableMargin);
341 SETUNWRITEABLEMARGIN
342 return NS_OK;
345 NS_IMETHODIMP
346 nsPrintSettingsQt::SetUnwriteableMarginTop(double aUnwriteableMarginTop)
348 nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop);
349 SETUNWRITEABLEMARGIN
350 return NS_OK;
353 NS_IMETHODIMP
354 nsPrintSettingsQt::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft)
356 nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft);
357 SETUNWRITEABLEMARGIN
358 return NS_OK;
361 NS_IMETHODIMP
362 nsPrintSettingsQt::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom)
364 nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom);
365 SETUNWRITEABLEMARGIN
366 return NS_OK;
369 NS_IMETHODIMP
370 nsPrintSettingsQt::SetUnwriteableMarginRight(double aUnwriteableMarginRight)
372 nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight);
373 SETUNWRITEABLEMARGIN
374 return NS_OK;
377 NS_IMETHODIMP
378 nsPrintSettingsQt::GetPaperWidth(double* aPaperWidth)
380 NS_ENSURE_ARG_POINTER(aPaperWidth);
381 QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit));
382 *aPaperWidth = papersize.width();
383 return NS_OK;
386 NS_IMETHODIMP
387 nsPrintSettingsQt::SetPaperWidth(double aPaperWidth)
389 QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit));
390 papersize.setWidth(aPaperWidth);
391 mQPrinter->setPaperSize(papersize, GetQtUnit(mPaperSizeUnit));
392 return NS_OK;
395 NS_IMETHODIMP
396 nsPrintSettingsQt::GetPaperHeight(double* aPaperHeight)
398 NS_ENSURE_ARG_POINTER(aPaperHeight);
399 QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit));
400 *aPaperHeight = papersize.height();
401 return NS_OK;
404 NS_IMETHODIMP
405 nsPrintSettingsQt::SetPaperHeight(double aPaperHeight)
407 QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit));
408 papersize.setHeight(aPaperHeight);
409 mQPrinter->setPaperSize(papersize, GetQtUnit(mPaperSizeUnit));
410 return NS_OK;
413 NS_IMETHODIMP
414 nsPrintSettingsQt::SetPaperSizeUnit(int16_t aPaperSizeUnit)
416 mPaperSizeUnit = aPaperSizeUnit;
417 return NS_OK;
420 NS_IMETHODIMP
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());
427 } else {
428 *aWidth = NS_INCHES_TO_INT_TWIPS(papersize.width());
429 *aHeight = NS_INCHES_TO_INT_TWIPS(papersize.height());
431 return NS_OK;