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.
907 lines
25 KiB
907 lines
25 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 <tqpainter.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include "elementvisitor.h"
|
||
|
#include "formulacursor.h"
|
||
|
#include "formulaelement.h"
|
||
|
#include "kformulacommand.h"
|
||
|
#include "sequenceelement.h"
|
||
|
#include "symbolelement.h"
|
||
|
|
||
|
KFORMULA_NAMESPACE_BEGIN
|
||
|
|
||
|
|
||
|
class SymbolSequenceElement : public SequenceElement {
|
||
|
typedef SequenceElement inherited;
|
||
|
public:
|
||
|
|
||
13 years ago
|
SymbolSequenceElement( BasicElement* parent = 0 ) : SequenceElement( parent ) {}
|
||
15 years ago
|
|
||
|
/**
|
||
|
* This is called by the container to get a command depending on
|
||
|
* the current cursor position (this is how the element gets chosen)
|
||
|
* and the request.
|
||
|
*
|
||
|
* @returns the command that performs the requested action with
|
||
|
* the containers active cursor.
|
||
|
*/
|
||
|
virtual KCommand* buildCommand( Container*, Request* );
|
||
|
};
|
||
|
|
||
|
|
||
|
KCommand* SymbolSequenceElement::buildCommand( Container* container, Request* request )
|
||
|
{
|
||
|
FormulaCursor* cursor = container->activeCursor();
|
||
|
if ( cursor->isReadOnly() ) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
switch ( *request ) {
|
||
|
case req_addIndex: {
|
||
|
FormulaCursor* cursor = container->activeCursor();
|
||
|
if ( cursor->isSelection() ||
|
||
|
( cursor->getPos() > 0 && cursor->getPos() < countChildren() ) ) {
|
||
|
break;
|
||
|
}
|
||
|
IndexRequest* ir = static_cast<IndexRequest*>( request );
|
||
|
if ( ( ir->index() == upperMiddlePos ) || ( ir->index() == lowerMiddlePos ) ) {
|
||
|
SymbolElement* element = static_cast<SymbolElement*>( getParent() );
|
||
|
ElementIndexPtr index = element->getIndex( ir->index() );
|
||
|
if ( !index->hasIndex() ) {
|
||
|
KFCAddGenericIndex* command = new KFCAddGenericIndex( container, index );
|
||
|
return command;
|
||
|
}
|
||
|
else {
|
||
|
index->moveToIndex( cursor, afterCursor );
|
||
|
cursor->setSelection( false );
|
||
|
formula()->cursorHasMoved( cursor );
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
return inherited::buildCommand( container, request );
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
SymbolElement::SymbolElement(SymbolType type, BasicElement* parent)
|
||
|
: BasicElement(parent), symbol( 0 ), symbolType( type )
|
||
15 years ago
|
{
|
||
|
content = new SymbolSequenceElement( this );
|
||
|
upper = 0;
|
||
|
lower = 0;
|
||
|
}
|
||
|
|
||
|
SymbolElement::~SymbolElement()
|
||
|
{
|
||
|
delete lower;
|
||
|
delete upper;
|
||
|
delete content;
|
||
|
delete symbol;
|
||
|
}
|
||
|
|
||
|
|
||
|
SymbolElement::SymbolElement( const SymbolElement& other )
|
||
|
: BasicElement( other ), symbol( 0 ), symbolType( other.symbolType )
|
||
|
{
|
||
|
content = new SymbolSequenceElement( *dynamic_cast<SymbolSequenceElement*>( other.content ) );
|
||
|
content->setParent( this );
|
||
|
|
||
|
if ( other.upper ) {
|
||
|
upper = new SequenceElement( *( other.upper ) );
|
||
|
upper->setParent( this );
|
||
|
}
|
||
|
else {
|
||
|
upper = 0;
|
||
|
}
|
||
|
if ( other.lower ) {
|
||
|
lower = new SequenceElement( *( other.lower ) );
|
||
|
lower->setParent( this );
|
||
|
}
|
||
|
else {
|
||
|
lower = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
bool SymbolElement::accept( ElementVisitor* visitor )
|
||
|
{
|
||
|
return visitor->visit( this );
|
||
|
}
|
||
|
|
||
|
|
||
|
BasicElement* SymbolElement::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;
|
||
|
}
|
||
|
if (hasLower()) {
|
||
|
e = lower->goToPos(cursor, handled, point, myPos);
|
||
|
if (e != 0) {
|
||
|
return e;
|
||
|
}
|
||
|
}
|
||
|
if (hasUpper()) {
|
||
|
e = upper->goToPos(cursor, handled, point, myPos);
|
||
|
if (e != 0) {
|
||
|
return e;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// the positions after the indexes.
|
||
|
luPixel dx = point.x() - myPos.x();
|
||
|
luPixel dy = point.y() - myPos.y();
|
||
|
if (dy < symbol->getY()) {
|
||
|
if (hasUpper() && (dx > upper->getX())) {
|
||
|
upper->moveLeft(cursor, this);
|
||
|
handled = true;
|
||
|
return upper;
|
||
|
}
|
||
|
}
|
||
|
else if (dy > symbol->getY()+symbol->getHeight()) {
|
||
|
if (hasLower() && (dx > lower->getX())) {
|
||
|
lower->moveLeft(cursor, this);
|
||
|
handled = true;
|
||
|
return lower;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Have the cursor jump behind the integral.
|
||
|
if ( ( dx < symbol->getX()+symbol->getWidth() ) &&
|
||
|
( dx > symbol->getX()+symbol->getWidth()/2 ) ) {
|
||
|
content->moveRight( cursor, this );
|
||
|
handled = true;
|
||
|
return content;
|
||
|
}
|
||
|
|
||
|
return this;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Calculates our width and height and
|
||
13 years ago
|
* our children's parentPosition.
|
||
15 years ago
|
*/
|
||
|
void SymbolElement::calcSizes( const ContextStyle& context,
|
||
|
ContextStyle::TextStyle tstyle,
|
||
|
ContextStyle::IndexStyle istyle,
|
||
|
StyleAttributes& style )
|
||
|
{
|
||
|
double factor = style.sizeFactor();
|
||
|
luPt mySize = context.getAdjustedSize( tstyle, factor );
|
||
|
luPixel distX = context.ptToPixelX( context.getThinSpace( tstyle, factor ) );
|
||
|
luPixel distY = context.ptToPixelY( context.getThinSpace( tstyle, factor ) );
|
||
|
|
||
|
//if ( symbol == 0 ) {
|
||
|
delete symbol;
|
||
|
symbol = context.fontStyle().createArtwork( symbolType );
|
||
|
//}
|
||
|
|
||
|
symbol->calcSizes(context, tstyle, factor, mySize);
|
||
|
content->calcSizes( context, tstyle, istyle, style );
|
||
|
|
||
14 years ago
|
//symbol->scale(((double)parentSize)/symbol->getHeight()*2);
|
||
15 years ago
|
|
||
|
luPixel upperWidth = 0;
|
||
|
luPixel upperHeight = 0;
|
||
|
if (hasUpper()) {
|
||
|
upper->calcSizes( context, context.convertTextStyleIndex( tstyle ),
|
||
|
context.convertIndexStyleUpper( istyle ), style );
|
||
|
upperWidth = upper->getWidth();
|
||
|
upperHeight = upper->getHeight() + distY;
|
||
|
}
|
||
|
|
||
|
luPixel lowerWidth = 0;
|
||
|
luPixel lowerHeight = 0;
|
||
|
if (hasLower()) {
|
||
|
lower->calcSizes( context, context.convertTextStyleIndex( tstyle ),
|
||
|
context.convertIndexStyleLower( istyle ), style );
|
||
|
lowerWidth = lower->getWidth();
|
||
|
lowerHeight = lower->getHeight() + distY;
|
||
|
}
|
||
|
|
||
|
// widths
|
||
14 years ago
|
luPixel xOffset = TQMAX(symbol->getWidth(), TQMAX(upperWidth, lowerWidth));
|
||
15 years ago
|
if (context.getCenterSymbol()) {
|
||
|
symbol->setX((xOffset - symbol->getWidth()) / 2);
|
||
|
}
|
||
|
else {
|
||
|
symbol->setX(xOffset - symbol->getWidth());
|
||
|
}
|
||
|
// ???
|
||
|
content->setX(xOffset +
|
||
|
static_cast<luPixel>( symbol->slant()*symbol->getHeight()/2 ) +
|
||
|
distX/2);
|
||
|
|
||
14 years ago
|
setWidth(TQMAX(content->getX() + content->getWidth(),
|
||
|
TQMAX(upperWidth, lowerWidth)));
|
||
15 years ago
|
|
||
|
// heights
|
||
14 years ago
|
//int toMidline = TQMAX(content->getHeight() / 2,
|
||
|
luPixel toMidline = TQMAX(content->axis( context, tstyle, factor ),
|
||
15 years ago
|
upperHeight + symbol->getHeight()/2);
|
||
14 years ago
|
//int fromMidline = TQMAX(content->getHeight() / 2,
|
||
|
luPixel fromMidline = TQMAX(content->getHeight() - content->axis( context, tstyle, factor ),
|
||
15 years ago
|
lowerHeight + symbol->getHeight()/2);
|
||
|
setHeight(toMidline + fromMidline);
|
||
|
//setMidline(toMidline);
|
||
|
|
||
|
symbol->setY(toMidline - symbol->getHeight()/2);
|
||
|
//content->setY(toMidline - content->getHeight()/2);
|
||
|
content->setY(toMidline - content->axis( context, tstyle, factor ));
|
||
|
|
||
|
if (hasUpper()) {
|
||
|
luPixel slant =
|
||
|
static_cast<luPixel>( symbol->slant()*( symbol->getHeight()+distY ) );
|
||
|
if (context.getCenterSymbol()) {
|
||
|
upper->setX((xOffset - upperWidth) / 2 + slant );
|
||
|
}
|
||
|
else {
|
||
|
if (upperWidth < symbol->getWidth()) {
|
||
|
upper->setX(symbol->getX() +
|
||
|
(symbol->getWidth() - upperWidth) / 2 + slant );
|
||
|
}
|
||
|
else {
|
||
|
upper->setX(xOffset - upperWidth);
|
||
|
}
|
||
|
}
|
||
|
upper->setY(toMidline - upperHeight - symbol->getHeight()/2);
|
||
|
}
|
||
|
if (hasLower()) {
|
||
|
luPixel slant = static_cast<luPixel>( -symbol->slant()*distY );
|
||
|
if (context.getCenterSymbol()) {
|
||
|
lower->setX((xOffset - lowerWidth) / 2 + slant);
|
||
|
}
|
||
|
else {
|
||
|
if (lowerWidth < symbol->getWidth()) {
|
||
|
lower->setX(symbol->getX() +
|
||
|
(symbol->getWidth() - lowerWidth) / 2 + slant );
|
||
|
}
|
||
|
else {
|
||
|
lower->setX(xOffset - lowerWidth);
|
||
|
}
|
||
|
}
|
||
|
lower->setY(toMidline + symbol->getHeight()/2 + distY);
|
||
|
}
|
||
|
setBaseline(content->getBaseline() + content->getY());
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Draws the whole element including its children.
|
||
13 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 SymbolElement::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;
|
||
|
|
||
|
luPt mySize = context.getAdjustedSize( tstyle, style.sizeFactor() );
|
||
|
symbol->draw( painter, r, context, tstyle, style, mySize, myPos );
|
||
|
content->draw( painter, r, context, tstyle, istyle, style, myPos );
|
||
|
if ( hasUpper() ) {
|
||
|
upper->draw( painter, r, context, context.convertTextStyleIndex( tstyle ),
|
||
|
context.convertIndexStyleUpper( istyle ), style, myPos );
|
||
|
}
|
||
|
if ( hasLower() ) {
|
||
|
lower->draw( painter, r, context, context.convertTextStyleIndex( tstyle ),
|
||
|
context.convertIndexStyleLower( istyle ), style, myPos );
|
||
|
}
|
||
|
|
||
|
// Debug
|
||
|
#if 0
|
||
14 years ago
|
painter.setBrush(TQt::NoBrush);
|
||
|
painter.setPen(TQt::red);
|
||
14 years ago
|
// painter.drawRect( context.layoutUnitToPixelX( myPos.x() ),
|
||
|
// context.layoutUnitToPixelY( myPos.y() ),
|
||
|
// context.layoutUnitToPixelX( getWidth() ),
|
||
|
// context.layoutUnitToPixelY( getHeight() ) );
|
||
|
painter.drawRect( context.layoutUnitToPixelX( myPos.x()+symbol->getX() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+symbol->getY() ),
|
||
|
context.layoutUnitToPixelX( symbol->getWidth() ),
|
||
|
context.layoutUnitToPixelY( symbol->getHeight() ) );
|
||
14 years ago
|
painter.setPen(TQt::green);
|
||
14 years ago
|
painter.drawLine( context.layoutUnitToPixelX( myPos.x() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+axis(context, tstyle) ),
|
||
|
context.layoutUnitToPixelX( myPos.x()+getWidth() ),
|
||
|
context.layoutUnitToPixelY( myPos.y()+axis(context, tstyle) ) );
|
||
15 years ago
|
#endif
|
||
|
}
|
||
|
|
||
|
|
||
|
void SymbolElement::dispatchFontCommand( FontCommand* cmd )
|
||
|
{
|
||
|
content->dispatchFontCommand( cmd );
|
||
|
if ( hasUpper() ) {
|
||
|
upper->dispatchFontCommand( cmd );
|
||
|
}
|
||
|
if ( hasLower() ) {
|
||
|
lower->dispatchFontCommand( cmd );
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// navigation
|
||
|
//
|
||
|
// The elements are responsible to handle cursor movement themselves.
|
||
|
// To do this they need to know the direction the cursor moves and
|
||
|
// the element it comes from.
|
||
|
//
|
||
|
// The cursor might be in normal or in selection mode.
|
||
|
|
||
|
/**
|
||
|
* 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 SymbolElement::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 if (from == content) {
|
||
|
if (linear && hasLower()) {
|
||
|
lower->moveLeft(cursor, this);
|
||
|
}
|
||
|
else if (linear && hasUpper()) {
|
||
|
upper->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveLeft(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == lower) {
|
||
|
if (linear && hasUpper()) {
|
||
|
upper->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveLeft(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == upper) {
|
||
|
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 SymbolElement::moveRight(FormulaCursor* cursor, BasicElement* from)
|
||
|
{
|
||
|
if (cursor->isSelectionMode()) {
|
||
|
getParent()->moveRight(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
bool linear = cursor->getLinearMovement();
|
||
|
if (from == getParent()) {
|
||
|
if (linear && hasUpper()) {
|
||
|
upper->moveRight(cursor, this);
|
||
|
}
|
||
|
else if (linear && hasLower()) {
|
||
|
lower->moveRight(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == upper) {
|
||
|
if (linear && hasLower()) {
|
||
|
lower->moveRight(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == lower) {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
else if (from == content) {
|
||
|
getParent()->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Enters this element while moving up starting inside
|
||
|
* the element `from'. Searches for a cursor position inside
|
||
|
* this element or above it.
|
||
|
*/
|
||
|
void SymbolElement::moveUp(FormulaCursor* cursor, BasicElement* from)
|
||
|
{
|
||
|
if (cursor->isSelectionMode()) {
|
||
|
getParent()->moveUp(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
if (from == content) {
|
||
|
if (hasUpper()) {
|
||
|
upper->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveUp(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == upper) {
|
||
|
getParent()->moveUp(cursor, this);
|
||
|
}
|
||
|
else if ((from == getParent()) || (from == lower)) {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Enters this element while moving down starting inside
|
||
|
* the element `from'. Searches for a cursor position inside
|
||
|
* this element or below it.
|
||
|
*/
|
||
|
void SymbolElement::moveDown(FormulaCursor* cursor, BasicElement* from)
|
||
|
{
|
||
|
if (cursor->isSelectionMode()) {
|
||
|
getParent()->moveDown(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
if (from == content) {
|
||
|
if (hasLower()) {
|
||
|
lower->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
getParent()->moveDown(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
else if (from == lower) {
|
||
|
getParent()->moveDown(cursor, this);
|
||
|
}
|
||
|
else if ((from == getParent()) || (from == upper)) {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
// children
|
||
15 years ago
|
|
||
|
// main child
|
||
|
//
|
||
13 years ago
|
// If an element has children one has to become the main one.
|
||
15 years ago
|
|
||
|
// void SymbolElement::setMainChild(SequenceElement* child)
|
||
|
// {
|
||
|
// formula()->elementRemoval(content);
|
||
|
// content = child;
|
||
|
// content->setParent(this);
|
||
|
// formula()->changed();
|
||
|
// }
|
||
|
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Inserts all new children at the cursor position. Places the
|
||
15 years ago
|
* cursor according to the direction.
|
||
|
*
|
||
|
* You only can insert one index at a time. So the list must contain
|
||
|
* exactly on SequenceElement. And the index you want to insert
|
||
|
* must not exist already.
|
||
|
*
|
||
|
* The list will be emptied but stays the property of the caller.
|
||
|
*/
|
||
|
void SymbolElement::insert(FormulaCursor* cursor,
|
||
14 years ago
|
TQPtrList<BasicElement>& newChildren,
|
||
15 years ago
|
Direction direction)
|
||
|
{
|
||
|
SequenceElement* index = static_cast<SequenceElement*>(newChildren.take(0));
|
||
|
index->setParent(this);
|
||
|
|
||
|
switch (cursor->getPos()) {
|
||
|
case upperMiddlePos:
|
||
|
upper = index;
|
||
|
break;
|
||
|
case lowerMiddlePos:
|
||
|
lower = index;
|
||
|
break;
|
||
|
default:
|
||
|
// this is an error!
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (direction == beforeCursor) {
|
||
|
index->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
index->moveRight(cursor, this);
|
||
|
}
|
||
|
cursor->setSelection(false);
|
||
|
formula()->changed();
|
||
|
}
|
||
|
|
||
|
/**
|
||
13 years ago
|
* Removes all selected children and returns them. Places the
|
||
|
* cursor to where the children have been.
|
||
15 years ago
|
*
|
||
|
* The cursor has to be inside one of our indexes which is supposed
|
||
|
* to be empty. The index will be removed and the cursor will
|
||
|
* be placed to the removed index so it can be inserted again.
|
||
|
* This methode is called by SequenceElement::remove only.
|
||
|
*
|
||
|
* The ownership of the list is passed to the caller.
|
||
|
*/
|
||
|
void SymbolElement::remove(FormulaCursor* cursor,
|
||
14 years ago
|
TQPtrList<BasicElement>& removedChildren,
|
||
15 years ago
|
Direction direction)
|
||
|
{
|
||
|
int pos = cursor->getPos();
|
||
|
switch (pos) {
|
||
|
case upperMiddlePos:
|
||
|
removedChildren.append(upper);
|
||
|
formula()->elementRemoval(upper);
|
||
|
upper = 0;
|
||
|
setToUpper(cursor);
|
||
|
break;
|
||
|
case lowerMiddlePos:
|
||
|
removedChildren.append(lower);
|
||
|
formula()->elementRemoval(lower);
|
||
|
lower = 0;
|
||
|
setToLower(cursor);
|
||
|
break;
|
||
|
case contentPos: {
|
||
13 years ago
|
BasicElement* parent = getParent();
|
||
|
parent->selectChild(cursor, this);
|
||
|
parent->remove(cursor, removedChildren, direction);
|
||
15 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
formula()->changed();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Moves the cursor to a normal place where new elements
|
||
|
* might be inserted.
|
||
|
*/
|
||
|
void SymbolElement::normalize(FormulaCursor* cursor, Direction direction)
|
||
|
{
|
||
|
if (direction == beforeCursor) {
|
||
|
content->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
content->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the child at the cursor.
|
||
|
*/
|
||
|
BasicElement* SymbolElement::getChild(FormulaCursor* cursor, Direction)
|
||
|
{
|
||
|
int pos = cursor->getPos();
|
||
|
switch (pos) {
|
||
|
case contentPos:
|
||
|
return content;
|
||
|
case upperMiddlePos:
|
||
|
return upper;
|
||
|
case lowerMiddlePos:
|
||
|
return lower;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets the cursor to select the child. The mark is placed before,
|
||
|
* the position behind it.
|
||
|
*/
|
||
|
void SymbolElement::selectChild(FormulaCursor* cursor, BasicElement* child)
|
||
|
{
|
||
|
if (child == content) {
|
||
|
setToContent(cursor);
|
||
|
}
|
||
|
else if (child == upper) {
|
||
|
setToUpper(cursor);
|
||
|
}
|
||
|
else if (child == lower) {
|
||
|
setToLower(cursor);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SymbolElement::setToUpper(FormulaCursor* cursor)
|
||
|
{
|
||
|
cursor->setTo(this, upperMiddlePos);
|
||
|
}
|
||
|
|
||
|
void SymbolElement::setToLower(FormulaCursor* cursor)
|
||
|
{
|
||
|
cursor->setTo(this, lowerMiddlePos);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Sets the cursor to point to the place where the content is.
|
||
|
* There always is a content so this is not a useful place.
|
||
|
* No insertion or removal will succeed as long as the cursor is
|
||
|
* there.
|
||
|
*/
|
||
|
void SymbolElement::setToContent(FormulaCursor* cursor)
|
||
|
{
|
||
|
cursor->setTo(this, contentPos);
|
||
|
}
|
||
|
|
||
|
|
||
|
void SymbolElement::moveToUpper(FormulaCursor* cursor, Direction direction)
|
||
|
{
|
||
|
if (hasUpper()) {
|
||
|
if (direction == beforeCursor) {
|
||
|
upper->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
upper->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SymbolElement::moveToLower(FormulaCursor* cursor, Direction direction)
|
||
|
{
|
||
|
if (hasLower()) {
|
||
|
if (direction == beforeCursor) {
|
||
|
lower->moveLeft(cursor, this);
|
||
|
}
|
||
|
else {
|
||
|
lower->moveRight(cursor, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
ElementIndexPtr SymbolElement::getIndex( int position )
|
||
|
{
|
||
|
switch ( position ) {
|
||
|
case lowerMiddlePos:
|
||
|
return getLowerIndex();
|
||
|
case upperMiddlePos:
|
||
|
return getUpperIndex();
|
||
|
}
|
||
|
return getUpperIndex();
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Appends our attributes to the dom element.
|
||
|
*/
|
||
14 years ago
|
void SymbolElement::writeDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
BasicElement::writeDom(element);
|
||
|
|
||
|
element.setAttribute("TYPE", symbolType);
|
||
|
|
||
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);
|
||
|
|
||
|
if(hasLower()) {
|
||
14 years ago
|
TQDomElement ind = doc.createElement("LOWER");
|
||
15 years ago
|
ind.appendChild(lower->getElementDom(doc));
|
||
|
element.appendChild(ind);
|
||
|
}
|
||
|
if(hasUpper()) {
|
||
14 years ago
|
TQDomElement ind = doc.createElement("UPPER");
|
||
15 years ago
|
ind.appendChild(upper->getElementDom(doc));
|
||
|
element.appendChild(ind);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads our attributes from the element.
|
||
|
* Returns false if it failed.
|
||
|
*/
|
||
14 years ago
|
bool SymbolElement::readAttributesFromDom(TQDomElement element)
|
||
15 years ago
|
{
|
||
|
if (!BasicElement::readAttributesFromDom(element)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString typeStr = element.attribute("TYPE");
|
||
15 years ago
|
if(!typeStr.isNull()) {
|
||
|
symbolType = static_cast<SymbolType>(typeStr.toInt());
|
||
|
}
|
||
|
|
||
|
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 SymbolElement::readContentFromDom(TQDomNode& node)
|
||
15 years ago
|
{
|
||
|
if (!BasicElement::readContentFromDom(node)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ( !buildChild( content, node, "CONTENT" ) ) {
|
||
|
kdWarning( DEBUGID ) << "Empty content in SymbolElement." << endl;
|
||
|
return false;
|
||
|
}
|
||
|
node = node.nextSibling();
|
||
|
|
||
|
bool lowerRead = false;
|
||
|
bool upperRead = false;
|
||
|
|
||
|
while (!node.isNull() && !(upperRead && lowerRead)) {
|
||
|
|
||
|
if (!lowerRead && (node.nodeName().upper() == "LOWER")) {
|
||
|
lowerRead = buildChild( lower=new SequenceElement( this ), node, "LOWER" );
|
||
|
if ( !lowerRead ) return false;
|
||
|
}
|
||
|
|
||
|
if (!upperRead && (node.nodeName().upper() == "UPPER")) {
|
||
|
upperRead = buildChild( upper=new SequenceElement( this ), node, "UPPER" );
|
||
|
if ( !upperRead ) return false;
|
||
|
}
|
||
|
|
||
|
node = node.nextSibling();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString SymbolElement::toLatex()
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString sym;
|
||
15 years ago
|
|
||
|
switch(symbolType) {
|
||
|
|
||
|
case 1001:
|
||
|
sym="\\int";
|
||
|
break;
|
||
|
case 1002:
|
||
|
sym="\\sum";
|
||
|
break;
|
||
|
case 1003:
|
||
|
sym="\\prod";
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
sym=" ";
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
if(hasLower()) {
|
||
|
sym+="_{";
|
||
|
sym+=lower->toLatex();
|
||
|
sym+="}";
|
||
|
}
|
||
|
|
||
|
if(hasUpper()) {
|
||
|
sym+="^{";
|
||
|
sym+=upper->toLatex();
|
||
|
sym+="} ";
|
||
|
}
|
||
|
|
||
|
sym += " ";
|
||
|
|
||
|
sym+=content->toLatex();
|
||
|
|
||
|
|
||
|
return sym;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString SymbolElement::formulaString()
|
||
15 years ago
|
{
|
||
14 years ago
|
TQString sym;
|
||
15 years ago
|
switch ( symbolType ) {
|
||
|
case 1001:
|
||
|
sym="int(";
|
||
|
break;
|
||
|
case 1002:
|
||
|
sym="sum(";
|
||
|
break;
|
||
|
case 1003:
|
||
|
sym="prod(";
|
||
|
break;
|
||
|
default:
|
||
|
sym="(";
|
||
|
}
|
||
|
sym += content->formulaString();
|
||
|
if ( hasLower() ) {
|
||
|
sym += ", " + lower->formulaString();
|
||
|
}
|
||
|
if ( hasUpper() ) {
|
||
|
sym += ", " + upper->formulaString();
|
||
|
}
|
||
|
return sym + ")";
|
||
|
}
|
||
|
|
||
13 years ago
|
void SymbolElement::writeMathML( TQDomDocument& doc, TQDomNode& parent, bool oasisFormat ) const
|
||
15 years ago
|
{
|
||
14 years ago
|
TQDomElement de = doc.createElement( oasisFormat ? "math:mrow" : "mrow" );
|
||
|
TQDomElement mo = doc.createElement( oasisFormat ? "math:mo" : "mo" );
|
||
15 years ago
|
|
||
14 years ago
|
TQString value;
|
||
15 years ago
|
|
||
|
switch( symbolType )
|
||
|
{
|
||
|
case EmptyBracket: break;
|
||
|
case LeftLineBracket: case RightLineBracket:
|
||
|
mo.appendChild( doc.createTextNode( "|" ) ); break;
|
||
|
case Integral:
|
||
|
mo.appendChild( doc.createEntityReference( "int" ) ); break;
|
||
|
case Sum:
|
||
|
mo.appendChild( doc.createEntityReference( "sum" ) ); break;
|
||
|
case Product:
|
||
|
mo.appendChild( doc.createEntityReference( "prod" ) ); break;
|
||
|
default:
|
||
14 years ago
|
mo.appendChild( doc.createTextNode( TQChar( symbolType ) ) );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQDomElement between;
|
||
15 years ago
|
if ( hasUpper() && hasLower() )
|
||
|
{
|
||
|
between = doc.createElement( oasisFormat ? "math:msubsup" : "msubsup" );
|
||
|
between.appendChild( mo );
|
||
|
lower->writeMathML( doc, between, oasisFormat );
|
||
|
upper->writeMathML( doc, between, oasisFormat );
|
||
|
}
|
||
|
else if ( hasUpper() )
|
||
|
{
|
||
|
between = doc.createElement( oasisFormat ? "math:msup" : "msup" );
|
||
|
between.appendChild( mo );
|
||
|
upper->writeMathML( doc, between, oasisFormat );
|
||
|
}
|
||
|
else if ( hasLower() )
|
||
|
{
|
||
|
between = doc.createElement( oasisFormat ? "math:msub" : "msub" );
|
||
|
between.appendChild( mo );
|
||
|
lower->writeMathML( doc, between, oasisFormat );
|
||
|
}
|
||
|
else
|
||
|
between = mo;
|
||
|
|
||
|
de.appendChild( between );
|
||
|
content->writeMathML( doc, de, oasisFormat );
|
||
|
|
||
13 years ago
|
parent.appendChild( de );
|
||
15 years ago
|
}
|
||
|
|
||
|
KFORMULA_NAMESPACE_END
|