3 #include "QDS_Validator.h"
5 #include <DDS_Dictionary.h>
12 #include <qwhatsthis.h>
13 #include <qvalidator.h>
14 #include <qmessagebox.h>
17 Class: QDS_Datum::Wrapper
18 Descr: Wrapper widget for sub widgets. [internal]
21 class QDS_Datum::Wrapper : public QWidget
24 Wrapper( QWidget* = 0 );
27 QWidget* widget() const;
28 void setWidget( QWidget* );
30 virtual bool eventFilter( QObject*, QEvent* );
33 virtual void resizeEvent( QResizeEvent* );
40 QDS_Datum::Wrapper::Wrapper( QWidget* parent )
44 QHBoxLayout* base = new QHBoxLayout( this );
45 base->setAutoAdd( true );
46 setFocusPolicy( StrongFocus );
49 QDS_Datum::Wrapper::~Wrapper()
53 QWidget* QDS_Datum::Wrapper::widget() const
58 void QDS_Datum::Wrapper::setWidget( QWidget* wid )
64 myWid->removeEventFilter( this );
71 if ( myWid->parent() != this )
72 myWid->reparent( this, QPoint( 0, 0 ) );
74 setTabOrder( this, myWid );
75 setFocusProxy( myWid );
77 myWid->updateGeometry();
80 myWid->installEventFilter( this );
83 bool QDS_Datum::Wrapper::eventFilter( QObject* o, QEvent* e )
85 if ( e->type() == QEvent::Resize && o == widget() )
87 QResizeEvent* re = (QResizeEvent*)e;
88 if ( re->size() != size() )
91 return QWidget::eventFilter( o, e );
94 void QDS_Datum::Wrapper::resizeEvent( QResizeEvent* e )
96 QWidget::resizeEvent( e );
98 if ( widget() && widget()->size() != size() )
99 widget()->resize( size() );
104 Descr: Base class for control used data dictionary. [public]
107 QDS_Datum::QDS_Datum( const QString& id, QWidget* parent, const int flags, const QString& comp )
114 myInitialised( false )
116 if ( myFlags & Label )
117 myWrapper.insert( Label, new Wrapper( parent ) );
118 if ( myFlags & Control )
119 myWrapper.insert( Control, new Wrapper( parent ) );
120 if ( myFlags & Units )
121 myWrapper.insert( Units, new Wrapper( parent ) );
123 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
124 connect( it.data(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
126 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
127 if ( aDict.IsNull() )
130 TCollection_AsciiString anId = toAsciiString( id );
131 TCollection_AsciiString aComp = toAsciiString( comp );
133 if ( aComp.IsEmpty() )
134 setDicItem( aDict->GetDicItem( anId ) );
136 setDicItem( aDict->GetDicItem( anId, aComp ) );
138 QTimer::singleShot( 0, this, SLOT( onInitDatum() ) );
141 parent->installEventFilter( this );
146 QDS_Datum::~QDS_Datum()
154 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
159 QDS_Datum::operator QWidget*() const
161 return widget( Control );
164 QString QDS_Datum::id() const
171 int QDS_Datum::type() const
175 int res = DDS_DicItem::Unknown;
176 if ( !myDicItem.IsNull() )
177 res = myDicItem->GetType();
181 QString QDS_Datum::label() const
186 if ( !myDicItem.IsNull() )
187 labStr = toQString( myDicItem->GetLabel() );
189 if ( flags() & NotAccel )
190 labStr = removeAccel( labStr );
195 QString QDS_Datum::units() const
200 if ( !myDicItem.IsNull() )
201 unitStr = toQString( myDicItem->GetUnits() );
205 QString QDS_Datum::filter() const
210 if ( !myDicItem.IsNull() )
211 fltr = toQString( myDicItem->GetFilter() );
215 QString QDS_Datum::format() const
220 if ( !myDicItem.IsNull() )
221 fmtStr = toQString( myDicItem->GetFormat( false ) );
225 QString QDS_Datum::defaultValue() const
229 QString pref = prefix();
230 QString suff = suffix();
233 if ( !myDicItem.IsNull() )
234 def = toQString( myDicItem->GetDefaultValue() );
236 QString aDef = def.stripWhiteSpace();
237 if ( !pref.isEmpty() && aDef.left( pref.length() ) == pref )
238 aDef = aDef.mid( pref.length() );
240 if ( !suff.isEmpty() && aDef.right( suff.length() ) == suff )
241 aDef = aDef.mid( 0, aDef.length() - suff.length() );
246 QString QDS_Datum::minimumValue() const
251 if ( !myDicItem.IsNull() && myDicItem->HasData( DDS_DicItem::MinValue ) )
252 min = format( format(), type(), myDicItem->GetMinValue() );
256 QString QDS_Datum::maximumValue() const
261 if ( !myDicItem.IsNull() && myDicItem->HasData( DDS_DicItem::MaxValue ) )
262 max = format( format(), type(), myDicItem->GetMaxValue() );
266 QString QDS_Datum::longDescription() const
271 if ( !myDicItem.IsNull() )
272 ldStr = toQString( myDicItem->GetLongDescription() );
276 QString QDS_Datum::shortDescription() const
281 if ( !myDicItem.IsNull() )
282 sdStr = toQString( myDicItem->GetLongDescription() );
286 QVariant QDS_Datum::value() const
294 QString QDS_Datum::stringValue() const
298 if ( getString() == myTargetValue )
299 return mySourceValue;
304 double QDS_Datum::doubleValue() const
309 if ( !myTargetValue.isNull() && myTargetValue == getString() )
310 res = mySourceValue.toDouble();
313 res = getString().toDouble();
314 if ( !myDicItem.IsNull() )
315 res = myDicItem->ToSI( res );
321 int QDS_Datum::integerValue() const
326 if ( !myTargetValue.isNull() && myTargetValue == getString() )
327 res = mySourceValue.toInt();
330 double val = getString().toDouble();
331 if ( !myDicItem.IsNull() )
332 res = (int)myDicItem->ToSI( val );
338 QString QDS_Datum::text() const
342 QString aLabel = label();
343 QString aData = stringValue();
344 QString aUnits = units();
346 QString res = aLabel;
347 if ( !res.isEmpty() && !aData.isEmpty() )
348 res += QString( ": " );
351 if ( !aUnits.isEmpty() )
352 res += QString( " " ) + aUnits;
357 bool QDS_Datum::isEmpty() const
359 return stringValue().isEmpty();
362 void QDS_Datum::reset()
366 mySourceValue = defaultValue();
367 setString( format( ( myFlags & NotFormat ) ? (QString) "" : format(), type(), mySourceValue ) );
371 QString str = getString();
373 emit paramChanged( str );
376 void QDS_Datum::clear()
380 if ( !getString().isEmpty() )
383 setString( mySourceValue );
387 QString str = getString();
389 emit paramChanged( str );
393 void QDS_Datum::setValue( const QVariant& val )
395 if ( val.isValid() && val.canCast( QVariant::String ) )
396 setStringValue( val.toString() );
401 void QDS_Datum::setStringValue( const QString& txt )
406 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), txt );
408 myTargetValue = aStr;
411 QString str = getString();
413 emit paramChanged( str );
416 void QDS_Datum::setDoubleValue( const double num )
420 mySourceValue = QString().setNum( num, 'g', 16 );
422 if ( !myDicItem.IsNull() )
423 val = myDicItem->FromSI( val );
425 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
427 myTargetValue = aStr;
430 QString str = getString();
432 emit paramChanged( str );
435 void QDS_Datum::setIntegerValue( const int num )
439 mySourceValue = QString().setNum( num );
441 if ( !myDicItem.IsNull() )
442 val = myDicItem->FromSI( val );
444 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
446 myTargetValue = aStr;
449 QString str = getString();
451 emit paramChanged( str );
455 Returns true if all subwidgets specified by 'element' enabled.
457 bool QDS_Datum::isEnabled( const int element ) const
462 if ( element & Label )
463 res = res && labelWidget() && labelWidget()->isEnabled();
464 if ( element & Units )
465 res = res && unitsWidget() && unitsWidget()->isEnabled();
466 if ( element & Control )
467 res = res && controlWidget() && controlWidget()->isEnabled();
472 Enable/Disable subwidgets specified by 'element'.
473 Values: Label, Control, Units or their combinations.
475 void QDS_Datum::setEnabled( const bool on, const int element )
479 if ( element & Label && labelWidget() )
480 labelWidget()->setEnabled( on );
481 if ( element & Units && unitsWidget() )
482 unitsWidget()->setEnabled( on );
483 if ( element & Control && controlWidget() )
484 controlWidget()->setEnabled( on );
488 Enable/Disable control.
490 void QDS_Datum::setEnabled( bool on )
492 setEnabled( on, Control );
495 void QDS_Datum::setShown( const bool visible, const int flags )
504 Show subwidgets specified by 'element'.
505 Values: Label, Control, Units or their combinations.
507 void QDS_Datum::show( const int element )
511 if ( ( element & Label ) && labelWidget() )
512 labelWidget()->show();
513 if ( ( element & Units ) && unitsWidget() )
514 unitsWidget()->show();
515 if ( ( element & Control ) && controlWidget() )
516 controlWidget()->show();
520 Hide subwidgets specified by 'element'.
521 Values: Label, Control, Units or their combinations.
523 void QDS_Datum::hide( const int element )
527 if ( ( element & Label ) && labelWidget() )
528 labelWidget()->hide();
529 if ( ( element & Units ) && unitsWidget() )
530 unitsWidget()->hide();
531 if ( ( element & Control ) && controlWidget() )
532 controlWidget()->hide();
536 Returns subwidget specified by 'element'.
537 Possible values: Label, Control, Units.
539 QWidget* QDS_Datum::widget( const int element ) const
542 return wrapper( element );
546 Set the input focus on the control widget.
548 void QDS_Datum::setFocus()
552 if ( controlWidget() )
553 controlWidget()->setFocus();
557 Returns true if control contains valid value otherwise returns false
558 and display warning message box if parameter msgBox is set.
560 bool QDS_Datum::isValid( const bool msgBox, const QString& extMsg, const QString& extLabel ) const
564 if ( type() == DDS_DicItem::String && isDoubleFormat( format() ) )
568 if ( !dicItem().IsNull() )
569 req = toQString( dicItem()->GetRequired() );
572 QString aStr = getString();
574 if ( aStr.isEmpty() )
575 aState = !( req == QString( "yes" ) || req == QString( "true" ) || req.toInt() );
577 aState = validate( aStr );
579 if ( msgBox && !aState )
582 if ( !label().isEmpty() )
583 info += tr( "DATA_INCORRECT_VALUE" ).arg( label() );
584 else if ( !extLabel.isEmpty() )
585 info += tr( "DATA_INCORRECT_VALUE" ).arg( extLabel );
590 case DDS_DicItem::String:
591 typeStr = tr( "DATA_STRING" );
593 case DDS_DicItem::Integer:
594 typeStr = tr( "DATA_INTEGER" );
596 case DDS_DicItem::Float:
597 typeStr = tr( "DATA_FLOAT" );
600 typeStr = tr( "DATA_NON_EMPTY" );
603 info += ( info.isEmpty() ? (QString) "" : QString( "\n" ) ) +
604 tr( "DATA_SHOULD_BE_VALUE" ).arg( typeStr );
606 if ( type() == DDS_DicItem::Float || type() == DDS_DicItem::Integer )
608 QString aMinValue = minValue();
609 QString aMaxValue = maxValue();
610 if ( !aMinValue.isEmpty() && !aMaxValue.isEmpty() )
611 limit = tr( "DATA_RANGE" ).arg( aMinValue ).arg( aMaxValue );
612 else if ( !aMinValue.isEmpty() )
613 limit = tr( "DATA_MIN_LIMIT" ).arg( aMinValue );
614 else if ( !aMaxValue.isEmpty() )
615 limit = tr( "DATA_MAX_LIMIT" ).arg( aMaxValue );
617 if ( !limit.isEmpty() )
620 info += QString( ".\n" ) + tr( "DATA_INPUT_VALUE" );
622 if ( !extMsg.isEmpty() )
623 info += QString( "\n" ) + extMsg;
626 for ( uint i = 0; i < info.length(); i++ )
627 if ( info.at( i ) == '\n' )
628 msg += QString( "<br>" );
632 info = QString( "<p><nobr>%1</nobr></p>" ).arg( msg );
634 QMessageBox::critical( controlWidget() ? controlWidget()->topLevelWidget() : 0,
635 tr( "DATA_ERR_TITLE" ), info, tr( "OK" ) );
636 if ( controlWidget() )
637 controlWidget()->setFocus();
643 Add widgets to the vertical layout.
645 void QDS_Datum::addTo( QVBoxLayout* l )
652 if ( wrapper( Label ) )
653 l->addWidget( wrapper( Label ) );
654 if ( wrapper( Control ) )
655 l->addWidget( wrapper( Control ) );
656 if ( wrapper( Units ) )
657 l->addWidget( wrapper( Units ) );
661 Add widgets to the horizaontal layout.
663 void QDS_Datum::addTo( QHBoxLayout* l )
670 if ( wrapper( Label ) )
671 l->addWidget( wrapper( Label ) );
672 if ( wrapper( Control ) )
673 l->addWidget( wrapper( Control ) );
674 if ( wrapper( Units ) )
675 l->addWidget( unitsWidget() );
679 Add widgets to the grid layout.
681 void QDS_Datum::addTo( QGridLayout* theLay, const int theRow, const int theCol, bool vertical )
690 if ( wrapper( Label ) )
692 theLay->addWidget( wrapper( Label ), row, col );
693 vertical ? row++ : col++;
695 if ( wrapper( Control ) )
697 theLay->addWidget( wrapper( Control ), row, col );
698 vertical ? row++ : col++;
700 if ( wrapper( Units ) )
701 theLay->addWidget( wrapper( Units ), row, col );
705 Set the aligment of Label or Units. For Control nothing happens.
707 void QDS_Datum::setAlignment( const int align, const int type )
711 if ( ( type & Label ) && labelWidget() )
712 labelWidget()->setAlignment( align );
713 if ( ( type & Units ) && unitsWidget() )
714 unitsWidget()->setAlignment( align );
717 bool QDS_Datum::eventFilter( QObject* o, QEvent* e )
721 if ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
722 ( e->type() == QEvent::ChildInserted && ((QChildEvent*)e)->child() == this ) )
725 return QObject::eventFilter( o, e );
729 Notify about parameter changing.
731 void QDS_Datum::onParamChanged()
736 Delayed initialization.
738 void QDS_Datum::onInitDatum()
744 Notify about subwidgets destroying. Allow to avoid repeated deleting in destructor.
746 void QDS_Datum::onDestroyed( QObject* obj )
748 myWrapper.remove( wrapperType( (Wrapper*)obj ) );
752 Returns QLabel instance which contains data dictionary label.
754 QLabel* QDS_Datum::labelWidget() const
761 Returns QLabel instance which contains data dictionary units.
763 QLabel* QDS_Datum::unitsWidget() const
770 Returns QWidget which contains user input data.
772 QWidget* QDS_Datum::controlWidget() const
779 Returns the dictionary item from the datum.
781 Handle(DDS_DicItem) QDS_Datum::dicItem() const
787 Set the dictionary item in to the datum.
789 void QDS_Datum::setDicItem( const Handle(DDS_DicItem)& item )
795 Creates QLabel widget for data label.
797 QLabel* QDS_Datum::createLabel( QWidget* parent )
799 return new QLabel( parent );
803 Creates QLabel widget for data units.
805 QLabel* QDS_Datum::createUnits( QWidget* parent )
807 return new QLabel( parent );
811 Returns validator accordance to data type.
813 QValidator* QDS_Datum::validator( const bool limits ) const
815 QValidator* aValidator = 0;
817 QString fltr = filter();
819 if ( type() == DDS_DicItem::String )
822 QString aFormat = canonicalFormat( format(), aFlags );
825 int pos = aFormat.find( "." );
828 QString numStr = aFormat.mid( pos + 1, aFormat.length() - pos - 2 );
830 int numVal = numStr.toInt( &ok );
835 QDS_StringValidator* aStrVal = new QDS_StringValidator( fltr, aFlags, (QObject*)this );
836 aStrVal->setLength( len );
838 aValidator = aStrVal;
840 else if ( type() == DDS_DicItem::Integer )
842 QDS_IntegerValidator* aIntVal = new QDS_IntegerValidator( fltr, (QObject*)this );
846 limit = minValue().toInt( &ok );
848 aIntVal->setBottom( limit );
849 limit = maxValue().toInt( &ok );
851 aIntVal->setTop( limit );
853 aValidator = aIntVal;
855 else if ( type() == DDS_DicItem::Float )
857 QDS_DoubleValidator* aFloatVal = new QDS_DoubleValidator( fltr, (QObject*)this );
861 limit = minValue().toDouble( &ok );
863 aFloatVal->setBottom( limit );
864 limit = maxValue().toDouble( &ok );
866 aFloatVal->setTop( limit );
868 aValidator = aFloatVal;
875 Checks the given string are valid or not.
877 bool QDS_Datum::validate( const QString& txt ) const
879 if ( type() == DDS_DicItem::Unknown ||
880 type() == DDS_DicItem::String && isDoubleFormat( format() ) )
883 QValidator* aValidator = validator( true );
890 bool res = aValidator->validate( str, pos ) == QValidator::Acceptable;
898 Retrieves information from data dictionary and create subwidgets using virtual mechanism.
899 Virtual mechanism doesn't work in constructor and destructor, therefore this method should
900 be called outside the constructor.
902 void QDS_Datum::initialize()
904 if ( wrapper( Label ) )
905 wrapper( Label )->setWidget( myLabel = createLabel( wrapper( Label ) ) );
906 if ( wrapper( Control ) )
907 wrapper( Control )->setWidget( myControl = createControl( wrapper( Control ) ) );
908 if ( wrapper( Units ) )
909 wrapper( Units )->setWidget( myUnits = createUnits( wrapper( Units ) ) );
911 TCollection_AsciiString comp;
912 Handle(DDS_DicItem) item = dicItem();
913 if ( !item.IsNull() )
914 comp = item->GetComponent();
917 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
919 unitSystem = toQString( comp.IsEmpty() ? dic->GetActiveUnitSystem() :
920 dic->GetActiveUnitSystem( comp ) );
922 unitSystemChanged( unitSystem );
924 QWidget* ctrl = controlWidget();
927 QString lDescr = longDescription();
928 QString sDescr = shortDescription();
929 if ( !sDescr.isEmpty() )
930 QToolTip::add( ctrl, sDescr );
931 if ( !lDescr.isEmpty() )
932 QWhatsThis::add( ctrl, lDescr );
935 if ( labelWidget() && ctrl && !( flags() & NotAccel ) )
936 labelWidget()->setBuddy( ctrl );
939 void QDS_Datum::unitSystemChanged( const QString& unitSystem )
941 QString labText = label();
942 QString unitText = unitsToText( units() );
944 if ( flags() & UnitsWithLabel )
946 if ( labText.isEmpty() )
948 else if ( !unitText.isEmpty() )
949 labText = QString( "%1 (%2)" ).arg( labText ).arg( unitText );
950 unitText = QString::null;
954 labelWidget()->setText( labText );
957 unitsWidget()->setText( unitText );
963 Covert units into text presentation.
965 QString QDS_Datum::unitsToText( const QString& uni )
968 QString aUnits = uni;
969 while ( ( pos = aUnits.find( "**" ) ) != -1 )
971 aUnits = aUnits.mid( 0, pos ) + QString( "<tt><font size=+2><sup>" ) +
972 aUnits.mid( pos + 2, 1 ) + QString( "</sup></font></tt>" ) +
973 aUnits.mid( pos + 3 );
979 Covert text presentation into internal units format.
981 QString QDS_Datum::textToUnits( const QString& txt )
984 QString aUnits = txt;
985 while ( ( pos = aUnits.find( "<sup>" ) ) != -1 )
987 aUnits.remove( pos, 5 );
988 aUnits.insert( pos, "**" );
990 while ( ( pos = aUnits.find( "</sup>" ) ) != -1 )
991 aUnits.remove( pos, 6 );
996 Format the specified integer as data dictionary value.
998 QString QDS_Datum::format( const int num, const QString& id, const bool convert )
1000 Handle(DDS_DicItem) anItem;
1005 int aType = DDS_DicItem::Unknown;
1006 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1007 if ( !aDict.IsNull() )
1009 anItem = aDict->GetDicItem( toAsciiString( id ) );
1010 if ( !anItem.IsNull() )
1012 aType = anItem->GetType();
1013 aFormat = toQString( anItem->GetFormat( false ) );
1015 aNum = ( int )anItem->FromSI( aNum );
1019 return format( aFormat, aType, aNum );
1023 Format the specified double as data dictionary value.
1025 QString QDS_Datum::format( const double num, const QString& id, const bool convert )
1027 Handle(DDS_DicItem) anItem;
1032 int aType = DDS_DicItem::Unknown;
1033 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1034 if ( !aDict.IsNull() )
1036 anItem = aDict->GetDicItem( toAsciiString( id ) );
1037 if ( !anItem.IsNull() )
1039 aType = anItem->GetType();
1040 aFormat = toQString( anItem->GetFormat( false ) );
1042 aNum = anItem->FromSI( aNum );
1046 return format( aFormat, aType, aNum );
1050 Format the specified string as data dictionary value.
1052 QString QDS_Datum::format( const QString& str, const QString& id, const bool convert )
1054 Handle(DDS_DicItem) anItem;
1059 int aType = DDS_DicItem::Unknown;
1060 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1061 if ( !aDict.IsNull() )
1063 anItem = aDict->GetDicItem( toAsciiString( id ) );
1064 if ( !anItem.IsNull() )
1066 aType = anItem->GetType();
1067 aFormat = toQString( anItem->GetFormat( false ) );
1069 aStr = QString::number( anItem->FromSI( aStr.toDouble() ), 'f', 16 );
1073 return format( aFormat, aType, aStr );
1077 Format the given string accordance to data format.
1079 QString QDS_Datum::format( const QString& aFormat, const int aType, const int aValue )
1083 if ( !aFormat.isEmpty() )
1087 case DDS_DicItem::Float:
1088 txt = sprintf( aFormat, (double)aValue );
1089 txt = txt.stripWhiteSpace();
1091 case DDS_DicItem::Integer:
1092 txt = sprintf( aFormat, aValue );
1093 txt = txt.stripWhiteSpace();
1095 case DDS_DicItem::String:
1097 txt = sprintf( aFormat, aValue );
1102 txt = QString().setNum( aValue );
1108 Format the given string accordance to data format.
1110 QString QDS_Datum::format( const QString& aFormat, const int aType, const double aValue )
1114 if ( !aFormat.isEmpty() )
1118 case DDS_DicItem::Float:
1119 txt = QString().sprintf( aFormat, aValue );
1120 txt = txt.stripWhiteSpace();
1122 case DDS_DicItem::Integer:
1123 txt = QString().sprintf( aFormat, (int)aValue );
1124 txt = txt.stripWhiteSpace();
1126 case DDS_DicItem::String:
1128 txt = QString().sprintf( aFormat, aValue );
1133 txt = QString().setNum( aValue, 'g', 16 );
1139 Format the given string accordance to data format.
1141 QString QDS_Datum::format( const QString& aFormat, const int aType, const QString& aValue )
1143 QString txt = aValue;
1145 if ( aType != DDS_DicItem::String )
1146 txt = txt.stripWhiteSpace();
1148 if ( aFormat.isEmpty() || txt.isEmpty() )
1153 case DDS_DicItem::Float:
1154 txt = txt.replace( 'd', 'e' ).replace( 'D', 'E' );
1155 txt = sprintf( aFormat, txt.toDouble() );
1156 txt = txt.stripWhiteSpace();
1158 case DDS_DicItem::Integer:
1159 txt = sprintf( aFormat, txt.toInt() );
1160 txt = txt.stripWhiteSpace();
1162 case DDS_DicItem::String:
1163 txt = sprintf( aFormat, txt );
1171 Wrapper around the standard sprintf function.
1172 Process some non standard flags from format string.
1174 QString QDS_Datum::sprintf( const QString& fmt, const int val )
1176 return QString().sprintf( canonicalFormat( fmt ), val );
1180 Wrapper around the standard sprintf function.
1181 Process some non standard flags from format string.
1183 QString QDS_Datum::sprintf( const QString& fmt, const double val )
1185 return QString().sprintf( canonicalFormat( fmt ), val );
1189 Wrapper around the standard sprintf function.
1190 Process some non standard flags from format string.
1192 QString QDS_Datum::sprintf( const QString& fmt, const QString& val )
1195 QString aFormat = canonicalFormat( fmt, aFlags );
1199 QRegExp rx( "^(%[0-9]*.?[0-9]*s)$" );
1200 if ( aFormat.find( rx ) != -1 )
1202 // QString().sprintf() always expects string in UTF8 encoding, so we cannot use it here
1203 char* buf = new char[txt.length() + 1];
1204 ::sprintf( buf, aFormat.latin1(), (const char*)(txt.local8Bit()) );
1205 txt = QString::fromLocal8Bit( buf );
1210 if ( isDoubleFormat( aFormat ) )
1213 double aVal = txt.toDouble( &isOk );
1216 txt = sprintf( aFormat, aVal );
1217 txt = txt.replace( 'e', 'D' );
1221 if ( aFlags.contains( "u", false ) )
1223 if ( aFlags.contains( "l", false ) )
1230 Returns the canonical sprintf format.
1232 QString QDS_Datum::canonicalFormat( const QString& fmt )
1235 return canonicalFormat( fmt, flags );
1239 Returns the canonical sprintf format and non standard flags.
1241 QString QDS_Datum::canonicalFormat( const QString& fmt, QString& flags )
1243 QString newFmt = fmt;
1244 flags = QString::null;
1246 QRegExp rx( "^(%[0-9]*.?[0-9]*)([a-z,A-Z]+)[g|c|d|i|o|u|x|e|f|n|p|s|X|E|G]$" );
1247 if ( rx.search( newFmt ) >= 0 )
1249 flags = rx.cap( 2 );
1250 newFmt.remove( rx.pos( 2 ), flags.length() );
1256 Returns displayable units string for given DD ID
1258 QString QDS_Datum::units( const QString& id )
1261 Handle(DDS_DicItem) anItem;
1263 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1264 if ( !aDict.IsNull() )
1266 anItem = aDict->GetDicItem( toAsciiString( id ) );
1267 if ( !anItem.IsNull() )
1268 anUnit = unitsToText( toQString( anItem->GetUnits() ) );
1274 Get prefix string from format.
1276 QString QDS_Datum::prefix() const
1278 return QString::null;
1282 Get suffix string from format.
1284 QString QDS_Datum::suffix() const
1286 return QString::null;
1292 QString QDS_Datum::minValue() const
1294 QString pref = prefix();
1295 QString suff = suffix();
1297 QString aMin = minimumValue().stripWhiteSpace();
1299 if ( !pref.isEmpty() && aMin.left( pref.length() ) == pref )
1300 aMin = aMin.mid( pref.length() );
1302 if ( !suff.isEmpty() && aMin.right( suff.length() ) == suff )
1303 aMin = aMin.mid( 0, aMin.length() - suff.length() );
1311 QString QDS_Datum::maxValue() const
1313 QString pref = prefix();
1314 QString suff = suffix();
1316 QString aMax = maximumValue().stripWhiteSpace();
1318 if ( !pref.isEmpty() && aMax.left( pref.length() ) == pref )
1319 aMax = aMax.mid( pref.length() );
1321 if ( !suff.isEmpty() && aMax.right( suff.length() ) == suff )
1322 aMax = aMax.mid( 0, aMax.length() - suff.length() );
1328 Reset the numeric value cache.
1330 void QDS_Datum::invalidateCache()
1332 myTargetValue = QString::null;
1335 QString QDS_Datum::removeAccel( const QString& src )
1339 for ( uint i = 0; i < trg.length(); )
1341 if ( trg.mid( i, 2 ) == QString( "&&" ) )
1343 else if ( trg.at( i ) == '&' )
1351 bool QDS_Datum::isDoubleFormat( const QString& theFormat )
1353 if ( theFormat.length() > 0 )
1355 QChar c = theFormat[ (int)( theFormat.length() - 1 ) ];
1356 return c == 'f' || c == 'g' || c == 'e' || c == 'G' || c == 'E';
1362 int QDS_Datum::flags() const
1367 void QDS_Datum::initDatum() const
1369 if ( myInitialised )
1372 QDS_Datum* that = (QDS_Datum*)this;
1373 that->myInitialised = true;
1377 parent()->removeEventFilter( this );
1380 QDS_Datum::Wrapper* QDS_Datum::wrapper( QWidget* wid ) const
1386 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && !wrap; ++it )
1388 if ( it.data() && it.data()->widget() == wid )
1394 QDS_Datum::Wrapper* QDS_Datum::wrapper( const int id ) const
1397 if ( myWrapper.contains( id ) )
1398 wrap = myWrapper[id];
1402 int QDS_Datum::wrapperType( QDS_Datum::Wrapper* wrap ) const
1405 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && id == -1; ++it )
1407 if ( it.data() == wrap )