You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1006 lines
32 KiB
1006 lines
32 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
|
||
|
Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.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 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.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqptrlist.h>
|
||
|
#include <tqpainter.h>
|
||
|
#include <tqpen.h>
|
||
|
#include <tqpointarray.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
|
||
|
#include "bracketelement.h"
|
||
|
#include "elementvisitor.h"
|
||
|
#include "fontstyle.h"
|
||
|
#include "formulacursor.h"
|
||
|
#include "formulaelement.h"
|
||
|
#include "sequenceelement.h"
|
||
|
|
||
|
KFORMULA_NAMESPACE_BEGIN
|
||
|
|
||
14 years ago
|
SingleContentElement::SingleContentElement(BasicElement* parent )
|
||
|
: BasicElement( parent )
|
||
15 years ago
|
{
|
||
|
content = new SequenceElement( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
SingleContentElement::SingleContentElement( const SingleContentElement& other )
|
||
|
: BasicElement( other )
|
||
|
{
|
||
|
content = new SequenceElement( other.content );
|
||
|
content->setParent( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
SingleContentElement::~SingleContentElement()
|
||
|
{
|
||
|
delete content;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
TQChar SingleContentElement::getCharacter() const
|
||
15 years ago
|
{
|
||
|
// This is meant to make the SingleContentElement text only.
|
||
14 years ago
|
// This "fixes" the parenthesis problem (parenthesis too large).
|
||
15 years ago
|
// I'm not sure if we really want this. There should be better ways.
|
||
|
if ( content->isTextOnly() ) {
|
||
|
return '\\';
|
||
|
}
|
||
|
return content->getCharacter();
|
||
|
}
|
||
|
|
||
|
BasicElement* SingleContentElement::goToPos( FormulaCursor* cursor, bool& handled,
|
||
14 years ago
|
const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
BasicElement* e = BasicElement::goToPos(cursor, handled, point, parentOrigin);
|
||
15 years ago
|
if (e != 0) {
|
||
14 years ago
|
LuPixelPoint myPos(parentOrigin.x() + getX(),
|
||
|
parentOrigin.y() + getY());
|
||
15 years ago
|
|
||
|
e = content->goToPos(cursor, handled, point, myPos);
|
||
|
if (e != 0) {
|
||
|
return e;
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::dispatchFontCommand( FontCommand* cmd )
|
||
|
{
|
||
|
content->dispatchFontCommand( cmd );
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::moveLeft(FormulaCursor* cursor, BasicElement* from)
|
||
|
{
|
||
|
if (cursor->isSelectionMode()) {
|
||
|
getParent()->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
//bool linear = cursor->getLinearMovement();
|
||
|
if (from == getParent()) {
|
||
|
content->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveLeft(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::moveRight(FormulaCursor* cursor, BasicElement* from)
|
||
|
{
|
||
|
if (cursor->isSelectionMode()) {
|
||
|
getParent()->moveRight(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
//bool linear = cursor->getLinearMovement();
|
||
|
if (from == getParent()) {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::moveUp(FormulaCursor* cursor, BasicElement* /*from*/)
|
||
|
{
|
||
|
getParent()->moveUp(cursor, this);
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::moveDown(FormulaCursor* cursor, BasicElement* /*from*/)
|
||
|
{
|
||
|
getParent()->moveDown(cursor, this);
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::remove( FormulaCursor* cursor,
|
||
14 years ago
|
TQPtrList<BasicElement>& removedChildren,
|
||
15 years ago
|
Direction direction )
|
||
|
{
|
||
|
switch (cursor->getPos()) {
|
||
|
case contentPos:
|
||
14 years ago
|
BasicElement* parent = getParent();
|
||
|
parent->selectChild(cursor, this);
|
||
|
parent->remove(cursor, removedChildren, direction);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::normalize( FormulaCursor* cursor, Direction direction )
|
||
|
{
|
||
|
if (direction == beforeCursor) {
|
||
|
content->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
SequenceElement* SingleContentElement::getMainChild()
|
||
|
{
|
||
|
return content;
|
||
|
}
|
||
|
|
||
|
void SingleContentElement::selectChild(FormulaCursor* cursor, BasicElement* child)
|
||
|
{
|
||
|
if (child == content) {
|
||
|
cursor->setTo(this, contentPos);
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void SingleContentElement::writeDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
BasicElement::writeDom(element);
|
||
|
|
||
14 years ago
|
TQDomDocument doc = element.ownerDocument();
|
||
15 years ago
|
|
||
14 years ago
|
TQDomElement con = doc.createElement("CONTENT");
|
||
15 years ago
|
con.appendChild(content->getElementDom(doc));
|
||
|
element.appendChild(con);
|
||
|
}
|
||
|
|
||
14 years ago
|
bool SingleContentElement::readContentFromDom(TQDomNode& node)
|
||
15 years ago
|
{
|
||
|
if (!BasicElement::readContentFromDom(node)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ( !buildChild( content, node, "CONTENT" ) ) {
|
||
|
kdWarning( DEBUGID ) << "Empty content in " << getTagName() << endl;
|
||
|
return false;
|
||
|
}
|
||
|
node = node.nextSibling();
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
int SingleContentElement::readContentFromMathMLDom( TQDomNode& node )
|
||
15 years ago
|
{
|
||
|
if ( BasicElement::readContentFromMathMLDom( node ) == -1 ) {
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int nodeCounter = content->buildMathMLChild( node );
|
||
|
if ( nodeCounter == -1 ) {
|
||
|
kdWarning( DEBUGID) << "Empty content in SingleContentElement\n";
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return nodeCounter;
|
||
|
}
|
||
|
|
||
14 years ago
|
void SingleContentElement::writeMathMLContent( TQDomDocument& doc, TQDomElement& element, bool oasisFormat ) const
|
||
15 years ago
|
{
|
||
|
content->writeMathML( doc, element, oasisFormat );
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
14 years ago
|
BracketElement::BracketElement(SymbolType l, SymbolType r, BasicElement* parent)
|
||
|
: SingleContentElement(parent),
|
||
15 years ago
|
left( 0 ), right( 0 ),
|
||
|
leftType( l ), rightType( r ),
|
||
|
m_operator( false ), m_customLeft( false ), m_customRight( false )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
BracketElement::~BracketElement()
|
||
|
{
|
||
|
delete left;
|
||
|
delete right;
|
||
|
}
|
||
|
|
||
|
|
||
|
BracketElement::BracketElement( const BracketElement& other )
|
||
|
: SingleContentElement( other ),
|
||
|
left( 0 ), right( 0 ),
|
||
|
leftType( other.leftType ), rightType( other.rightType ),
|
||
|
m_operator( other.m_operator ),
|
||
|
m_customLeft( other.m_customLeft ), m_customRight( other.m_customRight )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
bool BracketElement::accept( ElementVisitor* visitor )
|
||
|
{
|
||
|
return visitor->visit( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
void BracketElement::entered( SequenceElement* /*child*/ )
|
||
|
{
|
||
|
formula()->tell( i18n( "Delimited list" ) );
|
||
|
}
|
||
|
|
||
|
|
||
|
BasicElement* BracketElement::goToPos( FormulaCursor* cursor, bool& handled,
|
||
14 years ago
|
const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
BasicElement* e = BasicElement::goToPos(cursor, handled, point, parentOrigin);
|
||
15 years ago
|
if (e != 0) {
|
||
14 years ago
|
LuPixelPoint myPos(parentOrigin.x() + getX(),
|
||
|
parentOrigin.y() + getY());
|
||
15 years ago
|
e = getContent()->goToPos(cursor, handled, point, myPos);
|
||
|
if (e != 0) {
|
||
|
return e;
|
||
|
}
|
||
|
|
||
|
// We are in one of those gaps.
|
||
|
luPixel dx = point.x() - myPos.x();
|
||
|
luPixel dy = point.y() - myPos.y();
|
||
|
|
||
|
if ((dx > getContent()->getX()+getContent()->getWidth()) ||
|
||
|
(dy > getContent()->getY()+getContent()->getHeight())) {
|
||
|
getContent()->moveEnd(cursor);
|
||
|
handled = true;
|
||
|
return getContent();
|
||
|
}
|
||
|
return this;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Calculates our width and height and
|
||
13 years ago
|
* our children's parentPosition.
|
||
15 years ago
|
*/
|
||
|
void BracketElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
SequenceElement* content = getContent();
|
||
|
content->calcSizes( context, tstyle, istyle, style );
|
||
|
|
||
|
//if ( left == 0 ) {
|
||
|
delete left;
|
||
|
delete right;
|
||
|
left = context.fontStyle().createArtwork( leftType );
|
||
|
right = context.fontStyle().createArtwork( rightType );
|
||
|
//}
|
||
|
|
||
|
double factor = style.sizeFactor();
|
||
|
if (content->isTextOnly()) {
|
||
|
left->calcSizes(context, tstyle, factor);
|
||
|
right->calcSizes(context, tstyle, factor);
|
||
|
|
||
14 years ago
|
setBaseline(TQMAX(content->getBaseline(),
|
||
|
TQMAX(left->getBaseline(), right->getBaseline())));
|
||
15 years ago
|
|
||
|
content->setY(getBaseline() - content->getBaseline());
|
||
|
left ->setY(getBaseline() - left ->getBaseline());
|
||
|
right ->setY(getBaseline() - right ->getBaseline());
|
||
|
|
||
|
//setMidline(content->getY() + content->getMidline());
|
||
14 years ago
|
setHeight(TQMAX(content->getY() + content->getHeight(),
|
||
|
TQMAX(left ->getY() + left ->getHeight(),
|
||
15 years ago
|
right->getY() + right->getHeight())));
|
||
|
}
|
||
|
else {
|
||
|
//kdDebug( DEBUGID ) << "BracketElement::calcSizes " << content->axis( context, tstyle ) << " " << content->getHeight() << endl;
|
||
14 years ago
|
luPixel contentHeight = 2 * TQMAX( content->axis( context, tstyle, factor ),
|
||
15 years ago
|
content->getHeight() - content->axis( context, tstyle, factor ) );
|
||
|
left->calcSizes( context, tstyle, factor, contentHeight );
|
||
|
right->calcSizes( context, tstyle, factor, contentHeight );
|
||
|
|
||
|
// height
|
||
14 years ago
|
setHeight(TQMAX(contentHeight,
|
||
|
TQMAX(left->getHeight(), right->getHeight())));
|
||
15 years ago
|
//setMidline(getHeight() / 2);
|
||
|
|
||
|
content->setY(getHeight() / 2 - content->axis( context, tstyle, factor ));
|
||
|
setBaseline(content->getBaseline() + content->getY());
|
||
|
|
||
|
if ( left->isNormalChar() ) {
|
||
|
left->setY(getBaseline() - left->getBaseline());
|
||
|
}
|
||
|
else {
|
||
|
left->setY((getHeight() - left->getHeight())/2);
|
||
|
}
|
||
|
if ( right->isNormalChar() ) {
|
||
|
right->setY(getBaseline() - right->getBaseline());
|
||
|
}
|
||
|
else {
|
||
|
right->setY((getHeight() - right->getHeight())/2);
|
||
|
}
|
||
|
|
||
|
// kdDebug() << "BracketElement::calcSizes" << endl
|
||
|
// << "getHeight(): " << getHeight() << endl
|
||
|
// << "left->getHeight(): " << left->getHeight() << endl
|
||
|
// << "right->getHeight(): " << right->getHeight() << endl
|
||
|
// << "left->getY(): " << left->getY() << endl
|
||
|
// << "right->getY(): " << right->getY() << endl
|
||
|
// << endl;
|
||
|
}
|
||
|
|
||
|
// width
|
||
|
setWidth(left->getWidth() + content->getWidth() + right->getWidth());
|
||
|
content->setX(left->getWidth());
|
||
|
right ->setX(left->getWidth()+content->getWidth());
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Draws the whole element including its children.
|
||
14 years ago
|
* The `parentOrigin' is the point this element's parent starts.
|
||
14 years ago
|
* We can use our parentPosition to get our own origin then.
|
||
15 years ago
|
*/
|
||
14 years ago
|
void BracketElement::draw( TQPainter& painter, const LuPixelRect& r,
|
||
15 years ago
|
const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style,
|
||
14 years ago
|
const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
|
||
15 years ago
|
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
|
||
|
// return;
|
||
|
|
||
|
SequenceElement* content = getContent();
|
||
|
content->draw(painter, r, context, tstyle, istyle, style, myPos);
|
||
|
|
||
|
if (content->isTextOnly()) {
|
||
|
left->draw(painter, r, context, tstyle, style, myPos);
|
||
|
right->draw(painter, r, context, tstyle, style, myPos);
|
||
|
}
|
||
|
else {
|
||
|
double factor = style.sizeFactor();
|
||
14 years ago
|
luPixel contentHeight = 2 * TQMAX(content->axis( context, tstyle, factor ),
|
||
15 years ago
|
content->getHeight() - content->axis( context, tstyle, factor ));
|
||
|
left->draw(painter, r, context, tstyle, style, contentHeight, myPos);
|
||
|
right->draw(painter, r, context, tstyle, style, contentHeight, myPos);
|
||
|
}
|
||
|
|
||
|
// Debug
|
||
|
#if 0
|
||
14 years ago
|
painter.setBrush( TQt::NoBrush );
|
||
|
painter.setPen( TQt::red );
|
||
14 years ago
|
painter.drawRect( context.layoutUnitToPixelX( myPos.x()+left->getX() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+left->getY() ),
|
||
|
context.layoutUnitToPixelX( left->getWidth() ),
|
||
|
context.layoutUnitToPixelY( left->getHeight() ) );
|
||
|
painter.drawRect( context.layoutUnitToPixelX( myPos.x()+right->getX() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+right->getY() ),
|
||
|
context.layoutUnitToPixelX( right->getWidth() ),
|
||
|
context.layoutUnitToPixelY( right->getHeight() ) );
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Appends our attributes to the dom element.
|
||
|
*/
|
||
14 years ago
|
void BracketElement::writeDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
SingleContentElement::writeDom(element);
|
||
|
element.setAttribute("LEFT", leftType);
|
||
|
element.setAttribute("RIGHT", rightType);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our attributes from the element.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool BracketElement::readAttributesFromDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
if (!BasicElement::readAttributesFromDom(element)) {
|
||
|
return false;
|
||
|
}
|
||
14 years ago
|
TQString leftStr = element.attribute("LEFT");
|
||
15 years ago
|
if(!leftStr.isNull()) {
|
||
|
leftType = static_cast<SymbolType>(leftStr.toInt());
|
||
|
}
|
||
14 years ago
|
TQString rightStr = element.attribute("RIGHT");
|
||
15 years ago
|
if(!rightStr.isNull()) {
|
||
|
rightType = static_cast<SymbolType>(rightStr.toInt());
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our attributes from the MathML element.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool BracketElement::readAttributesFromMathMLDom(const TQDomElement& element)
|
||
15 years ago
|
{
|
||
|
if ( !BasicElement::readAttributesFromMathMLDom( element ) ) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ( element.tagName().lower() == "mo" ) {
|
||
|
m_operator = true;
|
||
|
// TODO: parse attributes in section 3.2.5.2
|
||
|
}
|
||
|
else { // mfenced, see attributes in section 3.3.8.2
|
||
|
leftType = LeftRoundBracket;
|
||
|
rightType = RightRoundBracket;
|
||
14 years ago
|
TQString openStr = element.attribute( "open" ).stripWhiteSpace();
|
||
15 years ago
|
if ( !openStr.isNull() ) {
|
||
|
m_customLeft = true;
|
||
|
if ( openStr == "[" )
|
||
|
leftType = LeftSquareBracket;
|
||
|
else if ( openStr == "]" )
|
||
|
leftType = RightSquareBracket;
|
||
|
else if ( openStr == "{" )
|
||
|
leftType = LeftCurlyBracket;
|
||
|
else if ( openStr == "}" )
|
||
|
leftType = RightCurlyBracket;
|
||
|
else if ( openStr == "<" )
|
||
|
leftType = LeftCornerBracket;
|
||
|
else if ( openStr == ">" )
|
||
|
leftType = RightCornerBracket;
|
||
|
else if ( openStr == "(" )
|
||
|
leftType = LeftRoundBracket;
|
||
|
else if ( openStr == ")" )
|
||
|
leftType = RightRoundBracket;
|
||
|
else if ( openStr == "/" )
|
||
|
leftType = SlashBracket;
|
||
|
else if ( openStr == "\\" )
|
||
|
leftType = BackSlashBracket;
|
||
|
else // TODO: Check for entity references
|
||
|
leftType = LeftRoundBracket;
|
||
|
}
|
||
14 years ago
|
TQString closeStr = element.attribute( "close" ).stripWhiteSpace();
|
||
15 years ago
|
if ( !closeStr.isNull() ) {
|
||
|
m_customRight = true;
|
||
|
if ( closeStr == "[" )
|
||
|
rightType = LeftSquareBracket;
|
||
|
else if ( closeStr == "]" )
|
||
|
rightType = RightSquareBracket;
|
||
|
else if ( closeStr == "{" )
|
||
|
rightType = LeftCurlyBracket;
|
||
|
else if ( closeStr == "}" )
|
||
|
rightType = RightCurlyBracket;
|
||
|
else if ( closeStr == "<" )
|
||
|
rightType = LeftCornerBracket;
|
||
|
else if ( closeStr == ">" )
|
||
|
rightType = RightCornerBracket;
|
||
|
else if ( closeStr == "(" )
|
||
|
rightType = LeftRoundBracket;
|
||
|
else if ( closeStr == ")" )
|
||
|
rightType = RightRoundBracket;
|
||
|
else if ( closeStr == "/" )
|
||
|
rightType = SlashBracket;
|
||
|
else if ( closeStr == "\\" )
|
||
|
rightType = BackSlashBracket;
|
||
|
else // TODO: Check for entity references
|
||
|
rightType = LeftRoundBracket;
|
||
|
}
|
||
|
m_separators = element.attribute( "separators" ).simplifyWhiteSpace();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our content from the MathML node. Sets the node to the next node
|
||
|
* that needs to be read.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
int BracketElement::readContentFromMathMLDom(TQDomNode& node)
|
||
15 years ago
|
{
|
||
|
bool empty = false;
|
||
|
int nodeCounter = 0;
|
||
|
if ( m_operator ) {
|
||
14 years ago
|
node = node.parentNode();
|
||
14 years ago
|
TQDomNode open = node;
|
||
14 years ago
|
TQDomNode parent = node.parentNode();
|
||
15 years ago
|
if ( ! operatorType( node, true ) )
|
||
|
return -1;
|
||
|
int nodeNum = searchOperator( node );
|
||
|
if ( nodeNum == -1 ) // Closing bracket not found
|
||
|
return -1;
|
||
|
if ( nodeNum == 0 ) { // Empty content
|
||
|
empty = true;
|
||
|
}
|
||
|
else if ( nodeNum == 1 ) {
|
||
|
do {
|
||
|
node = node.nextSibling();
|
||
|
nodeCounter++;
|
||
|
} while ( ! node.isElement() );
|
||
|
}
|
||
|
else { // More than two elements inside, infer a mrow
|
||
|
nodeCounter += nodeNum;
|
||
|
kdWarning() << "NodeNum: " << nodeNum << endl;
|
||
14 years ago
|
TQDomDocument doc = node.ownerDocument();
|
||
|
TQDomElement de = doc.createElement( "mrow" );
|
||
15 years ago
|
int i = 0;
|
||
|
do {
|
||
14 years ago
|
TQDomNode n = node.nextSibling();
|
||
15 years ago
|
de.appendChild( node.toElement() );
|
||
|
node = n;
|
||
|
} while ( ++i < nodeNum );
|
||
14 years ago
|
parent.insertAfter( de, open );
|
||
15 years ago
|
node = de;
|
||
|
kdWarning() << doc.toString() << endl;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
// if it's a mfence tag, we need to convert to equivalent expanded form.
|
||
|
// See section 3.3.8
|
||
|
while ( ! node.isNull() && ! node.isElement() )
|
||
|
node = node.nextSibling();
|
||
14 years ago
|
TQDomNode next = node.nextSibling();
|
||
15 years ago
|
while ( ! next.isNull() && ! next.isElement() )
|
||
|
next = next.nextSibling();
|
||
|
if ( ! next.isNull()) {
|
||
14 years ago
|
TQDomDocument doc = node.ownerDocument();
|
||
14 years ago
|
TQDomNode parent = node.parentNode();
|
||
|
TQString ns = parent.prefix();
|
||
14 years ago
|
TQDomElement de = doc.createElementNS( ns, "mrow" );
|
||
15 years ago
|
uint pos = 0;
|
||
|
while ( ! node.isNull() ) {
|
||
14 years ago
|
TQDomNode no = node.nextSibling();
|
||
15 years ago
|
while ( ! no.isNull() && ! no.isElement() )
|
||
|
no = no.nextSibling();
|
||
|
de.appendChild( node.toElement() );
|
||
|
if ( ! no.isNull() && ( m_separators.isNull() || ! m_separators.isEmpty() ) ) {
|
||
14 years ago
|
TQDomElement sep = doc.createElementNS( ns, "mo" );
|
||
15 years ago
|
de.appendChild( sep );
|
||
|
if ( m_separators.isNull() ) {
|
||
|
sep.appendChild( doc.createTextNode( "," ) );
|
||
|
}
|
||
|
else {
|
||
|
if ( m_separators.at( pos ).isSpace() ) {
|
||
|
pos++;
|
||
|
}
|
||
14 years ago
|
sep.appendChild( doc.createTextNode( TQString ( m_separators.at( pos ) ) ) );
|
||
15 years ago
|
}
|
||
|
if ( pos < m_separators.length() - 1 ) {
|
||
|
pos++;
|
||
|
}
|
||
|
}
|
||
|
node = no;
|
||
|
}
|
||
14 years ago
|
parent.appendChild( de );
|
||
|
node = parent.firstChild();
|
||
15 years ago
|
while ( ! node.isElement() )
|
||
|
node = node.nextSibling();
|
||
|
}
|
||
|
}
|
||
|
if ( ! empty ) {
|
||
|
int contentNumber = inherited::readContentFromMathMLDom( node );
|
||
|
if ( contentNumber == -1 )
|
||
|
return -1;
|
||
|
nodeCounter += contentNumber;
|
||
|
for (int i = 0; i < contentNumber; i++ ) {
|
||
|
if ( node.isNull() ) {
|
||
|
return -1;
|
||
|
}
|
||
|
node = node.nextSibling();
|
||
|
}
|
||
|
}
|
||
|
if ( m_operator ) {
|
||
|
int operatorNumber = operatorType( node, false );
|
||
|
if ( operatorNumber == -1 ) {
|
||
|
return -1;
|
||
|
}
|
||
|
nodeCounter += operatorNumber;
|
||
|
}
|
||
|
kdDebug( DEBUGID ) << "Number of bracket nodes: " << nodeCounter << endl;
|
||
|
return nodeCounter;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString BracketElement::toLatex()
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString ls,rs,cs;
|
||
15 years ago
|
cs=getContent()->toLatex();
|
||
|
ls="\\left"+latexString(leftType) + " ";
|
||
|
rs=" \\right"+latexString(rightType);
|
||
|
|
||
|
return ls+cs+rs;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString BracketElement::latexString(char type)
|
||
15 years ago
|
{
|
||
|
switch (type) {
|
||
|
case ']':
|
||
|
return "]";
|
||
|
case '[':
|
||
|
return "[";
|
||
|
case '{':
|
||
|
return "\\{";
|
||
|
case '}':
|
||
|
return "\\}";
|
||
|
case '(':
|
||
|
return "(";
|
||
|
case ')':
|
||
|
return ")";
|
||
|
case '|':
|
||
|
return "|";
|
||
|
case '<':
|
||
|
return "\\langle";
|
||
|
case '>':
|
||
|
return "\\rangle";
|
||
|
case '/':
|
||
|
return "/";
|
||
|
case '\\':
|
||
|
return "\\backslash";
|
||
|
}
|
||
|
return ".";
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString BracketElement::formulaString()
|
||
15 years ago
|
{
|
||
|
return "(" + getContent()->formulaString() + ")";
|
||
|
}
|
||
|
|
||
14 years ago
|
int BracketElement::operatorType( TQDomNode& node, bool open )
|
||
15 years ago
|
{
|
||
|
int counter = 1;
|
||
|
SymbolType* type = open ? &leftType : &rightType;
|
||
|
while ( ! node.isNull() && ! node.isElement() ) {
|
||
|
node = node.nextSibling();
|
||
|
counter++;
|
||
|
}
|
||
|
if ( node.isElement() ) {
|
||
14 years ago
|
TQDomElement e = node.toElement();
|
||
|
TQDomNode child = e.firstChild();
|
||
15 years ago
|
if ( child.isEntityReference() ) {
|
||
|
kdWarning() << "Entity Reference\n";
|
||
14 years ago
|
TQString name = node.nodeName();
|
||
15 years ago
|
// TODO: To fully support these, SymbolType has to be extended,
|
||
|
// and better Unicode support is a must
|
||
|
// CloseCurlyDoubleQuote 0x201D
|
||
|
// CloseCurlyQoute 0x2019
|
||
|
// LeftCeiling 0x2308
|
||
|
// LeftDoubleBracket 0x301A
|
||
|
// LeftFloor 0x230A
|
||
|
// OpenCurlyDoubleQuote 0x201C
|
||
|
// OpenCurlyQuote 0x2018
|
||
|
// RightCeiling 0x2309
|
||
|
// RightDoubleBracket 0x301B
|
||
|
// RightFloor 0x230B
|
||
|
if ( name == "LeftAngleBracket" ) {
|
||
|
*type = LeftCornerBracket;
|
||
|
}
|
||
|
else if ( name == "RightAngleBracket" ) {
|
||
|
*type = RightCornerBracket;
|
||
|
}
|
||
|
else {
|
||
|
if ( open ) {
|
||
|
*type = LeftRoundBracket;
|
||
|
}
|
||
|
else
|
||
|
*type = RightRoundBracket;
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
14 years ago
|
TQString s = e.text();
|
||
15 years ago
|
if ( s.isNull() )
|
||
|
return -1;
|
||
13 years ago
|
*type = static_cast<SymbolType>( TQString::number( s.at( 0 ).latin1() ).toInt() );
|
||
15 years ago
|
}
|
||
|
}
|
||
|
else {
|
||
|
return -1;
|
||
|
}
|
||
|
return counter;
|
||
|
}
|
||
|
|
||
14 years ago
|
int BracketElement::searchOperator( const TQDomNode& node )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomNode n = node;
|
||
15 years ago
|
for ( int i = -2; ! n.isNull(); n = n.nextSibling() ) {
|
||
|
if ( n.isElement() ) {
|
||
|
i++;
|
||
14 years ago
|
TQDomElement e = n.toElement();
|
||
15 years ago
|
if ( e.tagName().lower() == "mo" ) {
|
||
|
// Try to guess looking at attributes
|
||
14 years ago
|
TQString form = e.attribute( "form" );
|
||
|
TQString f;
|
||
15 years ago
|
if ( ! form.isNull() ) {
|
||
|
f = form.stripWhiteSpace().lower();
|
||
|
}
|
||
14 years ago
|
TQString fence = e.attribute( "fence" );
|
||
15 years ago
|
if ( ! fence.isNull() ) {
|
||
|
if ( fence.stripWhiteSpace().lower() == "false" ) {
|
||
|
continue;
|
||
|
}
|
||
|
if ( ! f.isNull() ) {
|
||
|
if ( f == "postfix" ) {
|
||
|
return i;
|
||
|
}
|
||
|
else {
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Guess looking at contents
|
||
14 years ago
|
TQDomNode child = e.firstChild();
|
||
|
TQString name;
|
||
15 years ago
|
if ( child.isText() )
|
||
|
name = child.toText().data().stripWhiteSpace();
|
||
|
else if ( child.isEntityReference() )
|
||
|
name = child.nodeName();
|
||
|
else
|
||
|
continue;
|
||
|
if ( name == ")"
|
||
|
|| name == "]"
|
||
|
|| name == "}"
|
||
|
|| name == "CloseCurlyDoubleQuote"
|
||
|
|| name == "CloseCurlyQuote"
|
||
|
|| name == "RightAngleBracket"
|
||
|
|| name == "RightCeiling"
|
||
|
|| name == "RightDoubleBracket"
|
||
|
|| name == "RightFloor" ) {
|
||
|
if ( f.isNull() || f == "postfix" )
|
||
|
return i;
|
||
|
}
|
||
|
if ( name == "("
|
||
|
|| name == "["
|
||
|
|| name == "{"
|
||
|
|| name == "LeftAngleBracket"
|
||
|
|| name == "LeftCeiling"
|
||
|
|| name == "LeftDoubleBracket"
|
||
|
|| name == "LeftFloor"
|
||
|
|| name == "OpenCurlyQuote" ) {
|
||
|
if ( ! f.isNull() && f == "postfix" )
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
void BracketElement::writeMathMLAttributes( TQDomElement& element ) const
|
||
15 years ago
|
{
|
||
|
if ( left->getType() != LeftRoundBracket ||
|
||
|
right->getType() != RightRoundBracket )
|
||
|
{
|
||
14 years ago
|
element.setAttribute( "open", TQString( TQChar( leftType ) ) );
|
||
|
element.setAttribute( "close", TQString( TQChar( rightType ) ) );
|
||
15 years ago
|
}
|
||
|
if ( ! m_separators.isNull() ) {
|
||
|
element.setAttribute( "separators", m_separators );
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
OverlineElement::OverlineElement( BasicElement* parent )
|
||
|
: SingleContentElement( parent )
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
OverlineElement::~OverlineElement()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
OverlineElement::OverlineElement( const OverlineElement& other )
|
||
|
: SingleContentElement( other )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
bool OverlineElement::accept( ElementVisitor* visitor )
|
||
|
{
|
||
|
return visitor->visit( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
void OverlineElement::entered( SequenceElement* /*child*/ )
|
||
|
{
|
||
|
formula()->tell( i18n( "Overline" ) );
|
||
|
}
|
||
|
|
||
|
|
||
|
void OverlineElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
SequenceElement* content = getContent();
|
||
|
content->calcSizes(context, tstyle,
|
||
|
context.convertIndexStyleLower(istyle), style );
|
||
|
|
||
|
//luPixel distX = context.ptToPixelX( context.getThinSpace( tstyle, style.sizeFactor() ) );
|
||
|
luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, style.sizeFactor() ) );
|
||
|
//luPixel unit = (content->getHeight() + distY)/ 3;
|
||
|
|
||
|
setWidth( content->getWidth() );
|
||
|
setHeight( content->getHeight() + distY );
|
||
|
|
||
|
content->setX( 0 );
|
||
|
content->setY( distY );
|
||
|
setBaseline(content->getBaseline() + content->getY());
|
||
|
}
|
||
|
|
||
14 years ago
|
void OverlineElement::draw( TQPainter& painter, const LuPixelRect& r,
|
||
15 years ago
|
const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style,
|
||
14 years ago
|
const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
|
||
15 years ago
|
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
|
||
|
// return;
|
||
|
|
||
|
SequenceElement* content = getContent();
|
||
|
content->draw( painter, r, context, tstyle,
|
||
|
context.convertIndexStyleLower( istyle ), style, myPos );
|
||
|
|
||
|
luPixel x = myPos.x();
|
||
|
luPixel y = myPos.y();
|
||
|
//int distX = context.getDistanceX(tstyle);
|
||
|
double factor = style.sizeFactor();
|
||
|
luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
|
||
|
//luPixel unit = (content->getHeight() + distY)/ 3;
|
||
|
|
||
14 years ago
|
painter.setPen( TQPen( context.getDefaultColor(),
|
||
14 years ago
|
context.layoutUnitToPixelY( context.getLineWidth( factor ) ) ) );
|
||
15 years ago
|
|
||
14 years ago
|
painter.drawLine( context.layoutUnitToPixelX( x ),
|
||
|
context.layoutUnitToPixelY( y+distY/3 ),
|
||
|
context.layoutUnitToPixelX( x+content->getWidth() ),
|
||
|
context.layoutUnitToPixelY( y+distY/3 ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
14 years ago
|
TQString OverlineElement::toLatex()
|
||
15 years ago
|
{
|
||
|
return "\\overline{" + getContent()->toLatex() + "}";
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString OverlineElement::formulaString()
|
||
15 years ago
|
{
|
||
|
return getContent()->formulaString();
|
||
|
}
|
||
|
|
||
14 years ago
|
void OverlineElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement de = doc.createElement( oasisFormat ? "math:mover" : "mover" );
|
||
15 years ago
|
SingleContentElement::writeMathML( doc, de, oasisFormat );
|
||
14 years ago
|
TQDomElement op = doc.createElement( oasisFormat ? "math:mo" : "mo" );
|
||
15 years ago
|
// is this the right entity? Mozilla renders it correctly.
|
||
|
op.appendChild( doc.createEntityReference( "OverBar" ) );
|
||
|
de.appendChild( op );
|
||
14 years ago
|
parent.appendChild( de );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
14 years ago
|
UnderlineElement::UnderlineElement( BasicElement* parent )
|
||
|
: SingleContentElement( parent )
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
UnderlineElement::~UnderlineElement()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
UnderlineElement::UnderlineElement( const UnderlineElement& other )
|
||
|
: SingleContentElement( other )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
|
||
|
bool UnderlineElement::accept( ElementVisitor* visitor )
|
||
|
{
|
||
|
return visitor->visit( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
void UnderlineElement::entered( SequenceElement* /*child*/ )
|
||
|
{
|
||
|
formula()->tell( i18n( "Underline" ) );
|
||
|
}
|
||
|
|
||
|
|
||
|
void UnderlineElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
SequenceElement* content = getContent();
|
||
|
double factor = style.sizeFactor();
|
||
|
content->calcSizes(context, tstyle,
|
||
|
context.convertIndexStyleLower(istyle), style );
|
||
|
|
||
|
//luPixel distX = context.ptToPixelX( context.getThinSpace( tstyle ) );
|
||
|
luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
|
||
|
//luPixel unit = (content->getHeight() + distY)/ 3;
|
||
|
|
||
|
setWidth( content->getWidth() );
|
||
|
setHeight( content->getHeight() + distY );
|
||
|
|
||
|
content->setX( 0 );
|
||
|
content->setY( 0 );
|
||
|
setBaseline(content->getBaseline() + content->getY());
|
||
|
}
|
||
|
|
||
14 years ago
|
void UnderlineElement::draw( TQPainter& painter, const LuPixelRect& r,
|
||
15 years ago
|
const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style,
|
||
14 years ago
|
const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
LuPixelPoint myPos( parentOrigin.x()+getX(), parentOrigin.y()+getY() );
|
||
15 years ago
|
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
|
||
|
// return;
|
||
|
|
||
|
SequenceElement* content = getContent();
|
||
|
content->draw( painter, r, context, tstyle,
|
||
|
context.convertIndexStyleLower( istyle ), style, myPos );
|
||
|
|
||
|
luPixel x = myPos.x();
|
||
|
luPixel y = myPos.y();
|
||
|
//int distX = context.getDistanceX(tstyle);
|
||
|
//luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle ) );
|
||
|
//luPixel unit = (content->getHeight() + distY)/ 3;
|
||
|
|
||
|
double factor = style.sizeFactor();
|
||
14 years ago
|
painter.setPen( TQPen( context.getDefaultColor(),
|
||
14 years ago
|
context.layoutUnitToPixelY( context.getLineWidth( factor ) ) ) );
|
||
15 years ago
|
|
||
14 years ago
|
painter.drawLine( context.layoutUnitToPixelX( x ),
|
||
|
context.layoutUnitToPixelY( y+getHeight()-context.getLineWidth( factor ) ),
|
||
|
context.layoutUnitToPixelX( x+content->getWidth() ),
|
||
|
context.layoutUnitToPixelY( y+getHeight()-context.getLineWidth( factor ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
14 years ago
|
TQString UnderlineElement::toLatex()
|
||
15 years ago
|
{
|
||
|
return "\\underline{" + getContent()->toLatex() + "}";
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString UnderlineElement::formulaString()
|
||
15 years ago
|
{
|
||
|
return getContent()->formulaString();
|
||
|
}
|
||
|
|
||
14 years ago
|
void UnderlineElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement de = doc.createElement( oasisFormat ? "math:munder" : "munder" );
|
||
15 years ago
|
SingleContentElement::writeMathML( doc, de, oasisFormat );
|
||
14 years ago
|
TQDomElement op = doc.createElement( oasisFormat ? "math:mo" : "mo" );
|
||
15 years ago
|
// is this the right entity? Mozilla renders it correctly.
|
||
|
op.appendChild( doc.createEntityReference( "UnderBar" ) );
|
||
|
de.appendChild( op );
|
||
14 years ago
|
parent.appendChild( de );
|
||
15 years ago
|
}
|
||
|
|
||
|
KFORMULA_NAMESPACE_END
|