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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/* This file is part of the KDE project
Copyright (C) 2001 David Faure <faure@kde.org>
Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KoUnit.h"
#include <cmath>
#include <QTransform>
#include <klocalizedstring.h>
#include <OdfDebug.h>
#include <QtGlobal>
// ensure the same order as in KoUnit::Unit
static const char* const unitNameList[KoUnit::TypeCount] =
{
"mm",
"pt",
"in",
"cm",
"dm",
"pi",
"cc",
"px"
};
QString KoUnit::unitDescription(KoUnit::Type type)
{
switch (type) {
case KoUnit::Millimeter:
return i18n("Millimeters (mm)");
case KoUnit::Centimeter:
return i18n("Centimeters (cm)");
case KoUnit::Decimeter:
return i18n("Decimeters (dm)");
case KoUnit::Inch:
return i18n("Inches (in)");
case KoUnit::Pica:
return i18n("Pica (pi)");
case KoUnit::Cicero:
return i18n("Cicero (cc)");
case KoUnit::Point:
return i18n("Points (pt)");
case KoUnit::Pixel:
return i18n("Pixels (px)");
default:
return i18n("Unsupported unit");
}
}
// grouped by units which are similar
static const KoUnit::Type typesInUi[KoUnit::TypeCount] =
{
KoUnit::Millimeter,
KoUnit::Centimeter,
KoUnit::Decimeter,
KoUnit::Inch,
KoUnit::Pica,
KoUnit::Cicero,
KoUnit::Point,
KoUnit::Pixel,
};
QStringList KoUnit::listOfUnitNameForUi(ListOptions listOptions)
{
QStringList lst;
for (int i = 0; i < KoUnit::TypeCount; ++i) {
const Type type = typesInUi[i];
if ((type != Pixel) || ((listOptions & HideMask) == ListAll))
lst.append(unitDescription(type));
}
return lst;
}
KoUnit KoUnit::fromListForUi(int index, ListOptions listOptions, qreal factor)
{
KoUnit::Type type = KoUnit::Point;
if ((0 <= index) && (index < KoUnit::TypeCount)) {
// iterate through all enums and skip the Pixel enum if needed
for (int i = 0; i < KoUnit::TypeCount; ++i) {
if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) {
++index;
continue;
}
if (i == index) {
type = typesInUi[i];
break;
}
}
}
return KoUnit(type, factor);
}
int KoUnit::indexInListForUi(ListOptions listOptions) const
{
if ((listOptions&HidePixel) && (m_type == Pixel)) {
return -1;
}
int result = -1;
int skipped = 0;
for (int i = 0; i < KoUnit::TypeCount; ++i) {
if ((listOptions&HidePixel) && (typesInUi[i] == Pixel)) {
++skipped;
continue;
}
if (typesInUi[i] == m_type) {
result = i - skipped;
break;
}
}
return result;
}
qreal KoUnit::toUserValue(qreal ptValue) const
{
switch (m_type) {
case Millimeter:
return toMillimeter(ptValue);
case Centimeter:
return toCentimeter(ptValue);
case Decimeter:
return toDecimeter(ptValue);
case Inch:
return toInch(ptValue);
case Pica:
return toPica(ptValue);
case Cicero:
return toCicero(ptValue);
case Pixel:
return ptValue * m_pixelConversion;
case Point:
default:
return toPoint(ptValue);
}
}
qreal KoUnit::ptToUnit(const qreal ptValue, const KoUnit &unit)
{
switch (unit.m_type) {
case Millimeter:
return POINT_TO_MM(ptValue);
case Centimeter:
return POINT_TO_CM(ptValue);
case Decimeter:
return POINT_TO_DM(ptValue);
case Inch:
return POINT_TO_INCH(ptValue);
case Pica:
return POINT_TO_PI(ptValue);
case Cicero:
return POINT_TO_CC(ptValue);
case Pixel:
return ptValue * unit.m_pixelConversion;
case Point:
default:
return ptValue;
}
}
QString KoUnit::toUserStringValue(qreal ptValue) const
{
return QLocale().toString(toUserValue(ptValue));
}
qreal KoUnit::fromUserValue(qreal value) const
{
switch (m_type) {
case Millimeter:
return MM_TO_POINT(value);
case Centimeter:
return CM_TO_POINT(value);
case Decimeter:
return DM_TO_POINT(value);
case Inch:
return INCH_TO_POINT(value);
case Pica:
return PI_TO_POINT(value);
case Cicero:
return CC_TO_POINT(value);
case Pixel:
return value / m_pixelConversion;
case Point:
default:
return value;
}
}
qreal KoUnit::fromUserValue(const QString &value, bool *ok) const
{
return fromUserValue(QLocale().toDouble(value, ok));
}
qreal KoUnit::parseValue(const QString& _value, qreal defaultVal)
{
if (_value.isEmpty())
return defaultVal;
QString value(_value.simplified());
value.remove(QLatin1Char(' '));
int firstLetter = -1;
for (int i = 0; i < value.length(); ++i) {
if (value.at(i).isLetter()) {
if (value.at(i) == QLatin1Char('e'))
continue;
firstLetter = i;
break;
}
}
if (firstLetter == -1)
return value.toDouble();
const QString symbol = value.mid(firstLetter);
value.truncate(firstLetter);
const qreal val = value.toDouble();
if (symbol == QLatin1String("pt"))
return val;
bool ok;
KoUnit u = KoUnit::fromSymbol(symbol, &ok);
if (ok)
return u.fromUserValue(val);
if (symbol == QLatin1String("m"))
return DM_TO_POINT(val * 10.0);
else if (symbol == QLatin1String("km"))
return DM_TO_POINT(val * 10000.0);
warnOdf << "KoUnit::parseValue: Unit " << symbol << " is not supported, please report.";
// TODO : add support for mi/ft ?
return defaultVal;
}
KoUnit KoUnit::fromSymbol(const QString &symbol, bool *ok)
{
Type result = Point;
if (symbol == QLatin1String("inch") /*compat*/) {
result = Inch;
if (ok)
*ok = true;
} else {
if (ok)
*ok = false;
for (int i = 0; i < TypeCount; ++i) {
if (symbol == QLatin1String(unitNameList[i])) {
result = static_cast<Type>(i);
if (ok)
*ok = true;
}
}
}
return KoUnit(result);
}
qreal KoUnit::convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor)
{
qreal pt;
switch (fromUnit.type()) {
case Millimeter:
pt = MM_TO_POINT(value);
break;
case Centimeter:
pt = CM_TO_POINT(value);
break;
case Decimeter:
pt = DM_TO_POINT(value);
break;
case Inch:
pt = INCH_TO_POINT(value);
break;
case Pica:
pt = PI_TO_POINT(value);
break;
case Cicero:
pt = CC_TO_POINT(value);
break;
case Pixel:
pt = value / factor;
break;
case Point:
default:
pt = value;
}
switch (toUnit.type()) {
case Millimeter:
return POINT_TO_MM(pt);
case Centimeter:
return POINT_TO_CM(pt);
case Decimeter:
return POINT_TO_DM(pt);
case Inch:
return POINT_TO_INCH(pt);
case Pica:
return POINT_TO_PI(pt);
case Cicero:
return POINT_TO_CC(pt);
case Pixel:
return pt * factor;
case Point:
default:
return pt;
}
}
QString KoUnit::symbol() const
{
return QLatin1String(unitNameList[m_type]);
}
qreal KoUnit::parseAngle(const QString& _value, qreal defaultVal)
{
if (_value.isEmpty())
return defaultVal;
QString value(_value.simplified());
value.remove(QLatin1Char(' '));
int firstLetter = -1;
for (int i = 0; i < value.length(); ++i) {
if (value.at(i).isLetter()) {
if (value.at(i) == QLatin1Char('e'))
continue;
firstLetter = i;
break;
}
}
if (firstLetter == -1)
return value.toDouble();
const QString type = value.mid(firstLetter);
value.truncate(firstLetter);
const qreal val = value.toDouble();
if (type == QLatin1String("deg"))
return val;
else if (type == QLatin1String("rad"))
return val * 180 / M_PI;
else if (type == QLatin1String("grad"))
return val * 0.9;
return defaultVal;
}
qreal KoUnit::approxTransformScale(const QTransform &t)
{
return std::sqrt(qAbs(t.determinant()));
}
void KoUnit::adjustByPixelTransform(const QTransform &t)
{
m_pixelConversion *= approxTransformScale(t);
}
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug debug, const KoUnit &unit)
{
#ifndef NDEBUG
debug.nospace() << unit.symbol();
#else
Q_UNUSED(unit);
#endif
return debug.space();
}
#endif
|