File: TableElement.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 261,632 kB
  • sloc: cpp: 650,836; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 17
file content (375 lines) | stat: -rw-r--r-- 10,910 bytes parent folder | download | duplicates (3)
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

/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
	              Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
		 2006 Martin Pfeiffer <hubipete@gmx.net>

   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 "TableElement.h"

#include "AttributeManager.h"
#include "TableRowElement.h"
#include "FormulaCursor.h"
#include "FormulaDebug.h"

#include <KoXmlReader.h>

#include <QPainter>
#include <QList>

TableElement::TableElement( BasicElement* parent ) : BasicElement( parent )
{
    m_framePenStyle = Qt::NoPen;
}

TableElement::~TableElement()
{}

void TableElement::paint( QPainter& painter, AttributeManager* am )
{
    // draw frame
    //if( m_framePenStyle != Qt::NoPen ) {
    //    painter.setPen( QPen( m_framePenStyle ) );
    // painter.drawRect( QRectF( 0.0, 0.0, width(), height() ) );
    //}
    painter.save();
    QList<qreal> frameSpacing = am->doubleListOf( "framespacing", this );
    QList<qreal> rowSpacing = am->doubleListOf( "rowspacing", this );
    debugFormula << frameSpacing;
    painter.setPen(QPen(Qt::NoPen));//debugging 
    painter.drawRect( QRectF( 0.0, 0.0, width(), height() ) );
    // draw rowlines
    qreal offset = frameSpacing[1];
    for( int i = 0; i < m_rowHeights.count()-1; i++ ) {
        offset += m_rowHeights[ i ];
        painter.drawLine( QPointF( 0.0, offset ), QPointF( width(), offset ) );     
    }

    // draw columnlines
    offset = frameSpacing[0];
    for( int i = 0; i < m_colWidths.count()-1; i++ ) {
        offset += m_colWidths[ i ];
        painter.drawLine( QPointF( offset, 0.0 ), QPointF( offset, height() ) );
    }
    painter.restore();
}

void TableElement::layout( const AttributeManager* am )
{
    // lookup attribute values needed for this table
    m_framePenStyle = am->penStyleOf( "frame", this );
    m_rowLinePenStyles = am->penStyleListOf( "rowlines", this );
    m_colLinePenStyles = am->penStyleListOf( "columnlines", this );
    QList<qreal> frameSpacing = am->doubleListOf( "framespacing", this );
    QList<qreal> rowSpacing = am->doubleListOf( "rowspacing", this );

    // layout the rows vertically
    qreal tmpX = frameSpacing[ 0 ];
    qreal tmpY = frameSpacing[ 1 ];
    for( int i = 0; i < m_rows.count(); i++ ) {
        m_rows[ i ]->setOrigin( QPointF( tmpX, tmpY ) );
        tmpY += m_rows[ i ]->height();
        tmpY += ( i < rowSpacing.count() ) ? rowSpacing[ i ] : rowSpacing.last();
    }

    // add the spacing to tmpX and tmpY
    tmpX += m_rows.first()->width();
    tmpX += frameSpacing[ 0 ];
    tmpY += frameSpacing[ 1 ];
    setWidth( tmpX );
    setHeight( tmpY );
    setBaseLine( height() / 2 );
}

ElementType TableElement::elementType() const 
{
    return Table;
}


void TableElement::determineDimensions()
{
    AttributeManager am;
    bool equalRows = am.boolOf( "equalrows", this );
    bool equalColumns = am.boolOf( "equalcolumns", this );
    m_rowHeights.clear();
    m_colWidths.clear();
    // determine the dimensions of each row and col based on the biggest element in it
    BasicElement* entry;
    qreal maxWidth = 0.0;
    qreal maxHeight = 0.0;
    for( int row = 0; row < m_rows.count(); row++ ) {
        m_rowHeights << 0.0;
        for( int col = 0; col < m_rows.first()->childElements().count(); col++ ) {
             if( m_colWidths.count() <= col )
                 m_colWidths << 0.0;

             entry = m_rows[ row ]->childElements()[ col ];
             m_colWidths[ col ] = qMax( m_colWidths[ col ], entry->width() );
             m_rowHeights[ row ] = qMax( m_rowHeights[ row ], entry->height() );
             maxWidth = qMax( entry->width(), maxWidth );
        }
        maxHeight = qMax( m_rowHeights[ row ], maxHeight );
    }

    // treat equalcol and equalrow attributes
    if( equalRows )
        for( int i = 0; i < m_rowHeights.count(); i++ )
            m_rowHeights.replace( i, maxHeight );

    if( equalColumns )
        for( int i = 0; i < m_colWidths.count(); i++ )
            m_colWidths.replace( i, maxWidth );
}

qreal TableElement::columnWidth( int column )
{
    //if( m_colWidths.isEmpty() )
        determineDimensions();

    return m_colWidths[ column ];
}

qreal TableElement::rowHeight( TableRowElement* row )
{
    //if( m_rowHeights.isEmpty() )
        determineDimensions();

    return m_rowHeights[ m_rows.indexOf( row ) ];
}

const QList<BasicElement*> TableElement::childElements() const
{
    QList<BasicElement*> tmp;
    foreach( TableRowElement* tmpRow, m_rows )
        tmp << tmpRow;
    return tmp;
}

int TableElement::positionOfChild(BasicElement* child) const 
{
    TableRowElement* temp=dynamic_cast<TableRowElement*>(child);
    if (temp==0) {
        return -1;
    } else {
        int p=m_rows.indexOf(temp);
        return (p==-1) ? -1 : 2*p;
    }
}


QLineF TableElement::cursorLine ( int position ) const
{
    QPointF top;
    QRectF rect=m_rows[position/2]->absoluteBoundingRect();
    if (position%2==0) {
        //we are in front of a row
        top=rect.topLeft();
    } else {
        top=rect.topRight();
    }
    QPointF bottom = top + QPointF( 0.0, rect.height() );
    return QLineF(top, bottom);
}

bool TableElement::setCursorTo(FormulaCursor& cursor, QPointF point)
{
    if (cursor.isSelecting()) {
        return false;
    }
    int i;
    for (i=0;i<m_rows.count()-1;i++) {
        if (m_rows[i]->boundingRect().bottom()>point.y()) {
            break;
        }
    }
    point-=m_rows[i]->origin();
    return m_rows[i]->setCursorTo(cursor, point);
}

bool TableElement::moveCursor(FormulaCursor& newcursor, FormulaCursor& oldcursor)
{
    Q_UNUSED( oldcursor )
    int p=newcursor.position();
    switch (newcursor.direction()) {
    case MoveLeft:
        if (p%2==0) {
            //we are in front of a table row
            return false;
        } else {
            if (newcursor.isSelecting()) {
                newcursor.moveTo( this , p-1 );
            } else {
                newcursor.moveTo( m_rows[ p / 2 ] , m_rows[ p / 2 ]->endPosition() );
            }
            break;
        }
    case MoveRight:
        if (p%2==1) {
            //we are behind a table row
            return false;
        } else {
            if (newcursor.isSelecting()) {
                newcursor.moveTo( this , p+1 );
            } else {
                newcursor.moveTo( m_rows[ p / 2 ] , 0 );
            }
            break;
        }
    case MoveUp:
        if (p<=1) {
            return false;
        } else {
            newcursor.moveTo(this,p-2);
            break;
        }
    case MoveDown:
        if (p<(m_rows.count()-1)*2) {
            newcursor.moveTo(this,p+2);
            break;
        } else {
            return false;
        }
    default:
        break;
    }
    return true;
}

int TableElement::endPosition() const 
{
    if (m_rows.count()>0) {
        return 2*m_rows.count()-1;
    } else {
        return 1;
    }
}


bool TableElement::acceptCursor( const FormulaCursor& cursor )
{
//    return false;
    return cursor.isSelecting();
}


QPainterPath TableElement::selectionRegion ( const int pos1, const int pos2 ) const
{
    int p1=pos1 % 2 == 0 ? pos1 : pos1+1; //pos1 should be before an element
    int p2=pos2 % 2 == 0 ? pos2 : pos2+1; //pos2 should be behind an element
    QPainterPath P;
    
    for (int i=p1;i<p2;i+=2) {
        P.addRect(m_rows[i/2]->absoluteBoundingRect());
    }
    return P;
}


QString TableElement::attributesDefaultValue( const QString& attribute ) const
{
    if( attribute == "align" )
        return "axis";
    else if( attribute == "rowalign" )
        return "baseline";
    else if( attribute == "columnalign" )
        return "center";
    else if( attribute == "groupalign" )
        return "left";
    else if( attribute == "alignmentscope" )
        return "true";
    else if( attribute == "columnwidth" )
        return "auto";
    else if( attribute == "width" )
        return "auto";
    else if( attribute == "rowspacing" )
        return "1.0ex";
    else if( attribute == "columnspacing" )
        return "0.8em";
    else if( attribute == "rowlines" || attribute == "columnlines" ||
             attribute == "frame" )
        return "none";
    else if( attribute == "framespacing" )
        return "0.4em 0.5ex";
    else if( attribute == "equalrows" || attribute == "equalcolumns" ||
             attribute == "displaystyle" )
        return "false";
    else if( attribute == "side" )
        return "right";
    else if( attribute == "minlabelspacing" )
        return "0.8em";
    else
        return QString();
}

bool TableElement::readMathMLContent( const KoXmlElement& element )
{  
    BasicElement* tmpElement = 0;
    KoXmlElement tmp;
    forEachElement( tmp, element )   // iterate over the elements
    {
        tmpElement = ElementFactory::createElement( tmp.tagName(), this );
        if( tmpElement->elementType() != TableRow )
            return false;

        m_rows << static_cast<TableRowElement*>( tmpElement );
    tmpElement->readMathML( tmp );
    }

    return true;
}

void TableElement::writeMathMLContent( KoXmlWriter* writer, const QString& ns ) const
{
    foreach( TableRowElement* tmpRow, m_rows ) {  // write each mtr element
        tmpRow->writeMathML( writer, ns );
    }
}


bool TableElement::insertChild ( int position, BasicElement* child )
{
    if (child->elementType()==TableRow &&
        !child->childElements().isEmpty() &&
        child->childElements()[0]->elementType()==TableData) {
        TableRowElement* tmp=static_cast<TableRowElement*>(child);
        m_rows.insert(position,tmp);
        tmp->setParentElement(this);
        //TODO: there must be a more efficient way for this
        determineDimensions();
        
        return true;
    } else {
        return false;
    }
}

bool TableElement::removeChild ( BasicElement* child )
{
    if (child->elementType()!=TableRow) {
        return false;
    }
    TableRowElement* tmp=static_cast<TableRowElement*>(child);
    if (m_rows.indexOf(tmp)==-1) {
        return false;
    } else {
        m_rows.removeAll(tmp);
        tmp->setParentElement(0);
    }
    return true;
}