File indexing completed on 2024-05-12 16:33:35

0001 /* This file is part of the KDE project
0002 
0003    Copyright 2010 Johannes Simon <johannes.simon@gmail.com>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018    Boston, MA 02110-1301, USA.
0019 */
0020 
0021 // Calligra
0022 #include <KoUnit.h>
0023 #include <KoDpi.h>
0024 
0025 // KoChart
0026 #include "ScreenConversions.h"
0027 
0028 // Qt
0029 #include <QPainter>
0030 #include <QWidget>
0031 #include <QSize>
0032 #include <QSizeF>
0033 #include <QRectF>
0034 #include <QRect>
0035 
0036 using namespace KoChart;
0037 
0038 qreal ScreenConversions::pxToPtX(qreal px)
0039 {
0040     return KoUnit(KoUnit::Inch).fromUserValue(px / KoDpi::dpiX());
0041 }
0042 
0043 qreal ScreenConversions::pxToPtY(qreal px)
0044 {
0045     return KoUnit(KoUnit::Inch).fromUserValue(px / KoDpi::dpiY());
0046 }
0047 
0048 qreal ScreenConversions::ptToPxX(qreal pt, const QPaintDevice* paintDevice)
0049 {
0050     const int dpiX = paintDevice ? paintDevice->logicalDpiX() : KoDpi::dpiX();
0051     return KoUnit::toInch(pt) * dpiX;
0052 }
0053 
0054 qreal ScreenConversions::ptToPxY(qreal pt, const QPaintDevice* paintDevice)
0055 {
0056     const int dpiY = paintDevice ? paintDevice->logicalDpiY() : KoDpi::dpiY();
0057     return KoUnit::toInch(pt) * dpiY;
0058 }
0059 
0060 qreal ScreenConversions::ptToPxX(qreal pt)
0061 {
0062     return KoUnit::toInch(pt) * KoDpi::dpiX();
0063 }
0064 
0065 qreal ScreenConversions::ptToPxY(qreal pt)
0066 {
0067     return KoUnit::toInch(pt) * KoDpi::dpiY();
0068 }
0069 
0070 void ScreenConversions::scaleFromPtToPx(QPainter &painter)
0071 {
0072     const qreal inPerPt = KoUnit::toInch(1.0);
0073     int dpiX;
0074     int dpiY;
0075     if (dynamic_cast<QWidget*>(painter.device()) != 0) {
0076         dpiX = KoDpi::dpiX();
0077         dpiY = KoDpi::dpiY();
0078     } else {
0079         dpiX = painter.device()->logicalDpiX();
0080         dpiY = painter.device()->logicalDpiY();
0081     }
0082     painter.scale(1.0 / (inPerPt * dpiX), 1.0 / (inPerPt * dpiY));
0083 }
0084 
0085 QSize ScreenConversions::scaleFromPtToPx(const QSizeF &size, const QPaintDevice* paintDevice)
0086 {
0087     return QSizeF(ptToPxX(size.width(), paintDevice),
0088                   ptToPxY(size.height(), paintDevice)).toSize();
0089 }
0090 
0091 QSize ScreenConversions::scaleFromPtToPx(const QSizeF &size, QPainter &painter)
0092 {
0093     QPaintDevice* paintDevice = painter.device();
0094     if (dynamic_cast<QWidget*>(paintDevice) != 0) {
0095         paintDevice = 0;
0096     }
0097 
0098     return scaleFromPtToPx(size, paintDevice);
0099 }
0100 
0101 QSize ScreenConversions::scaleFromPtToPx(const QSizeF &size)
0102 {
0103     return QSizeF(ptToPxX(size.width()), ptToPxY(size.height())).toSize();
0104 }
0105 
0106 QSizeF ScreenConversions::scaleFromPxToPt(const QSize &size)
0107 {
0108     return QSizeF(pxToPtX(size.width()), pxToPtY(size.height()));
0109 }
0110 
0111 QPoint ScreenConversions::scaleFromPtToPx(const QPointF &point, const QPaintDevice* paintDevice)
0112 {
0113     return QPointF(ptToPxX(point.x(), paintDevice),
0114                    ptToPxY(point.y(), paintDevice)).toPoint();
0115 }
0116 
0117 QRect ScreenConversions::scaleFromPtToPx(const QRectF &rect, QPainter &painter)
0118 {
0119     QPaintDevice* paintDevice = painter.device();
0120     if (dynamic_cast<QWidget*>(paintDevice) != 0) {
0121         paintDevice = 0;
0122     }
0123 
0124     return QRect(scaleFromPtToPx(rect.topLeft(), paintDevice),
0125                  scaleFromPtToPx(rect.size(), paintDevice));
0126 }
0127 
0128 QSizeF ScreenConversions::toWidgetDpi(QWidget *widget, const QSizeF &size)
0129 {
0130     QSizeF s = size;
0131     const qreal scaleX = static_cast<qreal>(widget->logicalDpiX()) / KoDpi::dpiX();
0132     const qreal scaleY = static_cast<qreal>(widget->logicalDpiY()) / KoDpi::dpiY();
0133     s.setWidth(s.width() * scaleX);
0134     s.setHeight(s.height() * scaleY);
0135     return s;
0136 }
0137 
0138 QSizeF ScreenConversions::fromWidgetDpi(QWidget *widget, const QSizeF &size)
0139 {
0140     QSizeF s = size;
0141     const qreal scaleX = static_cast<qreal>(KoDpi::dpiX()) / widget->logicalDpiX();
0142     const qreal scaleY = static_cast<qreal>(KoDpi::dpiY()) / widget->logicalDpiY();
0143     s.setWidth(s.width() * scaleX);
0144     s.setHeight(s.height() * scaleY);
0145     return s;
0146 }
0147 
0148 void ScreenConversions::scaleToWidgetDpi(QWidget *widget, QPainter &painter)
0149 {
0150     // only scale if we paint into a QWidget
0151     if (dynamic_cast<QWidget*>(painter.device())) {
0152         const qreal scaleX = static_cast<qreal>(KoDpi::dpiX()) / widget->logicalDpiX();
0153         const qreal scaleY = static_cast<qreal>(KoDpi::dpiY()) / widget->logicalDpiY();
0154         painter.scale(scaleX, scaleY);
0155     }
0156 }