1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/* This file is part of the KDE project
Copyright (C) 2006 Alfredo Beaumont Sainz <alfredo.beaumont@gmail.com>
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.
*/
#include <klocale.h>
#include "kformuladefs.h"
#include "textelement.h"
#include "identifierelement.h"
#include "operatorelement.h"
#include "numberelement.h"
#include "kformulacommand.h"
#include "kformulacontainer.h"
#include "kformuladocument.h"
#include "formulaelement.h"
#include "creationstrategy.h"
KFORMULA_NAMESPACE_BEGIN
IdentifierElement::IdentifierElement( BasicElement* tqparent ) : TokenElement( tqparent ) {}
/*
* Token elements' content has to be of homogeneous type. Every token element
* must (TODO: check this) appear inside a non-token sequence, and thus, if
* the command asks for a different content, a new element has to be created in
* tqparent sequence.
*/
KCommand* IdentifierElement::buildCommand( Container* container, Request* request )
{
FormulaCursor* cursor = container->activeCursor();
if ( cursor->isReadOnly() ) {
formula()->tell( i18n( "write protection" ) );
return 0;
}
if ( *request == req_addText ) {
KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
TextRequest* tr = static_cast<TextRequest*>( request );
for ( uint i = 0; i < tr->text().length(); i++ ) {
TextElement* element = creationStrategy->createTextElement( tr->text()[i] );
command->addElement( element );
}
return command;
}
else if ( *request == req_addTextChar ) {
KFCReplace* command = new KFCReplace( i18n("Add Text"), container );
TextCharRequest* tr = static_cast<TextCharRequest*>( request );
TextElement* element = creationStrategy->createTextElement( tr->ch(), tr->isSymbol() );
command->addElement( element );
return command;
}
if ( countChildren() == 0 || cursor->getPos() == countChildren() ) {
// We are in the last position, so it's easy, call the tqparent to
// create a new child
SequenceElement* tqparent = static_cast<SequenceElement*>( getParent() );
if ( tqparent ) {
uint pos = tqparent->childPos( this );
cursor->setTo( tqparent, pos + 1);
return tqparent->buildCommand( container, request );
}
}
if ( cursor->getPos() == 0 ) {
SequenceElement* tqparent = static_cast<SequenceElement*>( getParent() );
if ( tqparent ) {
uint pos = tqparent->childPos( this );
cursor->setTo( tqparent, pos );
return tqparent->buildCommand( container, request );
}
}
// We are in the middle of a token, so:
// a) Cut from mark to the end
// b) Create a new token and add an element from key pressed
// c) Create a new token and add elements cut previously
// d) Move cursor to tqparent so that it command execution works fine
switch( *request ) {
case req_addOperator: {
KFCSplitToken* command = new KFCSplitToken( i18n("Add Operator"), container );
OperatorRequest* opr = static_cast<OperatorRequest*>( request );
OperatorElement* op = creationStrategy->createOperatorElement();
TextElement* text = creationStrategy->createTextElement( opr->ch() );
command->addCursor( cursor );
command->addToken( op );
command->addContent( op, text );
SequenceElement* tqparent = static_cast< SequenceElement* >( getParent() );
if ( tqparent ) {
cursor->setTo( tqparent, tqparent->childPos( this ) + 1 );
}
return command;
}
case req_addNumber: {
KFCSplitToken* command = new KFCSplitToken( i18n("Add Number"), container );
NumberRequest* nr = static_cast<NumberRequest*>( request );
NumberElement* num = creationStrategy->createNumberElement();
TextElement* text = creationStrategy->createTextElement( nr->ch() );
command->addCursor( cursor );
command->addToken( num );
command->addContent( num, text );
SequenceElement* tqparent = static_cast< SequenceElement* >( getParent() );
if ( tqparent ) {
cursor->setTo( tqparent, tqparent->childPos( this ) + 1 );
}
return command;
}
case req_addEmptyBox:
case req_addNameSequence:
case req_addBracket:
case req_addSpace:
case req_addFraction:
case req_addRoot:
case req_addSymbol:
case req_addOneByTwoMatrix:
case req_addMatrix: {
uint pos = static_cast<SequenceElement*>(getParent())->childPos( this );
cursor->setTo( getParent(), pos + 1);
return getParent()->buildCommand( container, request );
}
default:
return SequenceElement::buildCommand( container, request );
}
return 0;
}
void IdentifierElement::setStyleVariant( StyleAttributes& style )
{
if ( customMathVariant() ) {
style.setCustomMathVariant ( true );
style.setCustomFontWeight( false );
style.setCustomFontStyle( false );
style.setCustomFont( false );
if ( customMathVariant() ) {
style.setCharFamily ( charFamily() );
style.setCharStyle( charStyle() );
}
else {
style.setCharFamily( style.charFamily() );
style.setCharStyle( style.charStyle() );
}
}
else {
style.setCustomMathVariant( false );
if ( customFontFamily() ) {
style.setCustomFont( true );
style.setFont( TQFont(fontFamily()) );
}
bool fontweight = false;
if ( customFontWeight() || style.customFontWeight() ) {
style.setCustomFontWeight( true );
if ( customFontWeight() ) {
fontweight = fontWeight();
}
else {
fontweight = style.customFontWeight();
}
style.setFontWeight( fontweight );
}
else {
style.setCustomFontWeight( false );
}
bool fontstyle;
if ( customFontStyle() ) {
fontstyle = fontStyle();
}
else if ( countChildren() == 1 ) {
fontstyle = true;
}
else {
fontstyle = false;
}
if ( fontweight && fontstyle ) {
style.setCharStyle( boldItalicChar );
}
else if ( fontweight && ! fontstyle ) {
style.setCharStyle( boldChar );
}
else if ( ! fontweight && fontstyle ) {
style.setCharStyle( italicChar );
}
else {
style.setCharStyle( normalChar );
}
}
}
KFORMULA_NAMESPACE_END
|