Show paper size names like "ISO/DIN A4" instead of just paper size in numbers

REVIEW: 107350
This commit is contained in:
Thomas Fischer 2012-12-31 11:14:54 +01:00 committed by Albert Astals Cid
parent 05e3e017c3
commit 96c69528d4
2 changed files with 104 additions and 2 deletions

View file

@ -153,6 +153,107 @@ QString DocumentPrivate::pagesSizeString() const
else return QString();
}
QString DocumentPrivate::namePaperSize(double inchesWidth, double inchesHeight) const
{
// Account for small deviations in paper sizes
static const double marginFactor = 0.03;
static const double lowerBoundFactor = 1.0 - marginFactor;
static const double upperBoundFactor = 1.0 + marginFactor;
const QPrinter::Orientation orientation = inchesWidth > inchesHeight ? QPrinter::Landscape : QPrinter::Portrait;
// enforce portrait mode for further tests
if (inchesWidth > inchesHeight)
qSwap(inchesWidth, inchesHeight);
// Use QPrinter to find which of the predefined paper sizes
// matches best the given paper width and height
QPrinter dummyPrinter;
QPrinter::PaperSize paperSize = QPrinter::Custom;
for (int i = 0; i < (int)QPrinter::NPaperSize; ++i)
{
const QPrinter::PaperSize ps = (QPrinter::PaperSize)i;
dummyPrinter.setPaperSize(ps);
const QSizeF definedPaperSize = dummyPrinter.paperSize(QPrinter::Inch);
if (inchesWidth > definedPaperSize.width() * lowerBoundFactor && inchesWidth < definedPaperSize.width() * upperBoundFactor
&& inchesHeight > definedPaperSize.height() * lowerBoundFactor && inchesHeight < definedPaperSize.height() * upperBoundFactor)
{
paperSize = ps;
break;
}
}
// Handle all paper sizes defined in QPrinter,
// return string depending if paper's orientation is landscape or portrait
switch (paperSize) {
case QPrinter::A0:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A0") : i18nc("paper size", "portrait DIN/ISO A0");
case QPrinter::A1:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A1") : i18nc("paper size", "portrait DIN/ISO A1");
case QPrinter::A2:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A2") : i18nc("paper size", "portrait DIN/ISO A2");
case QPrinter::A3:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A3") : i18nc("paper size", "portrait DIN/ISO A3");
case QPrinter::A4:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A4") : i18nc("paper size", "portrait DIN/ISO A4");
case QPrinter::A5:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A5") : i18nc("paper size", "portrait DIN/ISO A5");
case QPrinter::A6:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A6") : i18nc("paper size", "portrait DIN/ISO A6");
case QPrinter::A7:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A7") : i18nc("paper size", "portrait DIN/ISO A7");
case QPrinter::A8:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A8") : i18nc("paper size", "portrait DIN/ISO A8");
case QPrinter::A9:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO A9") : i18nc("paper size", "portrait DIN/ISO A9");
case QPrinter::B0:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B0") : i18nc("paper size", "portrait DIN/ISO B0");
case QPrinter::B1:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B1") : i18nc("paper size", "portrait DIN/ISO B1");
case QPrinter::B2:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B2") : i18nc("paper size", "portrait DIN/ISO B2");
case QPrinter::B3:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B3") : i18nc("paper size", "portrait DIN/ISO B3");
case QPrinter::B4:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B4") : i18nc("paper size", "portrait DIN/ISO B4");
case QPrinter::B5:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B5") : i18nc("paper size", "portrait DIN/ISO B5");
case QPrinter::B6:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B6") : i18nc("paper size", "portrait DIN/ISO B6");
case QPrinter::B7:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B7") : i18nc("paper size", "portrait DIN/ISO B7");
case QPrinter::B8:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B8") : i18nc("paper size", "portrait DIN/ISO B8");
case QPrinter::B9:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B9") : i18nc("paper size", "portrait DIN/ISO B9");
case QPrinter::B10:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DIN/ISO B10") : i18nc("paper size", "portrait DIN/ISO B10");
case QPrinter::Letter:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape letter") : i18nc("paper size", "portrait letter");
case QPrinter::Legal:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape legal") : i18nc("paper size", "portrait legal");
case QPrinter::Executive:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape executive") : i18nc("paper size", "portrait executive");
case QPrinter::C5E:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape C5E") : i18nc("paper size", "portrait C5E");
case QPrinter::Comm10E:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape Comm10E") : i18nc("paper size", "portrait Comm10E");
case QPrinter::DLE:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape DLE") : i18nc("paper size", "portrait DLE");
case QPrinter::Folio:
return orientation == QPrinter::Landscape ? i18nc("paper size", "landscape folio") : i18nc("paper size", "portrait folio");
case QPrinter::Tabloid:
case QPrinter::Ledger:
/// Ledger and Tabloid are the same, just rotated by 90 degrees
return orientation == QPrinter::Landscape ? i18nc("paper size", "ledger") : i18nc("paper size", "tabloid");
case QPrinter::Custom:
return orientation == QPrinter::Landscape ? i18nc("paper size", "unknown landscape paper size") : i18nc("paper size", "unknown portrait paper size");
}
kWarning() << "PaperSize" << paperSize << "has not been covered";
return QString();
}
QString DocumentPrivate::localizedSize(const QSizeF &size) const
{
double inchesWidth = 0, inchesHeight = 0;
@ -168,11 +269,11 @@ QString DocumentPrivate::localizedSize(const QSizeF &size) const
}
if (KGlobal::locale()->measureSystem() == KLocale::Imperial)
{
return i18n("%1 x %2 in", inchesWidth, inchesHeight);
return i18nc("%1 is width, %2 is height, %3 is paper size name", "%1 x %2 in (%3)", inchesWidth, inchesHeight, namePaperSize(inchesWidth, inchesHeight));
}
else
{
return i18n("%1 x %2 mm", inchesWidth * 25.4, inchesHeight * 25.4);
return i18nc("%1 is width, %2 is height, %3 is paper size name", "%1 x %2 mm (%3)", QString::number(inchesWidth * 25.4, 'd', 0), QString::number(inchesHeight * 25.4, 'd', 0), namePaperSize(inchesWidth, inchesHeight));
}
}

View file

@ -109,6 +109,7 @@ class DocumentPrivate
// private methods
QString pagesSizeString() const;
QString namePaperSize(double inchesWidth, double inchesHeight) const;
QString localizedSize(const QSizeF &size) const;
qulonglong calculateMemoryToFree();
void cleanupPixmapMemory();