@ -1,33 +1,33 @@
""" This file is part of the KDE project
""" This file is part of the KDE project
Copyright ( C ) 2001 Andrea Rizzi < rizzi @kde.org >
Copyright ( C ) 2001 Andrea Rizzi < rizzi @kde.org >
Ulrich Kuettler < ulrich . kuettler @mailbox.tu - dresden . de >
Ulrich Kuettler < ulrich . kuettler @mailbox.tu - dresden . de >
This library is free software ; you can redistribute it and / or
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Library General Public
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation ; either
License as published by the Free Software Foundation ; either
version 2 of the License , or ( at your option ) any later version .
version 2 of the License , or ( at your option ) any later version .
This library is distributed in the hope that it will be useful ,
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Library General Public License for more details .
Library General Public License for more details .
You should have received a copy of the GNU Library General Public License
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
along with this library ; see the file COPYING . LIB . If not , write to
the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
Boston , MA 02110 - 1301 , USA .
Boston , MA 02110 - 1301 , USA .
"""
"""
from qt import *
from PyTQt. qt import *
class BasicElement :
class BasicElement :
""" The interface for every element. """
""" The interface for every element. """
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
self . parent = parent
self . parent = parent
self . size = QSize( )
self . size = T QSize( )
self . pos = QPoint( )
self . pos = T QPoint( )
def x ( self ) : return self . pos . x ( )
def x ( self ) : return self . pos . x ( )
@ -48,7 +48,7 @@ class BasicElement:
x + = element . x ( )
x + = element . x ( )
y + = element . y ( )
y + = element . y ( )
element = element . parent
element = element . parent
return QPoint( x , y )
return T QPoint( x , y )
def elementAt ( self , point , startPoint ) :
def elementAt ( self , point , startPoint ) :
""" Returns the element that is at position point.
""" Returns the element that is at position point.
@ -58,13 +58,13 @@ class BasicElement:
y = point . y ( ) - startPoint . y ( )
y = point . y ( ) - startPoint . y ( )
if y > = 0 and y < self . height ( ) :
if y > = 0 and y < self . height ( ) :
return self
return self
def moveLeft ( self , cursor , fromElement ) :
def moveLeft ( self , cursor , fromElement ) :
""" Enters this element while moving to the left from
""" Enters this element while moving to the left from
the element ` fromElement ' . Searched for cursor position inside
the element ` fromElement ' . Searched for cursor position inside
this element of left of it . """
this element of left of it . """
pass
pass
def moveRight ( self , cursor , fromElement ) :
def moveRight ( self , cursor , fromElement ) :
""" Enters this element while moving to the right from
""" Enters this element while moving to the right from
the element ` fromElement ' . Searched for cursor position inside
the element ` fromElement ' . Searched for cursor position inside
@ -76,7 +76,7 @@ class BasicElement:
def moveDown ( self , cursor , fromElement ) :
def moveDown ( self , cursor , fromElement ) :
pass
pass
def formula ( self ) :
def formula ( self ) :
""" Returns the FormulaElement we are a child of. """
""" Returns the FormulaElement we are a child of. """
return self . parent . formula ( )
return self . parent . formula ( )
@ -118,7 +118,7 @@ class BasicElement:
class SequenceElement ( BasicElement ) :
class SequenceElement ( BasicElement ) :
""" The element that contains a number of children.
""" The element that contains a number of children.
The children are aligned in one line . """
The children are aligned in one line . """
def __init__ ( self , parent ) :
def __init__ ( self , parent ) :
BasicElement . __init__ ( self , parent )
BasicElement . __init__ ( self , parent )
self . children = [ ]
self . children = [ ]
@ -127,18 +127,18 @@ class SequenceElement (BasicElement):
""" Returns the element before the cursor. """
""" Returns the element before the cursor. """
if cursor . pos ( ) > 0 :
if cursor . pos ( ) > 0 :
return self . children [ cursor . pos ( ) - 1 ]
return self . children [ cursor . pos ( ) - 1 ]
def elementAt ( self , point , startPoint ) :
def elementAt ( self , point , startPoint ) :
r = BasicElement . elementAt ( self , point , startPoint )
r = BasicElement . elementAt ( self , point , startPoint )
if r != None :
if r != None :
for child in self . children :
for child in self . children :
r = child . elementAt ( point , QPoint( startPoint . x ( ) + child . x ( ) ,
r = child . elementAt ( point , T QPoint( startPoint . x ( ) + child . x ( ) ,
startPoint . y ( ) + child . y ( ) ) )
startPoint . y ( ) + child . y ( ) ) )
if r != None :
if r != None :
return r
return r
return self
return self
def moveLeft ( self , cursor , fromElement ) :
def moveLeft ( self , cursor , fromElement ) :
# Our parent asks us for a cursor position. Found.
# Our parent asks us for a cursor position. Found.
@ -166,7 +166,7 @@ class SequenceElement (BasicElement):
if not cursor . mouseMark ( ) :
if not cursor . mouseMark ( ) :
cursor . setMarkPos ( fromPos + 1 )
cursor . setMarkPos ( fromPos + 1 )
def moveRight ( self , cursor , fromElement ) :
def moveRight ( self , cursor , fromElement ) :
# Our parent asks us for a cursor position. Found.
# Our parent asks us for a cursor position. Found.
@ -193,7 +193,7 @@ class SequenceElement (BasicElement):
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
if not cursor . mouseMark ( ) :
if not cursor . mouseMark ( ) :
cursor . setMarkPos ( fromPos )
cursor . setMarkPos ( fromPos )
def moveUp ( self , cursor , fromElement ) :
def moveUp ( self , cursor , fromElement ) :
if fromElement == self . parent :
if fromElement == self . parent :
@ -209,7 +209,7 @@ class SequenceElement (BasicElement):
else :
else :
if self . parent != None :
if self . parent != None :
self . parent . moveDown ( cursor , self )
self . parent . moveDown ( cursor , self )
def moveHome ( self , cursor ) :
def moveHome ( self , cursor ) :
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
@ -229,20 +229,20 @@ class SequenceElement (BasicElement):
cursor . setMarkPos ( self . children . index ( element ) )
cursor . setMarkPos ( self . children . index ( element ) )
cursor . set ( self , len ( self . children ) )
cursor . set ( self , len ( self . children ) )
def draw ( self , painter , styleContext , startPoint ) :
def draw ( self , painter , styleContext , startPoint ) :
x , y = startPoint . x ( ) , startPoint . y ( )
x , y = startPoint . x ( ) , startPoint . y ( )
if len ( self . children ) > 0 :
if len ( self . children ) > 0 :
for child in self . children :
for child in self . children :
cX = child . x ( )
cX = child . x ( )
cY = child . y ( )
cY = child . y ( )
child . draw ( painter , styleContext , QPoint( x + cX , y + cY ) )
child . draw ( painter , styleContext , T QPoint( x + cX , y + cY ) )
# Debug
# Debug
#painter.setPen( Qt.green)
#painter.setPen( T Qt.green)
#painter.drawRect(x, y, self.width(), self.height())
#painter.drawRect(x, y, self.width(), self.height())
else :
else :
painter . setPen ( Qt. blue )
painter . setPen ( T Qt. blue )
painter . drawRect ( x , y , self . width ( ) , self . height ( ) )
painter . drawRect ( x , y , self . width ( ) , self . height ( ) )
def calcSizes ( self , styleContext ) :
def calcSizes ( self , styleContext ) :
@ -295,7 +295,7 @@ class SequenceElement (BasicElement):
# it is essential to set up the parent pointer for
# it is essential to set up the parent pointer for
# the notification to work.
# the notification to work.
element . parent = self
element . parent = self
seq = element . makeSequence ( )
seq = element . makeSequence ( )
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
f = min ( cursor . pos ( ) , cursor . markPos ( ) )
f = min ( cursor . pos ( ) , cursor . markPos ( ) )
@ -312,7 +312,7 @@ class SequenceElement (BasicElement):
self . replaceChild ( cursor , element )
self . replaceChild ( cursor , element )
else :
else :
self . insertChild ( cursor , element )
self . insertChild ( cursor , element )
element . setMainChild ( seq )
element . setMainChild ( seq )
@ -321,7 +321,7 @@ class SequenceElement (BasicElement):
( The main child is always a SequenceElement . ) """
( The main child is always a SequenceElement . ) """
assert element . parent == self
assert element . parent == self
self . formula ( ) . elementRemoved ( element )
self . formula ( ) . elementRemoved ( element )
seq = element . mainChild ( )
seq = element . mainChild ( )
pos = self . children . index ( element )
pos = self . children . index ( element )
self . children . remove ( element )
self . children . remove ( element )
@ -346,7 +346,7 @@ class SequenceElement (BasicElement):
element . parent = self
element . parent = self
cursor . set ( self , pos + 1 )
cursor . set ( self , pos + 1 )
self . formula ( ) . changed ( )
self . formula ( ) . changed ( )
def replaceChild ( self , cursor , element ) :
def replaceChild ( self , cursor , element ) :
""" Replaces the element before the cursor with the new one.
""" Replaces the element before the cursor with the new one.
No range checking . Be careful . """
No range checking . Be careful . """
@ -363,7 +363,7 @@ class SequenceElement (BasicElement):
self . parent . removeChild ( cursor , self )
self . parent . removeChild ( cursor , self )
return
return
self . formula ( ) . changed ( )
self . formula ( ) . changed ( )
def removeChildAt ( self , cursor ) :
def removeChildAt ( self , cursor ) :
pos = cursor . pos ( )
pos = cursor . pos ( )
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
@ -382,7 +382,7 @@ class SequenceElement (BasicElement):
if len ( self . children ) == 0 :
if len ( self . children ) == 0 :
if self . parent != None :
if self . parent != None :
self . parent . removeChild ( cursor , self )
self . parent . removeChild ( cursor , self )
def removeChildBefore ( self , cursor ) :
def removeChildBefore ( self , cursor ) :
pos = cursor . pos ( ) - 1
pos = cursor . pos ( ) - 1
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
@ -402,8 +402,8 @@ class SequenceElement (BasicElement):
if len ( self . children ) == 0 :
if len ( self . children ) == 0 :
if self . parent != None :
if self . parent != None :
self . parent . removeChild ( cursor , self )
self . parent . removeChild ( cursor , self )
def globalCursorPos ( self , pos ) :
def globalCursorPos ( self , pos ) :
""" Returns the position after the child at the position
""" Returns the position after the child at the position
in global Coords . """
in global Coords . """
@ -415,13 +415,13 @@ class SequenceElement (BasicElement):
d = self . width ( )
d = self . width ( )
else :
else :
d = 2
d = 2
point . setX ( point . x ( ) + d )
point . setX ( point . x ( ) + d )
return point
return point
def countChildren ( self ) :
def countChildren ( self ) :
return len ( self . children )
return len ( self . children )
class FormulaElement ( SequenceElement ) :
class FormulaElement ( SequenceElement ) :
""" The main element.
""" The main element.
@ -447,18 +447,18 @@ class FormulaElement (SequenceElement):
Caution ! The object tree must still contain the element by the time
Caution ! The object tree must still contain the element by the time
you call this methode . """
you call this methode . """
self . document . elementRemoved ( element )
self . document . elementRemoved ( element )
class TextElement ( BasicElement ) :
class TextElement ( BasicElement ) :
""" One char. """
""" One char. """
def __init__ ( self , parent , char ) :
def __init__ ( self , parent , char ) :
BasicElement . __init__ ( self , parent )
BasicElement . __init__ ( self , parent )
self . char = char
self . char = char
def moveLeft ( self , cursor , fromElement ) :
def moveLeft ( self , cursor , fromElement ) :
self . parent . moveLeft ( cursor , self )
self . parent . moveLeft ( cursor , self )
def moveRight ( self , cursor , fromElement ) :
def moveRight ( self , cursor , fromElement ) :
self . parent . moveRight ( cursor , self )
self . parent . moveRight ( cursor , self )
@ -484,7 +484,7 @@ class IndexElement (BasicElement):
contentElement . parent = self
contentElement . parent = self
else :
else :
BasicElement . __init__ ( self , None )
BasicElement . __init__ ( self , None )
self . content = contentElement
self . content = contentElement
self . upperLeft = self . upperRight = None
self . upperLeft = self . upperRight = None
self . lowerLeft = self . lowerRight = None
self . lowerLeft = self . lowerRight = None
@ -494,39 +494,39 @@ class IndexElement (BasicElement):
r = BasicElement . elementAt ( self , point , startPoint )
r = BasicElement . elementAt ( self , point , startPoint )
if r != None :
if r != None :
x , y = startPoint . x ( ) , startPoint . y ( )
x , y = startPoint . x ( ) , startPoint . y ( )
r = self . content . elementAt ( point , QPoint( x + self . content . x ( ) ,
r = self . content . elementAt ( point , T QPoint( x + self . content . x ( ) ,
y + self . content . y ( ) ) )
y + self . content . y ( ) ) )
if r != None : return r
if r != None : return r
if self . upperRight != None :
if self . upperRight != None :
r = self . upperRight . elementAt ( point , QPoint( x + self . upperRight . x ( ) ,
r = self . upperRight . elementAt ( point , T QPoint( x + self . upperRight . x ( ) ,
y + self . upperRight . y ( ) ) )
y + self . upperRight . y ( ) ) )
if r != None : return r
if r != None : return r
if self . upperLeft != None :
if self . upperLeft != None :
r = self . upperLeft . elementAt ( point , QPoint( x + self . upperLeft . x ( ) ,
r = self . upperLeft . elementAt ( point , T QPoint( x + self . upperLeft . x ( ) ,
y + self . upperLeft . y ( ) ) )
y + self . upperLeft . y ( ) ) )
if r != None : return r
if r != None : return r
if self . lowerRight != None :
if self . lowerRight != None :
r = self . lowerRight . elementAt ( point , QPoint( x + self . lowerRight . x ( ) ,
r = self . lowerRight . elementAt ( point , T QPoint( x + self . lowerRight . x ( ) ,
y + self . lowerRight . y ( ) ) )
y + self . lowerRight . y ( ) ) )
if r != None : return r
if r != None : return r
if self . lowerLeft != None :
if self . lowerLeft != None :
r = self . lowerLeft . elementAt ( point , QPoint( x + self . lowerLeft . x ( ) ,
r = self . lowerLeft . elementAt ( point , T QPoint( x + self . lowerLeft . x ( ) ,
y + self . lowerLeft . y ( ) ) )
y + self . lowerLeft . y ( ) ) )
if r != None : return r
if r != None : return r
return self
return self
def moveLeft ( self , cursor , fromElement ) :
def moveLeft ( self , cursor , fromElement ) :
assert fromElement != None
assert fromElement != None
if cursor . isSelection ( ) :
if cursor . isSelection ( ) :
self . parent . moveLeft ( cursor , self )
self . parent . moveLeft ( cursor , self )
elif fromElement == self . parent :
elif fromElement == self . parent :
if self . lowerRight != None :
if self . lowerRight != None :
self . lowerRight . moveLeft ( cursor , self )
self . lowerRight . moveLeft ( cursor , self )
@ -557,11 +557,11 @@ class IndexElement (BasicElement):
self . upperLeft . moveLeft ( cursor , self )
self . upperLeft . moveLeft ( cursor , self )
else :
else :
self . parent . moveLeft ( cursor , self )
self . parent . moveLeft ( cursor , self )
else :
else :
self . parent . moveLeft ( cursor , self )
self . parent . moveLeft ( cursor , self )
def moveRight ( self , cursor , fromElement ) :
def moveRight ( self , cursor , fromElement ) :
assert fromElement != None
assert fromElement != None
@ -598,7 +598,7 @@ class IndexElement (BasicElement):
self . lowerRight . moveRight ( cursor , self )
self . lowerRight . moveRight ( cursor , self )
else :
else :
self . parent . moveRight ( cursor , self )
self . parent . moveRight ( cursor , self )
else :
else :
self . parent . moveRight ( cursor , self )
self . parent . moveRight ( cursor , self )
@ -629,7 +629,7 @@ class IndexElement (BasicElement):
else : # should never happen.
else : # should never happen.
self . parent . moveUp ( cursor , self )
self . parent . moveUp ( cursor , self )
def moveDown ( self , cursor , fromElement ) :
def moveDown ( self , cursor , fromElement ) :
assert fromElement != None
assert fromElement != None
@ -654,35 +654,35 @@ class IndexElement (BasicElement):
elif fromElement == self . upperRight :
elif fromElement == self . upperRight :
self . content . moveLeft ( cursor , self )
self . content . moveLeft ( cursor , self )
else : # should never happen.
else : # should never happen.
self . parent . moveDown ( cursor , self )
self . parent . moveDown ( cursor , self )
def draw ( self , painter , styleContext , startPoint ) :
def draw ( self , painter , styleContext , startPoint ) :
x , y = startPoint . x ( ) , startPoint . y ( )
x , y = startPoint . x ( ) , startPoint . y ( )
self . content . draw ( painter , styleContext ,
self . content . draw ( painter , styleContext ,
QPoint( x + self . content . x ( ) ,
T QPoint( x + self . content . x ( ) ,
y + self . content . y ( ) ) )
y + self . content . y ( ) ) )
if self . upperLeft != None :
if self . upperLeft != None :
self . upperLeft . draw ( painter , styleContext ,
self . upperLeft . draw ( painter , styleContext ,
QPoint( x + self . upperLeft . x ( ) ,
T QPoint( x + self . upperLeft . x ( ) ,
y + self . upperLeft . y ( ) ) )
y + self . upperLeft . y ( ) ) )
if self . upperRight != None :
if self . upperRight != None :
self . upperRight . draw ( painter , styleContext ,
self . upperRight . draw ( painter , styleContext ,
QPoint( x + self . upperRight . x ( ) ,
T QPoint( x + self . upperRight . x ( ) ,
y + self . upperRight . y ( ) ) )
y + self . upperRight . y ( ) ) )
if self . lowerLeft != None :
if self . lowerLeft != None :
self . lowerLeft . draw ( painter , styleContext ,
self . lowerLeft . draw ( painter , styleContext ,
QPoint( x + self . lowerLeft . x ( ) ,
T QPoint( x + self . lowerLeft . x ( ) ,
y + self . lowerLeft . y ( ) ) )
y + self . lowerLeft . y ( ) ) )
if self . lowerRight != None :
if self . lowerRight != None :
self . lowerRight . draw ( painter , styleContext ,
self . lowerRight . draw ( painter , styleContext ,
QPoint( x + self . lowerRight . x ( ) ,
T QPoint( x + self . lowerRight . x ( ) ,
y + self . lowerRight . y ( ) ) )
y + self . lowerRight . y ( ) ) )
# Debug
# Debug
painter . setPen ( Qt. red )
painter . setPen ( T Qt. red )
painter . drawRect ( x , y , self . width ( ) , self . height ( ) )
painter . drawRect ( x , y , self . width ( ) , self . height ( ) )
@ -748,7 +748,7 @@ class IndexElement (BasicElement):
self . lowerRight . setX ( width )
self . lowerRight . setX ( width )
width + = max ( urWidth , lrWidth )
width + = max ( urWidth , lrWidth )
# calculate the y offsets
# calculate the y offsets
if ulHeight > urHeight :
if ulHeight > urHeight :
self . upperLeft . setY ( 0 )
self . upperLeft . setY ( 0 )
@ -812,7 +812,7 @@ class IndexElement (BasicElement):
self . parent . replaceElementByMainChild ( cursor , self )
self . parent . replaceElementByMainChild ( cursor , self )
else :
else :
self . formula ( ) . changed ( )
self . formula ( ) . changed ( )
def requireUpperLeft ( self ) :
def requireUpperLeft ( self ) :
if self . upperLeft == None :
if self . upperLeft == None :
@ -837,15 +837,15 @@ class IndexElement (BasicElement):
self . lowerRight = SequenceElement ( self )
self . lowerRight = SequenceElement ( self )
self . formula ( ) . changed ( )
self . formula ( ) . changed ( )
return self . lowerRight
return self . lowerRight
class Cursor :
class Cursor :
""" The selection. This might be a one position selection or
""" The selection. This might be a one position selection or
an area . Handles user input and object creation .
an area . Handles user input and object creation .
Note that it is up to the elements to actually move the cursor .
Note that it is up to the elements to actually move the cursor .
( The cursor has no chance to know how . ) """
( The cursor has no chance to know how . ) """
def __init__ ( self , formulaElement ) :
def __init__ ( self , formulaElement ) :
self . sequenceElement = formulaElement
self . sequenceElement = formulaElement
self . currentPos = 0
self . currentPos = 0
@ -868,25 +868,25 @@ class Cursor:
self . selectionFlag = 0
self . selectionFlag = 0
else :
else :
self . currentMarkPos = - 1
self . currentMarkPos = - 1
self . sequenceElement = sequenceElement
self . sequenceElement = sequenceElement
self . currentPos = pos
self . currentPos = pos
def markPos ( self ) :
def markPos ( self ) :
return self . currentMarkPos
return self . currentMarkPos
def setMarkPos ( self , markPos ) :
def setMarkPos ( self , markPos ) :
""" Gets called by elements if the cursor moves up to the parent. """
""" Gets called by elements if the cursor moves up to the parent. """
self . selectionFlag = ( markPos != - 1 )
self . selectionFlag = ( markPos != - 1 )
self . currentMarkPos = markPos
self . currentMarkPos = markPos
def pos ( self ) :
def pos ( self ) :
return self . currentPos
return self . currentPos
def element ( self ) :
def element ( self ) :
return self . sequenceElement
return self . sequenceElement
def draw ( self , painter ) :
def draw ( self , painter ) :
point = self . sequenceElement . globalCursorPos ( self . pos ( ) )
point = self . sequenceElement . globalCursorPos ( self . pos ( ) )
height = self . sequenceElement . height ( )
height = self . sequenceElement . height ( )
@ -896,18 +896,18 @@ class Cursor:
x = min ( point . x ( ) , markPoint . x ( ) )
x = min ( point . x ( ) , markPoint . x ( ) )
width = abs ( point . x ( ) - markPoint . x ( ) )
width = abs ( point . x ( ) - markPoint . x ( ) )
painter . setRasterOp ( Qt. XorROP )
painter . setRasterOp ( T Qt. XorROP )
#painter.setRasterOp( Qt.OrROP)
#painter.setRasterOp( T Qt.OrROP)
painter . fillRect ( x , point . y ( ) , width , height , QBrush( Qt. white ) )
painter . fillRect ( x , point . y ( ) , width , height , T QBrush( T Qt. white ) )
#painter.drawLine(point.x(), point.y()-2,
#painter.drawLine(point.x(), point.y()-2,
# point.x(), point.y()+height+2)
# point.x(), point.y()+height+2)
painter . setRasterOp ( Qt. CopyROP )
painter . setRasterOp ( T Qt. CopyROP )
else :
else :
painter . setPen ( Qt. blue )
painter . setPen ( T Qt. blue )
painter . drawLine ( point . x ( ) , point . y ( ) - 2 ,
painter . drawLine ( point . x ( ) , point . y ( ) - 2 ,
point . x ( ) , point . y ( ) + height + 2 )
point . x ( ) , point . y ( ) + height + 2 )
def findIndexElement ( self ) :
def findIndexElement ( self ) :
""" Looks if we are just behind an IndexElement or at the last
""" Looks if we are just behind an IndexElement or at the last
@ -921,7 +921,7 @@ class Cursor:
if self . sequenceElement == parent . mainChild ( ) :
if self . sequenceElement == parent . mainChild ( ) :
return parent
return parent
def addUpperRightIndex ( self ) :
def addUpperRightIndex ( self ) :
indexElement = self . findIndexElement ( )
indexElement = self . findIndexElement ( )
if indexElement == None :
if indexElement == None :
@ -931,7 +931,7 @@ class Cursor:
index . moveRight ( self , index . parent )
index . moveRight ( self , index . parent )
def addLowerRightIndex ( self ) :
def addLowerRightIndex ( self ) :
indexElement = self . findIndexElement ( )
indexElement = self . findIndexElement ( )
if indexElement == None :
if indexElement == None :
@ -940,20 +940,20 @@ class Cursor:
index = indexElement . requireLowerRight ( )
index = indexElement . requireLowerRight ( )
index . moveRight ( self , index . parent )
index . moveRight ( self , index . parent )
def addTextElement ( self , char ) :
def addTextElement ( self , char ) :
textElement = TextElement ( self . sequenceElement , QString( char ) )
textElement = TextElement ( self . sequenceElement , T QString( char ) )
self . sequenceElement . insertChild ( self , textElement )
self . sequenceElement . insertChild ( self , textElement )
def handleKey ( self , keyEvent ) :
def handleKey ( self , keyEvent ) :
action = keyEvent . key ( )
action = keyEvent . key ( )
state = keyEvent . state ( )
state = keyEvent . state ( )
char = keyEvent . text ( ) . at ( 0 )
char = keyEvent . text ( ) . at ( 0 )
self . mouseMarkFlag = 0
self . mouseMarkFlag = 0
if char . isPrint ( ) :
if char . isPrint ( ) :
#self.sequenceElement.handleKey(self, char)
#self.sequenceElement.handleKey(self, char)
latin1 = char . latin1 ( )
latin1 = char . latin1 ( )
@ -981,42 +981,42 @@ class Cursor:
pass
pass
else :
else :
self . addTextElement ( char )
self . addTextElement ( char )
else :
else :
if Qt. Key_BackSpace == action :
if T Qt. Key_BackSpace == action :
self . sequenceElement . removeChildBefore ( self )
self . sequenceElement . removeChildBefore ( self )
return
return
elif Qt. Key_Delete == action :
elif T Qt. Key_Delete == action :
self . sequenceElement . removeChildAt ( self )
self . sequenceElement . removeChildAt ( self )
return
return
self . selectionFlag = state & Qt. ShiftButton
self . selectionFlag = state & T Qt. ShiftButton
if Qt. Key_Left == action :
if T Qt. Key_Left == action :
if state & Qt. ControlButton :
if state & T Qt. ControlButton :
self . sequenceElement . moveHome ( self )
self . sequenceElement . moveHome ( self )
else :
else :
self . sequenceElement . moveLeft ( self , self . sequenceElement )
self . sequenceElement . moveLeft ( self , self . sequenceElement )
elif Qt. Key_Right == action :
elif T Qt. Key_Right == action :
if state & Qt. ControlButton :
if state & T Qt. ControlButton :
self . sequenceElement . moveEnd ( self )
self . sequenceElement . moveEnd ( self )
else :
else :
self . sequenceElement . moveRight ( self , self . sequenceElement )
self . sequenceElement . moveRight ( self , self . sequenceElement )
elif Qt. Key_Up == action :
elif T Qt. Key_Up == action :
self . sequenceElement . moveUp ( self , self . sequenceElement )
self . sequenceElement . moveUp ( self , self . sequenceElement )
elif Qt. Key_Down == action :
elif T Qt. Key_Down == action :
self . sequenceElement . moveDown ( self , self . sequenceElement )
self . sequenceElement . moveDown ( self , self . sequenceElement )
elif Qt. Key_Home == action :
elif T Qt. Key_Home == action :
self . sequenceElement . formula ( ) . moveHome ( self )
self . sequenceElement . formula ( ) . moveHome ( self )
elif Qt. Key_End == action :
elif T Qt. Key_End == action :
self . sequenceElement . formula ( ) . moveEnd ( self )
self . sequenceElement . formula ( ) . moveEnd ( self )
# Qt.Key_PageUp, Qt.Key_PageDown,
# T Qt.Key_PageUp, T Qt.Key_PageDown,
def handleMousePress ( self , mouseEvent ) :
def handleMousePress ( self , mouseEvent ) :
formula = self . sequenceElement . formula ( )
formula = self . sequenceElement . formula ( )
element = formula . elementAt ( mouseEvent . pos ( ) , QPoint( 0 , 0 ) )
element = formula . elementAt ( mouseEvent . pos ( ) , T QPoint( 0 , 0 ) )
if element != None :
if element != None :
if element . parent != None :
if element . parent != None :
element . moveLeft ( self , element . parent )
element . moveLeft ( self , element . parent )
@ -1028,15 +1028,15 @@ class Cursor:
def handleMouseRelease ( self , mouseEvent ) :
def handleMouseRelease ( self , mouseEvent ) :
self . mouseMarkFlag = 0
self . mouseMarkFlag = 0
def handleMouseMove ( self , mouseEvent ) :
def handleMouseMove ( self , mouseEvent ) :
self . selectionFlag = 1
self . selectionFlag = 1
formula = self . sequenceElement . formula ( )
formula = self . sequenceElement . formula ( )
element = formula . elementAt ( mouseEvent . pos ( ) , QPoint( 0 , 0 ) )
element = formula . elementAt ( mouseEvent . pos ( ) , T QPoint( 0 , 0 ) )
if element != None :
if element != None :
if element . parent != None :
if element . parent != None :
element . parent . moveLeft ( self , element )
element . parent . moveLeft ( self , element )
def elementRemoved ( self , element ) :
def elementRemoved ( self , element ) :
""" The cursor must not be inside a leaf which gets cut off.
""" The cursor must not be inside a leaf which gets cut off.
@ -1050,28 +1050,28 @@ class Cursor:
self . sequenceElement . moveHome ( self )
self . sequenceElement . moveHome ( self )
return
return
e = e . parent
e = e . parent
class StyleContext :
class StyleContext :
""" Contains all variable information that are needed to
""" Contains all variable information that are needed to
draw a formula . """
draw a formula . """
def __init__ ( self ) :
def __init__ ( self ) :
self . font = QFont( " helvetica " , 18 )
self . font = T QFont( " helvetica " , 18 )
def setupPainter ( self , painter ) :
def setupPainter ( self , painter ) :
painter . setFont ( self . font )
painter . setFont ( self . font )
painter . setPen ( Qt. black )
painter . setPen ( T Qt. black )
def fontMetrics ( self ) :
def fontMetrics ( self ) :
return QFontMetrics( self . font )
return T QFontMetrics( self . font )
class Widget ( QWidget) :
class Widget ( T QWidget) :
""" The widget that contains a formula. """
""" The widget that contains a formula. """
def __init__ ( self ) :
def __init__ ( self ) :
QWidget. __init__ ( self )
T QWidget. __init__ ( self )
f = self . formula = FormulaElement ( self )
f = self . formula = FormulaElement ( self )
self . cursor = Cursor ( self . formula )
self . cursor = Cursor ( self . formula )
self . styleContext = StyleContext ( )
self . styleContext = StyleContext ( )
@ -1105,13 +1105,13 @@ class Widget(QWidget):
s3 . addChild ( TextElement ( s3 , " f " ) )
s3 . addChild ( TextElement ( s3 , " f " ) )
s3 . addChild ( TextElement ( s3 , " u " ) )
s3 . addChild ( TextElement ( s3 , " u " ) )
s3 . addChild ( TextElement ( s3 , " n " ) )
s3 . addChild ( TextElement ( s3 , " n " ) )
i2 = IndexElement ( s3 )
i2 = IndexElement ( s3 )
i2 . requireUpperLeft ( )
i2 . requireUpperLeft ( )
i2 . requireUpperRight ( )
i2 . requireUpperRight ( )
i2 . requireLowerLeft ( )
i2 . requireLowerLeft ( )
i2 . requireLowerRight ( )
i2 . requireLowerRight ( )
f . addChild ( i2 )
f . addChild ( i2 )
f . addChild ( TextElement ( f , " : " ) )
f . addChild ( TextElement ( f , " : " ) )
@ -1132,28 +1132,28 @@ class Widget(QWidget):
self . changedFlag = 1
self . changedFlag = 1
def changed ( self ) :
def changed ( self ) :
""" Gets called each time the formula changes. """
""" Gets called each time the formula changes. """
self . changedFlag = 1
self . changedFlag = 1
def elementRemoved ( self , element ) :
def elementRemoved ( self , element ) :
""" The element is going to go real soon. """
""" The element is going to go real soon. """
self . cursor . elementRemoved ( element )
self . cursor . elementRemoved ( element )
def paintEvent ( self , e ) :
def paintEvent ( self , e ) :
if self . changedFlag :
if self . changedFlag :
# You need to use the same StyleContext you use for drawing.
# You need to use the same StyleContext you use for drawing.
self . formula . calcSizes ( self . styleContext )
self . formula . calcSizes ( self . styleContext )
self . changedFlag = 0
self . changedFlag = 0
painter = QPainter( )
painter = T QPainter( )
painter . begin ( self )
painter . begin ( self )
try :
try :
self . formula . draw ( painter , self . styleContext , QPoint( 0 , 0 ) )
self . formula . draw ( painter , self . styleContext , T QPoint( 0 , 0 ) )
self . cursor . draw ( painter )
self . cursor . draw ( painter )
finally :
finally :
painter . end ( )
painter . end ( )
@ -1162,19 +1162,18 @@ class Widget(QWidget):
def keyPressEvent ( self , e ) :
def keyPressEvent ( self , e ) :
self . cursor . handleKey ( e )
self . cursor . handleKey ( e )
self . update ( )
self . update ( )
def mousePressEvent ( self , e ) :
def mousePressEvent ( self , e ) :
self . cursor . handleMousePress ( e )
self . cursor . handleMousePress ( e )
self . update ( )
self . update ( )
def mouseReleaseEvent ( self , e ) :
def mouseReleaseEvent ( self , e ) :
self . cursor . handleMouseRelease ( e )
self . cursor . handleMouseRelease ( e )
self . update ( )
self . update ( )
def mouseDoubleClickEvent ( self , e ) :
def mouseDoubleClickEvent ( self , e ) :
pass
pass
def mouseMoveEvent ( self , e ) :
def mouseMoveEvent ( self , e ) :
self . cursor . handleMouseMove ( e )
self . cursor . handleMouseMove ( e )
self . update ( )
self . update ( )