bussplitter.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "bussplitter.h"
00012 #include "ecnode.h"
00013 #include "libraryitem.h"
00014 #include "wire.h"
00015 
00016 #include <klocale.h>
00017 #include <qpainter.h>
00018 
00019 Item* BusSplitter::construct( ItemDocument *itemDocument, bool newItem, const char *id )
00020 {
00021         return new BusSplitter( (ICNDocument*)itemDocument, newItem, id );
00022 }
00023 
00024 
00025 LibraryItem* BusSplitter::libraryItem()
00026 {
00027         return new LibraryItem(
00028                 "ec/bus",
00029                 i18n("Bus"),
00030                 i18n("Connections"),
00031                 "bus.png",
00032                 LibraryItem::lit_component,
00033                 BusSplitter::construct );
00034 }
00035 
00036 // TODO: move this to a global "limits" header. 
00037 const unsigned MAX_BUS_SIZE = 10000;
00038 
00039 
00040 BusSplitter::BusSplitter( ICNDocument *icnDocument, bool newItem, const char *id )
00041         : Component( icnDocument, newItem, id ? id : "Bus" )
00042 {
00043         m_name = i18n("Bus Splitter");
00044         m_desc = i18n("Merges several connections into one.");
00045 
00046         m_busSize = 0;  
00047         init1PinLeft();
00048         m_pInNode = m_pNNode[0];
00049 
00050         createProperty( "size", Variant::Type::Int );
00051         property("size")->setCaption( i18n("Size") );
00052         property("size")->setMinValue(1);
00053         property("size")->setMaxValue(MAX_BUS_SIZE);
00054         property("size")->setValue(8);
00055 }
00056 
00057 BusSplitter::~BusSplitter()
00058 {
00059 }
00060 
00061 void BusSplitter::dataChanged()
00062 {
00063         unsigned busSize = dataInt("size");
00064 
00065         if ( busSize < 1 ) busSize = 1;
00066         else if ( busSize > MAX_BUS_SIZE ) busSize = MAX_BUS_SIZE;
00067 
00068         if ( busSize == m_busSize ) return;
00069 
00070         m_pInNode->setNumPins(busSize);
00071 
00072         if ( busSize > m_busSize ) {
00073                 m_pWires.resize(busSize);
00074                 for ( unsigned i = m_busSize; i < unsigned(busSize); i++ )
00075                 {
00076                         Pin * pin = createPin( 16, 0, 180, outNodeID(i) )->pin();
00077                         m_pWires[i] = new Wire( m_pInNode->pin(i), pin );
00078                 }
00079         } else {
00080                 for ( unsigned i = busSize; i < unsigned(m_busSize); i++ ) {
00081                         removeNode( outNodeID(i) );
00082                         delete m_pWires[i];
00083                 }
00084                 m_pWires.resize(busSize);
00085         }
00086         m_busSize = busSize;
00087 
00088         // Position pins
00089         setSize( 0, -int(m_busSize+1)*8, 8, int(m_busSize+1)*16, true );
00090         for ( int i = 0; i < int(m_busSize); i++ )
00091                 m_nodeMap[ outNodeID(i) ].y = 16*i - int(m_busSize+1)*8 + 24;
00092         m_nodeMap["n1"].y = -int(m_busSize+1)*8 + 8;
00093 
00094         updateAttachedPositioning();
00095 }
00096 
00097 
00098 QString BusSplitter::outNodeID( unsigned node ) const
00099 {
00100         return QString("out_%1").arg(QString::number(node));
00101 }
00102 
00103 
00104 void BusSplitter::drawShape( QPainter &p )
00105 {
00106         initPainter(p);
00107 
00108 //      QPen pen(p.pen());
00109 //      pen.setWidth();
00110 //      p.setPen(pen);
00111 
00112         int _x = int(x());
00113         int _y = int(y());
00114 
00115         QRect r = m_sizeRect;
00116         r.moveBy( _x, _y );
00117         p.drawRect(r);
00118 
00119         deinitPainter(p);
00120 }
00121 
00122 
00123 

Generated on Tue May 8 17:05:28 2007 for KTechLab by  doxygen 1.5.1