Back to index

salome-gui  6.5.0
QtxColorScale.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:      QtxColorScale.cxx
00024 // Author:    Sergey TELKOV
00025 //
00026 #include "QtxColorScale.h"
00027 
00028 #include <QMap>
00029 #include <QRegExp>
00030 #include <QStringList>
00031 #include <QPixmap>
00032 #include <QPainter>
00033 #include <QTextDocument>
00034 
00035 #include <math.h>
00036 
00047 QtxColorScale::QtxColorScale( QWidget* parent, Qt::WindowFlags f )
00048 : QFrame( parent, f ),
00049   myMin( 0.0 ),
00050   myMax( 1.0 ),
00051   myTitle( "" ),
00052   myFormat( "%.4g" ),
00053   myInterval( 10 ),
00054   myDumpMode( NoDump ),
00055   myColorMode( Auto ),
00056   myLabelMode( Auto ),
00057   myFlags( AtBorder | WrapTitle ),
00058   myLabelPos( Right ),
00059   myTitlePos( Center )
00060 {
00061   setWindowTitle( tr ( "Color scale" ) );
00062 }
00063 
00070 QtxColorScale::QtxColorScale( const int num, QWidget* parent, Qt::WindowFlags f )
00071 : QFrame( parent, f ),
00072   myMin( 0.0 ),
00073   myMax( 1.0 ),
00074   myTitle( "" ),
00075   myFormat( "%.4g" ),
00076   myInterval( num ),
00077   myDumpMode( NoDump ),
00078   myColorMode( Auto ),
00079   myLabelMode( Auto ),
00080   myFlags( AtBorder | WrapTitle ),
00081   myLabelPos( Right ),
00082   myTitlePos( Center )
00083 {
00084   setWindowTitle( tr ( "Color scale" ) );
00085 }
00086 
00092 QtxColorScale::~QtxColorScale()
00093 {
00094 }
00095 
00100 double QtxColorScale::minimum() const
00101 {
00102   return myMin;
00103 }
00104 
00109 double QtxColorScale::maximum() const
00110 {
00111   return myMax;
00112 }
00113 
00119 void QtxColorScale::range( double& min, double& max ) const
00120 {
00121   min = myMin;
00122   max = myMax;
00123 }
00124 
00129 QString QtxColorScale::title() const
00130 {
00131   return myTitle;
00132 }
00133 
00142 QString QtxColorScale::format() const
00143 {
00144   return myFormat;
00145 }
00146 
00151 int QtxColorScale::dumpMode() const
00152 {
00153   return myDumpMode;
00154 }
00155 
00160 int QtxColorScale::labelMode() const
00161 {
00162   return myLabelMode;
00163 }
00164 
00169 int QtxColorScale::colorMode() const
00170 {
00171   return myColorMode;
00172 }
00173 
00178 int QtxColorScale::intervalsNumber() const
00179 {
00180   return myInterval;
00181 }
00182 
00188 QString QtxColorScale::label( const int idx ) const
00189 {
00190   QString res;
00191   if ( idx >= 0 && idx < (int)myLabels.count() )
00192     res = myLabels[idx];
00193   return res;
00194 }
00195 
00201 QColor QtxColorScale::color( const int idx ) const
00202 {
00203   QColor res;
00204   if ( idx >= 0 && idx < (int)myColors.count() )
00205     res = myColors[idx];
00206   return res;
00207 }
00208 
00213 void QtxColorScale::labels( QStringList& list ) const
00214 {
00215   list = myLabels;
00216 }
00217 
00222 void QtxColorScale::colors( QList<QColor>& list ) const
00223 {
00224   list = myColors;
00225 }
00226 
00231 int QtxColorScale::labelPosition() const
00232 {
00233   return myLabelPos;
00234 }
00235 
00240 int QtxColorScale::titlePosition() const
00241 {
00242   return myTitlePos;
00243 }
00244 
00249 void QtxColorScale::setMinimum( const double val )
00250 {
00251   setRange( val, maximum() );
00252 }
00253 
00258 void QtxColorScale::setMaximum( const double val )
00259 {
00260   setRange( minimum(), val );
00261 }
00262 
00268 void QtxColorScale::setRange( const double min, const double max )
00269 {
00270   if ( myMin == min && myMax == max )
00271     return;
00272   
00273   myMin = min;
00274   myMax = max;
00275   
00276   myPrecise = QString();
00277 
00278   if ( colorMode() == Auto || labelMode() == Auto )
00279     updateScale();
00280 }
00281 
00286 void QtxColorScale::setTitle( const QString& str )
00287 {
00288   if ( myTitle == str )
00289     return;
00290   
00291   myTitle = str;
00292   updateScale();
00293 }
00294 
00300 void QtxColorScale::setFormat( const QString& format )
00301 {
00302   if ( myFormat == format )
00303     return;
00304 
00305   myFormat = format;
00306   myPrecise = QString();
00307   if ( colorMode() == Auto )
00308     updateScale();
00309 }
00310 
00315 void QtxColorScale::setIntervalsNumber( const int num )
00316 {
00317   if ( myInterval == num || num < 1 )
00318     return;
00319   
00320   myInterval = num;
00321   myPrecise = QString();
00322   
00323   updateScale();
00324 }
00325 
00335 void QtxColorScale::setLabel( const QString& txt, const int idx )
00336 {
00337   bool changed = false;
00338   int i = idx < 0 ? myLabels.count() : idx;
00339   if ( i < myLabels.count() )
00340   {
00341     changed = myLabels[i] != txt;
00342     myLabels[i] = txt;
00343   }
00344   else
00345   {
00346     changed = true;
00347     while ( i >= myLabels.count() )
00348       myLabels.append( "" );
00349     myLabels[i] = txt;
00350   }
00351   if ( changed )
00352     updateScale();
00353 }
00354 
00364 void QtxColorScale::setColor( const QColor& clr, const int idx )
00365 {
00366   bool changed = false;
00367   int i = idx < 0 ? myColors.count() : idx;
00368   if ( i < myColors.count() )
00369   {
00370     changed = myColors[i] != clr;
00371     myColors[i] = clr;
00372   }
00373   else
00374   {
00375     changed = true;
00376     while ( i >= myColors.count() )
00377       myColors.append( QColor() );
00378     myColors[i] = clr;
00379   }
00380   if ( changed )
00381     updateScale();
00382 }
00383 
00388 void QtxColorScale::setLabels( const QStringList& list )
00389 {
00390   if ( list.isEmpty() )
00391     return;
00392 
00393   myLabels = list;
00394   updateScale();
00395 }
00396 
00401 void QtxColorScale::setColors( const QList<QColor>& list )
00402 {
00403   if ( list.isEmpty() )
00404     return;
00405 
00406   myColors = list;
00407   updateScale();
00408 }
00409 
00414 void QtxColorScale::setColorMode( const int mode )
00415 {
00416   if ( myColorMode == mode )
00417     return;
00418   
00419   myColorMode = mode;
00420   updateScale();
00421 }
00422 
00427 void QtxColorScale::setDumpMode( const int mode )
00428 {
00429   myDumpMode = mode;
00430 }
00431 
00436 void QtxColorScale::setLabelMode( const int mode )
00437 {
00438   if ( myLabelMode != mode )
00439   {
00440     myLabelMode = mode;
00441     updateScale();
00442   }
00443 }
00444 
00449 void QtxColorScale::setLabelPosition( const int pos )
00450 {
00451   if ( myLabelPos != pos && pos >= None && pos <= Center )
00452   {
00453     myLabelPos = pos;
00454     updateScale();
00455   }
00456 }
00457 
00462 void QtxColorScale::setTitlePosition( const int pos )
00463 {
00464   if ( myTitlePos != pos && pos >= None && pos <= Center )
00465   {
00466     myTitlePos = pos;
00467     updateScale();
00468   }
00469 }
00470 
00475 void QtxColorScale::setFlags( const int flags )
00476 {
00477   int prev = myFlags;
00478   myFlags |= flags;
00479   if ( prev != myFlags )
00480     updateScale();
00481 }
00482 
00487 bool QtxColorScale::testFlags( const int flags ) const
00488 {
00489   return ( myFlags & flags ) == flags;
00490 }
00491 
00496 void QtxColorScale::clearFlags( const int flags )
00497 {
00498   int prev = myFlags;
00499   myFlags &= ~flags;
00500   if ( prev != myFlags )
00501     updateScale();
00502 }
00503 
00508 QSize QtxColorScale::minimumSizeHint() const
00509 {
00510   QSize sz = calculateSize( true, myFlags, titlePosition() != None, labelPosition() != None, true );
00511   return sz + QSize( frameWidth(), frameWidth() );
00512 }
00513 
00518 QSize QtxColorScale::sizeHint() const
00519 {
00520   QSize sz = calculateSize( false, myFlags, titlePosition() != None, labelPosition() != None, true );
00521   return sz + QSize( frameWidth(), frameWidth() );
00522 }
00523 
00533 QSize QtxColorScale::calculateSize( const bool min, const int flags, const bool title,
00534                                     const bool labels, const bool colors ) const
00535 {
00536   int num = intervalsNumber();
00537   
00538   int spacer = 5;
00539   int textWidth = 0;
00540   int textHeight = fontMetrics().height();
00541   int colorWidth = 20;
00542   
00543   if ( labels && colors )
00544   {
00545     QtxColorScale* that = (QtxColorScale*)this;
00546     QString fmt = that->myFormat;
00547 
00548     for ( int idx = 0; idx < num; idx++ )
00549       textWidth = qMax( textWidth, fontMetrics().width( getLabel( idx ) ) );
00550     
00551     if ( !min )
00552       that->myFormat = that->myFormat.replace( QRegExp( "g" ), "f" );
00553     
00554     for ( int index = 0; index < num; index++ )
00555       textWidth = qMax( textWidth, fontMetrics().width( getLabel( index ) ) );
00556     
00557     that->myFormat = fmt;
00558   }
00559   
00560   int scaleWidth = 0;
00561   int scaleHeight = 0;
00562   
00563   int titleWidth = 0;
00564   int titleHeight = 0;
00565   
00566   if ( flags & AtBorder )
00567   {
00568     num++;
00569     if ( min && title && !myTitle.isEmpty() )
00570       titleHeight += 10;
00571   }
00572   
00573   if ( colors )
00574   {
00575     scaleWidth = colorWidth + textWidth + ( textWidth ? 3 : 2 ) * spacer;
00576     if ( min )
00577       scaleHeight = qMax( 2 * num, 3 * textHeight );
00578     else
00579       scaleHeight = (int)( 1.5 * ( num + 1 ) * textHeight );
00580   }
00581 
00582   if ( title )
00583   {
00584     QTextDocument* srt = textDocument( flags );
00585     if ( srt )
00586     {
00587       QPainter p( (QtxColorScale*)this );
00588       if ( scaleWidth )
00589         srt->setTextWidth( scaleWidth );
00590       
00591       titleHeight = (int)srt->size().height() + spacer;
00592       titleWidth = (int)srt->size().width() + 10;
00593       
00594     }
00595     delete srt;
00596   }
00597 
00598   int W = qMax( titleWidth, scaleWidth ) + width() - contentsRect().width();
00599   int H = scaleHeight + titleHeight + height() - contentsRect().height();
00600   
00601   return QSize( W, H );
00602 }
00603 
00608 QPixmap QtxColorScale::dump() const
00609 {
00610   QPixmap aPix;
00611   
00612   if ( dumpMode() != NoDump )
00613   {
00614     aPix = QPixmap( size() );
00615     if ( !aPix.isNull() )
00616     {
00617       bool scale = ( myDumpMode == ScaleDump || myDumpMode == FullDump );
00618       bool label = ( myDumpMode == ScaleDump || myDumpMode == FullDump ) &&
00619         labelPosition() != None;
00620       bool title = ( myDumpMode == TitleDump || myDumpMode == FullDump ) &&
00621         titlePosition() != None;
00622       QColor bgc = palette().color( backgroundRole() );
00623       QPainter p;
00624       p.begin( &aPix );
00625       p.fillRect( 0, 0, aPix.width(), aPix.height(), bgc );
00626       drawScale( &p, bgc, false, 0, 0, aPix.width(), aPix.height(), title, label, scale );
00627       p.end();
00628     }
00629   }
00630   
00631   return aPix;
00632 }
00633 
00640 QPixmap QtxColorScale::dump( const int w, const int h ) const
00641 {
00642   return dump( palette().color( backgroundRole() ), w, h );
00643 }
00644 
00652 QPixmap QtxColorScale::dump( const QColor& bg, const int w, const int h ) const
00653 {
00654   QPixmap aPix;
00655   if ( dumpMode() != NoDump )
00656   {
00657     bool scale = ( myDumpMode == ScaleDump || myDumpMode == FullDump );
00658     bool label = ( myDumpMode == ScaleDump || myDumpMode == FullDump ) &&
00659       labelPosition() != None;
00660     bool title = ( myDumpMode == TitleDump || myDumpMode == FullDump ) &&
00661       titlePosition() != None;
00662     
00663     int W = w;
00664     int H = h;
00665     if ( W < 0 || H < 0 )
00666     {
00667       QSize sz = calculateSize( false, myFlags & ~WrapTitle, title, label, scale );
00668       
00669       if ( W < 0 )
00670         W = sz.width();
00671       if ( H < 0 )
00672         H = sz.height();
00673     }
00674     
00675     aPix = QPixmap( W, H );
00676     if ( !aPix.isNull() )
00677     {
00678       QPainter p;
00679       p.begin( &aPix );
00680       p.fillRect( 0, 0, aPix.width(), aPix.height(), bg );
00681       drawScale( &p, bg, false, 0, 0, aPix.width(), aPix.height(), title, label, scale );
00682       p.end();
00683     }
00684   }
00685   
00686   return aPix;
00687 }
00688 
00692 void QtxColorScale::show()
00693 {
00694   QFrame::show();
00695 }
00696 
00700 void QtxColorScale::hide()
00701 {
00702   QFrame::hide();
00703 }
00704 
00709 void QtxColorScale::paintEvent( QPaintEvent* e )
00710 {
00711   QPainter p( this );
00712   drawFrame( &p );
00713   drawContents( &p );
00714 }
00715 
00720 void QtxColorScale::drawContents( QPainter* p )
00721 {
00722   if ( !updatesEnabled() )
00723     return;
00724   
00725   QRect aDrawRect = contentsRect();
00726   
00727   drawScale( p, false/*testFlags( Transparent )*/, aDrawRect.x(),
00728              aDrawRect.y(), aDrawRect.width(), aDrawRect.height(),
00729              titlePosition() != None, labelPosition() != None, true );
00730 }
00731 
00744 void QtxColorScale::drawScale( QPainter* p, const bool transp, const int X, const int Y,
00745                                const int W, const int H, const bool drawTitle,
00746                                const bool drawLabel, const bool drawColors ) const
00747 {
00748   QPixmap cache( W, H );
00749   QPainter cp( &cache );
00750   
00751   drawScale( &cp, palette().color( backgroundRole() ), transp, 0, 0, W, H, drawTitle, drawLabel, drawColors );
00752   cp.end();
00753   
00754   p->drawPixmap( X, Y, cache );
00755 }
00756 
00770 void QtxColorScale::drawScale( QPainter* p, const QColor& bg, const bool transp,
00771                                const int X, const int Y, const int W, const int H,
00772                                const bool drawTitle, const bool drawLabel, const bool drawColors ) const
00773 {
00774   if ( !transp )
00775     p->fillRect( X, Y, W, H, bg );
00776   
00777   int num = intervalsNumber();
00778   
00779   int labPos = labelPosition();
00780   
00781   int spacer = 5;
00782   int textWidth = 0;
00783   int textHeight = p->fontMetrics().height();
00784   
00785   QString aTitle = title();
00786   
00787   int titleWidth = 0;
00788   int titleHeight = 0;
00789   
00790   if ( qGray( bg.rgb() ) < 128 )
00791     p->setPen( QColor( 255, 255, 255 ) );
00792   else
00793     p->setPen( QColor( 0, 0, 0 ) );
00794   
00795   // Draw title
00796   if ( drawTitle )
00797   {
00798     QTextDocument* srt = textDocument( myFlags );
00799     if ( srt )
00800     {
00801       srt->setTextWidth( W - 10 );
00802       titleHeight = (int)srt->size().height() + spacer;
00803       titleWidth = (int)srt->size().width();
00804       p->save();
00805       p->translate( X + 5, Y );
00806       srt->drawContents( p );
00807       p->restore();
00808     }
00809     delete srt;
00810   }
00811 
00812   bool reverse = testFlags( Reverse );
00813   
00814   QList<QColor>  colors;
00815   QList<QString> labels;
00816   for ( int idx = 0; idx < num; idx++ )
00817   {
00818     if ( reverse )
00819     {
00820       colors.append( getColor( idx ) );
00821       labels.append( getLabel( idx ) );
00822     }
00823     else
00824     {
00825       colors.prepend( getColor( idx ) );
00826       labels.prepend( getLabel( idx ) );
00827     }
00828   }
00829   
00830   if ( testFlags( AtBorder ) )
00831   {
00832     if ( reverse )
00833       labels.append( getLabel( num ) );
00834     else
00835       labels.prepend( getLabel( num ) );
00836     if ( drawLabel )
00837       textWidth = qMax( textWidth, p->fontMetrics().width( labels.last() ) );
00838   }
00839   
00840   if ( drawLabel )
00841   {
00842     const QFontMetrics& fm = p->fontMetrics();
00843     for ( QStringList::ConstIterator it = labels.begin(); it != labels.end(); ++it )
00844       textWidth = qMax( textWidth, fm.width( *it) );
00845   }
00846   
00847   int lab = labels.count();
00848   
00849   double spc = ( H - ( ( qMin( lab, 2 ) + qAbs( lab - num - 1 ) ) * textHeight ) - titleHeight );
00850   double val = spc != 0 ? 1.0 * ( lab - qMin( lab, 2 ) ) * textHeight / spc : 0;
00851   double iPart;
00852   double fPart = modf( val, &iPart );
00853   int filter = (int)iPart + ( fPart != 0 ? 1 : 0 );
00854   filter = qMax( filter, 1 );
00855   
00856   double step = 1.0 * ( H - ( lab - num + qAbs( lab - num - 1 ) ) * textHeight - titleHeight ) / num;
00857   
00858   int ascent = p->fontMetrics().ascent();
00859   int colorWidth = qMax( 5, qMin( 20, W - textWidth - 3 * spacer ) );
00860   if ( labPos == Center || !drawLabel )
00861     colorWidth = W - 2 * spacer;
00862   
00863   // Draw colors
00864   int x = X + spacer;
00865   switch ( labPos )
00866   {
00867   case Left:
00868     x += textWidth + ( textWidth ? 1 : 0 ) * spacer;
00869     break;
00870   }
00871 
00872   double offset = 1.0 * textHeight / 2 * ( lab - num + qAbs( lab - num - 1 ) ) + titleHeight;
00873   QList<QColor>::Iterator cit = colors.begin();
00874   uint ci = 0;
00875   for ( ci = 0; cit != colors.end() && drawColors; ++cit, ci++ )
00876   {
00877     int y = (int)( Y + ci * step + offset );
00878     int h = (int)( Y + ( ci + 1 ) * step + offset ) - y;
00879     p->fillRect( x, y, colorWidth, h, *cit );
00880   }
00881   
00882   if ( drawColors )
00883     p->drawRect( int( x - 1 ), int( Y + offset - 1 ), int( colorWidth + 2 ), int( ci * step + 2 ) );
00884   
00885   // Draw labels
00886   offset = 1.0 * qAbs( lab - num - 1 ) * ( step - textHeight ) / 2 +
00887     1.0 * qAbs( lab - num - 1 ) * textHeight / 2;
00888   offset += titleHeight;
00889   if ( drawLabel && !labels.isEmpty() )
00890   {
00891     int i1 = 0;
00892     int i2 = lab - 1;
00893     int last1( i1 ), last2( i2 );
00894     int x = X + spacer;
00895     switch ( labPos )
00896     {
00897     case Center:
00898       x += ( colorWidth - textWidth ) / 2;
00899       break;
00900     case Right:
00901       x += colorWidth + spacer;
00902       break;
00903     }
00904     while ( i2 - i1 >= filter || ( i2 == 0 && i1 == 0 ) )
00905     {
00906       int pos1 = i1;
00907       int pos2 = lab - 1 - i2;
00908       if ( filter && !( pos1 % filter ) )
00909       {
00910         p->drawText( x, (int)( Y + i1 * step + ascent + offset ), labels[i1] );
00911         last1 = i1;
00912       }
00913       if ( filter && !( pos2 % filter ) )
00914       {
00915         p->drawText( x, (int)( Y + i2 * step + ascent + offset ), labels[i2] );
00916         last2 = i2;
00917       }
00918       i1++;
00919       i2--;
00920     }
00921     int pos = i1;
00922     int i0 = -1;
00923     while ( pos <= i2 && i0 == -1 )
00924     {
00925       if ( filter && !( pos % filter ) &&
00926            qAbs( pos - last1 ) >= filter && qAbs( pos - last2 ) >= filter )
00927         i0 = pos;
00928       pos++;
00929     }
00930 
00931     if ( i0 != -1 )
00932       p->drawText( x, (int)( Y + i0 * step + ascent + offset ), labels[i0] );
00933   }
00934 }
00935 
00940 QString QtxColorScale::getFormat() const
00941 {
00942   QString aFormat = format();
00943   
00944   if ( !testFlags( PreciseFormat ) || testFlags( Integer ) )
00945     return aFormat;
00946   
00947   if ( !myPrecise.isEmpty() )
00948     return myPrecise;
00949   
00950   if ( !aFormat.contains( QRegExp( "^(%[0-9]*.?[0-9]*[fegFEG])$" ) ) )
00951     return aFormat;
00952   
00953   int pos1 = aFormat.indexOf( '.' );
00954   int pos2 = aFormat.indexOf( QRegExp( "[fegFEG]") );
00955   
00956   QString aLocFormat;
00957   int precision = 1;
00958   if ( pos1 > 0 )
00959   {
00960     aLocFormat = aFormat.mid( 0, pos1 + 1 );
00961     precision = aFormat.mid( pos1 + 1, pos2 - pos1 - 1 ).toInt();
00962     if ( precision < 1 )
00963       precision = 1;
00964   }
00965   else
00966     return aFormat;
00967   
00968   QtxColorScale* that = (QtxColorScale*)this;
00969   
00970   // calculate format, maximum precision limited
00971   // to 7 digits after the decimal point.
00972   while ( myPrecise.isEmpty() && precision < 7 )
00973   {
00974     QString aTmpFormat = aLocFormat;
00975     aTmpFormat += QString( "%1" ).arg( precision );
00976     aTmpFormat += aFormat.mid( pos2 );
00977     
00978     QMap<QString, int> map;
00979     bool isHasTwinz = false;
00980     
00981     for ( int idx = 0; idx < intervalsNumber() && !isHasTwinz; idx++ )
00982     {
00983       double val = getNumber( idx );
00984       QString tmpname = QString().sprintf( aTmpFormat.toLatin1(), val );
00985       isHasTwinz = map.contains( tmpname );
00986       map.insert( tmpname, 1 );
00987     }
00988     
00989     if ( !isHasTwinz )
00990       that->myPrecise = aTmpFormat;
00991     precision++;
00992   }
00993   
00994   if ( !myPrecise.isEmpty() )
00995     aFormat = myPrecise;
00996   
00997   return aFormat;
00998 }
00999 
01005 double QtxColorScale::getNumber( const int idx ) const
01006 {
01007   double val = 0;
01008   if ( intervalsNumber() > 0 )
01009     val = minimum() + idx * ( qAbs( maximum() - minimum() ) / intervalsNumber() );
01010   return val;
01011 }
01012 
01018 QString QtxColorScale::getLabel( const int idx ) const
01019 {
01020   QString res;
01021   if ( labelMode() == User )
01022     res = label( idx );
01023   else
01024   {
01025     double val = getNumber( idx );
01026     res = QString().sprintf( getFormat().toLatin1(), testFlags( Integer ) ? (int)val : val );
01027   }
01028   return res;
01029 }
01030 
01036 QColor QtxColorScale::getColor( const int idx ) const
01037 {
01038   QColor res;
01039   if ( colorMode() == User )
01040     res = color( idx );
01041   else
01042     res = Qtx::scaleColor( idx, 0, intervalsNumber() - 1 );
01043   return res;
01044 }
01045 
01049 void QtxColorScale::updateScale()
01050 {
01051   update();
01052   updateGeometry();
01053 }
01054 
01064 QTextDocument* QtxColorScale::textDocument( const int /*flags*/ ) const
01065 {
01066   QTextDocument* doc = 0;
01067 
01068   QString aTitle;
01069   switch ( titlePosition() )
01070   {
01071   case Left:
01072     aTitle = QString( "<p align=\"left\">%1</p>" );
01073     break;
01074   case Right:
01075     aTitle = QString( "<p align=\"right\">%1</p>" );
01076     break;
01077   case Center:
01078     aTitle = QString( "<p align=\"center\">%1</p>" );
01079     break;
01080   case None:
01081   default:
01082     break;
01083   }
01084   
01085   if ( !aTitle.isEmpty() && !title().isEmpty() )
01086   {
01087     /*
01088     if ( !myStyleSheet )
01089     {
01090       QtxColorScale* that = (QtxColorScale*)this;
01091       that->myStyleSheet = new QStyleSheet( that );
01092     }
01093       
01094     if ( myStyleSheet )
01095     {
01096       QStyleSheetItem* item = myStyleSheet->item( "p" );
01097       if ( item )
01098       item->setWhiteSpaceMode( flags & WrapTitle ? QStyleSheetItem::WhiteSpaceNormal :
01099       QStyleSheetItem::WhiteSpaceNoWrap );
01100     }
01101     */
01102     aTitle = aTitle.arg( title() );
01103     doc = new QTextDocument( aTitle );
01104   }
01105 
01106   return doc;
01107 }