summaryrefslogtreecommitdiffstats
path: root/kviewshell/pageSize.h
blob: 5ba0194e5070606dfa71b971f2497fce7b75d633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// -*- C++ -*-
//
// pageSize.h
//
// Part of KVIEWSHELL - A framework for multipage text/gfx viewers
//
// (C) 2002-2005 Stefan Kebekus
// Distributed under the GPL

// Add header files alphabetically

#ifndef PAGESIZE_H
#define PAGESIZE_H

#include "simplePageSize.h"

#include <qobject.h>

class QString;
class QStringList;


/* \brief This class represents physical page sizes.

The main difference to the SimplePageSize class are the following.

- This class knows about standard page sizes and accepts page sizes in
  various formats, e.g. as a string "DIN A4", or by specifiying the
  page width and height. Several units (inch, millimeters,
  centimeters) are possible.

- It is made sure that page width an hight are always in a resonable
  range, which is currently set to 5cm .. 50cm

- The default constructor provides a locale-depending default.

@author Stefan Kebekus <kebekus@kde.org>
@version 1.0.0
*/

class pageSize : public QObject, public SimplePageSize
{
Q_OBJECT

public:
  /** \brief Default constructor, initializes the pageSize with a
      reasonable default

      The default chosen depends on the locale. At the moment, A4 size
      is chosen for countries with metric measurement system, and US
      letter otherwise.      
  */
  pageSize();

  /** \brief Initializes the pageSize with a SimplePageSize. */
  pageSize(const SimplePageSize&);

  /** \brief List of standard pageSizes

  This method returns the names of standard pageSizes,
  e.g. "A4". These can be used, e.g., by a QComboBox to let the user
  choose known sizes. The returned list is also a list of all possible
  return values of the formatName() method explained below. If you
  call pageSizeNames() more than once, it is guaranteed that the
  same list of strings will be returned.

  @returns QStringList that contains 
  */
  QStringList pageSizeNames();
  
  /** \brief Set page size by name.

  Acceptable strings are
      
  (1) a name from the list retured by pageSizeNames(), such as "DIN
      A4"
  
  (2) a string like "500x300", which describes a page of width 500mm
      and height 300mm.

  (3) a string like "3in, 4 cm". A number of different units,
      including "in", "mm" and "cm", and a few typographical units are
      recognized
	  
  If the name is not of these types, and error message is printed to
  stderr using kdError() and a default value, which depends on the
  locale, is set. 

  In any case, the values will be trimmed so as not to exceed the
  minima/maxima of 5cm and 50cm, respectively. If the page size found
  matches one of the standard sizes by an error of no more than 2mm,
  the standard page size will be set. The signal sizeChanged() will
  always be emitted.

  @param name string that represents the page size

  @returns 'True', if the parameter could be parsed, and 'false'
  otherwise.
  */
  bool        setPageSize(const QString& name);
  
  /** \brief Set page size from width and height strings

  Sets the page size to "width" and "height", given in the associated
  units. Currently, "mm", "cm" and "in" are supported. If a unit is
  not recognized, "mm" is siliently assumed, and error message is
  printed to stderr using kdError(). If the page size set matches one
  of the standard sizes by an error of no more than 2mm, the standard
  page size will be set.  If width or height does not contain a
  number, the result is an undefined value. However, it is guaranteed
  in any case that both width and height are between the minimal and
  maximal possible values, i.e. in the range 5..50 cm. If the newly
  set value differs from the old value by more that 2mm for width or
  height, the signal sizeChanged() will be emitted

  @param width string that represents the page width as a number,
  e.g., " 300 "

  @param widthUnits units for the width string. Currently "mm", "cm"
  and "in" are allowed.

  @param height string that represents the page height as a number,
  e.g., " 300 "

  @param heightUnits units for the height string. Currently "mm", "cm"
  and "in" are allowed.
  */
  void        setPageSize(const QString& width, const QString& widthUnits, const QString& height, const QString& heightUnits);

  /** \brief Set page size

  Sets the page size to "width" and "height", given in millimeter.  If
  the page size set matches one of the standard sizes by an error of
  no more than 2mm, the standard page size will be set.  Values are
  trimmed so that both width and height are between the minimal and
  maximal possible values, i.e. in the range 5..50 cm. If the newly
  set value differs from the old value by more that 2mm for width or
  height, the signal sizeChanged() will be emitted

  @param width_in_mm page width in mm
  
  @param height_in_mm page height in mm
  */
  virtual void setPageSize(double width_in_mm, double height_in_mm);

  /** \brief Copy operator. 

  This operator will emit the signal sizeChanged() if one of the
  dimensions of *this and src differ by more than two millimeters. 
  */
  pageSize &  operator= (const pageSize &src);

  /** \brief Preferred unit for the current page size

  @returns The name of the unit, one of "cm", "mm" or "in", which is
      most commonly used with the current page format. For instance,
      US Letter and US Legal are best given in inches, to avoid very
      odd numbers. If the page format is unknown, returns a guess
      based on the current locale. */
  QString     preferredUnit() const;

  /** \brief Returns the page width as a string

  @param unit The unit in which the page width shall be returned. This
  must be one of "cm", "mm" or "in".

  @returns a string containing a number, e.g. "3.1415", which gives the page
  width in the given unit.  If the unit is not recognized, the string "--" is returned.
  */
  QString     widthString(const QString& unit) const;

  /** \brief Returns the page height as a string

  @param unit The unit in height the page width shall be
  returned. This must be one of "cm", "mm" or "in".

  @returns a string containing a number which gives the page height in
  the given unit. If the unit is not recognized, the string "--" is
  returned.
  */
  QString     heightString(const QString& unit) const;

  /** \brief Returns a name for the page size, if this is a standard
      size
      
      @warning This method does not take care of orientation, e.g. it
      will return "DIN A4" if the page size is either 210x297 or
      297x210.
      
      @returns A name for the current page size, if the format has a
      name, or QString::null otherwise. If the result is not
      QString::null, it is guaranteed to be one of the strings
      returned by the pageSizeNames() method.
  */
  QString     formatName() const;

  /** \brief Returns an number for the page size, if this is a
      standard size
      
      @warning This method does not take care of orientation, e.g. it
      will return the same value if the page size is either 210x297 or
      297x210.
      
      @returns If the current format is one of the standard sizes, a
      non-negative integer is returned, which is an index to the
      QStringList returned by the pageSizeNames() method. If the
      current format is none of the standard sizes, -1 is returned.
  */
  int         formatNumber() const {return currentSize;}
  
  /** \brief Gets orientation for standard sizes

  If the pageSize is one of the standard sizes, i.e. formatNumber() !=
  -1, this method can be used to get the orientation. If the pageSize
  is not a standard size, this method prints an error message stderr
  using kdError().

  @returns 0 for 'portrait', or 1 for 'landscape'. If the size is none
  of the standard sizes, an undefined value is returned.
  */
  int         getOrientation() const;

  /** \brief Returns a string that can be read by setPageSize(QString)

  @returns This method returns a string like "210x297". The numbers
  are page width and height in millimeters. The setPageSize(QString)
  method will understand this output.
  */
  QString     serialize() const;

public slots:
  /** \brief Sets orientation

  If the pageSize is one of the standard sizes, i.e. formatNumber() !=
  -1, this method can be used to set the orientation. If the pageSize
  is not a standard size, this method prints an error message stderr
  using kdError() and does nothing.

  @param orient 0 sets 'portrait orientation', 1 sets 'landscape'
 */
  void        setOrientation(int orient);

signals:
  /** \brief Signal emitted when the page sizes changes

  emitted to indicate that the size changed. Not emitted immediately
  after construction, when the constructor sets the default
  size.

  @param t a pointer to this
  */
  void        sizeChanged(const SimplePageSize& t);

private:
  /** Makes sure that pageWidth and pageHeight are in the permissible
      range and not, e.g., negative. */
  void        rectifySizes();

  /** Tries to find one of the known sizes which matches pageWidth and
      pageHeight, with an error margin of 2 millimeters. If found, the
      value of 'currentsize' is set to point to the known size, and
      pageWidth and pageHeight are set to the correct values for that
      size. Otherwise, currentSize is set to -1 to indicate "custom
      size". Note: this method does not take care of orientation,
      e.g. the method will set 'currentsize' to point to "DIN A4" if
      either the page size is 210x297 or 297x210. */
  void        reconstructCurrentSize();

  /** Gives a default value for currentSize, which depends on the
      locale. In countries with metric system, this will be "DIN A4",
      in countries with the imperial system, "US Letter". */
  int         defaultPageSize();

  /** Permissible range: 0--#Size_of_array staticList, or -1 to
      indicate a "user defined setting". Other values may lead to a
      segfault. */
  int         currentSize;
};

#endif