Пример #1
0
void WinBarDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    if(index.isValid() && index.data(ChannelData).isValid()){
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing);

        /**************配置*****************/
        QRectF rect(option.rect.marginsAdded(margins));
        if(option.state & QStyle::State_Sunken && option.state & QStyle::State_HasFocus){
            rect.moveTo(rect.x() + 1, rect.y() + 1);
        }
        QRectF topRect(rect);
        topRect.setHeight(rect.height()/6);
        QFont font = option.font;
        font.setFamily(font.defaultFamily());

        /*****************色卡*****************/
        QRectF colorRect(topRect);
        colorRect.setHeight(topRect.height()/5 * 3);
        painter->setPen(QColor(0,0,0,0));
        painter->setBrush(QColor(index.data(ChannelColor).value<QColor>()));
        painter->drawRoundedRect(colorRect,5,5);

        /****************头部背景色**************/
        QRectF headRect(topRect);
        headRect.setTop(colorRect.center().y());
        painter->setBrush(QBrush(QColor(240,240,240)));
        painter->drawRect(headRect);

        /******************底部背景色*******************/
        QRectF bottomRect(rect);
        bottomRect.setTop(topRect.bottom() + 2);
        painter->setPen(QColor(0,0,0,0));
        painter->setBrush(QBrush(QColor(240,240,240)));
        painter->drawRect(bottomRect);

        /******************色柱********************/
        QRectF barRect(bottomRect);
        quint8 base = index.data(ChannelBarBase).toUInt();
        qreal mesureVal = index.data(ChannelData).toDouble();
        qreal rangeUpper = index.data(ChannelDispUpper).toDouble();
        qreal rangeDowner = index.data(ChannelDispDowner).toDouble();
        if(mesureVal >= 0){
            barRect.setTop(barRect.top() + barRect.height()/2 * (1 - mesureVal/rangeUpper));
        }else if(mesureVal < 0){
            barRect.setTop(barRect.top() + barRect.height()/2 * (1 + mesureVal/rangeDowner));
        }
        if(base == 1){
            barRect.setBottom(bottomRect.center().y());
        }else if(base == 2){
            barRect.setBottom(bottomRect.top());
        }
        painter->setPen(QColor(0,0,0,0));
        painter->setBrush(QColor(index.data(ChannelColor).value<QColor>()));
        painter->drawRect(barRect);

        /****************光泽效果*****************/
        QLinearGradient skinColor(rect.topLeft(),rect.topRight());
        skinColor.setColorAt(0,QColor(255,255,255,0));
        skinColor.setColorAt(0.09,QColor(255,255,255,0));
        skinColor.setColorAt(0.1,QColor(255,255,255,200));
        skinColor.setColorAt(0.13,QColor(255,255,255,200));
        skinColor.setColorAt(0.14,QColor(255,255,255,80));
        skinColor.setColorAt(0.50,QColor(0,0,0,20));
        skinColor.setColorAt(0.75,QColor(0,0,0,10));
        skinColor.setColorAt(0.76,QColor(255,255,255,30));
        skinColor.setColorAt(0.95,QColor(255,255,255,150));
        skinColor.setColorAt(0.96,QColor(255,255,255,0));
        skinColor.setColorAt(1,QColor(255,255,255,0));
        painter->setPen(QPen(QColor(200,230,255,0)));
        painter->setBrush(skinColor);
        painter->drawRect(rect);

        /*****************刻度*********************/
        qreal baseX = bottomRect.right();
        qreal baseY = bottomRect.top();
        qreal len = bottomRect.width()/8;
        quint8 count = index.data(ChannelBarDiv).toUInt();
        qreal span = bottomRect.height()/count;
        painter->setPen(Qt::black);
        painter->setBrush(QBrush(QColor(0,0,0,0)));
        for(quint8 i=1; i<count; ++i){
            painter->drawLine(baseX - len, baseY + i*span, baseX, baseY + i*span);
        }

        /**************量程*****************/
        QRectF valRect(bottomRect);
        valRect.setHeight(span);
        font.setPixelSize(valRect.height()/2);
        painter->setFont(font);
        painter->setPen(QPen(Qt::black,1));
        painter->drawText(valRect,QString::number(rangeUpper,'f',4));
        valRect.moveBottom(bottomRect.bottom());
        painter->drawText(valRect,Qt::AlignBottom,QString::number(rangeDowner,'f',4));

        /**************测量值*********************/
        valRect.moveBottom(barRect.top());
        if(valRect.top() < baseY + span){
            valRect.moveTop(baseY + span);
        }else if(valRect.bottom() > bottomRect.bottom() - 2 * span){
            valRect.moveBottom(bottomRect.bottom() - 2 * span);
        }
        painter->drawText(valRect,Qt::AlignBottom|Qt::AlignHCenter,QString::number(mesureVal,'f',4));

        /*******************单位***********************/
        valRect.moveTop(valRect.bottom());
        valRect.setRight(baseX - len);
        //painter->setPen(QColor(100,100,100));
        painter->drawText(valRect,Qt::AlignRight|Qt::AlignTop,index.data(ChannelUnit).toString());

        /****************标记********************/
        font.setPixelSize(headRect.height()/2);
        painter->setFont(font);
        painter->setPen(QColor(100,100,100));
        painter->drawText(headRect,Qt::AlignCenter,index.data(ChannelTag).toString());

        /*****************擦除头尾连接**********************/
        painter->setBrush(QBrush(QColor(0,0,0,0)));
        painter->setPen(QPen(Qt::black,1));
        painter->drawRect(bottomRect);
        painter->eraseRect(QRectF(topRect.bottomLeft(), bottomRect.topRight()));

        /*********************按压效果*************************/
        if(option.state & QStyle::State_Sunken && option.state & QStyle::State_HasFocus){
            painter->setPen(QPen(QColor(0,0,255,100),3));
            painter->setBrush(QBrush(QColor(180,180,180,50)));
            painter->drawRoundedRect(rect,6,6);
        }

        painter->restore();
    }
}
Пример #2
0
// -------------------------------------------------------------------------
void ctkQImageView::update( bool zoomChanged,
  bool sizeChanged )
{
  Q_D( ctkQImageView );
  if( d->SliceNumber >= 0 && d->SliceNumber < d->ImageList.size() )
    {
    const QImage * img = & ( d->ImageList[ d->SliceNumber ] );
    if( zoomChanged || sizeChanged )
      {
      if( this->width() > 0 &&  this->height() > 0 
        && d->TmpXMax > d->TmpXMin && d->TmpYMax > d->TmpYMin)
        {
        int tmpXRange = d->TmpXMax - d->TmpXMin;
        int tmpYRange = d->TmpYMax - d->TmpYMin;
        double tmpAspectRatio = static_cast<double>(tmpYRange) / tmpXRange;
        double screenAspectRatio = static_cast<double>(this->height())
          / this->width();
        if( screenAspectRatio > tmpAspectRatio )
          {
          int extraTmpYAbove = d->TmpYMin;
          int extraTmpYBelow = img->height() - d->TmpYMax;
          int extraTmpYNeeded = tmpXRange * screenAspectRatio 
            - tmpYRange;
          int minExtra = extraTmpYAbove;
          if( extraTmpYBelow < minExtra )
            {
            minExtra = extraTmpYBelow;
            }
          if(2 * minExtra >= extraTmpYNeeded)
            {
            int minNeeded = extraTmpYNeeded / 2.0;
            int maxNeeded = extraTmpYNeeded - minNeeded;
            d->TmpYMin -= minNeeded;
            d->TmpYMax += maxNeeded;
            }
          else if(extraTmpYAbove + extraTmpYBelow >= extraTmpYNeeded)
            {
            if(extraTmpYAbove < extraTmpYBelow)
              {
              d->TmpYMin = 0;
              d->TmpYMax += extraTmpYNeeded - extraTmpYAbove;
              }
            else
              {
              d->TmpYMax = img->height();
              d->TmpYMin -= extraTmpYNeeded - extraTmpYBelow;
              }
            }
          else
            {
            d->TmpYMin = 0;
            d->TmpYMax = img->height();
            }
          d->TmpImage = QPixmap( this->width(),
            static_cast<unsigned int>( 
              static_cast<double>(d->TmpYMax - d->TmpYMin) 
              / (d->TmpXMax - d->TmpXMin)
              * this->width() + 0.5 ) );
          }
        else if(screenAspectRatio < tmpAspectRatio)
          {
          int extraTmpXLeft = d->TmpXMin;
          int extraTmpXRight = img->width() - d->TmpXMax;
          int extraTmpXNeeded = static_cast<double>(tmpYRange) 
            / screenAspectRatio - tmpXRange;
          int minExtra = extraTmpXLeft;
          if( extraTmpXRight < minExtra )
            {
            minExtra = extraTmpXRight;
            }
          if(2 * minExtra >= extraTmpXNeeded)
            {
            int minNeeded = extraTmpXNeeded / 2.0;
            int maxNeeded = extraTmpXNeeded - minNeeded;
            d->TmpXMin -= minNeeded;
            d->TmpXMax += maxNeeded;
            }
          else if(extraTmpXLeft + extraTmpXRight >= extraTmpXNeeded)
            {
            if(extraTmpXLeft < extraTmpXRight)
              {
              d->TmpXMin = 0;
              d->TmpXMax += extraTmpXNeeded - extraTmpXLeft;
              }
            else
              {
              d->TmpXMax = img->width();
              d->TmpXMin -= extraTmpXNeeded - extraTmpXRight;
              }
            }
           else
            {
            d->TmpXMin = 0;
            d->TmpXMax = img->width();
            }
          d->TmpImage = QPixmap( static_cast<unsigned int>( this->height()
            / ( static_cast<double>(d->TmpYMax - d->TmpYMin) 
            / (d->TmpXMax - d->TmpXMin) ) 
            + 0.5 ), this->height() );
          }
        else
          {
          d->TmpImage = QPixmap( this->width(),  this->height() );
          }
        }
      }

    if( d->TmpImage.width() > 0 &&  d->TmpImage.height() > 0)
      {
      QRectF target( 0, 0, d->TmpImage.width(), d->TmpImage.height() );
      double sourceX = d->TmpXMin;
      double sourceY = d->TmpYMin;
      double sourceW = d->TmpXMax - d->TmpXMin;
      double sourceH = d->TmpYMax - d->TmpYMin;
      QPainter painter( &(d->TmpImage) );
      QImage tmpI = *img;
      if( d->InvertImage )
        {
        tmpI.invertPixels();
        }
      if( d->FlipXAxis || d->FlipYAxis )
        {
        tmpI = tmpI.mirrored( d->FlipXAxis, d->FlipYAxis );
        if( d->FlipXAxis )
          {
          sourceX = tmpI.width() - (d->TmpXMax - d->TmpXMin) - d->TmpXMin;
          }
        if( d->FlipYAxis )
          {
          sourceY = tmpI.height() - (d->TmpYMax - d->TmpYMin) - d->TmpYMin;
          }
        }
      QRectF source( sourceX, sourceY, sourceW, sourceH );
      painter.drawPixmap( target, QPixmap::fromImage( tmpI ), source );

      //if( ! sizeChanged )
        {
        int maxNumCharsPerLine = 50;
        int fontPointSize = 12;
        if( fontPointSize * maxNumCharsPerLine > this->width() )
          {
          fontPointSize = this->width() / maxNumCharsPerLine;
          }
        if( fontPointSize > 7 )
          {
          QString fontFamily( "Helvetica" );
          QFont textFont( fontFamily, fontPointSize );
          painter.setFont( textFont );
          QColor textColor;
          textColor.setNamedColor( "lime" );
          textColor.setAlpha( 128 );
          painter.setPen( textColor );
    
          int textFlags = Qt::AlignLeft | Qt::TextDontClip;
    
          QRectF pointRect( 0, 0, 1, 1 );
          QRectF spaceBound = painter.boundingRect( pointRect, textFlags,
            "X" );
    
          if( img->isGrayscale() )
            {
            QString intString = "Intensity Range = ";
            intString.append( QString::number( d->IntensityMin,
              'f', 3 ) );
            intString.append( " - " );
            intString.append( QString::number( d->IntensityMax,
              'f', 3 ) );
            QRectF intBound = painter.boundingRect( pointRect, textFlags,
              intString );
            QRectF intRect( 
              spaceBound.width()/2,
              spaceBound.height()/8,
              intBound.width(), intBound.height() );
            painter.drawText( intRect, textFlags, intString,
              &intBound );

            QString wlString = "W / L = ";
            wlString.append( QString::number( this->intensityWindow(),
              'f', 3 ) );
            wlString.append( " / " );
            wlString.append( QString::number( this->intensityLevel(),
              'f', 3 ) );
            QRectF wlBound = painter.boundingRect( pointRect, textFlags,
              wlString );
            QRectF wlRect( 
              spaceBound.width()/2,
              intRect.y() + intRect.height() + spaceBound.height()/8,
              wlBound.width(), wlBound.height() );
            painter.drawText( wlRect, textFlags, wlString,
              &wlBound );
            }

          QString spacingString = "Spacing = ";
          spacingString.append( QString::number( this->xSpacing(),
            'f', 3 ) );
          spacingString.append( ", " );
          spacingString.append( QString::number( this->ySpacing(),
            'f', 3 ) );
          spacingString.append( ", " );
          spacingString.append( QString::number( this->sliceThickness(),
            'f', 3 ) );
          QRectF spacingBound = painter.boundingRect( pointRect, textFlags,
            spacingString );
          QRectF spacingRect( 
            this->width() - spacingBound.width() - spaceBound.width()/2,
            this->height() - spacingBound.height() - spaceBound.height()/8,
            spacingBound.width(), spacingBound.height() );
          painter.drawText( spacingRect, textFlags, spacingString,
            &spacingBound );
    
          QString dimString = "Size = ";
          dimString.append( 
            QString::number( d->ImageList[ d->SliceNumber ].width() ) );
          dimString.append( ", " );
          dimString.append( 
            QString::number( d->ImageList[ d->SliceNumber ].height() ) );
          dimString.append( ", " );
          dimString.append( 
            QString::number( d->ImageList.size() ) );
          QRectF dimBound = painter.boundingRect( pointRect, textFlags,
            dimString );
          QRectF dimRect( 
            this->width() - dimBound.width() - spaceBound.width()/2,
            spacingBound.y() - dimBound.height() - spaceBound.height()/8,
            dimBound.width(), dimBound.height() );
          painter.drawText( dimRect, textFlags, dimString, &dimBound );
    
          QString rasString = "RAS = ";
          rasString.append( QString::number( 
            this->xPosition() * this->xSpacing(), 'f', 3 ) );
          rasString.append( ", " );
          rasString.append( QString::number( 
            this->yPosition() * this->ySpacing(), 'f', 3 ) );
          rasString.append( ", " );
          rasString.append( QString::number( 
            this->slicePosition() * this->sliceSpacing(), 'f', 3 ) );
          QRectF rasBound = painter.boundingRect( pointRect, textFlags,
            rasString );
          QRectF rasRect( 
            spaceBound.width()/2,
            this->height() - rasBound.height() - spaceBound.height()/8,
            rasBound.width(), rasBound.height() );
          painter.drawText( rasRect, textFlags, rasString,
            &rasBound );
    
          QString ijkString = "IJK = ";
          ijkString.append( QString::number( this->xPosition() ) );
          ijkString.append( ", " );
          ijkString.append( QString::number( this->yPosition() ) );
          ijkString.append( ", " );
          ijkString.append( QString::number( this->slicePosition() ) );
          QRectF ijkBound = painter.boundingRect( pointRect, textFlags,
            ijkString );
          QRectF ijkRect( 
            spaceBound.width()/2,
            rasBound.y() - ijkBound.height() - spaceBound.height()/8,
            ijkBound.width(), ijkBound.height() );
          painter.drawText( ijkRect, textFlags, ijkString, &ijkBound );
    
          QString valString = "Value = ";
          valString.append( QString::number( this->positionValue(),
            'f', 3 ) );
          QRectF valBound = painter.boundingRect( pointRect, textFlags,
            valString );
          QRectF valRect( 
            spaceBound.width()/2,
            ijkBound.y() - valBound.height() - spaceBound.height()/8,
            valBound.width(), valBound.height() );
          painter.drawText( valRect, textFlags, valString, &valBound );
          }
    
        QColor lineColor;
        lineColor.setNamedColor( "red" );
        lineColor.setAlpha( 128 );
        painter.setPen( lineColor );
        double x = ( this->xPosition() - d->TmpXMin ) 
          / (d->TmpXMax - d->TmpXMin) * this->width();
        double y = ( this->yPosition() - d->TmpYMin ) 
          / (d->TmpYMax - d->TmpYMin) * this->height();
        if( d->FlipXAxis )
          {
          x = this->width() - x;
          }
        if( d->FlipYAxis )
          {
          y = this->height() - y;
          }
        QLine lineX( x, 0, x, this->height() );
        painter.drawLine( lineX );
        QLine lineY( 0, y, this->width(), y );
        painter.drawLine( lineY );
        }
      }

    d->Window->setPixmap( d->TmpImage );
    }
  else
    {
    d->Window->setText( "No Image Loaded." );
    }
}