Exemplo n.º 1
0
bool ChartTableModel::loadOdf( const KoXmlElement &tableElement,
                               KoShapeLoadingContext &context )
{
    Q_UNUSED( context );

    setRowCount( 0 );
    setColumnCount( 0 );

    ///const QDomNode &node = tableElement.asQDomNode( QDomDocument() );

    //QTextStream stream(stdout);
    //stream << node;

    // FIXME: Rewrite this without the for loop.  I think there can
    //        only be one table-rows and one table-header-rows element
    //        in each table.
    int           row = 0;
    KoXmlElement  n;
    int           found = false;
    forEachElement ( n, tableElement ) {
        if ( n.namespaceURI() != KoXmlNS::table )
            continue;

        if ( n.localName() == "table-rows"
             || n.localName() == "table-header-rows" )
        {
            found = true;

            KoXmlElement  _n;
            forEachElement ( _n, n ) {

                // Must be a table:table-row, else go to next element.
                if ( _n.namespaceURI() != KoXmlNS::table
                     || _n.localName() != "table-row" )
                    continue;

                // Add a row to the internal representation.
                setRowCount( rowCount() + 1 );

                // Loop through all cells in a table row.
                int           column = 0;
                KoXmlElement  __n;
                forEachElement ( __n, _n ) {

                    // Must be a table:table-cell, otherwise go to
                    // next element.
                    if ( __n.namespaceURI() == KoXmlNS::table
                         && __n.localName() == "table-cell" )
                    {
//                         continue;

                    // If this row is wider than any previous one,
                    // then add another column.
                    // Is this efficient enough?
                    if ( column >= columnCount() )
                        setColumnCount( columnCount() + 1 );

                    const QString valueType = __n.attributeNS( KoXmlNS::office, "value-type" );

                    QString valueString = __n.attributeNS( KoXmlNS::office, "value" );
                    const KoXmlElement valueElement = __n.namedItemNS( KoXmlNS::text, "p" ).toElement();
                    if ( ( valueElement.isNull() || !valueElement.isElement() ) && valueString.isEmpty() )
                    {
                        qWarning() << "ChartTableModel::loadOdf(): Cell contains no valid <text:p> element, cannnot load cell data.";
                        // Even if it doesn't contain any value, it's still a cell.
                        column++;
                        continue;
                    }

                    // Read the actual value in the cell.
                    QVariant value;
                    if ( valueString.isEmpty() )
                        valueString = valueElement.text();
                    if ( valueType == "float" )
                        value = valueString.toDouble();
                    else if ( valueType == "boolean" )
                        value = (bool)valueString.toInt();
                    else // if ( valueType == "string" )
                        value = valueString;

                    setData( index( row, column ), value );

                    ++column;
                    }

                } // foreach table:table-cell
                ++row;

            } // foreach table:table-row
        }
    }