File indexing completed on 2024-05-12 16:06:38

0001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; c-brace-offset: 0; -*-
0002 //
0003 // pageSize.h
0004 //
0005 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
0006 //
0007 // SPDX-FileCopyrightText: 2002-2005 Stefan Kebekus
0008 // SPDX-License-Identifier: GPL-2.0-or-later
0009 
0010 #ifndef PAGESIZE_H
0011 #define PAGESIZE_H
0012 
0013 #include "simplePageSize.h"
0014 
0015 #include <QObject>
0016 
0017 class QString;
0018 
0019 /* \brief This class represents physical page sizes.
0020 
0021 The main difference to the SimplePageSize class are the following.
0022 
0023 - This class knows about standard page sizes and accepts page sizes in
0024   various formats, e.g. as a string "DIN A4", or by specifying the
0025   page width and height. Several units (inch, millimeters,
0026   centimeters) are possible.
0027 
0028 - It is made sure that page width an height are always in a reasonable
0029   range, which is currently set to 5cm .. 50cm
0030 
0031 - The default constructor provides a locale-depending default.
0032 
0033 @author Stefan Kebekus <kebekus@kde.org>
0034 @version 1.0.0
0035 */
0036 
0037 class pageSize : public QObject, public SimplePageSize
0038 {
0039     Q_OBJECT
0040 
0041 public:
0042     /** \brief Default constructor, initializes the pageSize with a
0043         reasonable default
0044 
0045         The default chosen depends on the locale. At the moment, A4 size
0046         is chosen for countries with metric measurement system, and US
0047         letter otherwise.
0048     */
0049     pageSize();
0050 
0051     /** \brief Initializes the pageSize with a SimplePageSize. */
0052     explicit pageSize(const SimplePageSize &);
0053 
0054     pageSize(const pageSize &) = delete;
0055     pageSize &operator=(const pageSize &) = delete;
0056 
0057     /** \brief List of standard pageSizes
0058 
0059     This method returns the names of standard pageSizes,
0060     e.g. "A4". These can be used, e.g., by a QComboBox to let the user
0061     choose known sizes. The returned list is also a list of all possible
0062     return values of the formatName() method explained below. If you
0063     call pageSizeNames() more than once, it is guaranteed that the
0064     same list of strings will be returned.
0065 
0066     @returns QStringList that contains
0067     */
0068     QStringList pageSizeNames();
0069 
0070     /** \brief Set page size by name.
0071 
0072     Acceptable strings are
0073 
0074     (1) a name from the list returned by pageSizeNames(), such as "DIN
0075         A4"
0076 
0077     (2) a string like "500x300", which describes a page of width 500mm
0078         and height 300mm.
0079 
0080     (3) a string like "3in, 4 cm". A number of different units,
0081         including "in", "mm" and "cm", and a few typographical units are
0082         recognized
0083 
0084     If the name is not of these types, and error message is printed to
0085     stderr using kError() and a default value, which depends on the
0086     locale, is set.
0087 
0088     In any case, the values will be trimmed so as not to exceed the
0089     minima/maxima of 5cm and 50cm, respectively. If the page size found
0090     matches one of the standard sizes by an error of no more than 2mm,
0091     the standard page size will be set. The signal sizeChanged() will
0092     always be emitted.
0093 
0094     @param name string that represents the page size
0095 
0096     @returns 'True', if the parameter could be parsed, and 'false'
0097     otherwise.
0098     */
0099     bool setPageSize(const QString &name);
0100 
0101     /** \brief Set page size from width and height strings
0102 
0103     Sets the page size to "width" and "height", given in the associated
0104     units. Currently, "mm", "cm" and "in" are supported. If a unit is
0105     not recognized, "mm" is silently assumed, and error message is
0106     printed to stderr using kError(). If the page size set matches one
0107     of the standard sizes by an error of no more than 2mm, the standard
0108     page size will be set.  If width or height does not contain a
0109     number, the result is an undefined value. However, it is guaranteed
0110     in any case that both width and height are between the minimal and
0111     maximal possible values, i.e. in the range 5..50 cm. If the newly
0112     set value differs from the old value by more that 2mm for width or
0113     height, the signal sizeChanged() will be emitted
0114 
0115     @param width string that represents the page width as a number,
0116     e.g., " 300 "
0117 
0118     @param widthUnits units for the width string. Currently "mm", "cm"
0119     and "in" are allowed.
0120 
0121     @param height string that represents the page height as a number,
0122     e.g., " 300 "
0123 
0124     @param heightUnits units for the height string. Currently "mm", "cm"
0125     and "in" are allowed.
0126     */
0127     void setPageSize(const QString &width, const QString &widthUnits, const QString &height, const QString &heightUnits);
0128 
0129     /** \brief Set page size
0130 
0131     Sets the page size to "width" and "height", given in millimeter.  If
0132     the page size set matches one of the standard sizes by an error of
0133     no more than 2mm, the standard page size will be set.  Values are
0134     trimmed so that both width and height are between the minimal and
0135     maximal possible values, i.e. in the range 5..50 cm. If the newly
0136     set value differs from the old value by more that 2mm for width or
0137     height, the signal sizeChanged() will be emitted
0138 
0139     @param width_in_mm page width in mm
0140 
0141     @param height_in_mm page height in mm
0142     */
0143     void setPageSize(double width_in_mm, double height_in_mm);
0144     using SimplePageSize::setPageSize;
0145 
0146     /** \brief Preferred unit for the current page size
0147 
0148     @returns The name of the unit, one of "cm", "mm" or "in", which is
0149         most commonly used with the current page format. For instance,
0150         US Letter and US Legal are best given in inches, to avoid very
0151         odd numbers. If the page format is unknown, returns a guess
0152         based on the current locale. */
0153     QString preferredUnit() const;
0154 
0155     /** \brief Returns the page width as a string
0156 
0157     @param unit The unit in which the page width shall be returned. This
0158     must be one of "cm", "mm" or "in".
0159 
0160     @returns a string containing a number, e.g. "3.1415", which gives the page
0161     width in the given unit.  If the unit is not recognized, the string "--" is returned.
0162     */
0163     QString widthString(const QString &unit) const;
0164 
0165     /** \brief Returns the page height as a string
0166 
0167     @param unit The unit in height the page width shall be
0168     returned. This must be one of "cm", "mm" or "in".
0169 
0170     @returns a string containing a number which gives the page height in
0171     the given unit. If the unit is not recognized, the string "--" is
0172     returned.
0173     */
0174     QString heightString(const QString &unit) const;
0175 
0176     /** \brief Returns a name for the page size, if this is a standard
0177         size
0178 
0179         @warning This method does not take care of orientation, e.g. it
0180         will return "DIN A4" if the page size is either 210x297 or
0181         297x210.
0182 
0183         @returns A name for the current page size, if the format has a
0184         name, or QString() otherwise. If the result is not
0185         QString(), it is guaranteed to be one of the strings
0186         returned by the pageSizeNames() method.
0187     */
0188     QString formatName() const;
0189 
0190     /** \brief Returns an number for the page size, if this is a
0191         standard size
0192 
0193         @warning This method does not take care of orientation, e.g. it
0194         will return the same value if the page size is either 210x297 or
0195         297x210.
0196 
0197         @returns If the current format is one of the standard sizes, a
0198         non-negative integer is returned, which is an index to the
0199         QStringList returned by the pageSizeNames() method. If the
0200         current format is none of the standard sizes, -1 is returned.
0201     */
0202     int formatNumber() const
0203     {
0204         return currentSize;
0205     }
0206 
0207     /** \brief Gets orientation for standard sizes
0208 
0209     If the pageSize is one of the standard sizes, i.e. formatNumber() !=
0210     -1, this method can be used to get the orientation. If the pageSize
0211     is not a standard size, this method prints an error message stderr
0212     using kError().
0213 
0214     @returns 0 for 'portrait', or 1 for 'landscape'. If the size is none
0215     of the standard sizes, an undefined value is returned.
0216     */
0217     int getOrientation() const;
0218 
0219     /** \brief Returns a string that can be read by setPageSize(QString)
0220 
0221     @returns This method returns a string like "210x297". The numbers
0222     are page width and height in millimeters. The setPageSize(QString)
0223     method will understand this output.
0224     */
0225     QString serialize() const;
0226 
0227     /** \brief Returns a textual description of the page size. */
0228     QString description() const;
0229 
0230 public Q_SLOTS:
0231     /** \brief Sets orientation
0232 
0233     If the pageSize is one of the standard sizes, i.e. formatNumber() !=
0234     -1, this method can be used to set the orientation. If the pageSize
0235     is not a standard size, this method prints an error message stderr
0236     using kError() and does nothing.
0237 
0238     @param orient 0 sets 'portrait orientation', 1 sets 'landscape'
0239    */
0240     void setOrientation(int orient);
0241 
0242 Q_SIGNALS:
0243     /** \brief Signal emitted when the page sizes changes
0244 
0245     emitted to indicate that the size changed. Not emitted immediately
0246     after construction, when the constructor sets the default
0247     size.
0248 
0249     @param t a pointer to this
0250     */
0251     void sizeChanged(const SimplePageSize &t);
0252 
0253 private:
0254     /** Makes sure that pageWidth and pageHeight are in the permissible
0255         range and not, e.g., negative. */
0256     void rectifySizes();
0257 
0258     /** Tries to find one of the known sizes which matches pageWidth and
0259         pageHeight, with an error margin of 2 millimeters. If found, the
0260         value of 'currentsize' is set to point to the known size, and
0261         pageWidth and pageHeight are set to the correct values for that
0262         size. Otherwise, currentSize is set to -1 to indicate "custom
0263         size". Note: this method does not take care of orientation,
0264         e.g. the method will set 'currentsize' to point to "DIN A4" if
0265         either the page size is 210x297 or 297x210. */
0266     void reconstructCurrentSize();
0267 
0268     /** Gives a default value for currentSize, which depends on the
0269         locale. In countries with metric system, this will be "DIN A4",
0270         in countries with the imperial system, "US Letter". */
0271     int defaultPageSize();
0272 
0273     /** Permissible range: 0--#Size_of_array staticList, or -1 to
0274         indicate a "user defined setting". Other values may lead to a
0275         segfault. */
0276     int currentSize;
0277 };
0278 
0279 #endif