24#include "kspread_cell.h"
25#include "kspread_sheet.h"
26#include "kspread_doc.h"
27#include "kspread_style.h"
28#include "kspread_style_manager.h"
29#include "kspread_util.h"
31#include <KoGenStyles.h>
33#include <KoXmlWriter.h>
39#include "kspread_condition.h"
43Conditional::Conditional():
44 val1( 0.0 ), val2( 0.0 ), strVal1( 0 ), strVal2( 0 ),
45 colorcond( 0 ), fontcond( 0 ), styleName( 0 ),
46 style( 0 ), cond(
None )
50Conditional::~Conditional()
66 strVal1 = d.strVal1 ?
new TQString( *d.strVal1 ) : 0;
67 strVal2 = d.strVal2 ?
new TQString( *d.strVal2 ) : 0;
68 styleName = d.styleName ?
new TQString( *d.styleName ) : 0;
69 fontcond = d.fontcond ?
new TQFont( *d.fontcond ) : 0;
70 colorcond = d.colorcond ?
new TQColor( *d.colorcond ) : 0;
79bool Conditional::operator==(
const Conditional& other)
const
81 if ( cond == other.cond &&
84 *strVal1 == *other.strVal1 &&
85 *strVal2 == *other.strVal2 &&
86 *colorcond == *other.colorcond &&
87 *fontcond == *other.fontcond &&
88 *styleName == *other.styleName &&
89 *style == *other.style )
96Conditions::Conditions(
const Cell * ownerCell )
97 : m_cell( ownerCell ), m_matchedStyle( 0 )
99 Q_ASSERT( ownerCell != NULL );
102Conditions::~Conditions()
107void Conditions::checkMatches()
112 m_matchedStyle = condition.style;
121 TQValueList<Conditional>::const_iterator it;
126 for ( it = m_condList.begin(); it != m_condList.end(); ++it )
139 switch ( condition.cond )
141 case Conditional::Equal:
142 if ( condition.strVal1 )
144 if ( strVal == *condition.strVal1 )
148 if ( value - condition.val1 < DBL_EPSILON &&
149 value - condition.val1 > (0.0 - DBL_EPSILON) )
155 case Conditional::Superior:
156 if ( condition.strVal1 )
158 if ( strVal > *condition.strVal1 )
162 if ( value > condition.val1 )
168 case Conditional::Inferior:
169 if ( condition.strVal1 )
171 if ( strVal < *condition.strVal1 )
175 if ( value < condition.val1 )
181 case Conditional::SuperiorEqual :
182 if ( condition.strVal1 )
184 if ( strVal >= *condition.strVal1 )
188 if ( value >= condition.val1 )
194 case Conditional::InferiorEqual :
195 if ( condition.strVal1 )
197 if ( strVal <= *condition.strVal1 )
201 if ( value <= condition.val1 )
207 case Conditional::Between :
208 if ( condition.strVal1 && condition.strVal2 )
210 if ( strVal > *condition.strVal1 && strVal < *condition.strVal2 )
214 if ( ( value > TQMIN(condition.val1, condition.val2 ) )
215 && ( value < TQMAX(condition.val1, condition.val2 ) ) )
221 case Conditional::Different :
222 if ( condition.strVal1 && condition.strVal2 )
224 if ( strVal < *condition.strVal1 || strVal > *condition.strVal2 )
228 if ( ( value < TQMIN(condition.val1, condition.val2 ) )
229 || ( value > TQMAX(condition.val1, condition.val2) ) )
234 case Conditional::DifferentTo :
235 if ( condition.strVal1 )
237 if ( strVal != *condition.strVal1 )
241 if ( value != condition.val1 )
263 TQValueList<Conditional>::const_iterator it;
264 for ( it = list.begin(); it != list.end(); ++it )
271void Conditions::saveOasisConditions( KoGenStyle ¤tCellStyle )
274 if ( m_condList.isEmpty() )
276 TQValueList<Conditional>::const_iterator it;
278 for ( it = m_condList.begin(); it != m_condList.end(); ++it, ++i )
282 TQMap<TQString, TQString> map;
283 map.insert(
"style:condition", saveOasisConditionValue( condition ) );
284 map.insert(
"style:apply-style-name", *( condition.styleName ) );
286 currentCellStyle.addStyleMap( map );
290TQString Conditions::saveOasisConditionValue(
Conditional &condition)
295 switch( condition.cond )
297 case Conditional::None:
299 case Conditional::Equal:
300 value=
"cell-content()=";
301 if ( condition.strVal1 )
302 value+=*condition.strVal1;
304 value+=TQString::number( condition.val1 );
306 case Conditional::Superior:
307 value=
"cell-content()>";
308 if ( condition.strVal1 )
309 value+=*condition.strVal1;
311 value+=TQString::number( condition.val1 );
313 case Conditional::Inferior:
314 value=
"cell-content()<";
315 if ( condition.strVal1 )
316 value+=*condition.strVal1;
318 value+=TQString::number( condition.val1 );
320 case Conditional::SuperiorEqual:
321 value=
"cell-content()>=";
322 if ( condition.strVal1 )
323 value+=*condition.strVal1;
325 value+=TQString::number( condition.val1 );
327 case Conditional::InferiorEqual:
328 value=
"cell-content()<=";
329 if ( condition.strVal1 )
330 value+=*condition.strVal1;
332 value+=TQString::number( condition.val1 );
334 case Conditional::Between:
335 value=
"cell-content-is-between(";
336 if ( condition.strVal1 )
338 value+=*condition.strVal1;
340 if ( condition.strVal2 )
341 value+=*condition.strVal2;
345 value+=TQString::number( condition.val1 );
347 value+=TQString::number( condition.val2 );
351 case Conditional::DifferentTo:
352 value=
"cell-content()!=";
353 if ( condition.strVal1 )
354 value+=*condition.strVal1;
356 value+=TQString::number( condition.val1 );
358 case Conditional::Different:
359 value=
"cell-content-is-not-between(";
360 if ( condition.strVal1 )
362 value+=*condition.strVal1;
364 if ( condition.strVal2 )
365 value+=*condition.strVal2;
369 value+=TQString::number( condition.val1 );
371 value+=TQString::number( condition.val2 );
382 TQDomElement conditions = doc.createElement(
"condition");
383 TQValueList<Conditional>::const_iterator it;
388 for ( it = m_condList.begin(); it != m_condList.end(); ++it )
397 name.prepend(
"condition" );
399 child = doc.createElement( name );
400 child.setAttribute(
"cond", (
int) condition.cond );
403 if ( condition.strVal1 )
405 child.setAttribute(
"strval1", *condition.strVal1 );
406 if ( condition.strVal2 )
407 child.setAttribute(
"strval2", *condition.strVal2 );
411 child.setAttribute(
"val1", condition.val1 );
412 child.setAttribute(
"val2", condition.val2 );
414 if ( condition.styleName )
416 child.setAttribute(
"style", *condition.styleName );
420 child.setAttribute(
"color", condition.colorcond->name() );
421 child.appendChild( util_createElement(
"font", *condition.fontcond, doc ) );
424 conditions.appendChild( child );
432 return TQDomElement();
440void Conditions::loadOasisConditions(
const TQDomElement & element )
442 kdDebug(36003) <<
"Loading conditional styles" << endl;
443 TQDomNode node( element );
444 StyleManager * manager = m_cell->
sheet()->doc()->styleManager();
446 while ( !node.isNull() )
448 TQDomElement elementItem = node.toElement();
449 if ( elementItem.tagName()==
"map" && elementItem.namespaceURI() == KoXmlNS::style )
452 kdDebug(36003) <<
"\tcondition: "<< elementItem.attributeNS( KoXmlNS::style,
"condition", TQString() )<<endl;
454 loadOasisConditionValue( elementItem.attributeNS( KoXmlNS::style,
"condition", TQString() ), newCondition );
455 if ( elementItem.hasAttributeNS( KoXmlNS::style,
"apply-style-name" ) )
457 kdDebug(36003)<<
"\tstyle: "<<elementItem.attributeNS( KoXmlNS::style,
"apply-style-name", TQString() )<<endl;
458 newCondition.styleName =
new TQString( elementItem.attributeNS( KoXmlNS::style,
"apply-style-name", TQString() ) );
459 newCondition.style = manager->style( *newCondition.styleName );
460 if ( !newCondition.style )
467 m_condList.append( newCondition );
469 kdDebug(36003) <<
"Error loading condition " << elementItem.nodeName()<< endl;
471 node = node.nextSibling();
475void Conditions::loadOasisConditionValue(
const TQString &styleCondition,
Conditional &newCondition )
477 TQString val( styleCondition );
478 if ( val.contains(
"cell-content()" ) )
480 val = val.remove(
"cell-content()" );
481 loadOasisCondition( val,newCondition );
485 if ( val.contains(
"cell-content-is-between(" ) )
487 val = val.remove(
"cell-content-is-between(" );
488 val = val.remove(
")" );
489 TQStringList listVal = TQStringList::split(
"," , val );
490 loadOasisValidationValue( listVal, newCondition );
491 newCondition.cond = Conditional::Between;
493 if ( val.contains(
"cell-content-is-not-between(" ) )
495 val = val.remove(
"cell-content-is-not-between(" );
496 val = val.remove(
")" );
497 TQStringList listVal = TQStringList::split(
",", val );
498 loadOasisValidationValue( listVal,newCondition );
499 newCondition.cond = Conditional::Different;
505void Conditions::loadOasisCondition( TQString &valExpression,
Conditional &newCondition )
508 if (valExpression.find(
"<=" )==0 )
510 value = valExpression.remove( 0,2 );
511 newCondition.cond = Conditional::InferiorEqual;
513 else if (valExpression.find(
">=" )==0 )
515 value = valExpression.remove( 0,2 );
516 newCondition.cond = Conditional::SuperiorEqual;
518 else if (valExpression.find(
"!=" )==0 )
521 value = valExpression.remove( 0,2 );
522 newCondition.cond = Conditional::DifferentTo;
524 else if ( valExpression.find(
"<" )==0 )
526 value = valExpression.remove( 0,1 );
527 newCondition.cond = Conditional::Inferior;
529 else if(valExpression.find(
">" )==0 )
531 value = valExpression.remove( 0,1 );
532 newCondition.cond = Conditional::Superior;
534 else if (valExpression.find(
"=" )==0 )
536 value = valExpression.remove( 0,1 );
537 newCondition.cond = Conditional::Equal;
540 kdDebug()<<
" I don't know how to parse it :"<<valExpression<<endl;
541 kdDebug(36003) <<
"\tvalue: " << value << endl;
543 newCondition.val1 = value.toDouble(&ok);
546 newCondition.val1 = value.toInt(&ok);
549 newCondition.strVal1 =
new TQString( value );
550 kdDebug()<<
" Try to parse this value :"<<value<<endl;
556void Conditions::loadOasisValidationValue(
const TQStringList &listVal,
Conditional &newCondition )
559 kdDebug()<<
" listVal[0] :"<<listVal[0]<<
" listVal[1] :"<<listVal[1]<<endl;
561 newCondition.val1 = listVal[0].toDouble(&ok);
564 newCondition.val1 = listVal[0].toInt(&ok);
567 newCondition.strVal1 =
new TQString( listVal[0] );
568 kdDebug()<<
" Try to parse this value :"<<listVal[0]<<endl;
572 newCondition.val2 = listVal[1].toDouble(&ok);
575 newCondition.val2 = listVal[1].toInt(&ok);
578 newCondition.strVal2 =
new TQString( listVal[1] );
579 kdDebug()<<
" Try to parse this value :"<<listVal[1]<<endl;
587 TQDomNodeList nodeList = element.childNodes();
590 StyleManager * manager = m_cell->
sheet()->doc()->styleManager();
592 for (
int i = 0; i < (int)(nodeList.length()); ++i )
594 newCondition.strVal1 = 0;
595 newCondition.strVal2 = 0;
596 newCondition.styleName = 0;
597 newCondition.fontcond = 0;
598 newCondition.colorcond = 0;
600 TQDomElement conditionElement = nodeList.item( i ).toElement();
602 ok = conditionElement.hasAttribute(
"cond" );
605 newCondition.cond = (Conditional::Type) conditionElement.attribute(
"cond" ).toInt( &ok );
608 if ( conditionElement.hasAttribute(
"val1" ) )
610 newCondition.val1 = conditionElement.attribute(
"val1" ).toDouble( &ok );
612 if ( conditionElement.hasAttribute(
"val2" ) )
613 newCondition.val2 = conditionElement.attribute(
"val2").toDouble( &ok );
616 if ( conditionElement.hasAttribute(
"strval1" ) )
618 newCondition.strVal1 =
new TQString( conditionElement.attribute(
"strval1" ) );
620 if ( conditionElement.hasAttribute(
"strval2" ) )
621 newCondition.strVal2 =
new TQString( conditionElement.attribute(
"strval2" ) );
624 if ( conditionElement.hasAttribute(
"color" ) )
625 newCondition.colorcond =
new TQColor( conditionElement.attribute(
"color" ) );
627 TQDomElement font = conditionElement.namedItem(
"font" ).toElement();
628 if ( !font.isNull() )
629 newCondition.fontcond =
new TQFont( util_toFont( font ) );
631 if ( conditionElement.hasAttribute(
"style" ) )
633 newCondition.styleName =
new TQString( conditionElement.attribute(
"style" ) );
634 newCondition.style = manager->style( *newCondition.styleName );
635 if ( !newCondition.style )
641 m_condList.append( newCondition );
645 kdDebug(36001) <<
"Error loading condition " << conditionElement.nodeName()<< endl;
650bool Conditions::operator==(
const Conditions& other )
const
652 if ( !( *m_matchedStyle == *other.m_matchedStyle ) )
654 if ( m_condList.count() != other.m_condList.count() )
656 TQValueList<Conditional>::ConstIterator end( m_condList.end() );
657 for ( TQValueList<Conditional>::ConstIterator it( m_condList.begin() ); it != end; ++it )
660 TQValueList<Conditional>::ConstIterator otherEnd( other.m_condList.end() );
661 for ( TQValueList<Conditional>::ConstIterator otherIt( other.m_condList.begin() ); otherIt != otherEnd; ++otherIt )
663 if ( (*it) == (*otherIt) )
For every cell in the spread sheet there is a Cell object.
const Value value() const
Returns the value that this cell holds.
Sheet * sheet() const
Returns the worksheet which owns this cell.
Structure to indicate the condition we're testing on a cell and the special font and color that will ...
Manages a set of conditions for a cell.
TQValueList< Conditional > conditionList() const
Retrieve the current list of conditions we're checking.
bool currentCondition(Conditional &condition)
Use this function to see what conditions actually apply currently.
void setConditionList(const TQValueList< Conditional > &list)
Replace the current list of conditions with this new one.
TQDomElement saveConditions(TQDomDocument &doc) const
Saves the conditions to a DOM tree structure.
void loadConditions(const TQDomElement &element)
Takes a parsed DOM element and recreates the conditions structure out of it.
bool isNumber() const
Returns true if the type of this value is either integer or floating-point.
double asFloat() const
Returns the floating-point value of this value.
TQString asString() const
Returns the string value of this value.