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 if ( aComp.IsEmpty() )
132 setDicItem( aDict->GetDicItem( anId ) );
134 setDicItem( aDict->GetDicItem( anId, aComp ) );
136 QTimer::singleShot( 0, this, SLOT( onInitDatum() ) );
139 parent->installEventFilter( this );
144 QDS_Datum::~QDS_Datum()
152 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
157 QString QDS_Datum::id() const
164 int QDS_Datum::type() const
168 int res = DDS_DicItem::Unknown;
169 if ( !myDicItem.IsNull() )
170 res = myDicItem->GetType();
174 QString QDS_Datum::label() const
179 if ( !myDicItem.IsNull() )
180 labStr = toQString( myDicItem->GetLabel() );
182 if ( flags() & NotAccel )
183 labStr = removeAccel( labStr );
188 QString QDS_Datum::units() const
193 if ( !myDicItem.IsNull() )
194 unitStr = toQString( myDicItem->GetUnits() );
198 QString QDS_Datum::filter() const
203 if ( !myDicItem.IsNull() )
204 fltr = toQString( myDicItem->GetFilter() );
208 QString QDS_Datum::format() const
213 if ( !myDicItem.IsNull() )
214 fmtStr = toQString( myDicItem->GetFormat( false ) );
218 QString QDS_Datum::defaultValue() const
222 QString pref = prefix();
223 QString suff = suffix();
226 if ( !myDicItem.IsNull() )
227 def = toQString( myDicItem->GetDefaultValue() );
229 QString aDef = def.stripWhiteSpace();
230 if ( !pref.isEmpty() && aDef.left( pref.length() ) == pref )
231 aDef = aDef.mid( pref.length() );
233 if ( !suff.isEmpty() && aDef.right( suff.length() ) == suff )
234 aDef = aDef.mid( 0, aDef.length() - suff.length() );
239 QString QDS_Datum::minimumValue() const
244 if ( !myDicItem.IsNull() )
245 min = format( format(), type(), myDicItem->GetMinValue() );
249 QString QDS_Datum::maximumValue() const
254 if ( !myDicItem.IsNull() )
255 max = format( format(), type(), myDicItem->GetMaxValue() );
259 QString QDS_Datum::longDescription() const
264 if ( !myDicItem.IsNull() )
265 ldStr = toQString( myDicItem->GetLongDescription() );
269 QString QDS_Datum::shortDescription() const
274 if ( !myDicItem.IsNull() )
275 sdStr = toQString( myDicItem->GetLongDescription() );
279 QString QDS_Datum::stringValue() const
283 if ( getString() == myTargetValue )
284 return mySourceValue;
289 double QDS_Datum::doubleValue() const
294 if ( !myTargetValue.isNull() && myTargetValue == getString() )
295 res = mySourceValue.toDouble();
298 res = getString().toDouble();
299 if ( !myDicItem.IsNull() )
300 res = myDicItem->ToSI( res );
306 int QDS_Datum::integerValue() const
311 if ( !myTargetValue.isNull() && myTargetValue == getString() )
312 res = mySourceValue.toInt();
315 double val = getString().toDouble();
316 if ( !myDicItem.IsNull() )
317 res = (int)myDicItem->ToSI( val );
323 QString QDS_Datum::text() const
327 QString aLabel = label();
328 QString aData = stringValue();
329 QString aUnits = units();
331 QString res = aLabel;
332 if ( !res.isEmpty() && !aData.isEmpty() )
333 res += QString( ": " );
336 if ( !aUnits.isEmpty() )
337 res += QString( " " ) + aUnits;
342 bool QDS_Datum::isEmpty() const
344 return stringValue().isEmpty();
347 void QDS_Datum::reset()
351 mySourceValue = defaultValue();
352 setString( format( ( myFlags & NotFormat ) ? (QString) "" : format(), type(), mySourceValue ) );
356 QString str = getString();
358 emit paramChanged( str );
361 void QDS_Datum::clear()
365 if ( !getString().isEmpty() )
368 setString( mySourceValue );
372 QString str = getString();
374 emit paramChanged( str );
378 void QDS_Datum::setStringValue( const QString& txt )
383 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), txt );
385 myTargetValue = aStr;
388 QString str = getString();
390 emit paramChanged( str );
393 void QDS_Datum::setDoubleValue( const double num )
397 mySourceValue = QString().setNum( num, 'g', 16 );
399 if ( !myDicItem.IsNull() )
400 val = myDicItem->FromSI( val );
402 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
404 myTargetValue = aStr;
407 QString str = getString();
409 emit paramChanged( str );
412 void QDS_Datum::setIntegerValue( const int num )
416 mySourceValue = QString().setNum( num );
418 if ( !myDicItem.IsNull() )
419 val = myDicItem->FromSI( val );
421 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
423 myTargetValue = aStr;
426 QString str = getString();
428 emit paramChanged( str );
432 Returns true if all subwidgets specified by 'element' enabled.
434 bool QDS_Datum::isEnabled( const int element ) const
439 if ( element & Label )
440 res = res && labelWidget() && labelWidget()->isEnabled();
441 if ( element & Units )
442 res = res && unitsWidget() && unitsWidget()->isEnabled();
443 if ( element & Control )
444 res = res && controlWidget() && controlWidget()->isEnabled();
449 Enable/Disable subwidgets specified by 'element'.
450 Values: Label, Control, Units or their combinations.
452 void QDS_Datum::setEnabled( const bool on, const int element )
456 if ( element & Label && labelWidget() )
457 labelWidget()->setEnabled( on );
458 if ( element & Units && unitsWidget() )
459 unitsWidget()->setEnabled( on );
460 if ( element & Control && controlWidget() )
461 controlWidget()->setEnabled( on );
465 Enable/Disable control.
467 void QDS_Datum::setEnabled( bool on )
469 setEnabled( on, Control );
472 void QDS_Datum::setShown( const bool visible, const int flags )
481 Show subwidgets specified by 'element'.
482 Values: Label, Control, Units or their combinations.
484 void QDS_Datum::show( const int element )
488 if ( ( element & Label ) && labelWidget() )
489 labelWidget()->show();
490 if ( ( element & Units ) && unitsWidget() )
491 unitsWidget()->show();
492 if ( ( element & Control ) && controlWidget() )
493 controlWidget()->show();
497 Hide subwidgets specified by 'element'.
498 Values: Label, Control, Units or their combinations.
500 void QDS_Datum::hide( const int element )
504 if ( ( element & Label ) && labelWidget() )
505 labelWidget()->hide();
506 if ( ( element & Units ) && unitsWidget() )
507 unitsWidget()->hide();
508 if ( ( element & Control ) && controlWidget() )
509 controlWidget()->hide();
513 Returns subwidget specified by 'element'.
514 Possible values: Label, Control, Units.
516 QWidget* QDS_Datum::widget( const int element ) const
519 return wrapper( element );
523 Set the input focus on the control widget.
525 void QDS_Datum::setFocus()
529 if ( controlWidget() )
530 controlWidget()->setFocus();
534 Returns true if control contains valid value otherwise returns false
535 and display warning message box if parameter msgBox is set.
537 bool QDS_Datum::isValid( const bool msgBox, const QString& extMsg, const QString& extLabel ) const
541 if ( type() == DDS_DicItem::String && isDoubleFormat( format() ) )
545 QString aStr = getString();
546 if ( aStr.isEmpty() )
549 aState = validate( aStr );
551 if ( msgBox && !aState )
554 if ( !label().isEmpty() )
555 info += tr( "DATA_INCORRECT_VALUE" ).arg( label() );
556 else if ( !extLabel.isEmpty() )
557 info += tr( "DATA_INCORRECT_VALUE" ).arg( extLabel );
562 case DDS_DicItem::String:
563 typeStr = tr( "DATA_STRING" );
565 case DDS_DicItem::Integer:
566 typeStr = tr( "DATA_INTEGER" );
568 case DDS_DicItem::Float:
569 typeStr = tr( "DATA_FLOAT" );
572 typeStr = tr( "DATA_NON_EMPTY" );
575 info += ( info.isEmpty() ? (QString) "" : QString( "\n" ) ) +
576 tr( "DATA_SHOULD_BE_VALUE" ).arg( typeStr );
578 if ( type() == DDS_DicItem::Float || type() == DDS_DicItem::Integer )
580 QString aMinValue = minValue();
581 QString aMaxValue = maxValue();
582 if ( !aMinValue.isEmpty() && !aMaxValue.isEmpty() )
583 limit = tr( "DATA_RANGE" ).arg( aMinValue ).arg( aMaxValue );
584 else if ( !aMinValue.isEmpty() )
585 limit = tr( "DATA_MIN_LIMIT" ).arg( aMinValue );
586 else if ( !aMaxValue.isEmpty() )
587 limit = tr( "DATA_MAX_LIMIT" ).arg( aMaxValue );
589 if ( !limit.isEmpty() )
592 info += QString( ".\n" ) + tr( "DATA_INPUT_VALUE" );
594 if ( !extMsg.isEmpty() )
595 info += QString( "\n" ) + extMsg;
598 for ( uint i = 0; i < info.length(); i++ )
599 if ( info.at( i ) == '\n' )
600 msg += QString( "<br>" );
604 info = QString( "<p><nobr>%1</nobr></p>" ).arg( msg );
606 QMessageBox::critical( controlWidget() ? controlWidget()->topLevelWidget() : 0,
607 tr( "DATA_ERR_TITLE" ), info, tr( "BUT_OK" ) );
608 if ( controlWidget() )
609 controlWidget()->setFocus();
615 Add widgets to the vertical layout.
617 void QDS_Datum::addTo( QVBoxLayout* l )
624 if ( wrapper( Label ) )
625 l->addWidget( wrapper( Label ) );
626 if ( wrapper( Control ) )
627 l->addWidget( wrapper( Control ) );
628 if ( wrapper( Units ) )
629 l->addWidget( wrapper( Units ) );
633 Add widgets to the horizaontal layout.
635 void QDS_Datum::addTo( QHBoxLayout* l )
642 if ( wrapper( Label ) )
643 l->addWidget( wrapper( Label ) );
644 if ( wrapper( Control ) )
645 l->addWidget( wrapper( Control ) );
646 if ( wrapper( Units ) )
647 l->addWidget( unitsWidget() );
651 Add widgets to the grid layout.
653 void QDS_Datum::addTo( QGridLayout* theLay, const int theRow, const int theCol, bool vertical )
662 if ( wrapper( Label ) )
664 theLay->addWidget( wrapper( Label ), row, col );
665 vertical ? row++ : col++;
667 if ( wrapper( Control ) )
669 theLay->addWidget( wrapper( Control ), row, col );
670 vertical ? row++ : col++;
672 if ( wrapper( Units ) )
673 theLay->addWidget( wrapper( Units ), row, col );
677 Set the aligment of Label or Units. For Control nothing happens.
679 void QDS_Datum::setAlignment( const int align, const int type )
683 if ( ( type & Label ) && labelWidget() )
684 labelWidget()->setAlignment( align );
685 if ( ( type & Units ) && unitsWidget() )
686 unitsWidget()->setAlignment( align );
689 bool QDS_Datum::eventFilter( QObject* o, QEvent* e )
693 if ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
694 ( e->type() == QEvent::ChildInserted && ((QChildEvent*)e)->child() == this ) )
697 return QObject::eventFilter( o, e );
701 Notify about parameter changing.
703 void QDS_Datum::onParamChanged()
708 Delayed initialization.
710 void QDS_Datum::onInitDatum()
716 Notify about subwidgets destroying. Allow to avoid repeated deleting in destructor.
718 void QDS_Datum::onDestroyed( QObject* obj )
720 myWrapper.remove( wrapperType( (Wrapper*)obj ) );
724 Returns QLabel instance which contains data dictionary label.
726 QLabel* QDS_Datum::labelWidget() const
733 Returns QLabel instance which contains data dictionary units.
735 QLabel* QDS_Datum::unitsWidget() const
742 Returns QWidget which contains user input data.
744 QWidget* QDS_Datum::controlWidget() const
751 Returns the dictionary item from the datum.
753 Handle(DDS_DicItem) QDS_Datum::dicItem() const
759 Set the dictionary item in to the datum.
761 void QDS_Datum::setDicItem( const Handle(DDS_DicItem)& item )
767 Creates QLabel widget for data label.
769 QLabel* QDS_Datum::createLabel( QWidget* parent )
771 return new QLabel( parent );
775 Creates QLabel widget for data units.
777 QLabel* QDS_Datum::createUnits( QWidget* parent )
779 return new QLabel( parent );
783 Returns validator accordance to data type.
785 QValidator* QDS_Datum::validator( const bool limits ) const
787 QValidator* aValidator = 0;
789 QString fltr = filter();
791 if ( type() == DDS_DicItem::String )
794 QString aFormat = canonicalFormat( format(), aFlags );
797 int pos = aFormat.find( "." );
800 QString numStr = aFormat.mid( pos + 1, aFormat.length() - pos - 2 );
802 int numVal = numStr.toInt( &ok );
807 QDS_StringValidator* aStrVal = new QDS_StringValidator( fltr, aFlags, (QObject*)this );
808 aStrVal->setLength( len );
810 aValidator = aStrVal;
812 else if ( type() == DDS_DicItem::Integer )
814 QDS_IntegerValidator* aIntVal = new QDS_IntegerValidator( fltr, (QObject*)this );
818 limit = minValue().toInt( &ok );
820 aIntVal->setBottom( limit );
821 limit = maxValue().toInt( &ok );
823 aIntVal->setTop( limit );
825 aValidator = aIntVal;
827 else if ( type() == DDS_DicItem::Float )
829 QDS_DoubleValidator* aFloatVal = new QDS_DoubleValidator( fltr, (QObject*)this );
833 limit = minValue().toDouble( &ok );
835 aFloatVal->setBottom( limit );
836 limit = maxValue().toDouble( &ok );
838 aFloatVal->setTop( limit );
840 aValidator = aFloatVal;
847 Checks the given string are valid or not.
849 bool QDS_Datum::validate( const QString& txt ) const
851 if ( type() == DDS_DicItem::Unknown ||
852 type() == DDS_DicItem::String && isDoubleFormat( format() ) )
855 QValidator* aValidator = validator( true );
862 bool res = aValidator->validate( str, pos ) == QValidator::Acceptable;
870 Retrieves information from data dictionary and create subwidgets using virtual mechanism.
871 Virtual mechanism doesn't work in constructor and destructor, therefore this method should
872 be called outside the constructor.
874 void QDS_Datum::initialize()
876 if ( wrapper( Label ) )
877 wrapper( Label )->setWidget( myLabel = createLabel( wrapper( Label ) ) );
878 if ( wrapper( Control ) )
879 wrapper( Control )->setWidget( myControl = createControl( wrapper( Control ) ) );
880 if ( wrapper( Units ) )
881 wrapper( Units )->setWidget( myUnits = createUnits( wrapper( Units ) ) );
883 TCollection_AsciiString comp;
884 Handle(DDS_DicItem) item = dicItem();
885 if ( !item.IsNull() )
886 comp = item->GetComponent();
889 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
891 unitSystem = toQString( comp.IsEmpty() ? dic->GetActiveUnitSystem() :
892 dic->GetActiveUnitSystem( comp ) );
894 unitSystemChanged( unitSystem );
896 QWidget* ctrl = controlWidget();
899 QString lDescr = longDescription();
900 QString sDescr = shortDescription();
901 if ( !sDescr.isEmpty() )
902 QToolTip::add( ctrl, sDescr );
903 if ( !lDescr.isEmpty() )
904 QWhatsThis::add( ctrl, lDescr );
907 if ( labelWidget() && ctrl && !( flags() & NotAccel ) )
908 labelWidget()->setBuddy( ctrl );
911 void QDS_Datum::unitSystemChanged( const QString& unitSystem )
913 QString labText = label();
914 QString unitText = unitsToText( units() );
916 if ( flags() & UnitsWithLabel )
918 if ( labText.isEmpty() )
920 else if ( !unitText.isEmpty() )
921 labText = QString( "%1 (%2)" ).arg( labText ).arg( unitText );
922 unitText = QString::null;
926 labelWidget()->setText( labText );
929 unitsWidget()->setText( unitText );
935 Covert units into text presentation.
937 QString QDS_Datum::unitsToText( const QString& uni )
940 QString aUnits = uni;
941 while ( ( pos = aUnits.find( "**" ) ) != -1 )
943 aUnits = aUnits.mid( 0, pos ) + QString( "<tt><font size=+2><sup>" ) +
944 aUnits.mid( pos + 2, 1 ) + QString( "</sup></font></tt>" ) +
945 aUnits.mid( pos + 3 );
951 Covert text presentation into internal units format.
953 QString QDS_Datum::textToUnits( const QString& txt )
956 QString aUnits = txt;
957 while ( ( pos = aUnits.find( "<sup>" ) ) != -1 )
959 aUnits.remove( pos, 5 );
960 aUnits.insert( pos, "**" );
962 while ( ( pos = aUnits.find( "</sup>" ) ) != -1 )
963 aUnits.remove( pos, 6 );
968 Format the specified integer as data dictionary value.
970 QString QDS_Datum::format( const int num, const QString& id, const bool convert )
972 Handle(DDS_DicItem) anItem;
977 int aType = DDS_DicItem::Unknown;
978 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
979 if ( !aDict.IsNull() )
981 anItem = aDict->GetDicItem( toAsciiString( id ) );
982 if ( !anItem.IsNull() )
984 aType = anItem->GetType();
985 aFormat = toQString( anItem->GetFormat( false ) );
987 aNum = ( int )anItem->FromSI( aNum );
991 return format( aFormat, aType, aNum );
995 Format the specified double as data dictionary value.
997 QString QDS_Datum::format( const double num, const QString& id, const bool convert )
999 Handle(DDS_DicItem) anItem;
1004 int aType = DDS_DicItem::Unknown;
1005 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1006 if ( !aDict.IsNull() )
1008 anItem = aDict->GetDicItem( toAsciiString( id ) );
1009 if ( !anItem.IsNull() )
1011 aType = anItem->GetType();
1012 aFormat = toQString( anItem->GetFormat( false ) );
1014 aNum = anItem->FromSI( aNum );
1018 return format( aFormat, aType, aNum );
1022 Format the specified string as data dictionary value.
1024 QString QDS_Datum::format( const QString& str, const QString& id, const bool convert )
1026 Handle(DDS_DicItem) anItem;
1031 int aType = DDS_DicItem::Unknown;
1032 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1033 if ( !aDict.IsNull() )
1035 anItem = aDict->GetDicItem( toAsciiString( id ) );
1036 if ( !anItem.IsNull() )
1038 aType = anItem->GetType();
1039 aFormat = toQString( anItem->GetFormat( false ) );
1041 aStr = QString::number( anItem->FromSI( aStr.toDouble() ), 'f', 16 );
1045 return format( aFormat, aType, aStr );
1049 Format the given string accordance to data format.
1051 QString QDS_Datum::format( const QString& aFormat, const int aType, const int aValue )
1055 if ( !aFormat.isEmpty() )
1059 case DDS_DicItem::Float:
1060 txt = sprintf( aFormat, (double)aValue );
1061 txt = txt.stripWhiteSpace();
1063 case DDS_DicItem::Integer:
1064 txt = sprintf( aFormat, aValue );
1065 txt = txt.stripWhiteSpace();
1067 case DDS_DicItem::String:
1069 txt = sprintf( aFormat, aValue );
1074 txt = QString().setNum( aValue );
1080 Format the given string accordance to data format.
1082 QString QDS_Datum::format( const QString& aFormat, const int aType, const double aValue )
1086 if ( !aFormat.isEmpty() )
1090 case DDS_DicItem::Float:
1091 txt = QString().sprintf( aFormat, aValue );
1092 txt = txt.stripWhiteSpace();
1094 case DDS_DicItem::Integer:
1095 txt = QString().sprintf( aFormat, (int)aValue );
1096 txt = txt.stripWhiteSpace();
1098 case DDS_DicItem::String:
1100 txt = QString().sprintf( aFormat, aValue );
1105 txt = QString().setNum( aValue, 'g', 16 );
1111 Format the given string accordance to data format.
1113 QString QDS_Datum::format( const QString& aFormat, const int aType, const QString& aValue )
1115 QString txt = aValue;
1117 if ( aType != DDS_DicItem::String )
1118 txt = txt.stripWhiteSpace();
1120 if ( aFormat.isEmpty() || txt.isEmpty() )
1125 case DDS_DicItem::Float:
1126 txt = txt.replace( 'd', 'e' ).replace( 'D', 'E' );
1127 txt = sprintf( aFormat, txt.toDouble() );
1128 txt = txt.stripWhiteSpace();
1130 case DDS_DicItem::Integer:
1131 txt = sprintf( aFormat, txt.toInt() );
1132 txt = txt.stripWhiteSpace();
1134 case DDS_DicItem::String:
1135 txt = sprintf( aFormat, txt );
1143 Wrapper around the standard sprintf function.
1144 Process some non standard flags from format string.
1146 QString QDS_Datum::sprintf( const QString& fmt, const int val )
1148 return QString().sprintf( canonicalFormat( fmt ), val );
1152 Wrapper around the standard sprintf function.
1153 Process some non standard flags from format string.
1155 QString QDS_Datum::sprintf( const QString& fmt, const double val )
1157 return QString().sprintf( canonicalFormat( fmt ), val );
1161 Wrapper around the standard sprintf function.
1162 Process some non standard flags from format string.
1164 QString QDS_Datum::sprintf( const QString& fmt, const QString& val )
1167 QString aFormat = canonicalFormat( fmt, aFlags );
1171 QRegExp rx( "^(%[0-9]*.?[0-9]*s)$" );
1172 if ( aFormat.find( rx ) != -1 )
1174 // QString().sprintf() always expects string in UTF8 encoding, so we cannot use it here
1175 char* buf = new char[txt.length() + 1];
1176 ::sprintf( buf, aFormat.latin1(), (const char*)(txt.local8Bit()) );
1177 txt = QString::fromLocal8Bit( buf );
1182 if ( isDoubleFormat( aFormat ) )
1185 double aVal = txt.toDouble( &isOk );
1188 txt = sprintf( aFormat, aVal );
1189 txt = txt.replace( 'e', 'D' );
1193 if ( aFlags.contains( "u", false ) )
1195 if ( aFlags.contains( "l", false ) )
1202 Returns the canonical sprintf format.
1204 QString QDS_Datum::canonicalFormat( const QString& fmt )
1207 return canonicalFormat( fmt, flags );
1211 Returns the canonical sprintf format and non standard flags.
1213 QString QDS_Datum::canonicalFormat( const QString& fmt, QString& flags )
1215 QString newFmt = fmt;
1216 flags = QString::null;
1218 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]$" );
1219 if ( rx.search( newFmt ) >= 0 )
1221 flags = rx.cap( 2 );
1222 newFmt.remove( rx.pos( 2 ), flags.length() );
1228 Returns displayable units string for given DD ID
1230 QString QDS_Datum::units( const QString& id )
1233 Handle(DDS_DicItem) anItem;
1235 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1236 if ( !aDict.IsNull() )
1238 anItem = aDict->GetDicItem( toAsciiString( id ) );
1239 if ( !anItem.IsNull() )
1240 anUnit = unitsToText( toQString( anItem->GetUnits() ) );
1246 Get prefix string from format.
1248 QString QDS_Datum::prefix() const
1250 return QString::null;
1254 Get suffix string from format.
1256 QString QDS_Datum::suffix() const
1258 return QString::null;
1264 QString QDS_Datum::minValue() const
1266 QString pref = prefix();
1267 QString suff = suffix();
1269 QString aMin = minimumValue().stripWhiteSpace();
1271 if ( !pref.isEmpty() && aMin.left( pref.length() ) == pref )
1272 aMin = aMin.mid( pref.length() );
1274 if ( !suff.isEmpty() && aMin.right( suff.length() ) == suff )
1275 aMin = aMin.mid( 0, aMin.length() - suff.length() );
1283 QString QDS_Datum::maxValue() const
1285 QString pref = prefix();
1286 QString suff = suffix();
1288 QString aMax = maximumValue().stripWhiteSpace();
1290 if ( !pref.isEmpty() && aMax.left( pref.length() ) == pref )
1291 aMax = aMax.mid( pref.length() );
1293 if ( !suff.isEmpty() && aMax.right( suff.length() ) == suff )
1294 aMax = aMax.mid( 0, aMax.length() - suff.length() );
1300 Reset the numeric value cache.
1302 void QDS_Datum::invalidateCache()
1304 myTargetValue = QString::null;
1307 QString QDS_Datum::removeAccel( const QString& src )
1311 for ( uint i = 0; i < trg.length(); )
1313 if ( trg.mid( i, 2 ) == QString( "&&" ) )
1315 else if ( trg.at( i ) == '&' )
1323 bool QDS_Datum::isDoubleFormat( const QString& theFormat )
1325 if ( theFormat.length() > 0 )
1327 QChar c = theFormat[ (int)( theFormat.length() - 1 ) ];
1328 return c == 'f' || c == 'g' || c == 'e' || c == 'G' || c == 'E';
1334 int QDS_Datum::flags() const
1339 void QDS_Datum::initDatum() const
1341 if ( myInitialised )
1344 QDS_Datum* that = (QDS_Datum*)this;
1345 that->myInitialised = true;
1349 parent()->removeEventFilter( this );
1352 QDS_Datum::Wrapper* QDS_Datum::wrapper( QWidget* wid ) const
1358 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && !wrap; ++it )
1360 if ( it.data() && it.data()->widget() == wid )
1366 QDS_Datum::Wrapper* QDS_Datum::wrapper( const int id ) const
1369 if ( myWrapper.contains( id ) )
1370 wrap = myWrapper[id];
1374 int QDS_Datum::wrapperType( QDS_Datum::Wrapper* wrap ) const
1377 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && id == -1; ++it )
1379 if ( it.data() == wrap )