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

QImage ItemLibrary::itemImage ( Item item,
const uint  maxSize = 36000 
)

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

Parameters:
cnItem A pointer to the CNItem
maxSize The maximum size (in pixels) before the image is cached

Definition at line 337 of file itemlibrary.cpp.

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

{
      Component *component = dynamic_cast<Component*>(item);
      
    QRect bound = item->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(item->type()) )
            return m_imageMap[item->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( item->pen() );
      p.setBrush( item->brush() );
      
      // Now draw the shape :-)
      const bool sel = item->isSelected();
      if (sel)
      {
            // We block the signals as we end up in an infinite loop with cnitem emitting a selected signal
            item->blockSignals(true);
            item->setSelected(false);
            item->blockSignals(false);
      }
      item->drawShape(p);
      item->drawShape(maskPainter);
      if (sel)
      {
            item->blockSignals(true);
            item->setSelected(sel);
            item->blockSignals(false);
      }
      
      maskPainter.setPen( Qt::color1 );
      maskPainter.setBrush( Qt::color1 );
      
      QWMatrix transMatrix; // Matrix to apply to the image
      
      CNItem *cnItem = dynamic_cast<CNItem*>(item);
      if (cnItem)
      {
            NodeMap nodes = cnItem->nodeMap();
            const NodeMap::iterator nodesEnd = nodes.end();
            for ( NodeMap::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)  )
                  {
                        const bool showVB = ecnode->showVoltageBars();
                        ecnode->setShowVoltageBars(false);
                        ecnode->drawShape(p);
                        ecnode->drawShape(maskPainter);
                        ecnode->setShowVoltageBars(showVB);
                  }
                  else
                  {
                        node->drawShape(p);
                        node->drawShape(maskPainter);
                  }
                  if (sel)
                        node->setSelected(sel);
            }
            
            p.setPen(Qt::black);
            TextMap text = cnItem->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 );
            cnItem->drawWidgets(p);
//          cnItem->drawWidgets(maskPainter);
            
            transMatrix = Component::transMatrix( component->angleDegrees(), component->flipped(), bound.width()/2, bound.height()/2, true );
      }
      
      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.xForm(transMatrix);
      im = im.smoothScale( 50, 50, QImage::ScaleMin );
      
      if (cache)
            m_imageMap[item->type()] = im;
      
      return im;
}


Generated by  Doxygen 1.6.0   Back to index