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
|
/* This file is part of the KDE project
Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
Copyright (C) 2001 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
Copyright (C) 2006 Martin Pfeiffer <hubipete@gmx.net>
Copyright (C) 2006-2007 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
2009 Jeremias Epperlein <jeeree@web.de>
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 shouldlemente 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 "TableRowElement.h"
#include "TableElement.h"
#include "FormulaCursor.h"
#include "TableDataElement.h"
#include "AttributeManager.h"
#include "FormulaDebug.h"
#include <KoXmlReader.h>
#include <QStringList>
#include <QPainter>
TableRowElement::TableRowElement( BasicElement* parent ) : BasicElement( parent )
{}
TableRowElement::~TableRowElement()
{}
void TableRowElement::paint( QPainter& painter, AttributeManager* am )
{
// nothing to paint
Q_UNUSED( painter )
Q_UNUSED( am )
}
void TableRowElement::layout( const AttributeManager* am )
{
Q_UNUSED( am )
// Get the parent table to query width/ height values
TableElement* parentTable = static_cast<TableElement*>( parentElement() );
setHeight( parentTable->rowHeight( this ) );
// Get alignment for every table data
QList<Align> verticalAlign = alignments( Qt::Vertical );
QList<Align> horizontalAlign = alignments( Qt::Horizontal );
// align the row's entries
QPointF origin;
qreal hOffset = 0.0;
for ( int i = 0; i < m_data.count(); i++ ) {
// origin = QPointF();
hOffset = 0.0;
if( verticalAlign[ i ] == Bottom )
origin.setY( height() - m_data[ i ]->height() );
else if( verticalAlign[ i ] == Center || verticalAlign[ i ] == BaseLine )
origin.setY( ( height() - m_data[ i ]->height() ) / 2 );
// Baseline is treated like Center for the moment until someone also refines
// TableElement::determineDimensions so that it pays attention to baseline.
// Axis as alignment option is ignored as it is tought to be an option for
// the table itsself.
// debugFormula << horizontalAlign[ i ]<<","<<Axis;
if( horizontalAlign[ i ] == Center ) {
hOffset = ( parentTable->columnWidth( i ) - m_data[ i ]->width() ) / 2;
}
else if( horizontalAlign[ i ] == Right ) {
hOffset = parentTable->columnWidth( i ) - m_data[ i ]->width();
}
m_data[ i ]->setOrigin( origin + QPointF( hOffset, 0.0 ) );
origin += QPointF( parentTable->columnWidth( i ), 0.0 );
}
setWidth( origin.x() );
// setting of the baseline should not be needed as the table row will only occur
// inside a table where it does not matter if a table row has a baseline or not
}
bool TableRowElement::acceptCursor( const FormulaCursor& cursor )
{
return (cursor.isSelecting());
}
int TableRowElement::positionOfChild(BasicElement* child) const
{
TableDataElement* temp=dynamic_cast<TableDataElement*>(child);
if (temp==0) {
return -1;
} else {
return m_data.indexOf(temp);
}
}
int TableRowElement::endPosition() const {
return m_data.count();
}
QLineF TableRowElement::cursorLine ( int position ) const
{
TableElement* parentTable = static_cast<TableElement*>( parentElement() );
QPointF top=absoluteBoundingRect().topLeft();
qreal hOffset = 0;
if( childElements().isEmpty() ) {
// center cursor in elements that have no children
top += QPointF( width()/2, 0 );
} else {
for (int i=0; i<position; ++i) {
hOffset+=parentTable->columnWidth(i);
}
top += QPointF( hOffset, 0.0 );
}
QPointF bottom = top + QPointF( 0.0, height() );
return QLineF(top, bottom);
}
bool TableRowElement::setCursorTo(FormulaCursor& cursor, QPointF point)
{
if (cursor.isSelecting()) {
if (m_data.isEmpty() || point.x()<0.0) {
cursor.setCurrentElement(this);
cursor.setPosition(0);
return true;
}
//check if the point is behind all child elements
if (point.x() >= width()) {
cursor.setCurrentElement(this);
cursor.setPosition(endPosition());
return true;
}
}
int i=0;
qreal x=0.0;
TableElement* parentTable = static_cast<TableElement*>( parentElement() );
for (; i<m_data.count()-1; ++i) {
//Find the child element the point is in
x+=parentTable->columnWidth( i );
if (x>=point.x()) {
break;
}
}
if (cursor.isSelecting()) {
//we don't need to change current element because we are already in this element
if (cursor.mark()<=i) {
cursor.setPosition(i+1);
}
else {
cursor.setPosition(i);
}
return true;
} else {
point-=m_data[i]->origin();
return m_data[i]->setCursorTo(cursor,point);
}
}
bool TableRowElement::moveCursor(FormulaCursor& newcursor, FormulaCursor& oldcursor)
{
//TODO: Moving the cursor vertically in the tableelement is a little bit fragile
if ( (newcursor.isHome() && newcursor.direction()==MoveLeft) ||
(newcursor.isEnd() && newcursor.direction()==MoveRight) ) {
return false;
}
int rowpos=parentElement()->positionOfChild(this);
int colpos=(newcursor.position()!=endPosition() ? newcursor.position() : newcursor.position()-1);
if (newcursor.isSelecting()) {
switch(newcursor.direction()) {
case MoveLeft:
newcursor.moveTo(this,newcursor.position()-1);
break;
case MoveRight:
newcursor.moveTo(this,newcursor.position()+1);
break;
case MoveUp:
case MoveDown:
return false;
default:
break;
}
} else {
switch(newcursor.direction()) {
case MoveLeft:
newcursor.setCurrentElement(m_data[newcursor.position()-1]);
newcursor.moveEnd();
break;
case MoveRight:
newcursor.setCurrentElement(m_data[newcursor.position()]);
newcursor.moveHome();
break;
case MoveUp:
if ( rowpos>1 ) {
BasicElement* b=parentElement()->childElements()[rowpos/2-1]->childElements()[colpos];
return newcursor.moveCloseTo(b, oldcursor);
} else {
return false;
}
case MoveDown:
if ( rowpos<endPosition()-1 ) {
BasicElement* b=parentElement()->childElements()[rowpos/2+1]->childElements()[colpos];
return newcursor.moveCloseTo(b, oldcursor);
} else {
return false;
}
default:
break;
}
}
return true;
}
const QList<BasicElement*> TableRowElement::childElements() const
{
QList<BasicElement*> tmp;
foreach( TableDataElement* element, m_data )
tmp << element;
return tmp;
}
QList<Align> TableRowElement::alignments( Qt::Orientation orientation )
{
// choose name of the attribute to query
QString align = ( orientation == Qt::Horizontal ) ? "columnalign" : "rowalign";
// get the alignment values of the parental TableElement
AttributeManager am;
QList<Align> parentAlignList = am.alignListOf( align, parentElement() );
// iterate over all entries and look on per data specification of alignment
QList<Align> alignList;
for( int i = 0; i < m_data.count(); i++ ) {
// element got own value for align
if( !m_data[ i ]->attribute( align ).isEmpty() )
alignList << am.alignOf( align, m_data[ i ] );
else if( i < parentAlignList.count() )
alignList << parentAlignList[ i ];
else
alignList << parentAlignList.last();
}
return alignList;
}
bool TableRowElement::readMathMLContent( const KoXmlElement& element )
{
BasicElement* tmpElement = 0;
KoXmlElement tmp;
forEachElement( tmp, element ) {
tmpElement = ElementFactory::createElement( tmp.tagName(), this );
if (tmpElement->elementType() != TableData)
return false;
m_data << static_cast<TableDataElement*>( tmpElement );
tmpElement->readMathML( tmp );
}
return true;
}
void TableRowElement::writeMathMLContent( KoXmlWriter* writer, const QString& ns ) const
{
foreach( TableDataElement* tmpData, m_data ) {
tmpData->writeMathML( writer, ns );
}
}
ElementType TableRowElement::elementType() const
{
return TableRow;
}
bool TableRowElement::insertChild ( int position, BasicElement* child )
{
if (child->elementType()==TableData) {
TableDataElement* tmp=static_cast<TableDataElement*>(child);
m_data.insert(position,tmp);
tmp->setParentElement(this);
return true;
} else {
return false;
}
}
bool TableRowElement::removeChild ( BasicElement* child )
{
if (child->elementType()!=TableData) {
return false;
}
TableDataElement* tmp=static_cast<TableDataElement*>(child);
if (m_data.indexOf(tmp)==-1) {
return false;
} else {
m_data.removeAll(tmp);
tmp->setParentElement(0);
}
return true;
}
|