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 );
48 QDS_Datum::Wrapper::~Wrapper()
52 QWidget* QDS_Datum::Wrapper::widget() const
57 void QDS_Datum::Wrapper::setWidget( QWidget* wid )
63 myWid->removeEventFilter( this );
70 if ( myWid->parent() != this )
71 myWid->reparent( this, QPoint( 0, 0 ) );
73 setFocusProxy( myWid );
75 myWid->updateGeometry();
78 myWid->installEventFilter( this );
81 bool QDS_Datum::Wrapper::eventFilter( QObject* o, QEvent* e )
83 if ( e->type() == QEvent::Resize && o == widget() )
85 QResizeEvent* re = (QResizeEvent*)e;
86 if ( re->size() != size() )
89 return QWidget::eventFilter( o, e );
92 void QDS_Datum::Wrapper::resizeEvent( QResizeEvent* e )
94 QWidget::resizeEvent( e );
96 if ( widget() && widget()->size() != size() )
97 widget()->resize( size() );
102 Descr: Base class for control used data dictionary. [public]
105 QDS_Datum::QDS_Datum( const QString& id, QWidget* parent, const int flags, const QString& comp )
112 myInitialised( false )
114 if ( myFlags & Label )
115 myWrapper.insert( Label, new Wrapper( parent ) );
116 if ( myFlags & Control )
117 myWrapper.insert( Control, new Wrapper( parent ) );
118 if ( myFlags & Units )
119 myWrapper.insert( Units, new Wrapper( parent ) );
121 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
122 connect( it.data(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
124 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
125 if ( aDict.IsNull() )
128 TCollection_AsciiString anId = toAsciiString( id );
129 TCollection_AsciiString aComp = toAsciiString( comp );
131 setDicItem( aDict->GetDicItem( anId, aComp ) );
133 QTimer::singleShot( 0, this, SLOT( onInitDatum() ) );
136 parent->installEventFilter( this );
141 QDS_Datum::~QDS_Datum()
149 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
154 QString QDS_Datum::id() const
161 int QDS_Datum::type() const
165 int res = DDS_DicItem::Unknown;
166 if ( !myDicItem.IsNull() )
167 res = myDicItem->GetType();
171 QString QDS_Datum::label() const
176 if ( !myDicItem.IsNull() )
177 labStr = toQString( myDicItem->GetLabel() );
179 if ( flags() & NotAccel )
180 labStr = removeAccel( labStr );
185 QString QDS_Datum::units() const
190 if ( !myDicItem.IsNull() )
191 unitStr = toQString( myDicItem->GetUnits() );
195 QString QDS_Datum::filter() const
200 if ( !myDicItem.IsNull() )
201 fltr = toQString( myDicItem->GetFilter() );
205 QString QDS_Datum::format() const
210 if ( !myDicItem.IsNull() )
211 fmtStr = toQString( myDicItem->GetFormat( false ) );
215 QString QDS_Datum::defaultValue() const
219 QString pref = prefix();
220 QString suff = suffix();
223 if ( !myDicItem.IsNull() )
224 def = toQString( myDicItem->GetDefaultValue() );
226 QString aDef = def.stripWhiteSpace();
227 if ( !pref.isEmpty() && aDef.left( pref.length() ) == pref )
228 aDef = aDef.mid( pref.length() );
230 if ( !suff.isEmpty() && aDef.right( suff.length() ) == suff )
231 aDef = aDef.mid( 0, aDef.length() - suff.length() );
236 QString QDS_Datum::minimumValue() const
241 if ( !myDicItem.IsNull() )
242 min = format( format(), type(), myDicItem->GetMinValue() );
246 QString QDS_Datum::maximumValue() const
251 if ( !myDicItem.IsNull() )
252 max = format( format(), type(), myDicItem->GetMaxValue() );
256 QString QDS_Datum::longDescription() const
261 if ( !myDicItem.IsNull() )
262 ldStr = toQString( myDicItem->GetLongDescription() );
266 QString QDS_Datum::shortDescription() const
271 if ( !myDicItem.IsNull() )
272 sdStr = toQString( myDicItem->GetLongDescription() );
276 QString QDS_Datum::stringValue() const
280 if ( getString() == myTargetValue )
281 return mySourceValue;
286 double QDS_Datum::doubleValue() const
291 if ( !myTargetValue.isNull() && myTargetValue == getString() )
292 res = mySourceValue.toDouble();
295 res = getString().toDouble();
296 if ( !myDicItem.IsNull() )
297 res = myDicItem->ToSI( res );
303 int QDS_Datum::integerValue() const
308 if ( !myTargetValue.isNull() && myTargetValue == getString() )
309 res = mySourceValue.toInt();
312 double val = getString().toDouble();
313 if ( !myDicItem.IsNull() )
314 res = (int)myDicItem->ToSI( val );
320 QString QDS_Datum::text() const
324 QString aLabel = label();
325 QString aData = stringValue();
326 QString aUnits = units();
328 QString res = aLabel;
329 if ( !res.isEmpty() && !aData.isEmpty() )
330 res += QString( ": " );
333 if ( !aUnits.isEmpty() )
334 res += QString( " " ) + aUnits;
339 bool QDS_Datum::isEmpty() const
341 return stringValue().isEmpty();
344 void QDS_Datum::reset()
348 mySourceValue = defaultValue();
349 setString( format( ( myFlags & NotFormat ) ? (QString) "" : format(), type(), mySourceValue ) );
353 QString str = getString();
355 emit paramChanged( str );
358 void QDS_Datum::clear()
362 if ( !getString().isEmpty() )
365 setString( mySourceValue );
369 QString str = getString();
371 emit paramChanged( str );
375 void QDS_Datum::setStringValue( const QString& txt )
380 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), txt );
382 myTargetValue = aStr;
385 QString str = getString();
387 emit paramChanged( str );
390 void QDS_Datum::setDoubleValue( const double num )
394 mySourceValue = QString().setNum( num, 'g', 16 );
396 if ( !myDicItem.IsNull() )
397 val = myDicItem->FromSI( val );
399 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
401 myTargetValue = aStr;
404 QString str = getString();
406 emit paramChanged( str );
409 void QDS_Datum::setIntegerValue( const int num )
413 mySourceValue = QString().setNum( num );
415 if ( !myDicItem.IsNull() )
416 val = myDicItem->FromSI( val );
418 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
420 myTargetValue = aStr;
423 QString str = getString();
425 emit paramChanged( str );
429 Returns true if all subwidgets specified by 'element' enabled.
431 bool QDS_Datum::isEnabled( const int element ) const
436 if ( element & Label )
437 res = res && labelWidget() && labelWidget()->isEnabled();
438 if ( element & Units )
439 res = res && unitsWidget() && unitsWidget()->isEnabled();
440 if ( element & Control )
441 res = res && controlWidget() && controlWidget()->isEnabled();
446 Enable/Disable subwidgets specified by 'element'.
447 Values: Label, Control, Units or their combinations.
449 void QDS_Datum::setEnabled( const bool on, const int element )
453 if ( element & Label && labelWidget() )
454 labelWidget()->setEnabled( on );
455 if ( element & Units && unitsWidget() )
456 unitsWidget()->setEnabled( on );
457 if ( element & Control && controlWidget() )
458 controlWidget()->setEnabled( on );
462 Enable/Disable control.
464 void QDS_Datum::setEnabled( bool on )
466 setEnabled( on, Control );
469 void QDS_Datum::setShown( const bool visible, const int flags )
478 Show subwidgets specified by 'element'.
479 Values: Label, Control, Units or their combinations.
481 void QDS_Datum::show( const int element )
485 if ( ( element & Label ) && labelWidget() )
486 labelWidget()->show();
487 if ( ( element & Units ) && unitsWidget() )
488 unitsWidget()->show();
489 if ( ( element & Control ) && controlWidget() )
490 controlWidget()->show();
494 Hide subwidgets specified by 'element'.
495 Values: Label, Control, Units or their combinations.
497 void QDS_Datum::hide( const int element )
501 if ( ( element & Label ) && labelWidget() )
502 labelWidget()->hide();
503 if ( ( element & Units ) && unitsWidget() )
504 unitsWidget()->hide();
505 if ( ( element & Control ) && controlWidget() )
506 controlWidget()->hide();
510 Returns subwidget specified by 'element'.
511 Possible values: Label, Control, Units.
513 QWidget* QDS_Datum::widget( const int element ) const
516 return wrapper( element );
520 Set the input focus on the control widget.
522 void QDS_Datum::setFocus()
526 if ( controlWidget() )
527 controlWidget()->setFocus();
531 Returns true if control contains valid value otherwise returns false
532 and display warning message box if parameter msgBox is set.
534 bool QDS_Datum::isValid( const bool msgBox, const QString& extMsg, const QString& extLabel ) const
538 if ( type() == DDS_DicItem::String && isDoubleFormat( format() ) )
542 QString aStr = getString();
543 if ( aStr.isEmpty() )
546 aState = validate( aStr );
548 if ( msgBox && !aState )
551 if ( !label().isEmpty() )
552 info += tr( "DATA_INCORRECT_VALUE" ).arg( label() );
553 else if ( !extLabel.isEmpty() )
554 info += tr( "DATA_INCORRECT_VALUE" ).arg( extLabel );
559 case DDS_DicItem::String:
560 typeStr = tr( "DATA_STRING" );
562 case DDS_DicItem::Integer:
563 typeStr = tr( "DATA_INTEGER" );
565 case DDS_DicItem::Float:
566 typeStr = tr( "DATA_FLOAT" );
569 typeStr = tr( "DATA_NON_EMPTY" );
572 info += ( info.isEmpty() ? (QString) "" : QString( "\n" ) ) +
573 tr( "DATA_SHOULD_BE_VALUE" ).arg( typeStr );
575 if ( type() == DDS_DicItem::Float || type() == DDS_DicItem::Integer )
577 QString aMinValue = minValue();
578 QString aMaxValue = maxValue();
579 if ( !aMinValue.isEmpty() && !aMaxValue.isEmpty() )
580 limit = tr( "DATA_RANGE" ).arg( aMinValue ).arg( aMaxValue );
581 else if ( !aMinValue.isEmpty() )
582 limit = tr( "DATA_MIN_LIMIT" ).arg( aMinValue );
583 else if ( !aMaxValue.isEmpty() )
584 limit = tr( "DATA_MAX_LIMIT" ).arg( aMaxValue );
586 if ( !limit.isEmpty() )
589 info += QString( ".\n" ) + tr( "DATA_INPUT_VALUE" );
591 if ( !extMsg.isEmpty() )
592 info += QString( "\n" ) + extMsg;
595 for ( uint i = 0; i < info.length(); i++ )
596 if ( info.at( i ) == '\n' )
597 msg += QString( "<br>" );
601 info = QString( "<p><nobr>%1</nobr></p>" ).arg( msg );
603 QMessageBox::critical( controlWidget() ? controlWidget()->topLevelWidget() : 0,
604 tr( "DATA_ERR_TITLE" ), info, tr( "BUT_OK" ) );
605 if ( controlWidget() )
606 controlWidget()->setFocus();
612 Add widgets to the vertical layout.
614 void QDS_Datum::addTo( QVBoxLayout* l )
621 if ( wrapper( Label ) )
622 l->addWidget( wrapper( Label ) );
623 if ( wrapper( Control ) )
624 l->addWidget( wrapper( Control ) );
625 if ( wrapper( Units ) )
626 l->addWidget( wrapper( Units ) );
630 Add widgets to the horizaontal layout.
632 void QDS_Datum::addTo( QHBoxLayout* l )
639 if ( wrapper( Label ) )
640 l->addWidget( wrapper( Label ) );
641 if ( wrapper( Control ) )
642 l->addWidget( wrapper( Control ) );
643 if ( wrapper( Units ) )
644 l->addWidget( unitsWidget() );
648 Add widgets to the grid layout.
650 void QDS_Datum::addTo( QGridLayout* theLay, const int theRow, const int theCol, bool vertical )
659 if ( wrapper( Label ) )
661 theLay->addWidget( wrapper( Label ), row, col );
662 vertical ? row++ : col++;
664 if ( wrapper( Control ) )
666 theLay->addWidget( wrapper( Control ), row, col );
667 vertical ? row++ : col++;
669 if ( wrapper( Units ) )
670 theLay->addWidget( wrapper( Units ), row, col );
674 Set the aligment of Label or Units. For Control nothing happens.
676 void QDS_Datum::setAlignment( const int align, const int type )
680 if ( ( type & Label ) && labelWidget() )
681 labelWidget()->setAlignment( align );
682 if ( ( type & Units ) && unitsWidget() )
683 unitsWidget()->setAlignment( align );
686 bool QDS_Datum::eventFilter( QObject* o, QEvent* e )
690 if ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
691 ( e->type() == QEvent::ChildInserted && ((QChildEvent*)e)->child() == this ) )
694 return QObject::eventFilter( o, e );
698 Notify about parameter changing.
700 void QDS_Datum::onParamChanged()
705 Delayed initialization.
707 void QDS_Datum::onInitDatum()
713 Notify about subwidgets destroying. Allow to avoid repeated deleting in destructor.
715 void QDS_Datum::onDestroyed( QObject* obj )
717 myWrapper.remove( wrapperType( (Wrapper*)obj ) );
721 Returns QLabel instance which contains data dictionary label.
723 QLabel* QDS_Datum::labelWidget() const
730 Returns QLabel instance which contains data dictionary units.
732 QLabel* QDS_Datum::unitsWidget() const
739 Returns QWidget which contains user input data.
741 QWidget* QDS_Datum::controlWidget() const
748 Returns the dictionary item from the datum.
750 Handle(DDS_DicItem) QDS_Datum::dicItem() const
756 Set the dictionary item in to the datum.
758 void QDS_Datum::setDicItem( Handle(DDS_DicItem)& item )
764 Creates QLabel widget for data label.
766 QLabel* QDS_Datum::createLabel( QWidget* parent )
768 return new QLabel( parent );
772 Creates QLabel widget for data units.
774 QLabel* QDS_Datum::createUnits( QWidget* parent )
776 return new QLabel( parent );
780 Returns validator accordance to data type.
782 QValidator* QDS_Datum::validator( const bool limits ) const
784 QValidator* aValidator = 0;
786 QString fltr = filter();
788 if ( type() == DDS_DicItem::String )
791 QString aFormat = canonicalFormat( format(), aFlags );
794 int pos = aFormat.find( "." );
797 QString numStr = aFormat.mid( pos + 1, aFormat.length() - pos - 2 );
799 int numVal = numStr.toInt( &ok );
804 QDS_StringValidator* aStrVal = new QDS_StringValidator( fltr, aFlags, (QObject*)this );
805 aStrVal->setLength( len );
807 aValidator = aStrVal;
809 else if ( type() == DDS_DicItem::Integer )
811 QDS_IntegerValidator* aIntVal = new QDS_IntegerValidator( fltr, (QObject*)this );
815 limit = minValue().toInt( &ok );
817 aIntVal->setBottom( limit );
818 limit = maxValue().toInt( &ok );
820 aIntVal->setTop( limit );
822 aValidator = aIntVal;
824 else if ( type() == DDS_DicItem::Float )
826 QDS_DoubleValidator* aFloatVal = new QDS_DoubleValidator( fltr, (QObject*)this );
830 limit = minValue().toDouble( &ok );
832 aFloatVal->setBottom( limit );
833 limit = maxValue().toDouble( &ok );
835 aFloatVal->setTop( limit );
837 aValidator = aFloatVal;
844 Checks the given string are valid or not.
846 bool QDS_Datum::validate( const QString& txt ) const
848 if ( type() == DDS_DicItem::Unknown ||
849 type() == DDS_DicItem::String && isDoubleFormat( format() ) )
852 QValidator* aValidator = validator( true );
859 bool res = aValidator->validate( str, pos ) == QValidator::Acceptable;
867 Retrieves information from data dictionary and create subwidgets using virtual mechanism.
868 Virtual mechanism doesn't work in constructor and destructor, therefore this method should
869 be called outside the constructor.
871 void QDS_Datum::initialize()
873 if ( wrapper( Label ) )
874 wrapper( Label )->setWidget( myLabel = createLabel( wrapper( Label ) ) );
875 if ( wrapper( Control ) )
876 wrapper( Control )->setWidget( myControl = createControl( wrapper( Control ) ) );
877 if ( wrapper( Units ) )
878 wrapper( Units )->setWidget( myUnits = createUnits( wrapper( Units ) ) );
880 TCollection_AsciiString comp;
881 Handle(DDS_DicItem) item = dicItem();
882 if ( !item.IsNull() )
883 comp = item->GetComponent();
886 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
888 unitSystem = toQString( comp.IsEmpty() ? dic->GetActiveUnitSystem() :
889 dic->GetActiveUnitSystem( comp ) );
891 unitSystemChanged( unitSystem );
893 QWidget* ctrl = controlWidget();
896 QString lDescr = longDescription();
897 QString sDescr = shortDescription();
898 if ( !sDescr.isEmpty() )
899 QToolTip::add( ctrl, sDescr );
900 if ( !lDescr.isEmpty() )
901 QWhatsThis::add( ctrl, lDescr );
904 if ( labelWidget() && ctrl && !( flags() & NotAccel ) )
905 labelWidget()->setBuddy( ctrl );
908 void QDS_Datum::unitSystemChanged( const QString& unitSystem )
910 QString labText = label();
911 QString unitText = unitsToText( units() );
913 if ( flags() & UnitsWithLabel )
915 if ( labText.isEmpty() )
917 else if ( !unitText.isEmpty() )
918 labText = QString( "%1 (%2)" ).arg( labText ).arg( unitText );
919 unitText = QString::null;
923 labelWidget()->setText( labText );
927 unitsWidget()->setText( unitText );
933 Covert units into text presentation.
935 QString QDS_Datum::unitsToText( const QString& uni )
938 QString aUnits = uni;
939 while ( ( pos = aUnits.find( "**" ) ) != -1 )
941 aUnits = aUnits.mid( 0, pos ) + QString( "<tt><font size=+2><sup>" ) +
942 aUnits.mid( pos + 2, 1 ) + QString( "</sup></font></tt>" ) +
943 aUnits.mid( pos + 3 );
949 Covert text presentation into internal units format.
951 QString QDS_Datum::textToUnits( const QString& txt )
954 QString aUnits = txt;
955 while ( ( pos = aUnits.find( "<sup>" ) ) != -1 )
957 aUnits.remove( pos, 5 );
958 aUnits.insert( pos, "**" );
960 while ( ( pos = aUnits.find( "</sup>" ) ) != -1 )
961 aUnits.remove( pos, 6 );
966 Format the specified integer as data dictionary value.
968 QString QDS_Datum::format( const int num, const QString& id, const bool convert )
970 Handle(DDS_DicItem) anItem;
975 int aType = DDS_DicItem::Unknown;
976 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
977 if ( !aDict.IsNull() )
979 anItem = aDict->GetDicItem( toAsciiString( id ) );
980 if ( !anItem.IsNull() )
982 aType = anItem->GetType();
983 aFormat = toQString( anItem->GetFormat( false ) );
985 aNum = anItem->FromSI( aNum );
989 return format( aFormat, aType, aNum );
993 Format the specified double as data dictionary value.
995 QString QDS_Datum::format( const double num, const QString& id, const bool convert )
997 Handle(DDS_DicItem) anItem;
1002 int aType = DDS_DicItem::Unknown;
1003 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1004 if ( !aDict.IsNull() )
1006 anItem = aDict->GetDicItem( toAsciiString( id ) );
1007 if ( !anItem.IsNull() )
1009 aType = anItem->GetType();
1010 aFormat = toQString( anItem->GetFormat( false ) );
1012 aNum = anItem->FromSI( aNum );
1016 return format( aFormat, aType, aNum );
1020 Format the specified string as data dictionary value.
1022 QString QDS_Datum::format( const QString& str, const QString& id, const bool convert )
1024 Handle(DDS_DicItem) anItem;
1029 int aType = DDS_DicItem::Unknown;
1030 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1031 if ( !aDict.IsNull() )
1033 anItem = aDict->GetDicItem( toAsciiString( id ) );
1034 if ( !anItem.IsNull() )
1036 aType = anItem->GetType();
1037 aFormat = toQString( anItem->GetFormat( false ) );
1039 aStr = QString::number( anItem->FromSI( aStr.toDouble() ), 'f', 16 );
1043 return format( aFormat, aType, aStr );
1047 Format the given string accordance to data format.
1049 QString QDS_Datum::format( const QString& aFormat, const int aType, const int aValue )
1053 if ( !aFormat.isEmpty() )
1057 case DDS_DicItem::Float:
1058 txt = sprintf( aFormat, (double)aValue );
1059 txt = txt.stripWhiteSpace();
1061 case DDS_DicItem::Integer:
1062 txt = sprintf( aFormat, aValue );
1063 txt = txt.stripWhiteSpace();
1065 case DDS_DicItem::String:
1067 txt = sprintf( aFormat, aValue );
1072 txt = QString().setNum( aValue );
1078 Format the given string accordance to data format.
1080 QString QDS_Datum::format( const QString& aFormat, const int aType, const double aValue )
1084 if ( !aFormat.isEmpty() )
1088 case DDS_DicItem::Float:
1089 txt = QString().sprintf( aFormat, aValue );
1090 txt = txt.stripWhiteSpace();
1092 case DDS_DicItem::Integer:
1093 txt = QString().sprintf( aFormat, (int)aValue );
1094 txt = txt.stripWhiteSpace();
1096 case DDS_DicItem::String:
1098 txt = QString().sprintf( aFormat, aValue );
1103 txt = QString().setNum( aValue, 'g', 16 );
1109 Format the given string accordance to data format.
1111 QString QDS_Datum::format( const QString& aFormat, const int aType, const QString& aValue )
1113 QString txt = aValue;
1115 if ( aType != DDS_DicItem::String )
1116 txt = txt.stripWhiteSpace();
1118 if ( aFormat.isEmpty() || txt.isEmpty() )
1123 case DDS_DicItem::Float:
1124 txt = txt.replace( 'd', 'e' ).replace( 'D', 'E' );
1125 txt = sprintf( aFormat, txt.toDouble() );
1126 txt = txt.stripWhiteSpace();
1128 case DDS_DicItem::Integer:
1129 txt = sprintf( aFormat, txt.toInt() );
1130 txt = txt.stripWhiteSpace();
1132 case DDS_DicItem::String:
1133 txt = sprintf( aFormat, txt );
1141 Wrapper around the standard sprintf function.
1142 Process some non standard flags from format string.
1144 QString QDS_Datum::sprintf( const QString& fmt, const int val )
1146 return QString().sprintf( canonicalFormat( fmt ), val );
1150 Wrapper around the standard sprintf function.
1151 Process some non standard flags from format string.
1153 QString QDS_Datum::sprintf( const QString& fmt, const double val )
1155 return QString().sprintf( canonicalFormat( fmt ), val );
1159 Wrapper around the standard sprintf function.
1160 Process some non standard flags from format string.
1162 QString QDS_Datum::sprintf( const QString& fmt, const QString& val )
1165 QString aFormat = canonicalFormat( fmt, aFlags );
1169 QRegExp rx( "^(%[0-9]*.?[0-9]*s)$" );
1170 if ( aFormat.find( rx ) != -1 )
1172 // QString().sprintf() always expects string in UTF8 encoding, so we cannot use it here
1173 char* buf = new char[txt.length() + 1];
1174 ::sprintf( buf, aFormat.latin1(), (const char*)(txt.local8Bit()) );
1175 txt = QString::fromLocal8Bit( buf );
1180 if ( isDoubleFormat( aFormat ) )
1183 double aVal = txt.toDouble( &isOk );
1186 txt = sprintf( aFormat, aVal );
1187 txt = txt.replace( 'e', 'D' );
1191 if ( aFlags.contains( "u", false ) )
1193 if ( aFlags.contains( "l", false ) )
1200 Returns the canonical sprintf format.
1202 QString QDS_Datum::canonicalFormat( const QString& fmt )
1205 return canonicalFormat( fmt, flags );
1209 Returns the canonical sprintf format and non standard flags.
1211 QString QDS_Datum::canonicalFormat( const QString& fmt, QString& flags )
1213 QString newFmt = fmt;
1214 flags = QString::null;
1216 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]$" );
1217 if ( rx.search( newFmt ) >= 0 )
1219 flags = rx.cap( 2 );
1220 newFmt.remove( rx.pos( 2 ), flags.length() );
1226 Returns displayable units string for given DD ID
1228 QString QDS_Datum::units( const QString& id )
1231 Handle(DDS_DicItem) anItem;
1233 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1234 if ( !aDict.IsNull() )
1236 anItem = aDict->GetDicItem( toAsciiString( id ) );
1237 if ( !anItem.IsNull() )
1238 anUnit = unitsToText( toQString( anItem->GetUnits() ) );
1244 Get prefix string from format.
1246 QString QDS_Datum::prefix() const
1248 return QString::null;
1252 Get suffix string from format.
1254 QString QDS_Datum::suffix() const
1256 return QString::null;
1262 QString QDS_Datum::minValue() const
1264 QString pref = prefix();
1265 QString suff = suffix();
1267 QString aMin = minimumValue().stripWhiteSpace();
1269 if ( !pref.isEmpty() && aMin.left( pref.length() ) == pref )
1270 aMin = aMin.mid( pref.length() );
1272 if ( !suff.isEmpty() && aMin.right( suff.length() ) == suff )
1273 aMin = aMin.mid( 0, aMin.length() - suff.length() );
1281 QString QDS_Datum::maxValue() const
1283 QString pref = prefix();
1284 QString suff = suffix();
1286 QString aMax = maximumValue().stripWhiteSpace();
1288 if ( !pref.isEmpty() && aMax.left( pref.length() ) == pref )
1289 aMax = aMax.mid( pref.length() );
1291 if ( !suff.isEmpty() && aMax.right( suff.length() ) == suff )
1292 aMax = aMax.mid( 0, aMax.length() - suff.length() );
1298 Reset the numeric value cache.
1300 void QDS_Datum::invalidateCache()
1302 myTargetValue = QString::null;
1305 QString QDS_Datum::removeAccel( const QString& src )
1309 for ( uint i = 0; i < trg.length(); )
1311 if ( trg.mid( i, 2 ) == QString( "&&" ) )
1313 else if ( trg.at( i ) == '&' )
1321 bool QDS_Datum::isDoubleFormat( const QString& theFormat )
1323 if ( theFormat.length() > 0 )
1325 QChar c = theFormat[ (int)( theFormat.length() - 1 ) ];
1326 return c == 'f' || c == 'g' || c == 'e' || c == 'G' || c == 'E';
1332 int QDS_Datum::flags() const
1337 void QDS_Datum::initDatum() const
1339 if ( myInitialised )
1342 QDS_Datum* that = (QDS_Datum*)this;
1343 that->myInitialised = true;
1347 parent()->removeEventFilter( this );
1350 QDS_Datum::Wrapper* QDS_Datum::wrapper( QWidget* wid ) const
1356 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && !wrap; ++it )
1358 if ( it.data() && it.data()->widget() == wid )
1364 QDS_Datum::Wrapper* QDS_Datum::wrapper( const int id ) const
1367 if ( myWrapper.contains( id ) )
1368 wrap = myWrapper[id];
1372 int QDS_Datum::wrapperType( QDS_Datum::Wrapper* wrap ) const
1375 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && id == -1; ++it )
1377 if ( it.data() == wrap )