~smartboyhw/ubuntu/raring/calligra/2.6.0-0ubuntu1

« back to all changes in this revision

Viewing changes to libs/kotext/styles/KoParagraphStyle.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-08-08 11:05:31 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120808110531-43wco1j5sdm8n47s
Tags: 1:2.5.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1265
1265
    context.styleStack().restore();
1266
1266
}
1267
1267
 
 
1268
struct ParagraphBorderData {
 
1269
    enum Values {Style = 1, Color = 2, Width = 4};
 
1270
 
 
1271
    ParagraphBorderData()
 
1272
    : values(0) {}
 
1273
 
 
1274
    ParagraphBorderData(const ParagraphBorderData &other)
 
1275
    : values(other.values), style(other.style), color(other.color), width(other.width) {}
 
1276
 
 
1277
    // flag defining which data is set
 
1278
    int values;
 
1279
 
 
1280
    KoBorder::BorderStyle style;
 
1281
    QColor color;
 
1282
    qreal width; ///< in pt
 
1283
};
 
1284
 
 
1285
 
 
1286
/// Parses the @p dataString as value defined by CSS2 §7.29.3 "border"
 
1287
/// Adds parsed data to the data as set for @p defaultParagraphBorderData.
 
1288
/// Returns the enriched border data on success, the original @p defaultParagraphBorderData on a parsing error
 
1289
static ParagraphBorderData parseParagraphBorderData(const QString &dataString, const ParagraphBorderData &defaultParagraphBorderData)
 
1290
{
 
1291
    const QStringList bv = dataString.split(QLatin1Char(' '), QString::SkipEmptyParts);
 
1292
    // too many items? ignore complete value
 
1293
    if (bv.count() > 3) {
 
1294
        return defaultParagraphBorderData;
 
1295
    }
 
1296
 
 
1297
    ParagraphBorderData borderData = defaultParagraphBorderData;
 
1298
    int parsedValues = 0; ///< used to track what is read from the given string
 
1299
 
 
1300
    foreach(const QString &v, bv) {
 
1301
        // try style
 
1302
        if (! (parsedValues & ParagraphBorderData::Style)) {
 
1303
            bool success = false;
 
1304
            KoBorder::BorderStyle style = KoBorder::odfBorderStyle(v, &success);
 
1305
            // workaround for not yet supported "hidden"
 
1306
            if (! success && (v == QLatin1String("hidden"))) {
 
1307
                // map to "none" for now TODO: KoBorder needs to support "hidden"
 
1308
                style = KoBorder::BorderNone;
 
1309
                success = true;
 
1310
            }
 
1311
            if (success) {
 
1312
                borderData.style = style;
 
1313
                borderData.values |= ParagraphBorderData::Style;
 
1314
                parsedValues |= ParagraphBorderData::Style;
 
1315
                continue;
 
1316
            }
 
1317
        }
 
1318
        // try color
 
1319
        if (! (parsedValues & ParagraphBorderData::Color)) {
 
1320
            const QColor color(v);
 
1321
            if (color.isValid()) {
 
1322
                borderData.color = color;
 
1323
                borderData.values |= ParagraphBorderData::Color;
 
1324
                parsedValues |= ParagraphBorderData::Color;
 
1325
                continue;
 
1326
            }
 
1327
        }
 
1328
        // try width
 
1329
        if (! (parsedValues & ParagraphBorderData::Width)) {
 
1330
            const qreal width = KoUnit::parseValue(v);
 
1331
            if (width >= 0.0) {
 
1332
                borderData.width = width;
 
1333
                borderData.values |= ParagraphBorderData::Width;
 
1334
                parsedValues |= ParagraphBorderData::Width;
 
1335
                continue;
 
1336
            }
 
1337
        }
 
1338
        // still here? found a value which cannot be parsed
 
1339
        return defaultParagraphBorderData;
 
1340
    }
 
1341
    return borderData;
 
1342
}
 
1343
 
1268
1344
void KoParagraphStyle::loadOdfProperties(KoShapeLoadingContext &scontext)
1269
1345
{
1270
1346
    KoStyleStack &styleStack = scontext.odfLoadingContext().styleStack();
1541
1617
#endif
1542
1618
 
1543
1619
    // Borders
1544
 
    const QString borderLeft(styleStack.property(KoXmlNS::fo, "border", "left"));
1545
 
    if (!borderLeft.isEmpty() && borderLeft != "none" && borderLeft != "hidden") {
1546
 
        QStringList bv = borderLeft.split(' ', QString::SkipEmptyParts);
1547
 
        setLeftBorderWidth(KoUnit::parseValue(bv.value(0), 1.0));
1548
 
        setLeftBorderStyle(KoBorder::odfBorderStyle(bv.value(1)));
1549
 
        setLeftBorderColor(QColor(bv.value(2)));
1550
 
        //setLeftInnerBorderWidth(qreal width);
1551
 
        //setLeftBorderSpacing(qreal width);
1552
 
    }
1553
 
    const QString borderTop(styleStack.property(KoXmlNS::fo, "border", "top"));
1554
 
    if (!borderTop.isEmpty() && borderTop != "none" && borderTop != "hidden") {
1555
 
        QStringList bv = borderTop.split(' ', QString::SkipEmptyParts);
1556
 
        setTopBorderWidth(KoUnit::parseValue(bv.value(0), 1.0));
1557
 
        setTopBorderStyle(KoBorder::odfBorderStyle(bv.value(1)));
1558
 
        setTopBorderColor(QColor(bv.value(2)));
1559
 
    }
1560
 
    const QString borderRight(styleStack.property(KoXmlNS::fo, "border", "right"));
1561
 
    if (!borderRight.isEmpty() && borderRight != "none" && borderRight != "hidden") {
1562
 
        QStringList bv = borderRight.split(' ', QString::SkipEmptyParts);
1563
 
        setRightBorderWidth(KoUnit::parseValue(bv.value(0), 1.0));
1564
 
        setRightBorderStyle(KoBorder::odfBorderStyle(bv.value(1)));
1565
 
        setRightBorderColor(QColor(bv.value(2)));
1566
 
    }
1567
 
    const QString borderBottom(styleStack.property(KoXmlNS::fo, "border", "bottom"));
1568
 
    if (!borderBottom.isEmpty() && borderBottom != "none" && borderBottom != "hidden") {
1569
 
        QStringList bv = borderBottom.split(' ', QString::SkipEmptyParts);
1570
 
        setBottomBorderWidth(KoUnit::parseValue(bv.value(0), 1.0));
1571
 
        setBottomBorderStyle(KoBorder::odfBorderStyle(bv.value(1)));
1572
 
        setBottomBorderColor(QColor(bv.value(2)));
1573
 
    }
 
1620
    // The border attribute is actually three attributes in one string, all optional
 
1621
    // and with no given order. Also there is a hierachy, first the common for all
 
1622
    // sides and then overwrites per side, while in the code only the sides are stored.
 
1623
    // So first the common data border is fetched, then this is overwritten per
 
1624
    // side and the result stored.
 
1625
    const QString border(styleStack.property(KoXmlNS::fo, "border"));
 
1626
    const ParagraphBorderData borderData = parseParagraphBorderData(border, ParagraphBorderData());
 
1627
 
 
1628
    const QString borderLeft(styleStack.property(KoXmlNS::fo, "border-left"));
 
1629
    const ParagraphBorderData leftParagraphBorderData = parseParagraphBorderData(borderLeft, borderData);
 
1630
    if (leftParagraphBorderData.values & ParagraphBorderData::Width) {
 
1631
        setLeftBorderWidth(leftParagraphBorderData.width);
 
1632
    }
 
1633
    if (leftParagraphBorderData.values & ParagraphBorderData::Style) {
 
1634
        setLeftBorderStyle(leftParagraphBorderData.style);
 
1635
    }
 
1636
    if (leftParagraphBorderData.values & ParagraphBorderData::Color) {
 
1637
        setLeftBorderColor(leftParagraphBorderData.color);
 
1638
    }
 
1639
 
 
1640
    const QString borderTop(styleStack.property(KoXmlNS::fo, "border-top"));
 
1641
    const ParagraphBorderData topParagraphBorderData = parseParagraphBorderData(borderTop, borderData);
 
1642
    if (topParagraphBorderData.values & ParagraphBorderData::Width) {
 
1643
        setTopBorderWidth(topParagraphBorderData.width);
 
1644
    }
 
1645
    if (topParagraphBorderData.values & ParagraphBorderData::Style) {
 
1646
        setTopBorderStyle(topParagraphBorderData.style);
 
1647
    }
 
1648
    if (topParagraphBorderData.values & ParagraphBorderData::Color) {
 
1649
        setTopBorderColor(topParagraphBorderData.color);
 
1650
    }
 
1651
 
 
1652
    const QString borderRight(styleStack.property(KoXmlNS::fo, "border-right"));
 
1653
    const ParagraphBorderData rightParagraphBorderData = parseParagraphBorderData(borderRight, borderData);
 
1654
    if (rightParagraphBorderData.values & ParagraphBorderData::Width) {
 
1655
        setRightBorderWidth(rightParagraphBorderData.width);
 
1656
    }
 
1657
    if (rightParagraphBorderData.values & ParagraphBorderData::Style) {
 
1658
        setRightBorderStyle(rightParagraphBorderData.style);
 
1659
    }
 
1660
    if (rightParagraphBorderData.values & ParagraphBorderData::Color) {
 
1661
        setRightBorderColor(rightParagraphBorderData.color);
 
1662
    }
 
1663
 
 
1664
    const QString borderBottom(styleStack.property(KoXmlNS::fo, "border-bottom"));
 
1665
    const ParagraphBorderData bottomParagraphBorderData = parseParagraphBorderData(borderBottom, borderData);
 
1666
    if (bottomParagraphBorderData.values & ParagraphBorderData::Width) {
 
1667
        setBottomBorderWidth(bottomParagraphBorderData.width);
 
1668
    }
 
1669
    if (bottomParagraphBorderData.values & ParagraphBorderData::Style) {
 
1670
        setBottomBorderStyle(bottomParagraphBorderData.style);
 
1671
    }
 
1672
    if (bottomParagraphBorderData.values & ParagraphBorderData::Color) {
 
1673
        setBottomBorderColor(bottomParagraphBorderData.color);
 
1674
    }
 
1675
 
1574
1676
    const QString borderLineWidthLeft(styleStack.property(KoXmlNS::style, "border-line-width", "left"));
1575
 
    if (!borderLineWidthLeft.isEmpty() && borderLineWidthLeft != "none" && borderLineWidthLeft != "hidden") {
 
1677
    if (!borderLineWidthLeft.isEmpty()) {
1576
1678
        QStringList blw = borderLineWidthLeft.split(' ', QString::SkipEmptyParts);
1577
1679
        setLeftInnerBorderWidth(KoUnit::parseValue(blw.value(0), 0.1));
1578
1680
        setLeftBorderSpacing(KoUnit::parseValue(blw.value(1), 1.0));
1579
1681
        setLeftBorderWidth(KoUnit::parseValue(blw.value(2), 0.1));
1580
1682
    }
1581
1683
    const QString borderLineWidthTop(styleStack.property(KoXmlNS::style, "border-line-width", "top"));
1582
 
    if (!borderLineWidthTop.isEmpty() && borderLineWidthTop != "none" && borderLineWidthTop != "hidden") {
 
1684
    if (!borderLineWidthTop.isEmpty()) {
1583
1685
        QStringList blw = borderLineWidthTop.split(' ', QString::SkipEmptyParts);
1584
1686
        setTopInnerBorderWidth(KoUnit::parseValue(blw.value(0), 0.1));
1585
1687
        setTopBorderSpacing(KoUnit::parseValue(blw.value(1), 1.0));
1586
1688
        setTopBorderWidth(KoUnit::parseValue(blw.value(2), 0.1));
1587
1689
    }
1588
1690
    const QString borderLineWidthRight(styleStack.property(KoXmlNS::style, "border-line-width", "right"));
1589
 
    if (!borderLineWidthRight.isEmpty() && borderLineWidthRight != "none" && borderLineWidthRight != "hidden") {
 
1691
    if (!borderLineWidthRight.isEmpty()) {
1590
1692
        QStringList blw = borderLineWidthRight.split(' ', QString::SkipEmptyParts);
1591
1693
        setRightInnerBorderWidth(KoUnit::parseValue(blw.value(0), 0.1));
1592
1694
        setRightBorderSpacing(KoUnit::parseValue(blw.value(1), 1.0));
1593
1695
        setRightBorderWidth(KoUnit::parseValue(blw.value(2), 0.1));
1594
1696
    }
1595
1697
    const QString borderLineWidthBottom(styleStack.property(KoXmlNS::style, "border-line-width", "bottom"));
1596
 
    if (!borderLineWidthBottom.isEmpty() && borderLineWidthBottom != "none" && borderLineWidthBottom != "hidden") {
 
1698
    if (!borderLineWidthBottom.isEmpty()) {
1597
1699
        QStringList blw = borderLineWidthBottom.split(' ', QString::SkipEmptyParts);
1598
1700
        setBottomInnerBorderWidth(KoUnit::parseValue(blw.value(0), 0.1));
1599
1701
        setBottomBorderSpacing(KoUnit::parseValue(blw.value(1), 1.0));