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.
401 lines
10 KiB
401 lines
10 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 <tqstring.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
|
||
|
#include "contextstyle.h"
|
||
|
#include "basicelement.h"
|
||
|
#include "formulacursor.h"
|
||
|
#include "formulaelement.h"
|
||
|
#include "sequenceelement.h"
|
||
|
|
||
|
KFORMULA_NAMESPACE_BEGIN
|
||
|
using namespace std;
|
||
|
|
||
|
|
||
|
int BasicElement::evilDestructionCount = 0;
|
||
|
|
||
|
BasicElement::BasicElement( BasicElement* p )
|
||
13 years ago
|
: parent( p ), m_baseline( 0 ), elementType( 0 )
|
||
15 years ago
|
{
|
||
|
setX( 0 );
|
||
|
setY( 0 );
|
||
|
setWidth( 0 );
|
||
|
setHeight( 0 );
|
||
|
evilDestructionCount++;
|
||
|
}
|
||
|
|
||
|
BasicElement::~BasicElement()
|
||
|
{
|
||
|
evilDestructionCount--;
|
||
|
}
|
||
|
|
||
|
BasicElement::BasicElement( const BasicElement& other )
|
||
13 years ago
|
: parent( 0 ),
|
||
15 years ago
|
m_baseline( other.m_baseline ),
|
||
|
elementType( other.elementType )
|
||
|
{
|
||
|
setX( other.getX() );
|
||
|
setY( other.getY() );
|
||
|
setWidth( other.getWidth() );
|
||
|
setHeight( other.getHeight() );
|
||
|
evilDestructionCount++;
|
||
|
}
|
||
|
|
||
|
|
||
|
bool BasicElement::readOnly( const BasicElement* /*child*/ ) const
|
||
|
{
|
||
13 years ago
|
return parent->readOnly( this );
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
FormulaElement* BasicElement::formula()
|
||
|
{
|
||
13 years ago
|
//if ( parent != 0 ) {
|
||
|
return parent->formula();
|
||
15 years ago
|
//}
|
||
|
//return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Returns the element the point is in.
|
||
|
*/
|
||
|
BasicElement* BasicElement::goToPos( FormulaCursor*, bool&,
|
||
14 years ago
|
const LuPixelPoint& point, const LuPixelPoint& parentOrigin )
|
||
15 years ago
|
{
|
||
14 years ago
|
luPixel x = point.x() - (parentOrigin.x() + getX());
|
||
15 years ago
|
if ((x >= 0) && (x < getWidth())) {
|
||
14 years ago
|
luPixel y = point.y() - (parentOrigin.y() + getY());
|
||
15 years ago
|
if ((y >= 0) && (y < getHeight())) {
|
||
|
return this;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns our position inside the widget.
|
||
|
*/
|
||
|
LuPixelPoint BasicElement::widgetPos()
|
||
|
{
|
||
|
luPixel x = 0;
|
||
|
luPixel y = 0;
|
||
13 years ago
|
for (BasicElement* element = this; element != 0; element = element->parent) {
|
||
15 years ago
|
x += element->getX();
|
||
|
y += element->getY();
|
||
|
}
|
||
|
return LuPixelPoint(x, y);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Sets the cursor inside this element to its start position.
|
||
|
* For most elements that is the main child.
|
||
|
*/
|
||
|
void BasicElement::goInside(FormulaCursor* cursor)
|
||
|
{
|
||
|
BasicElement* mainChild = getMainChild();
|
||
|
if (mainChild != 0) {
|
||
|
mainChild->goInside(cursor);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void BasicElement::entered( SequenceElement* /*child*/ )
|
||
|
{
|
||
|
formula()->tell( "" );
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Enters this element while moving to the left starting inside
|
||
|
* the element `from'. Searches for a cursor position inside
|
||
|
* this element or to the left of it.
|
||
|
*/
|
||
|
void BasicElement::moveLeft(FormulaCursor* cursor, BasicElement*)
|
||
|
{
|
||
|
getParent()->moveLeft(cursor, this);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Enters this element while moving to the right starting inside
|
||
|
* the element `from'. Searches for a cursor position inside
|
||
|
* this element or to the right of it.
|
||
|
*/
|
||
|
void BasicElement::moveRight(FormulaCursor* cursor, BasicElement*)
|
||
|
{
|
||
|
getParent()->moveRight(cursor, this);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Moves the cursor to a normal place where new elements
|
||
|
* might be inserted.
|
||
|
*/
|
||
|
void BasicElement::normalize(FormulaCursor* cursor, Direction direction)
|
||
|
{
|
||
|
BasicElement* element = getMainChild();
|
||
|
if (element != 0) {
|
||
|
if (direction == beforeCursor) {
|
||
|
element->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
element->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
14 years ago
|
TQDomElement BasicElement::getElementDom( TQDomDocument& doc)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement de = doc.createElement(getTagName());
|
||
15 years ago
|
writeDom(de);
|
||
|
return de;
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
void BasicElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement de = doc.createElement( oasisFormat ? "math:" + getElementName() : getElementName() );
|
||
15 years ago
|
writeMathMLAttributes( de );
|
||
|
writeMathMLContent( doc, de, oasisFormat );
|
||
13 years ago
|
parent.appendChild( de );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
bool BasicElement::buildFromDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
if (element.tagName() != getTagName()) {
|
||
|
kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
|
||
|
return false;
|
||
|
}
|
||
|
if (!readAttributesFromDom(element)) {
|
||
|
return false;
|
||
|
}
|
||
14 years ago
|
TQDomNode node = element.firstChild();
|
||
15 years ago
|
return readContentFromDom(node);
|
||
|
}
|
||
|
|
||
14 years ago
|
int BasicElement::buildFromMathMLDom(TQDomElement element)
|
||
15 years ago
|
{/*
|
||
|
if (element.tagName() != getTagName()) {
|
||
|
kdWarning( DEBUGID ) << "Wrong tag name " << element.tagName().latin1() << " for " << getTagName().latin1() << ".\n";
|
||
|
return false;
|
||
|
}*/
|
||
|
if (!readAttributesFromMathMLDom(element)) {
|
||
|
return -1;
|
||
|
}
|
||
14 years ago
|
TQDomNode node = element.firstChild();
|
||
15 years ago
|
return readContentFromMathMLDom(node);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Appends our attributes to the dom element.
|
||
|
*/
|
||
14 years ago
|
void BasicElement::writeDom(TQDomElement)
|
||
15 years ago
|
{
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our attributes from the element.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool BasicElement::readAttributesFromDom(TQDomElement)
|
||
15 years ago
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our content from the node. Sets the node to the next node
|
||
|
* that needs to be read.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool BasicElement::readContentFromDom(TQDomNode&)
|
||
15 years ago
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a SequenceElement constructed from the nodes first child
|
||
|
* if the nodes name matches the given name.
|
||
|
*/
|
||
14 years ago
|
bool BasicElement::buildChild( SequenceElement* child, TQDomNode node, TQString name )
|
||
15 years ago
|
{
|
||
|
if (node.isElement()) {
|
||
14 years ago
|
TQDomElement e = node.toElement();
|
||
15 years ago
|
if (e.tagName().upper() == name) {
|
||
14 years ago
|
TQDomNode nodeInner = e.firstChild();
|
||
15 years ago
|
if (nodeInner.isElement()) {
|
||
14 years ago
|
TQDomElement element = nodeInner.toElement();
|
||
15 years ago
|
return child->buildFromDom( element );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our attributes from the MathML element.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool BasicElement::readAttributesFromMathMLDom(const TQDomElement& )
|
||
15 years ago
|
{
|
||
|
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 BasicElement::readContentFromMathMLDom(TQDomNode&)
|
||
15 years ago
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString BasicElement::toLatex()
|
||
15 years ago
|
{
|
||
|
return "{}";
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Utility function that sets the size type and returns the size value from
|
||
|
* a MathML attribute string with unit as defined in Section 2.4.4.2
|
||
|
*
|
||
|
* @returns the size value
|
||
|
*
|
||
|
* @param str the attribute string.
|
||
|
* @param st size type container. It will be properly assigned to its size
|
||
|
* type or NoSize if str is invalid
|
||
|
*/
|
||
14 years ago
|
double BasicElement::getSize( const TQString& str, SizeType* st )
|
||
15 years ago
|
{
|
||
13 years ago
|
int index = str.find( "%" );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, RelativeSize ) / 100.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "pt", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, AbsoluteSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "mm", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, AbsoluteSize ) * 72.0 / 20.54;
|
||
|
}
|
||
13 years ago
|
index = str.find( "cm", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, AbsoluteSize ) * 72.0 / 2.54;
|
||
|
}
|
||
13 years ago
|
index = str.find( "in", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, AbsoluteSize ) * 72.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "em", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, RelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "ex", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, RelativeSize );
|
||
|
}
|
||
13 years ago
|
index = str.find( "pc", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, AbsoluteSize ) * 12.0;
|
||
|
}
|
||
13 years ago
|
index = str.find( "px", 0, false );
|
||
15 years ago
|
if ( index != -1 ) {
|
||
|
return str2size( str, st, index, PixelSize );
|
||
|
}
|
||
|
// If there's no unit, assume 'pt'
|
||
|
return str2size( str, st, str.length(),AbsoluteSize );
|
||
|
}
|
||
|
|
||
14 years ago
|
SizeType BasicElement::getSpace( const TQString& str )
|
||
15 years ago
|
{
|
||
|
if ( str == "negativeveryverythinmathspace" ) {
|
||
|
return NegativeVeryVeryThinMathSpace;
|
||
|
}
|
||
|
if ( str == "negativeverythinmathspace" ) {
|
||
|
return NegativeVeryThinMathSpace;
|
||
|
}
|
||
|
if ( str == "negativethinmathspace" ) {
|
||
|
return NegativeThinMathSpace;
|
||
|
}
|
||
|
if ( str == "negativemediummathspace" ) {
|
||
|
return NegativeMediumMathSpace;
|
||
|
}
|
||
|
if ( str == "negativethickmathspace" ) {
|
||
|
return NegativeThickMathSpace;
|
||
|
}
|
||
|
if ( str == "negativeverythickmathspace" ) {
|
||
|
return NegativeVeryThickMathSpace;
|
||
|
}
|
||
|
if ( str == "negativeveryverythickmathspace" ) {
|
||
|
return NegativeVeryVeryThickMathSpace;
|
||
|
}
|
||
|
if ( str == "veryverythinmathspace" ) {
|
||
|
return VeryVeryThinMathSpace;
|
||
|
}
|
||
|
if ( str == "verythinmathspace" ) {
|
||
|
return VeryThinMathSpace;
|
||
|
}
|
||
|
if ( str == "thinmathspace" ) {
|
||
|
return ThinMathSpace;
|
||
|
}
|
||
|
if ( str == "mediummathspace" ) {
|
||
|
return MediumMathSpace;
|
||
|
}
|
||
|
if ( str == "thickmathspace" ) {
|
||
|
return ThickMathSpace;
|
||
|
}
|
||
|
if ( str == "verythickmathspace" ) {
|
||
|
return VeryThickMathSpace;
|
||
|
}
|
||
|
if ( str == "veryverythickmathspace" ) {
|
||
|
return VeryVeryThickMathSpace;
|
||
|
}
|
||
|
return NoSize;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Used internally by getSize()
|
||
|
*/
|
||
14 years ago
|
double BasicElement::str2size( const TQString& str, SizeType *st, uint index, SizeType type )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString num = str.left( index );
|
||
15 years ago
|
bool ok;
|
||
|
double size = num.toDouble( &ok );
|
||
|
if ( ok ) {
|
||
|
if ( st ) {
|
||
|
*st = type;
|
||
|
}
|
||
|
return size;
|
||
|
}
|
||
|
if ( st ) {
|
||
|
*st = NoSize;
|
||
|
}
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
KFORMULA_NAMESPACE_END
|