File indexing completed on 2024-05-12 04:33:58

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
0002 //
0003 // simplePageSize.h
0004 //
0005 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
0006 //
0007 // SPDX-FileCopyrightText: 2002-2004 Stefan Kebekus
0008 // SPDX-License-Identifier: GPL-2.0-or-later
0009 
0010 #ifndef SIMPLEPAGESIZE_H
0011 #define SIMPLEPAGESIZE_H
0012 
0013 #include "length.h"
0014 
0015 #include <QSize>
0016 
0017 class QPaintDevice;
0018 
0019 /** \brief This class represents physical page sizes.
0020 
0021 This class represents page sizes. It contains nothing but two numbers,
0022 the page width, and page height, and a few utility functions that
0023 convert page sizes to pixel sizes and to compute the aspect ratio. A
0024 page with width<=1mm or height<=1mm is considered invalid. pageSize is
0025 a more elaborate class that is derived from SimplePageSize and knows
0026 about standard paper sizes.
0027 
0028 @author Stefan Kebekus <kebekus@kde.org>
0029 @version 1.0 0
0030 */
0031 
0032 class SimplePageSize
0033 {
0034 public:
0035     /** Constructs an invalid SimplePageSize, with size 0x0mm */
0036     SimplePageSize()
0037     {
0038         pageWidth.setLength_in_mm(0.0);
0039         pageHeight.setLength_in_mm(0.0);
0040     }
0041 
0042     /** Constructs a SimplePagesize with given page width and height in
0043         mm. Recall that if width or height is less or equal than 1mm,
0044         the page size is considered 'invalid' by the isValid()
0045         method.
0046 
0047         @param width
0048         @param height
0049     */
0050     SimplePageSize(const Length width, const Length height)
0051         : pageWidth(width)
0052         , pageHeight(height)
0053     {
0054     }
0055 
0056     ~SimplePageSize()
0057     {
0058     }
0059 
0060     /** \brief Sets the page width and height
0061 
0062     If width or height is less or equal than 1mm, the page size is
0063     considered 'invalid' by the isValid() method.
0064 
0065     @param width
0066     @param height
0067     */
0068     void setPageSize(const Length width, const Length height)
0069     {
0070         pageWidth = width;
0071         pageHeight = height;
0072     }
0073 
0074     /** \brief Returns the page width. */
0075     Length width() const
0076     {
0077         return pageWidth;
0078     }
0079 
0080     /** \brief Returns the page height. */
0081     Length height() const
0082     {
0083         return pageHeight;
0084     }
0085 
0086     /** \brief Aspect ratio
0087 
0088     @returns if the paper size is valid, this method returns the ratio
0089     width/height. Returns 1.0 otherwise. */
0090     double aspectRatio() const
0091     {
0092         return isValid() ? (pageWidth / pageHeight) : 1.0;
0093     }
0094 
0095     /** \brief Converts the physical page size to a pixel size
0096 
0097     @param resolution in dots per inch
0098 
0099     @returns the pixel size, represented by a QSize. If the page size is
0100         invalid, the result is undefined. */
0101     QSize sizeInPixel(double resolution) const
0102     {
0103         return QSize((int)(resolution * pageWidth.getLength_in_inch() + 0.5), (int)(resolution * pageHeight.getLength_in_inch() + 0.5));
0104     }
0105 
0106     /** \brief Zoom value required to scale to a certain height
0107 
0108     If the pageSize is valid, this method returns the zoom value
0109     required to scale the page size down to 'height' pixels on the
0110     currently used display. If the pageSize is invalid,
0111     an error message is printed, and an undefined value is returned.
0112 
0113     @param height target height in pixels
0114     @param pd the widget to be printed on.
0115 
0116     @returns the zoom value required to scale the page size down to
0117     'height' pixels. If the pageSize is invalid, an undefined value is
0118     returned.
0119     */
0120     double zoomForHeight(quint32 height, const QPaintDevice &pd) const;
0121 
0122     /** \brief Zoom value required to scale to a certain height
0123 
0124     If the pageSize is valid, this method returns the zoom value
0125     required to scale the page size down to 'width' pixels on the
0126     currently used display. If the pageSize is invalid,
0127     an error message is printed, and an undefined value is returned.
0128 
0129     @param width target width in pixels
0130     @param pd the widget to be printed on.
0131 
0132     @returns the zoom value required to scale the page size down to
0133     'width' pixels. If the pageSize is invalid, an undefined value is
0134     returned.
0135     */
0136     double zoomForWidth(quint32 width, const QPaintDevice &pd) const;
0137 
0138     /** \brief Returns a zoom to fit into a certain page size
0139 
0140     This method computes the larget zoom value such that *this, zoomed
0141     by the computed values fits into the page size 'target'. If *this or
0142     if target are invalid, or is this->isSmall() is true, an undefined
0143     value is returned. If height or width of this is nearly 0.0, a
0144     floating point exception may occur.
0145     */
0146     double zoomToFitInto(const SimplePageSize &target) const;
0147 
0148     /** \brief Validity check
0149 
0150     @returns 'True' if the page width and height are both larger than
0151     1mm */
0152     bool isValid() const
0153     {
0154         return ((pageWidth.getLength_in_mm() > 1.0) && (pageHeight.getLength_in_mm() > 1.0));
0155     }
0156 
0157     /** \brief Validity check:
0158 
0159     @returns 'True' if the page ares is less than 1.0 square mm
0160     */
0161     bool isSmall() const
0162     {
0163         return (pageWidth.getLength_in_mm() * pageHeight.getLength_in_mm() < 1.0);
0164     }
0165 
0166     /** \brief Approximate equality
0167 
0168     @param size pageSize object to compare this object with
0169 
0170     @returns 'True' if height and width of the two objects differ by at
0171         most 2mm, 'false' otherwise
0172     */
0173     bool isNearlyEqual(const SimplePageSize &size) const
0174     {
0175         return (pageWidth.isNearlyEqual(size.pageWidth) && pageHeight.isNearlyEqual(size.pageHeight));
0176     }
0177 
0178     /** Test if paper size is higher than wide
0179 
0180     @returns 'True' if the paper size is higher than wide
0181      */
0182     bool isPortrait() const
0183     {
0184         return (pageHeight >= pageWidth);
0185     }
0186 
0187     /** Rotates by 90 degrees
0188 
0189     @returns a SimplePageSize with height and width swapped. The
0190     original instance is unchanged.
0191      */
0192     SimplePageSize rotate90() const
0193     {
0194         return SimplePageSize(pageHeight, pageWidth);
0195     }
0196 
0197 protected:
0198     Length pageWidth;
0199     Length pageHeight;
0200 };
0201 
0202 #endif