Logo Search packages:      
Sourcecode: ktechlab version File versions  Download package

QImage ItemLibrary::componentImage ( Component component,
const uint  maxSize = 36000 
)

Returns an image of the given component. As QPixmap::convertToImage is a slow function, this will cache the result and return that for large images.

Parameters:
component A pointer to the Component.
maxSize The maximum size (in pixels) before the image is cached.

Definition at line 384 of file itemlibrary.cpp.

References Component::angleDegrees(), Node::drawShape(), CNItem::drawShape(), CIWidgetMgr::drawWidgets(), Component::flipped(), CNItem::nodeMap(), Component::setAngleDegrees(), Component::setFlipped(), Item::setSelected(), CNItem::textMap(), Item::type(), and Item::width().

{
      // Default orientation for painting
      const int angleDegrees = component->angleDegrees();
      const bool flipped = component->flipped();
      component->setAngleDegrees( 0 );
      component->setFlipped( false );
      
      QRect bound = component->boundingRect().normalize();
      bound.setLeft( bound.left()-8 );
      bound.setRight( bound.right()+8 );
      bound.setTop( bound.top()-8 );
      bound.setBottom( bound.bottom()+8 );

      // We want a nice square bounding rect
      const int dy = bound.width() - bound.height();
      if ( dy > 0 ) {
            bound.setTop( bound.top()-(dy/2) );
            bound.setBottom( bound.bottom()+(dy/2) );
      } else if ( dy < 0 ) {
            bound.setLeft( bound.left()+(dy/2) );
            bound.setRight( bound.right()-(dy/2) );
      }
      
      const bool cache = ((bound.width()*bound.height()) > (int)maxSize);
      QString type;
      if ( cache && m_imageMap.contains(component->type()) )
            return m_imageMap[component->type()];
      
      // Create pixmap big enough to contain CNItem and surrounding nodes
      // and copy the button grab to it
      
      QPixmap pm( bound.size() );
      
      QBitmap mask( bound.size() );
      mask.fill( Qt::color0 );
      
      QPainter maskPainter(&mask);
      maskPainter.translate( -bound.x(), -bound.y() );
      maskPainter.setPen( Qt::color1 );
      maskPainter.setBrush( Qt::color1 );
      
      
      QPainter p(&pm);
      p.translate( -bound.x(), -bound.y() );
      p.setPen( component->pen() );
      p.setBrush( component->brush() );
      
      //BEGIN Draw the component
      const bool sel = component->isSelected();
      
      if (sel) {
            // We block the signals as we end up in an infinite loop with component emitting a selected signal
            component->blockSignals(true);
            component->setSelected(false);
            component->blockSignals(false);
      }
      
      component->drawShape(p);
      component->drawShape(maskPainter);
      
      if (sel) {
            component->blockSignals(true);
            component->setSelected(sel);
            component->blockSignals(false);
      }
      //END Draw the component
      
      maskPainter.setPen( Qt::color1 );
      maskPainter.setBrush( Qt::color1 );
      
      QWMatrix transMatrix; // Matrix to apply to the image
      
      
      NodeInfoMap nodes = component->nodeMap();
      const NodeInfoMap::iterator nodesEnd = nodes.end();
      for ( NodeInfoMap::iterator it = nodes.begin(); it != nodesEnd; ++it )
      {
            Node *node = it.data().node;
            const bool sel = node->isSelected();
            if (sel)
                  node->setSelected(false);
            if ( ECNode *ecnode = dynamic_cast<ECNode*>(node)  )
            {
                  bool showVB = ecnode->showVoltageBars();
                  bool showVC = ecnode->showVoltageColor();
                        
                  ecnode->setShowVoltageBars( false );
                  ecnode->setShowVoltageColor( false );
                        
                  ecnode->drawShape(p);
                  ecnode->drawShape( maskPainter );
                        
                  ecnode->setShowVoltageBars( showVB );
                  ecnode->setShowVoltageColor( showVC );
            } else {
                  node->drawShape(p);
                  node->drawShape(maskPainter);
            }

            if (sel) node->setSelected(sel);
      }
            
      p.setPen(Qt::black);
      TextMap text = component->textMap();
      const TextMap::iterator textEnd = text.end();
      for ( TextMap::iterator it = text.begin(); it != textEnd; ++it )
      {
            it.data()->drawShape(p);
            it.data()->drawShape(maskPainter);
      }
            
//    maskPainter.setPen( Qt::color1 );
//    maskPainter.setBrush( Qt::color1 );
      component->drawWidgets(p);
//    component->drawWidgets(maskPainter);
      
      pm.setMask(mask);
            
      // Now, rotate the image so that it's the right way up, and scale it to size
      QImage im = pm.convertToImage();
      im = im.smoothScale( 50, 50, QImage::ScaleMin );
      
      if (cache)
            m_imageMap[component->type()] = im;
      
      // Restore original orientation
      component->setAngleDegrees( angleDegrees );
      component->setFlipped( flipped );
      
      return im;
}


Generated by  Doxygen 1.6.0   Back to index