127
169
* Calculates our width and height and
128
170
* our children's parentPosition.
130
void SequenceElement::calcSizes(const ContextStyle& context, ContextStyle::TextStyle tstyle, ContextStyle::IndexStyle istyle)
172
void SequenceElement::calcSizes(const ContextStyle& style,
173
ContextStyle::TextStyle tstyle,
174
ContextStyle::IndexStyle istyle)
132
176
if (!isEmpty()) {
133
double mySize = context.getAdjustedSize( tstyle );
135
double toBaseline = 0;
136
double fromBaseline = 0;
138
QFont font = context.getDefaultFont();
139
font.setPointSizeFloat(mySize);
140
QFontMetrics fm(font);
141
double fromMidline = fm.strikeOutPos();
143
//uint count = children.count();
178
luPixel toBaseline = 0;
179
luPixel fromBaseline = 0;
145
181
// Let's do all normal elements that have a base line.
146
QListIterator<BasicElement> it( children );
182
QPtrListIterator<BasicElement> it( children );
147
183
for ( ; it.current(); ++it ) {
148
184
BasicElement* child = it.current();
150
double spaceBefore = 0;
186
luPixel spaceBefore = 0;
151
187
if ( isFirstOfToken( child ) ) {
152
spaceBefore = child->getElementType()->getSpaceBefore( context, tstyle );
189
style.ptToPixelX( child->getElementType()->getSpaceBefore( style,
155
193
if ( !child->isInvisible() ) {
156
child->calcSizes( context, tstyle, istyle );
194
child->calcSizes( style, tstyle, istyle );
157
195
child->setX( width + spaceBefore );
158
196
width += child->getWidth() + spaceBefore;
160
if ( child->getBaseline() > -1 ) {
161
toBaseline = QMAX( toBaseline, child->getBaseline() );
162
fromBaseline = QMAX( fromBaseline, child->getHeight()-child->getBaseline() );
198
luPixel childBaseline = child->getBaseline();
199
if ( childBaseline > -1 ) {
200
toBaseline = QMAX( toBaseline, childBaseline );
201
fromBaseline = QMAX( fromBaseline,
202
child->getHeight() - childBaseline );
205
luPixel bl = child->getHeight()/2 + style.axisHeight( tstyle );
206
toBaseline = QMAX( toBaseline, bl );
207
fromBaseline = QMAX( fromBaseline, child->getHeight() - bl );
220
245
* The `parentOrigin' is the point this element's parent starts.
221
246
* We can use our parentPosition to get our own origin then.
223
void SequenceElement::draw(QPainter& painter, const QRect& r,
224
const ContextStyle& context,
225
ContextStyle::TextStyle tstyle,
226
ContextStyle::IndexStyle istyle,
227
const KoPoint& parentOrigin)
248
void SequenceElement::draw( QPainter& painter, const LuPixelRect& r,
249
const ContextStyle& context,
250
ContextStyle::TextStyle tstyle,
251
ContextStyle::IndexStyle istyle,
252
const LuPixelPoint& parentOrigin )
229
KoPoint myPos(parentOrigin.x() + getX(),
230
parentOrigin.y() + getY());
231
if (!QRect(myPos.x(), myPos.y(), getWidth(), getHeight()).intersects(r))
254
LuPixelPoint myPos( parentOrigin.x() + getX(), parentOrigin.y() + getY() );
255
// There might be zero sized elements that still want to be drawn at least
256
// in edit mode. (EmptyElement)
257
//if ( !LuPixelRect( myPos.x(), myPos.y(), getWidth(), getHeight() ).intersects( r ) )
234
260
if (!isEmpty()) {
235
QListIterator<BasicElement> it( children );
236
for ( ; it.current(); ++it ) {
261
QPtrListIterator<BasicElement> it( children );
262
for ( ; it.current(); ) {
237
263
BasicElement* child = it.current();
238
if (!child->isInvisible()) {
264
if (!child->isInvisible()) {
239
265
child->draw(painter, r, context, tstyle, istyle, myPos);
242
//painter.setPen(Qt::green);
243
//painter.drawRect(parentOrigin.x() + getX(), parentOrigin.y() + getY(),
244
// getWidth(), getHeight());
267
// Each starting element draws the whole token
268
// This only concerns TextElements.
269
ElementType* token = child->getElementType();
271
it += token->end() - token->start();
247
else if ( painter.device()->devType() != QInternal::Printer ) {
283
drawEmptyRect( painter, context, myPos );
286
//painter.setPen(Qt::green);
287
//painter.drawRect(parentOrigin.x() + getX(), parentOrigin.y() + getY(),
288
// getWidth(), getHeight());
289
// painter.drawLine( context.layoutUnitToPixelX( parentOrigin.x() + getX() ),
290
// context.layoutUnitToPixelY( parentOrigin.y() + getY() + axis( context, tstyle ) ),
291
// context.layoutUnitToPixelX( parentOrigin.x() + getX() + getWidth() ),
292
// context.layoutUnitToPixelY( parentOrigin.y() + getY() + axis( context, tstyle ) ) );
293
// painter.setPen(Qt::red);
294
// painter.drawLine( context.layoutUnitToPixelX( parentOrigin.x() + getX() ),
295
// context.layoutUnitToPixelY( parentOrigin.y() + getY() + getBaseline() ),
296
// context.layoutUnitToPixelX( parentOrigin.x() + getX() + getWidth() ),
297
// context.layoutUnitToPixelY( parentOrigin.y() + getY() + getBaseline() ) );
301
void SequenceElement::dispatchFontCommand( FontCommand* cmd )
303
QPtrListIterator<BasicElement> it( children );
304
for ( ; it.current(); ++it ) {
305
BasicElement* child = it.current();
306
child->dispatchFontCommand( cmd );
311
void SequenceElement::drawEmptyRect( QPainter& painter, const ContextStyle& context,
312
const LuPixelPoint& upperLeft )
314
if ( context.edit() ) {
248
315
painter.setBrush(Qt::NoBrush);
249
painter.setPen(QPen(context.getEmptyColor(), context.getLineWidth()));
250
painter.drawRect(myPos.x(), myPos.y(), getWidth(), getHeight());
251
// kdDebug( 40000 ) << "SequenceElement::calcCursorSize: "
252
// << myPos.x() << " " << myPos.y() << " "
253
// << getWidth() << " " << getHeight()
316
painter.setPen( QPen( context.getEmptyColor(),
317
context.layoutUnitToPixelX( context.getLineWidth() ) ) );
318
painter.drawRect( context.layoutUnitToPixelX( upperLeft.x() ),
319
context.layoutUnitToPixelY( upperLeft.y() ),
320
context.layoutUnitToPixelX( getWidth() ),
321
context.layoutUnitToPixelY( getHeight() ) );
259
void SequenceElement::calcCursorSize(FormulaCursor* cursor, bool smallCursor)
325
void SequenceElement::calcCursorSize( const ContextStyle& context,
326
FormulaCursor* cursor, bool smallCursor )
261
KoPoint point = widgetPos();
328
LuPixelPoint point = widgetPos();
262
329
uint pos = cursor->getPos();
264
double posX = getChildPosition(pos);
265
double height = getHeight();
331
luPixel posX = getChildPosition( context, pos );
332
luPixel height = getHeight();
334
luPixel unitX = context.ptToLayoutUnitPixX( 1 );
335
luPixel unitY = context.ptToLayoutUnitPixY( 1 );
267
337
// Here are those evil constants that describe the cursor size.
269
if (cursor->isSelection()) {
339
if ( cursor->isSelection() ) {
270
340
uint mark = cursor->getMark();
271
double markX = getChildPosition(mark);
272
double x = QMIN(posX, markX);
273
double width = fabs(posX - markX);
341
luPixel markX = getChildPosition( context, mark );
342
luPixel x = QMIN(posX, markX);
343
luPixel width = abs(posX - markX);
276
cursor->cursorSize.setRect(point.x()+x, point.y(), width, height);
346
cursor->cursorSize.setRect( point.x()+x, point.y(), width, height );
279
cursor->cursorSize.setRect(point.x()+x, point.y()-2, width+1, height+4);
349
cursor->cursorSize.setRect( point.x()+x, point.y() - 2*unitY,
350
width + unitX, height + 4*unitY );
284
cursor->cursorSize.setRect(point.x()+posX, point.y(), 1, height);
355
cursor->cursorSize.setRect( point.x()+posX, point.y(),
287
cursor->cursorSize.setRect(point.x(), point.y()-2, getWidth()+2, height+6);
359
cursor->cursorSize.setRect( point.x(), point.y() - 2*unitY,
360
getWidth() + unitX, height + 4*unitY );
291
cursor->cursorPoint.setX(point.x()+posX);
292
cursor->cursorPoint.setY(point.y()+getHeight()/2);
364
cursor->cursorPoint.setX( point.x()+posX );
365
cursor->cursorPoint.setY( point.y()+getHeight()/2 );
297
370
* If the cursor is inside a sequence it needs to be drawn.
299
void SequenceElement::drawCursor(FormulaCursor* cursor, QPainter& painter, bool smallCursor)
372
void SequenceElement::drawCursor( QPainter& painter, const ContextStyle& context,
373
FormulaCursor* cursor, bool smallCursor )
301
painter.setRasterOp(Qt::XorROP);
302
if (cursor->isSelection()) {
303
const QRect& r = cursor->cursorSize;
304
painter.fillRect(r.x(), r.y(), r.width(), r.height(), Qt::white);
375
painter.setRasterOp( Qt::XorROP );
376
if ( cursor->isSelection() ) {
377
const LuPixelRect& r = cursor->cursorSize;
378
painter.fillRect( context.layoutUnitToPixelX( r.x() ),
379
context.layoutUnitToPixelY( r.y() ),
380
context.layoutUnitToPixelX( r.width() ),
381
context.layoutUnitToPixelY( r.height() ),
307
painter.setPen(Qt::white);
308
const QPoint& point = cursor->cursorPoint;
309
const QRect& size = cursor->cursorSize;
311
painter.drawLine(point.x(), size.top(),
312
point.x(), size.bottom());
385
painter.setPen( QPen( Qt::white,
386
context.layoutUnitToPixelX( context.getLineWidth()/2 ) ) );
387
const LuPixelPoint& point = cursor->getCursorPoint();
388
const LuPixelRect& size = cursor->getCursorSize();
390
painter.drawLine( context.layoutUnitToPixelX( point.x() ),
391
context.layoutUnitToPixelY( size.top() ),
392
context.layoutUnitToPixelX( point.x() ),
393
context.layoutUnitToPixelY( size.bottom() )-1 );
315
painter.drawLine(point.x(), size.top(),
316
point.x(), size.bottom()-1);
317
painter.drawLine(size.left(), size.bottom(),
318
size.right(), size.bottom());
396
painter.drawLine( context.layoutUnitToPixelX( point.x() ),
397
context.layoutUnitToPixelY( size.top() ),
398
context.layoutUnitToPixelX( point.x() ),
399
context.layoutUnitToPixelY( size.bottom() )-1 );
400
painter.drawLine( context.layoutUnitToPixelX( size.left() ),
401
context.layoutUnitToPixelY( size.bottom() )-1,
402
context.layoutUnitToPixelX( size.right() )-1,
403
context.layoutUnitToPixelY( size.bottom() )-1 );
321
painter.setRasterOp(Qt::CopyROP);
406
// This might be wrong but probably isn't.
407
painter.setRasterOp( Qt::CopyROP );
325
double SequenceElement::getChildPosition(uint child)
411
luPixel SequenceElement::getChildPosition( const ContextStyle& context, uint child )
327
413
if (child < children.count()) {
328
414
return children.at(child)->getX();
785
866
cursor->setTo(this, children.count(), 0);
869
bool SequenceElement::onlyTextSelected( FormulaCursor* cursor )
871
if ( cursor->isSelection() ) {
872
uint from = QMIN( cursor->getPos(), cursor->getMark() );
873
uint to = QMAX( cursor->getPos(), cursor->getMark() );
874
for ( uint i = from; i < to; i++ ) {
875
BasicElement* element = getChild( i );
876
if ( element->getCharacter() == QChar::null ) {
885
KCommand* SequenceElement::buildCommand( Container* container, Request* request )
887
FormulaCursor* cursor = container->activeCursor();
888
if ( cursor->isReadOnly() ) {
889
formula()->tell( i18n( "write protection" ) );
893
switch ( *request ) {
895
KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
896
TextRequest* tr = static_cast<TextRequest*>( request );
897
for ( uint i = 0; i < tr->text().length(); i++ ) {
898
command->addElement( creationStrategy->createTextElement( tr->text()[i] ) );
902
case req_addTextChar: {
903
KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
904
TextCharRequest* tr = static_cast<TextCharRequest*>( request );
905
TextElement* element = creationStrategy->createTextElement( tr->ch(), tr->isSymbol() );
906
command->addElement( element );
909
case req_addEmptyBox: {
910
EmptyElement* element = creationStrategy->createEmptyElement();
911
if ( element != 0 ) {
912
KFCReplace* command = new KFCReplace( i18n("Add Empty Box"), container );
913
command->addElement( element );
918
case req_addNameSequence:
919
if ( onlyTextSelected( container->activeCursor() ) ) {
920
NameSequence* nameSequence = creationStrategy->createNameSequence();
921
if ( nameSequence != 0 ) {
922
KFCAddReplacing* command = new KFCAddReplacing( i18n( "Add Name" ), container );
923
command->setElement( nameSequence );
928
case req_addBracket: {
929
BracketRequest* br = static_cast<BracketRequest*>( request );
930
BracketElement* bracketElement =
931
creationStrategy->createBracketElement( br->left(), br->right() );
932
if ( bracketElement != 0 ) {
933
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Bracket"), container);
934
command->setElement( bracketElement );
939
case req_addOverline: {
940
OverlineElement* overline = creationStrategy->createOverlineElement();
941
if ( overline != 0 ) {
942
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Overline"), container);
943
command->setElement( overline );
948
case req_addUnderline: {
949
UnderlineElement* underline = creationStrategy->createUnderlineElement();
950
if ( underline != 0 ) {
951
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Underline"), container);
952
command->setElement( underline );
957
case req_addMultiline: {
958
MultilineElement* multiline = creationStrategy->createMultilineElement();
959
if ( multiline != 0 ) {
960
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Multiline"), container);
961
command->setElement( multiline );
967
SpaceRequest* sr = static_cast<SpaceRequest*>( request );
968
SpaceElement* element = creationStrategy->createSpaceElement( sr->space() );
969
if ( element != 0 ) {
970
KFCReplace* command = new KFCReplace( i18n("Add Space"), container );
971
command->addElement( element );
976
case req_addFraction: {
977
FractionElement* fraction = creationStrategy->createFractionElement();
978
if ( fraction != 0 ) {
979
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Fraction"), container);
980
command->setElement( fraction );
986
RootElement* root = creationStrategy->createRootElement();
988
KFCAddReplacing* command = new KFCAddReplacing(i18n("Add Root"), container);
989
command->setElement( root );
994
case req_addSymbol: {
995
SymbolRequest* sr = static_cast<SymbolRequest*>( request );
996
SymbolElement* symbol = creationStrategy->createSymbolElement( sr->type() );
998
KFCAddReplacing* command = new KFCAddReplacing( i18n( "Add Symbol" ), container );
999
command->setElement( symbol );
1004
case req_addOneByTwoMatrix: {
1005
FractionElement* element = creationStrategy->createFractionElement();
1006
if ( element != 0 ) {
1007
KFCAddReplacing* command = new KFCAddReplacing( i18n("Add 1x2 Matrix"), container );
1008
element->showLine(false);
1009
command->setElement(element);
1013
case req_addMatrix: {
1014
MatrixRequest* mr = static_cast<MatrixRequest*>( request );
1015
uint rows = mr->rows(), cols = mr->columns();
1016
if ( ( rows == 0 ) || ( cols == 0 ) ) {
1017
MatrixDialog* dialog = new MatrixDialog( 0 );
1018
if ( dialog->exec() ) {
1025
if ( ( rows != 0 ) && ( cols != 0 ) ) {
1026
KFCAddReplacing* command = new KFCAddReplacing( i18n( "Add Matrix" ), container );
1027
command->setElement( creationStrategy->createMatrixElement( rows, cols ) );
1033
case req_addIndex: {
1034
if ( cursor->getPos() > 0 && !cursor->isSelection() ) {
1035
IndexElement* element =
1036
dynamic_cast<IndexElement*>( children.at( cursor->getPos()-1 ) );
1037
if ( element != 0 ) {
1038
element->getMainChild()->goInside( cursor );
1039
return element->getMainChild()->buildCommand( container, request );
1042
IndexElement* element = creationStrategy->createIndexElement();
1043
if ( element != 0 ) {
1044
if ( !cursor->isSelection() ) {
1045
cursor->moveLeft( SelectMovement | WordMovement );
1047
IndexRequest* ir = static_cast<IndexRequest*>( request );
1048
KFCAddIndex* command = new KFCAddIndex( container, element,
1049
element->getIndex( ir->index() ) );
1054
case req_removeEnclosing: {
1055
if ( !cursor->isSelection() ) {
1056
DirectedRemove* dr = static_cast<DirectedRemove*>( request );
1057
KFCRemoveEnclosing* command = new KFCRemoveEnclosing( container, dr->direction() );
1062
SequenceElement* sequence = cursor->normal();
1064
( sequence == sequence->formula() ) &&
1065
( sequence->countChildren() == 0 ) ) {
1066
sequence->formula()->removeFormula( cursor );
1070
DirectedRemove* dr = static_cast<DirectedRemove*>( request );
1072
// empty removes are not legal!
1073
if ( !cursor->isSelection() ) {
1074
if ( countChildren() > 0 ) {
1075
if ( ( cursor->getPos() == 0 ) && ( dr->direction() == beforeCursor ) ) {
1078
if ( ( cursor->getPos() == countChildren() ) && ( dr->direction() == afterCursor ) ) {
1082
else if ( getParent() == 0 ) {
1087
KFCRemove* command = new KFCRemove( container, dr->direction() );
1091
case req_compactExpression: {
1093
cursor->moveRight();
1094
formula()->cursorHasMoved( cursor );
1097
case req_makeGreek: {
1098
TextElement* element = cursor->getActiveTextElement();
1099
if ((element != 0) && !element->isSymbol()) {
1100
cursor->selectActiveElement();
1101
const SymbolTable& table = container->document()->getSymbolTable();
1102
if (table.greekLetters().find(element->getCharacter()) != -1) {
1103
KFCReplace* command = new KFCReplace( i18n( "Change Char to Symbol" ), container );
1104
TextElement* symbol = creationStrategy->createTextElement( table.unicodeFromSymbolFont( element->getCharacter() ), true );
1105
command->addElement( symbol );
1108
cursor->setSelection( false );
1116
case req_formatBold:
1117
case req_formatItalic: {
1118
if ( cursor->isSelection() ) {
1119
CharStyleRequest* csr = static_cast<CharStyleRequest*>( request );
1120
CharStyle cs = normalChar;
1121
if ( csr->bold() ) cs = static_cast<CharStyle>( cs | boldChar );
1122
if ( csr->italic() ) cs = static_cast<CharStyle>( cs | italicChar );
1123
CharStyleCommand* cmd = new CharStyleCommand( cs, i18n( "Change Char Style" ), container );
1124
int end = cursor->getSelectionEnd();
1125
for ( int i = cursor->getSelectionStart(); i<end; ++i ) {
1126
cmd->addElement( children.at( i ) );
1132
case req_formatFamily: {
1133
if ( cursor->isSelection() ) {
1134
CharFamilyRequest* cfr = static_cast<CharFamilyRequest*>( request );
1135
CharFamily cf = cfr->charFamily();
1136
CharFamilyCommand* cmd = new CharFamilyCommand( cf, i18n( "Change Char Family" ), container );
1137
int end = cursor->getSelectionEnd();
1138
for ( int i = cursor->getSelectionStart(); i<end; ++i ) {
1139
cmd->addElement( children.at( i ) );
1152
KCommand* SequenceElement::input( Container* container, QKeyEvent* event )
1154
QChar ch = event->text().at( 0 );
1155
if ( ch.isPrint() ) {
1156
return input( container, ch );
1159
int action = event->key();
1160
int state = event->state();
1161
MoveFlag flag = movementFlag(state);
1164
case Qt::Key_BackSpace: {
1165
DirectedRemove r( req_remove, beforeCursor );
1166
return buildCommand( container, &r );
1168
case Qt::Key_Delete: {
1169
DirectedRemove r( req_remove, afterCursor );
1170
return buildCommand( container, &r );
1172
case Qt::Key_Left: {
1173
FormulaCursor* cursor = container->activeCursor();
1174
cursor->moveLeft( flag );
1175
formula()->cursorHasMoved( cursor );
1178
case Qt::Key_Right: {
1179
FormulaCursor* cursor = container->activeCursor();
1180
cursor->moveRight( flag );
1181
formula()->cursorHasMoved( cursor );
1185
FormulaCursor* cursor = container->activeCursor();
1186
cursor->moveUp( flag );
1187
formula()->cursorHasMoved( cursor );
1190
case Qt::Key_Down: {
1191
FormulaCursor* cursor = container->activeCursor();
1192
cursor->moveDown( flag );
1193
formula()->cursorHasMoved( cursor );
1196
case Qt::Key_Home: {
1197
FormulaCursor* cursor = container->activeCursor();
1198
cursor->moveHome( flag );
1199
formula()->cursorHasMoved( cursor );
1203
FormulaCursor* cursor = container->activeCursor();
1204
cursor->moveEnd( flag );
1205
formula()->cursorHasMoved( cursor );
1209
if ( state & Qt::ControlButton ) {
1210
switch ( event->key() ) {
1211
case Qt::Key_AsciiCircum: {
1212
IndexRequest r( upperLeftPos );
1213
return buildCommand( container, &r );
1215
case Qt::Key_Underscore: {
1216
IndexRequest r( lowerLeftPos );
1217
return buildCommand( container, &r );
1229
KCommand* SequenceElement::input( Container* container, QChar ch )
1231
int unicode = ch.unicode();
1234
BracketRequest r( container->document()->leftBracketChar(),
1235
container->document()->rightBracketChar() );
1236
return buildCommand( container, &r );
1239
BracketRequest r( LeftSquareBracket, RightSquareBracket );
1240
return buildCommand( container, &r );
1243
BracketRequest r( LeftCurlyBracket, RightCurlyBracket );
1244
return buildCommand( container, &r );
1247
BracketRequest r( LeftLineBracket, RightLineBracket );
1248
return buildCommand( container, &r );
1251
IndexRequest r( upperRightPos );
1252
return buildCommand( container, &r );
1255
IndexRequest r( lowerRightPos );
1256
return buildCommand( container, &r );
1259
Request r( req_compactExpression );
1260
return buildCommand( container, &r );
1263
Request r( req_addEmptyBox );
1264
return buildCommand( container, &r );
1270
Request r( req_addNameSequence );
1271
return buildCommand( container, &r );
1274
TextCharRequest r( ch );
1275
return buildCommand( container, &r );
790
1282
* Stores the given childrens dom in the element.
792
void SequenceElement::getChildrenDom(QDomDocument& doc, QDomElement& elem,
1284
void SequenceElement::getChildrenDom(QDomDocument doc, QDomElement elem,
793
1285
uint from, uint to)
795
1287
for (uint i = from; i < to; i++) {
1432
QString SequenceElement::formulaString()
1435
uint count = children.count();
1436
for ( uint i = 0; i < count; i++ ) {
1437
BasicElement* child = children.at( i );
1438
//if ( isFirstOfToken( child ) ) {
1441
content += child->formulaString();
1447
void SequenceElement::writeMathML( QDomDocument doc, QDomNode parent )
1449
QDomElement de = doc.createElement( "mrow" );
1451
BasicElement* last = children.last();
1453
// Create a list (right order!)
1454
QPtrList<ElementType> tokenList;
1455
ElementType* token = last->getElementType();
1456
while ( token != 0 ) {
1458
tokenList.prepend( token );
1459
token = token->getPrev();
1462
if ( tokenList.count() == 1 ) {
1463
tokenList.first()->saveMathML( this, doc, parent.toElement() );
1467
for ( uint i = 0; i < tokenList.count(); ++i ) {
1468
tokenList.at( i )->saveMathML( this, doc, de );
1471
parent.appendChild( de );
1475
int SequenceElement::childPos( const BasicElement* child ) const
1477
QPtrListIterator<BasicElement> it( children );
1478
uint count = it.count();
1479
for ( uint i=0; i<count; ++i, ++it ) {
1480
if ( it.current() == child ) {
1488
NameSequence::NameSequence( BasicElement* parent )
1489
: SequenceElement( parent )
1494
bool NameSequence::accept( ElementVisitor* visitor )
1496
return visitor->visit( this );
1500
void NameSequence::calcCursorSize( const ContextStyle& context,
1501
FormulaCursor* cursor, bool smallCursor )
1503
inherited::calcCursorSize( context, cursor, smallCursor );
1504
LuPixelPoint point = widgetPos();
1505
luPixel unitX = context.ptToLayoutUnitPixX( 1 );
1506
luPixel unitY = context.ptToLayoutUnitPixY( 1 );
1507
cursor->addCursorSize( LuPixelRect( point.x()-unitX, point.y()-unitY,
1508
getWidth()+2*unitX, getHeight()+2*unitY ) );
1511
void NameSequence::drawCursor( QPainter& painter, const ContextStyle& context,
1512
FormulaCursor* cursor, bool smallCursor )
1514
LuPixelPoint point = widgetPos();
1515
painter.setPen( QPen( context.getEmptyColor(),
1516
context.layoutUnitToPixelX( context.getLineWidth()/2 ) ) );
1517
luPixel unitX = context.ptToLayoutUnitPixX( 1 );
1518
luPixel unitY = context.ptToLayoutUnitPixY( 1 );
1519
painter.drawRect( context.layoutUnitToPixelX( point.x()-unitX ),
1520
context.layoutUnitToPixelY( point.y()-unitY ),
1521
context.layoutUnitToPixelX( getWidth()+2*unitX ),
1522
context.layoutUnitToPixelY( getHeight()+2*unitY ) );
1524
inherited::drawCursor( painter, context, cursor, smallCursor );
1527
void NameSequence::moveWordLeft( FormulaCursor* cursor )
1529
uint pos = cursor->getPos();
1531
cursor->setTo( this, 0 );
1534
moveLeft( cursor, this );
1538
void NameSequence::moveWordRight( FormulaCursor* cursor )
1540
int pos = cursor->getPos();
1541
if ( pos < countChildren() ) {
1542
cursor->setTo( this, countChildren() );
1545
moveRight( cursor, this );
1550
KCommand* NameSequence::compactExpressionCmd( Container* container )
1552
BasicElement* element = replaceElement( container->document()->getSymbolTable() );
1553
if ( element != 0 ) {
1554
getParent()->selectChild( container->activeCursor(), this );
1556
KFCReplace* command = new KFCReplace( i18n( "Add Element" ), container );
1557
command->addElement( element );
1563
KCommand* NameSequence::buildCommand( Container* container, Request* request )
1565
switch ( *request ) {
1566
case req_compactExpression:
1567
return compactExpressionCmd( container );
1571
case req_addOneByTwoMatrix:
1574
case req_addFraction:
1575
case req_addBracket:
1576
case req_addNameSequence:
1581
return inherited::buildCommand( container, request );
1585
KCommand* NameSequence::input( Container* container, QChar ch )
1587
int unicode = ch.unicode();
1598
// KCommand* compact = compactExpressionCmd( container );
1599
// KCommand* cmd = static_cast<SequenceElement*>( getParent() )->input( container, ch );
1600
// if ( compact != 0 ) {
1601
// KMacroCommand* macro = new KMacroCommand( cmd->name() );
1602
// macro->addCommand( compact );
1603
// macro->addCommand( cmd );
1613
Request r( req_compactExpression );
1614
return buildCommand( container, &r );
1617
TextCharRequest r( ch );
1618
return buildCommand( container, &r );
1624
void NameSequence::setElementType( ElementType* t )
1626
inherited::setElementType( t );
1630
BasicElement* NameSequence::replaceElement( const SymbolTable& table )
1632
QString name = buildName();
1633
QChar ch = table.unicode( name );
1634
if ( !ch.isNull() ) {
1635
return new TextElement( ch, true );
1638
ch = table.unicode( i18n( name.latin1() ) );
1639
if ( !ch.isNull() ) {
1640
return new TextElement( ch, true );
1644
if ( name == "!" ) return new SpaceElement( NEGTHIN );
1645
if ( name == "," ) return new SpaceElement( THIN );
1646
if ( name == ">" ) return new SpaceElement( MEDIUM );
1647
if ( name == ";" ) return new SpaceElement( THICK );
1648
if ( name == "quad" ) return new SpaceElement( QUAD );
1650
if ( name == "frac" ) return new FractionElement();
1651
if ( name == "atop" ) {
1652
FractionElement* frac = new FractionElement();
1653
frac->showLine( false );
1656
if ( name == "sqrt" ) return new RootElement();
1661
BasicElement* NameSequence::createElement( QString type )
1663
if ( type == "TEXT" ) return new TextElement();
1667
// void NameSequence::parse()
1669
// // A name sequence is known as name and so are its children.
1670
// // Caution: this is fake!
1671
// for ( int i = 0; i < countChildren(); i++ ) {
1672
// getChild( i )->setElementType( getElementType() );
1676
QString NameSequence::buildName()
1679
for ( int i = 0; i < countChildren(); i++ ) {
1680
name += getChild( i )->getCharacter();
1685
bool NameSequence::isValidSelection( FormulaCursor* cursor )
1687
SequenceElement* sequence = cursor->normal();
1688
if ( sequence == 0 ) {
1691
return sequence->onlyTextSelected( cursor );
1694
void NameSequence::writeMathML( QDomDocument doc, QDomNode parent )
1696
QDomElement de = doc.createElement( "mi" );
1698
for ( int i = 0; i < countChildren(); ++i ) {
1699
// these are supposed to by TextElements
1700
value += getChild( i )->getCharacter();
1702
de.appendChild( doc.createTextNode( value ) );
1703
parent.appendChild( de );
941
1706
KFORMULA_NAMESPACE_END