Warning, file /office/calligra/libs/text/KoText.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C)  2006 Thomas Zander <zander@kde.org>
0003  * Copyright (C)  2008 Girish Ramakrishnan <girish@forwardbias.in>
0004  * Copyright (C)  2011 Pierre Ducroquet <pinaraf@pinaraf.info>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  */
0021 #include "KoText.h"
0022 
0023 #include <KoUnit.h>
0024 
0025 #include <klocalizedstring.h>
0026 
0027 using namespace KoText;
0028 
0029 QStringList KoText::underlineTypeList()
0030 {
0031     QStringList lst;
0032     lst << i18nc("Underline Style", "None");
0033     lst << i18nc("Underline Style", "Single");
0034     lst << i18nc("Underline Style", "Double");
0035     return lst;
0036 }
0037 
0038 QStringList KoText::underlineStyleList()
0039 {
0040     QStringList lst;
0041     lst << "_________";  // solid
0042     lst << "___ ___ __"; // dash
0043     lst << "_ _ _ _ _ _"; // dot
0044     lst << "___ _ ___ _"; // dash_dot
0045     lst << "___ _ _ ___"; // dash_dot_dot
0046     lst << "~~~~~~~"; // wavy lines
0047     return lst;
0048 }
0049 
0050 KoText::Tab::Tab()
0051         : position(0.),
0052         type(QTextOption::LeftTab),
0053         leaderType(KoCharacterStyle::NoLineType),
0054         leaderStyle(KoCharacterStyle::NoLineStyle),
0055         leaderWeight(KoCharacterStyle::AutoLineWeight),
0056         leaderWidth(0)
0057 {
0058 }
0059 
0060 bool KoText::Tab::operator==(const Tab &other) const
0061 {
0062     return other.position == position &&
0063            other.type == type &&
0064            other.delimiter == delimiter &&
0065            other.leaderStyle == leaderStyle &&
0066            other.leaderColor == leaderColor &&
0067            other.leaderText == leaderText ;
0068 }
0069 
0070 Qt::Alignment KoText::alignmentFromString(const QString &align)
0071 {
0072     Qt::Alignment alignment = Qt::AlignLeft;
0073     if (align == "left")
0074         alignment = Qt::AlignLeft | Qt::AlignAbsolute;
0075     else if (align == "right")
0076         alignment = Qt::AlignRight | Qt::AlignAbsolute;
0077     else if (align == "start")
0078         alignment = Qt::AlignLeading;
0079     else if (align == "end")
0080         alignment = Qt::AlignTrailing;
0081     else if (align == "center")
0082         alignment = Qt::AlignHCenter;
0083     else if (align == "justify")
0084         alignment = Qt::AlignJustify;
0085     else if (align == "margins") // in tables this is effectively the same as justify
0086         alignment = Qt::AlignJustify;
0087     return alignment;
0088 }
0089 
0090 QString KoText::alignmentToString(Qt::Alignment alignment)
0091 {
0092     QString align;
0093 
0094     alignment &= Qt::AlignHorizontal_Mask;
0095     if (alignment == (Qt::AlignLeft | Qt::AlignAbsolute))
0096         align = "left";
0097     else if (alignment == (Qt::AlignRight | Qt::AlignAbsolute))
0098         align = "right";
0099     else if (alignment == Qt::AlignLeading)
0100         align = "start";
0101     else if (alignment == Qt::AlignTrailing)
0102         align = "end";
0103     else if (alignment == Qt::AlignHCenter)
0104         align = "center";
0105     else if (alignment == Qt::AlignJustify)
0106         align = "justify";
0107     return align;
0108 }
0109 
0110 Qt::Alignment KoText::valignmentFromString(const QString &align)
0111 {
0112     Qt::Alignment alignment = Qt::AlignTop;
0113     if (align == "top")
0114         alignment = Qt::AlignTop;
0115     else if (align == "middle")
0116         alignment = Qt::AlignVCenter;
0117     else if (align == "bottom")
0118         alignment = Qt::AlignBottom;
0119     return alignment;
0120 }
0121 
0122 QString KoText::valignmentToString(Qt::Alignment alignment)
0123 {
0124     QString align;
0125     alignment &= Qt::AlignVertical_Mask;
0126     if (alignment == (Qt::AlignTop))
0127         align = "top";
0128     else if (alignment == Qt::AlignVCenter)
0129         align = "middle";
0130     else if (alignment == Qt::AlignBottom)
0131         align = "bottom";
0132     else
0133         align = "automatic";
0134     return align;
0135 }
0136 
0137 KoText::Direction KoText::directionFromString(const QString &writingMode)
0138 {
0139     // LTR is lr-tb. RTL is rl-tb
0140     if (writingMode == "lr" || writingMode == "lr-tb")
0141         return KoText::LeftRightTopBottom;
0142     if (writingMode == "rl" || writingMode == "rl-tb")
0143         return KoText::RightLeftTopBottom;
0144     if (writingMode == "tb" || writingMode == "tb-rl")
0145         return KoText::TopBottomRightLeft;
0146     if (writingMode == "tb-lr")
0147         return KoText::TopBottomLeftRight;
0148     if (writingMode == "page")
0149         return KoText::InheritDirection;
0150     return KoText::AutoDirection;
0151 }
0152 
0153 QString KoText::directionToString(KoText::Direction direction)
0154 {
0155     if (direction == KoText::LeftRightTopBottom)
0156         return "lr";
0157     if (direction == KoText::RightLeftTopBottom)
0158         return "rl";
0159     if (direction == KoText::TopBottomRightLeft)
0160         return "tb-rl";
0161     if (direction == KoText::TopBottomLeftRight)
0162         return "tb-lr";
0163     if (direction == KoText::InheritDirection)
0164         return "page";
0165 
0166     return "auto";
0167 }
0168 
0169 KoText::KoTextBreakProperty KoText::textBreakFromString(const QString& textBreak)
0170 {
0171     if (textBreak == "page")
0172         return KoText::PageBreak;
0173     if (textBreak == "column")
0174         return KoText::ColumnBreak;
0175     return KoText::NoBreak;
0176 }
0177 
0178 QString KoText::textBreakToString(KoText::KoTextBreakProperty textBreak)
0179 {
0180     if (textBreak == KoText::PageBreak)
0181         return "page";
0182     if (textBreak == KoText::ColumnBreak)
0183         return "column";
0184     return "auto";
0185 }
0186 
0187 QTextLength KoText::parseLength(const QString &length)
0188 {
0189     if (length.contains('%'))
0190     {
0191         QString lengthValue = length.left(length.indexOf('%'));
0192         bool ok = false;
0193         qreal realLength = lengthValue.toDouble(&ok);
0194         if (ok)
0195             return QTextLength(QTextLength::PercentageLength, realLength);
0196         else
0197             return QTextLength(QTextLength::PercentageLength, 0);
0198     }
0199     else
0200     {
0201         return QTextLength(QTextLength::FixedLength, KoUnit::parseValue(length));
0202     }
0203 }
0204