1 // Copyright (C) 2005 CEA/DEN, EDF R&D, OPEN CASCADE, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "QDS_Datum.h"
21 #include "QDS_Validator.h"
23 #include <DDS_Dictionary.h>
30 #include <qwhatsthis.h>
31 #include <qvalidator.h>
32 #include <qmessagebox.h>
35 Class: QDS_Datum::Wrapper
36 Descr: Wrapper widget for sub widgets. [internal]
39 class QDS_Datum::Wrapper : public QWidget
42 Wrapper( QWidget* = 0 );
45 QWidget* widget() const;
46 void setWidget( QWidget* );
48 virtual bool eventFilter( QObject*, QEvent* );
51 virtual void resizeEvent( QResizeEvent* );
58 QDS_Datum::Wrapper::Wrapper( QWidget* parent )
62 QHBoxLayout* base = new QHBoxLayout( this );
63 base->setAutoAdd( true );
64 setFocusPolicy( StrongFocus );
67 QDS_Datum::Wrapper::~Wrapper()
71 QWidget* QDS_Datum::Wrapper::widget() const
76 void QDS_Datum::Wrapper::setWidget( QWidget* wid )
82 myWid->removeEventFilter( this );
89 if ( myWid->parent() != this )
90 myWid->reparent( this, QPoint( 0, 0 ) );
92 setTabOrder( this, myWid );
93 setFocusProxy( myWid );
95 myWid->updateGeometry();
98 myWid->installEventFilter( this );
101 bool QDS_Datum::Wrapper::eventFilter( QObject* o, QEvent* e )
103 if ( e->type() == QEvent::Resize && o == widget() )
105 QResizeEvent* re = (QResizeEvent*)e;
106 if ( re->size() != size() )
107 resize( re->size() );
109 return QWidget::eventFilter( o, e );
112 void QDS_Datum::Wrapper::resizeEvent( QResizeEvent* e )
114 QWidget::resizeEvent( e );
116 if ( widget() && widget()->size() != size() )
117 widget()->resize( size() );
122 Descr: Base class for control used data dictionary. [public]
125 QDS_Datum::QDS_Datum( const QString& id, QWidget* parent, const int flags, const QString& comp )
132 myInitialised( false )
134 if ( myFlags & Label )
135 myWrapper.insert( Label, new Wrapper( parent ) );
136 if ( myFlags & Control )
137 myWrapper.insert( Control, new Wrapper( parent ) );
138 if ( myFlags & Units )
139 myWrapper.insert( Units, new Wrapper( parent ) );
141 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
142 connect( it.data(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
144 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
145 if ( aDict.IsNull() )
148 TCollection_AsciiString anId = toAsciiString( id );
149 TCollection_AsciiString aComp = toAsciiString( comp );
151 if ( aComp.IsEmpty() )
152 setDicItem( aDict->GetDicItem( anId ) );
154 setDicItem( aDict->GetDicItem( anId, aComp ) );
156 QTimer::singleShot( 0, this, SLOT( onInitDatum() ) );
159 parent->installEventFilter( this );
164 QDS_Datum::~QDS_Datum()
172 for ( QMap<int, Wrapper*>::Iterator it = myWrapper.begin(); it != myWrapper.end(); ++it )
177 QDS_Datum::operator QWidget*() const
179 return widget( Control );
182 QString QDS_Datum::id() const
189 int QDS_Datum::type() const
193 int res = DDS_DicItem::Unknown;
194 if ( !myDicItem.IsNull() )
195 res = myDicItem->GetType();
199 QString QDS_Datum::label() const
204 if ( !myDicItem.IsNull() )
205 labStr = toQString( myDicItem->GetLabel() );
207 if ( flags() & NotAccel )
208 labStr = removeAccel( labStr );
213 QString QDS_Datum::units() const
218 if ( !myDicItem.IsNull() )
219 unitStr = toQString( myDicItem->GetUnits() );
223 QString QDS_Datum::filter() const
228 if ( !myDicItem.IsNull() )
229 fltr = toQString( myDicItem->GetFilter() );
233 QString QDS_Datum::format() const
238 if ( !myDicItem.IsNull() )
239 fmtStr = toQString( myDicItem->GetFormat( false ) );
243 QString QDS_Datum::defaultValue() const
247 QString pref = prefix();
248 QString suff = suffix();
251 if ( !myDicItem.IsNull() )
252 def = toQString( myDicItem->GetDefaultValue() );
254 QString aDef = def.stripWhiteSpace();
255 if ( !pref.isEmpty() && aDef.left( pref.length() ) == pref )
256 aDef = aDef.mid( pref.length() );
258 if ( !suff.isEmpty() && aDef.right( suff.length() ) == suff )
259 aDef = aDef.mid( 0, aDef.length() - suff.length() );
264 QString QDS_Datum::minimumValue() const
269 if ( !myDicItem.IsNull() && myDicItem->HasData( DDS_DicItem::MinValue ) )
270 min = format( format(), type(), myDicItem->GetMinValue() );
274 QString QDS_Datum::maximumValue() const
279 if ( !myDicItem.IsNull() && myDicItem->HasData( DDS_DicItem::MaxValue ) )
280 max = format( format(), type(), myDicItem->GetMaxValue() );
284 QString QDS_Datum::longDescription() const
289 if ( !myDicItem.IsNull() )
290 ldStr = toQString( myDicItem->GetLongDescription() );
294 QString QDS_Datum::shortDescription() const
299 if ( !myDicItem.IsNull() )
300 sdStr = toQString( myDicItem->GetLongDescription() );
304 QVariant QDS_Datum::value() const
312 QString QDS_Datum::stringValue() const
316 if ( getString() == myTargetValue )
317 return mySourceValue;
322 double QDS_Datum::doubleValue() const
327 if ( !myTargetValue.isNull() && myTargetValue == getString() )
328 res = mySourceValue.toDouble();
331 res = getString().toDouble();
332 if ( !myDicItem.IsNull() )
333 res = myDicItem->ToSI( res );
339 int QDS_Datum::integerValue() const
344 if ( !myTargetValue.isNull() && myTargetValue == getString() )
345 res = mySourceValue.toInt();
348 double val = getString().toDouble();
349 if ( !myDicItem.IsNull() )
350 res = (int)myDicItem->ToSI( val );
356 QString QDS_Datum::text() const
360 QString aLabel = label();
361 QString aData = stringValue();
362 QString aUnits = units();
364 QString res = aLabel;
365 if ( !res.isEmpty() && !aData.isEmpty() )
366 res += QString( ": " );
369 if ( !aUnits.isEmpty() )
370 res += QString( " " ) + aUnits;
375 bool QDS_Datum::isEmpty() const
377 return stringValue().isEmpty();
380 void QDS_Datum::reset()
384 mySourceValue = defaultValue();
385 setString( format( ( myFlags & NotFormat ) ? (QString) "" : format(), type(), mySourceValue ) );
389 QString str = getString();
391 emit paramChanged( str );
394 void QDS_Datum::clear()
398 if ( !getString().isEmpty() )
401 setString( mySourceValue );
405 QString str = getString();
407 emit paramChanged( str );
411 void QDS_Datum::setValue( const QVariant& val )
413 if ( val.isValid() && val.canCast( QVariant::String ) )
414 setStringValue( val.toString() );
419 void QDS_Datum::setStringValue( const QString& txt )
424 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), txt );
426 myTargetValue = aStr;
429 QString str = getString();
431 emit paramChanged( str );
434 void QDS_Datum::setDoubleValue( const double num )
438 mySourceValue = QString().setNum( num, 'g', 16 );
440 if ( !myDicItem.IsNull() )
441 val = myDicItem->FromSI( val );
443 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
445 myTargetValue = aStr;
448 QString str = getString();
450 emit paramChanged( str );
453 void QDS_Datum::setIntegerValue( const int num )
457 mySourceValue = QString().setNum( num );
459 if ( !myDicItem.IsNull() )
460 val = myDicItem->FromSI( val );
462 QString aStr = format( ( flags() & NotFormat ) ? (QString) "" : format(), type(), val );
464 myTargetValue = aStr;
467 QString str = getString();
469 emit paramChanged( str );
473 Returns true if all subwidgets specified by 'element' enabled.
475 bool QDS_Datum::isEnabled( const int element ) const
480 if ( element & Label )
481 res = res && labelWidget() && labelWidget()->isEnabled();
482 if ( element & Units )
483 res = res && unitsWidget() && unitsWidget()->isEnabled();
484 if ( element & Control )
485 res = res && controlWidget() && controlWidget()->isEnabled();
490 Enable/Disable subwidgets specified by 'element'.
491 Values: Label, Control, Units or their combinations.
493 void QDS_Datum::setEnabled( const bool on, const int element )
497 if ( element & Label && labelWidget() )
498 labelWidget()->setEnabled( on );
499 if ( element & Units && unitsWidget() )
500 unitsWidget()->setEnabled( on );
501 if ( element & Control && controlWidget() )
502 controlWidget()->setEnabled( on );
506 Enable/Disable control.
508 void QDS_Datum::setEnabled( bool on )
510 setEnabled( on, Control );
513 void QDS_Datum::setShown( const bool visible, const int flags )
522 Show subwidgets specified by 'element'.
523 Values: Label, Control, Units or their combinations.
525 void QDS_Datum::show( const int element )
529 if ( ( element & Label ) && labelWidget() )
530 labelWidget()->show();
531 if ( ( element & Units ) && unitsWidget() )
532 unitsWidget()->show();
533 if ( ( element & Control ) && controlWidget() )
534 controlWidget()->show();
538 Hide subwidgets specified by 'element'.
539 Values: Label, Control, Units or their combinations.
541 void QDS_Datum::hide( const int element )
545 if ( ( element & Label ) && labelWidget() )
546 labelWidget()->hide();
547 if ( ( element & Units ) && unitsWidget() )
548 unitsWidget()->hide();
549 if ( ( element & Control ) && controlWidget() )
550 controlWidget()->hide();
554 Returns subwidget specified by 'element'.
555 Possible values: Label, Control, Units.
557 QWidget* QDS_Datum::widget( const int element ) const
560 return wrapper( element );
564 Set the input focus on the control widget.
566 void QDS_Datum::setFocus()
570 if ( controlWidget() )
571 controlWidget()->setFocus();
575 Returns true if control contains valid value otherwise returns false
576 and display warning message box if parameter msgBox is set.
578 bool QDS_Datum::isValid( const bool msgBox, const QString& extMsg, const QString& extLabel ) const
582 if ( type() == DDS_DicItem::String && isDoubleFormat( format() ) )
586 if ( !dicItem().IsNull() )
587 req = toQString( dicItem()->GetRequired() );
590 QString aStr = getString();
592 if ( aStr.isEmpty() )
593 aState = !( req == QString( "yes" ) || req == QString( "true" ) || req.toInt() );
595 aState = validate( aStr );
597 if ( msgBox && !aState )
600 if ( !label().isEmpty() )
601 info += tr( "DATA_INCORRECT_VALUE" ).arg( label() );
602 else if ( !extLabel.isEmpty() )
603 info += tr( "DATA_INCORRECT_VALUE" ).arg( extLabel );
608 case DDS_DicItem::String:
609 typeStr = tr( "DATA_STRING" );
611 case DDS_DicItem::Integer:
612 typeStr = tr( "DATA_INTEGER" );
614 case DDS_DicItem::Float:
615 typeStr = tr( "DATA_FLOAT" );
618 typeStr = tr( "DATA_NON_EMPTY" );
621 info += ( info.isEmpty() ? (QString) "" : QString( "\n" ) ) +
622 tr( "DATA_SHOULD_BE_VALUE" ).arg( typeStr );
624 if ( type() == DDS_DicItem::Float || type() == DDS_DicItem::Integer )
626 QString aMinValue = minValue();
627 QString aMaxValue = maxValue();
628 if ( !aMinValue.isEmpty() && !aMaxValue.isEmpty() )
629 limit = tr( "DATA_RANGE" ).arg( aMinValue ).arg( aMaxValue );
630 else if ( !aMinValue.isEmpty() )
631 limit = tr( "DATA_MIN_LIMIT" ).arg( aMinValue );
632 else if ( !aMaxValue.isEmpty() )
633 limit = tr( "DATA_MAX_LIMIT" ).arg( aMaxValue );
635 if ( !limit.isEmpty() )
638 info += QString( ".\n" ) + tr( "DATA_INPUT_VALUE" );
640 if ( !extMsg.isEmpty() )
641 info += QString( "\n" ) + extMsg;
644 for ( uint i = 0; i < info.length(); i++ )
645 if ( info.at( i ) == '\n' )
646 msg += QString( "<br>" );
650 info = QString( "<p><nobr>%1</nobr></p>" ).arg( msg );
652 QMessageBox::critical( controlWidget() ? controlWidget()->topLevelWidget() : 0,
653 tr( "DATA_ERR_TITLE" ), info, tr( "OK" ) );
654 if ( controlWidget() )
655 controlWidget()->setFocus();
661 Add widgets to the vertical layout.
663 void QDS_Datum::addTo( QVBoxLayout* 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( wrapper( Units ) );
679 Add widgets to the horizaontal layout.
681 void QDS_Datum::addTo( QHBoxLayout* l )
688 if ( wrapper( Label ) )
689 l->addWidget( wrapper( Label ) );
690 if ( wrapper( Control ) )
691 l->addWidget( wrapper( Control ) );
692 if ( wrapper( Units ) )
693 l->addWidget( unitsWidget() );
697 Add widgets to the grid layout.
699 void QDS_Datum::addTo( QGridLayout* theLay, const int theRow, const int theCol, bool vertical )
708 if ( wrapper( Label ) )
710 theLay->addWidget( wrapper( Label ), row, col );
711 vertical ? row++ : col++;
713 if ( wrapper( Control ) )
715 theLay->addWidget( wrapper( Control ), row, col );
716 vertical ? row++ : col++;
718 if ( wrapper( Units ) )
719 theLay->addWidget( wrapper( Units ), row, col );
723 Set the aligment of Label or Units. For Control nothing happens.
725 void QDS_Datum::setAlignment( const int align, const int type )
729 if ( ( type & Label ) && labelWidget() )
730 labelWidget()->setAlignment( align );
731 if ( ( type & Units ) && unitsWidget() )
732 unitsWidget()->setAlignment( align );
735 bool QDS_Datum::eventFilter( QObject* o, QEvent* e )
739 if ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ||
740 ( e->type() == QEvent::ChildInserted && ((QChildEvent*)e)->child() == this ) )
743 return QObject::eventFilter( o, e );
747 Notify about parameter changing.
749 void QDS_Datum::onParamChanged()
754 Delayed initialization.
756 void QDS_Datum::onInitDatum()
762 Notify about subwidgets destroying. Allow to avoid repeated deleting in destructor.
764 void QDS_Datum::onDestroyed( QObject* obj )
766 myWrapper.remove( wrapperType( (Wrapper*)obj ) );
770 Returns QLabel instance which contains data dictionary label.
772 QLabel* QDS_Datum::labelWidget() const
779 Returns QLabel instance which contains data dictionary units.
781 QLabel* QDS_Datum::unitsWidget() const
788 Returns QWidget which contains user input data.
790 QWidget* QDS_Datum::controlWidget() const
797 Returns the dictionary item from the datum.
799 Handle(DDS_DicItem) QDS_Datum::dicItem() const
805 Set the dictionary item in to the datum.
807 void QDS_Datum::setDicItem( const Handle(DDS_DicItem)& item )
813 Creates QLabel widget for data label.
815 QLabel* QDS_Datum::createLabel( QWidget* parent )
817 return new QLabel( parent );
821 Creates QLabel widget for data units.
823 QLabel* QDS_Datum::createUnits( QWidget* parent )
825 return new QLabel( parent );
829 Returns validator accordance to data type.
831 QValidator* QDS_Datum::validator( const bool limits ) const
833 QValidator* aValidator = 0;
835 QString fltr = filter();
837 if ( type() == DDS_DicItem::String )
840 QString aFormat = canonicalFormat( format(), aFlags );
843 int pos = aFormat.find( "." );
846 QString numStr = aFormat.mid( pos + 1, aFormat.length() - pos - 2 );
848 int numVal = numStr.toInt( &ok );
853 QDS_StringValidator* aStrVal = new QDS_StringValidator( fltr, aFlags, (QObject*)this );
854 aStrVal->setLength( len );
856 aValidator = aStrVal;
858 else if ( type() == DDS_DicItem::Integer )
860 QDS_IntegerValidator* aIntVal = new QDS_IntegerValidator( fltr, (QObject*)this );
864 limit = minValue().toInt( &ok );
866 aIntVal->setBottom( limit );
867 limit = maxValue().toInt( &ok );
869 aIntVal->setTop( limit );
871 aValidator = aIntVal;
873 else if ( type() == DDS_DicItem::Float )
875 QDS_DoubleValidator* aFloatVal = new QDS_DoubleValidator( fltr, (QObject*)this );
879 limit = minValue().toDouble( &ok );
881 aFloatVal->setBottom( limit );
882 limit = maxValue().toDouble( &ok );
884 aFloatVal->setTop( limit );
886 aValidator = aFloatVal;
893 Checks the given string are valid or not.
895 bool QDS_Datum::validate( const QString& txt ) const
897 if ( type() == DDS_DicItem::Unknown ||
898 type() == DDS_DicItem::String && isDoubleFormat( format() ) )
901 QValidator* aValidator = validator( true );
908 bool res = aValidator->validate( str, pos ) == QValidator::Acceptable;
916 Retrieves information from data dictionary and create subwidgets using virtual mechanism.
917 Virtual mechanism doesn't work in constructor and destructor, therefore this method should
918 be called outside the constructor.
920 void QDS_Datum::initialize()
922 if ( wrapper( Label ) )
923 wrapper( Label )->setWidget( myLabel = createLabel( wrapper( Label ) ) );
924 if ( wrapper( Control ) )
925 wrapper( Control )->setWidget( myControl = createControl( wrapper( Control ) ) );
926 if ( wrapper( Units ) )
927 wrapper( Units )->setWidget( myUnits = createUnits( wrapper( Units ) ) );
929 TCollection_AsciiString comp;
930 Handle(DDS_DicItem) item = dicItem();
931 if ( !item.IsNull() )
932 comp = item->GetComponent();
935 Handle(DDS_Dictionary) dic = DDS_Dictionary::Get();
937 unitSystem = toQString( comp.IsEmpty() ? dic->GetActiveUnitSystem() :
938 dic->GetActiveUnitSystem( comp ) );
940 unitSystemChanged( unitSystem );
942 QWidget* ctrl = controlWidget();
945 QString lDescr = longDescription();
946 QString sDescr = shortDescription();
947 if ( !sDescr.isEmpty() )
948 QToolTip::add( ctrl, sDescr );
949 if ( !lDescr.isEmpty() )
950 QWhatsThis::add( ctrl, lDescr );
953 if ( labelWidget() && ctrl && !( flags() & NotAccel ) )
954 labelWidget()->setBuddy( ctrl );
957 void QDS_Datum::unitSystemChanged( const QString& unitSystem )
959 QString labText = label();
960 QString unitText = unitsToText( units() );
962 if ( flags() & UnitsWithLabel )
964 if ( labText.isEmpty() )
966 else if ( !unitText.isEmpty() )
967 labText = QString( "%1 (%2)" ).arg( labText ).arg( unitText );
968 unitText = QString::null;
972 labelWidget()->setText( labText );
975 unitsWidget()->setText( unitText );
981 Covert units into text presentation.
983 QString QDS_Datum::unitsToText( const QString& uni )
986 QString aUnits = uni;
987 while ( ( pos = aUnits.find( "**" ) ) != -1 )
989 aUnits = aUnits.mid( 0, pos ) + QString( "<tt><font size=+2><sup>" ) +
990 aUnits.mid( pos + 2, 1 ) + QString( "</sup></font></tt>" ) +
991 aUnits.mid( pos + 3 );
997 Covert text presentation into internal units format.
999 QString QDS_Datum::textToUnits( const QString& txt )
1002 QString aUnits = txt;
1003 while ( ( pos = aUnits.find( "<sup>" ) ) != -1 )
1005 aUnits.remove( pos, 5 );
1006 aUnits.insert( pos, "**" );
1008 while ( ( pos = aUnits.find( "</sup>" ) ) != -1 )
1009 aUnits.remove( pos, 6 );
1014 Format the specified integer as data dictionary value.
1016 QString QDS_Datum::format( const int num, const QString& id, const bool convert )
1018 Handle(DDS_DicItem) anItem;
1023 int aType = DDS_DicItem::Unknown;
1024 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1025 if ( !aDict.IsNull() )
1027 anItem = aDict->GetDicItem( toAsciiString( id ) );
1028 if ( !anItem.IsNull() )
1030 aType = anItem->GetType();
1031 aFormat = toQString( anItem->GetFormat( false ) );
1033 aNum = ( int )anItem->FromSI( aNum );
1037 return format( aFormat, aType, aNum );
1041 Format the specified double as data dictionary value.
1043 QString QDS_Datum::format( const double num, const QString& id, const bool convert )
1045 Handle(DDS_DicItem) anItem;
1050 int aType = DDS_DicItem::Unknown;
1051 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1052 if ( !aDict.IsNull() )
1054 anItem = aDict->GetDicItem( toAsciiString( id ) );
1055 if ( !anItem.IsNull() )
1057 aType = anItem->GetType();
1058 aFormat = toQString( anItem->GetFormat( false ) );
1060 aNum = anItem->FromSI( aNum );
1064 return format( aFormat, aType, aNum );
1068 Format the specified string as data dictionary value.
1070 QString QDS_Datum::format( const QString& str, const QString& id, const bool convert )
1072 Handle(DDS_DicItem) anItem;
1077 int aType = DDS_DicItem::Unknown;
1078 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1079 if ( !aDict.IsNull() )
1081 anItem = aDict->GetDicItem( toAsciiString( id ) );
1082 if ( !anItem.IsNull() )
1084 aType = anItem->GetType();
1085 aFormat = toQString( anItem->GetFormat( false ) );
1087 aStr = QString::number( anItem->FromSI( aStr.toDouble() ), 'f', 16 );
1091 return format( aFormat, aType, aStr );
1095 Format the given string accordance to data format.
1097 QString QDS_Datum::format( const QString& aFormat, const int aType, const int aValue )
1101 if ( !aFormat.isEmpty() )
1105 case DDS_DicItem::Float:
1106 txt = sprintf( aFormat, (double)aValue );
1107 txt = txt.stripWhiteSpace();
1109 case DDS_DicItem::Integer:
1110 txt = sprintf( aFormat, aValue );
1111 txt = txt.stripWhiteSpace();
1113 case DDS_DicItem::String:
1115 txt = sprintf( aFormat, aValue );
1120 txt = QString().setNum( aValue );
1126 Format the given string accordance to data format.
1128 QString QDS_Datum::format( const QString& aFormat, const int aType, const double aValue )
1132 if ( !aFormat.isEmpty() )
1136 case DDS_DicItem::Float:
1137 txt = QString().sprintf( aFormat, aValue );
1138 txt = txt.stripWhiteSpace();
1140 case DDS_DicItem::Integer:
1141 txt = QString().sprintf( aFormat, (int)aValue );
1142 txt = txt.stripWhiteSpace();
1144 case DDS_DicItem::String:
1146 txt = QString().sprintf( aFormat, aValue );
1151 txt = QString().setNum( aValue, 'g', 16 );
1157 Format the given string accordance to data format.
1159 QString QDS_Datum::format( const QString& aFormat, const int aType, const QString& aValue )
1161 QString txt = aValue;
1163 if ( aType != DDS_DicItem::String )
1164 txt = txt.stripWhiteSpace();
1166 if ( aFormat.isEmpty() || txt.isEmpty() )
1171 case DDS_DicItem::Float:
1172 txt = txt.replace( 'd', 'e' ).replace( 'D', 'E' );
1173 txt = sprintf( aFormat, txt.toDouble() );
1174 txt = txt.stripWhiteSpace();
1176 case DDS_DicItem::Integer:
1177 txt = sprintf( aFormat, txt.toInt() );
1178 txt = txt.stripWhiteSpace();
1180 case DDS_DicItem::String:
1181 txt = sprintf( aFormat, txt );
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 int val )
1194 return QString().sprintf( canonicalFormat( fmt ), val );
1198 Wrapper around the standard sprintf function.
1199 Process some non standard flags from format string.
1201 QString QDS_Datum::sprintf( const QString& fmt, const double val )
1203 return QString().sprintf( canonicalFormat( fmt ), val );
1207 Wrapper around the standard sprintf function.
1208 Process some non standard flags from format string.
1210 QString QDS_Datum::sprintf( const QString& fmt, const QString& val )
1213 QString aFormat = canonicalFormat( fmt, aFlags );
1217 QRegExp rx( "^(%[0-9]*.?[0-9]*s)$" );
1218 if ( aFormat.find( rx ) != -1 )
1220 // QString().sprintf() always expects string in UTF8 encoding, so we cannot use it here
1221 char* buf = new char[txt.length() + 1];
1222 ::sprintf( buf, aFormat.latin1(), (const char*)(txt.local8Bit()) );
1223 txt = QString::fromLocal8Bit( buf );
1228 if ( isDoubleFormat( aFormat ) )
1231 double aVal = txt.toDouble( &isOk );
1234 txt = sprintf( aFormat, aVal );
1235 txt = txt.replace( 'e', 'D' );
1239 if ( aFlags.contains( "u", false ) )
1241 if ( aFlags.contains( "l", false ) )
1248 Returns the canonical sprintf format.
1250 QString QDS_Datum::canonicalFormat( const QString& fmt )
1253 return canonicalFormat( fmt, flags );
1257 Returns the canonical sprintf format and non standard flags.
1259 QString QDS_Datum::canonicalFormat( const QString& fmt, QString& flags )
1261 QString newFmt = fmt;
1262 flags = QString::null;
1264 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]$" );
1265 if ( rx.search( newFmt ) >= 0 )
1267 flags = rx.cap( 2 );
1268 newFmt.remove( rx.pos( 2 ), flags.length() );
1274 Returns displayable units string for given DD ID
1276 QString QDS_Datum::units( const QString& id )
1279 Handle(DDS_DicItem) anItem;
1281 Handle(DDS_Dictionary) aDict = DDS_Dictionary::Get();
1282 if ( !aDict.IsNull() )
1284 anItem = aDict->GetDicItem( toAsciiString( id ) );
1285 if ( !anItem.IsNull() )
1286 anUnit = unitsToText( toQString( anItem->GetUnits() ) );
1292 Get prefix string from format.
1294 QString QDS_Datum::prefix() const
1296 return QString::null;
1300 Get suffix string from format.
1302 QString QDS_Datum::suffix() const
1304 return QString::null;
1310 QString QDS_Datum::minValue() const
1312 QString pref = prefix();
1313 QString suff = suffix();
1315 QString aMin = minimumValue().stripWhiteSpace();
1317 if ( !pref.isEmpty() && aMin.left( pref.length() ) == pref )
1318 aMin = aMin.mid( pref.length() );
1320 if ( !suff.isEmpty() && aMin.right( suff.length() ) == suff )
1321 aMin = aMin.mid( 0, aMin.length() - suff.length() );
1329 QString QDS_Datum::maxValue() const
1331 QString pref = prefix();
1332 QString suff = suffix();
1334 QString aMax = maximumValue().stripWhiteSpace();
1336 if ( !pref.isEmpty() && aMax.left( pref.length() ) == pref )
1337 aMax = aMax.mid( pref.length() );
1339 if ( !suff.isEmpty() && aMax.right( suff.length() ) == suff )
1340 aMax = aMax.mid( 0, aMax.length() - suff.length() );
1346 Reset the numeric value cache.
1348 void QDS_Datum::invalidateCache()
1350 myTargetValue = QString::null;
1353 QString QDS_Datum::removeAccel( const QString& src )
1357 for ( uint i = 0; i < trg.length(); )
1359 if ( trg.mid( i, 2 ) == QString( "&&" ) )
1361 else if ( trg.at( i ) == '&' )
1369 bool QDS_Datum::isDoubleFormat( const QString& theFormat )
1371 if ( theFormat.length() > 0 )
1373 QChar c = theFormat[ (int)( theFormat.length() - 1 ) ];
1374 return c == 'f' || c == 'g' || c == 'e' || c == 'G' || c == 'E';
1380 int QDS_Datum::flags() const
1385 void QDS_Datum::initDatum() const
1387 if ( myInitialised )
1390 QDS_Datum* that = (QDS_Datum*)this;
1391 that->myInitialised = true;
1395 parent()->removeEventFilter( this );
1398 QDS_Datum::Wrapper* QDS_Datum::wrapper( QWidget* wid ) const
1404 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && !wrap; ++it )
1406 if ( it.data() && it.data()->widget() == wid )
1412 QDS_Datum::Wrapper* QDS_Datum::wrapper( const int id ) const
1415 if ( myWrapper.contains( id ) )
1416 wrap = myWrapper[id];
1420 int QDS_Datum::wrapperType( QDS_Datum::Wrapper* wrap ) const
1423 for ( QMap<int, Wrapper*>::ConstIterator it = myWrapper.begin(); it != myWrapper.end() && id == -1; ++it )
1425 if ( it.data() == wrap )