1
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29 #include "kpDocumentMetaInfo.h"
30
31 #include <cmath>
32
33 #include <QPoint>
34
35 #include "kpLogCategories.h"
36
37 #include "kpDefs.h"
38
39
40 //
41 // Constants which "ought to be enough for anybody"
42 // LOTODO: Maybe there are some QImage constants somewhere?
43 //
44
45 // public static
46
47 // (round up to guarantee at least 1 dot per inch)
48 const int kpDocumentMetaInfo::MinDotsPerMeter =
49 int (std::ceil (1/*single dot per inch - a very low DPI*/ * KP_INCHES_PER_METER) + 0.1);
50
51 const int kpDocumentMetaInfo::MaxDotsPerMeter =
52 int ((600 * 100)/*a lot of DPI*/ * KP_INCHES_PER_METER);
53
54 // public static
55 const int kpDocumentMetaInfo::MaxOffset = (4000/*big image*/ * 100)/*a very big image*/;
56 const int kpDocumentMetaInfo::MinOffset = -kpDocumentMetaInfo::MaxOffset;
57
58 //---------------------------------------------------------------------
59
60 struct kpDocumentMetaInfoPrivate
61 {
62 int m_dotsPerMeterX{}, m_dotsPerMeterY{};
63 QPoint m_offset;
64
65 QMap <QString, QString> m_textMap;
66 };
67
68 //---------------------------------------------------------------------
69
70 // public
kpDocumentMetaInfo()71 kpDocumentMetaInfo::kpDocumentMetaInfo ()
72 : d (new kpDocumentMetaInfoPrivate ())
73 {
74 d->m_dotsPerMeterX = 0;
75 d->m_dotsPerMeterY = 0;
76 d->m_offset = QPoint (0, 0);
77 }
78
79 //---------------------------------------------------------------------
80
kpDocumentMetaInfo(const kpDocumentMetaInfo & rhs)81 kpDocumentMetaInfo::kpDocumentMetaInfo (const kpDocumentMetaInfo &rhs)
82 : d (new kpDocumentMetaInfoPrivate ())
83 {
84 d->m_dotsPerMeterX = rhs.dotsPerMeterX ();
85 d->m_dotsPerMeterY = rhs.dotsPerMeterY ();
86 d->m_offset = rhs.offset ();
87 d->m_textMap = rhs.textMap ();
88 }
89
90 //---------------------------------------------------------------------
91
92 // public
~kpDocumentMetaInfo()93 kpDocumentMetaInfo::~kpDocumentMetaInfo ()
94 {
95 delete d;
96 }
97
98 //---------------------------------------------------------------------
99
100 // public
operator ==(const kpDocumentMetaInfo & rhs) const101 bool kpDocumentMetaInfo::operator== (const kpDocumentMetaInfo &rhs) const
102 {
103 return (d->m_dotsPerMeterX == rhs.d->m_dotsPerMeterX &&
104 d->m_dotsPerMeterY == rhs.d->m_dotsPerMeterY &&
105 d->m_offset == rhs.d->m_offset &&
106 d->m_textMap == rhs.d->m_textMap);
107 }
108
109 //---------------------------------------------------------------------
110
111 // public
operator !=(const kpDocumentMetaInfo & rhs) const112 bool kpDocumentMetaInfo::operator!= (const kpDocumentMetaInfo &rhs) const
113 {
114 return !(*this == rhs);
115 }
116
117 //---------------------------------------------------------------------
118
119 // public
operator =(const kpDocumentMetaInfo & rhs)120 kpDocumentMetaInfo &kpDocumentMetaInfo::operator= (const kpDocumentMetaInfo &rhs)
121 {
122 if (this == &rhs) {
123 return *this;
124 }
125
126 d->m_dotsPerMeterX = rhs.dotsPerMeterX ();
127 d->m_dotsPerMeterY = rhs.dotsPerMeterY ();
128 d->m_offset = rhs.offset ();
129 d->m_textMap = rhs.textMap ();
130
131 return *this;
132 }
133
134 //---------------------------------------------------------------------
135
136 // public
printDebug(const QString & prefix) const137 void kpDocumentMetaInfo::printDebug (const QString &prefix) const
138 {
139 const QString usedPrefix = !prefix.isEmpty() ? QString(prefix + QLatin1String(":")) : QString();
140
141 qCDebug(kpLogImagelib) << usedPrefix;
142
143 qCDebug(kpLogImagelib) << "dotsPerMeter X=" << dotsPerMeterX ()
144 << " Y=" << dotsPerMeterY ()
145 << " offset=" << offset ();
146
147 foreach (const QString &key, textKeys())
148 qCDebug(kpLogImagelib) << "key=" << key << " text=" << text(key);
149
150 qCDebug(kpLogImagelib) << usedPrefix << "ENDS";
151 }
152
153 //---------------------------------------------------------------------
154
155 // public
size() const156 kpCommandSize::SizeType kpDocumentMetaInfo::size () const
157 {
158 kpCommandSize::SizeType ret = 0;
159
160 for (const auto &key : d->m_textMap.keys ())
161 {
162 ret += kpCommandSize::StringSize (key) +
163 kpCommandSize::StringSize (d->m_textMap [key]);
164 }
165
166 // We don't know what the QMap size overhead is so overestimate the size
167 // rather than underestimating it.
168 // LOTODO: Find the proper size in bytes.
169 return ret * 3;
170 }
171
172 //---------------------------------------------------------------------
173
174 // public
dotsPerMeterX() const175 int kpDocumentMetaInfo::dotsPerMeterX () const
176 {
177 return d->m_dotsPerMeterX;
178 }
179
180 //---------------------------------------------------------------------
181
182 // public
setDotsPerMeterX(int val)183 void kpDocumentMetaInfo::setDotsPerMeterX (int val)
184 {
185 // Unspecified resolution?
186 if (val == 0)
187 {
188 d->m_dotsPerMeterX = 0;
189 return;
190 }
191
192 d->m_dotsPerMeterX = qBound (MinDotsPerMeter, val, MaxDotsPerMeter);
193 }
194
195 //---------------------------------------------------------------------
196
197 // public
dotsPerMeterY() const198 int kpDocumentMetaInfo::dotsPerMeterY () const
199 {
200 return d->m_dotsPerMeterY;
201 }
202
203 //---------------------------------------------------------------------
204
205 // public
setDotsPerMeterY(int val)206 void kpDocumentMetaInfo::setDotsPerMeterY (int val)
207 {
208 // Unspecified resolution?
209 if (val == 0)
210 {
211 d->m_dotsPerMeterY = 0;
212 return;
213 }
214
215 d->m_dotsPerMeterY = qBound (MinDotsPerMeter, val, MaxDotsPerMeter);
216 }
217
218 //---------------------------------------------------------------------
219
220 // public
offset() const221 QPoint kpDocumentMetaInfo::offset () const
222 {
223 return d->m_offset;
224 }
225
226 //---------------------------------------------------------------------
227
228 // public
setOffset(const QPoint & point)229 void kpDocumentMetaInfo::setOffset (const QPoint &point)
230 {
231 const int x = qBound (MinOffset, point.x (), MaxOffset);
232 const int y = qBound (MinOffset, point.y (), MaxOffset);
233
234 d->m_offset = QPoint (x, y);
235 }
236
237 //---------------------------------------------------------------------
238
239 // public
textMap() const240 QMap <QString, QString> kpDocumentMetaInfo::textMap () const
241 {
242 return d->m_textMap;
243 }
244
245 //---------------------------------------------------------------------
246
247 // public
textKeys() const248 QList <QString> kpDocumentMetaInfo::textKeys () const
249 {
250 return d->m_textMap.keys ();
251 }
252
253 //---------------------------------------------------------------------
254
255 // public
text(const QString & key) const256 QString kpDocumentMetaInfo::text (const QString &key) const
257 {
258 if (key.isEmpty ()) {
259 return {};
260 }
261
262 return d->m_textMap [key];
263 }
264
265 //---------------------------------------------------------------------
266
267 // public
setText(const QString & key,const QString & value)268 void kpDocumentMetaInfo::setText (const QString &key, const QString &value)
269 {
270 if (key.isEmpty ()) {
271 return;
272 }
273
274 d->m_textMap [key] = value;
275 }
276
277 //---------------------------------------------------------------------
278