summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/bussplitter.cpp
blob: 85619a494e5fee7b731632380692299c1b92ace8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "bussplitter.h"
#include "ecnode.h"
#include "libraryitem.h"
#include "wire.h"

#include <klocale.h>
#include <tqpainter.h>

Item* BusSplitter::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new BusSplitter( (ICNDocument*)itemDocument, newItem, id );
}


LibraryItem* BusSplitter::libraryItem()
{
	return new LibraryItem(
		"ec/bus",
		i18n("Bus"),
		i18n("Connections"),
		"bus.png",
		LibraryItem::lit_component,
		BusSplitter::construct );
}


const unsigned MAX_BUS_SIZE = 10000;


BusSplitter::BusSplitter( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id ? id : "Bus" )
{
	m_name = i18n("Bus Splitter");
	m_desc = i18n("Merges several connections into one.");

	m_busSize = 0;	
	init1PinLeft();
	m_pInNode = m_pNNode[0];
	
	createProperty( "size", Variant::Type::Int );
	property("size")->setCaption( i18n("Size") );
	property("size")->setMinValue(1);
	property("size")->setMaxValue(MAX_BUS_SIZE);
	property("size")->setValue(8);
}


BusSplitter::~BusSplitter()
{
}


void BusSplitter::dataChanged()
{
	unsigned busSize = dataInt("size");
	
	if ( busSize < 1 )
		busSize = 1;
	
	else if ( busSize > MAX_BUS_SIZE )
		busSize = MAX_BUS_SIZE;
	
	if ( busSize == m_busSize )
		return;
	
	m_pInNode->setNumPins(busSize);
	
	if ( busSize > m_busSize )
	{
		m_pWires.resize(busSize);
		for ( unsigned i = m_busSize; i < unsigned(busSize); i++ )
		{
			Pin * pin = createPin( 16, 0, 180, outNodeID(i) )->pin();
			m_pWires[i] = new Wire( m_pInNode->pin(i), pin );
		}
	}
	else
	{
		for ( unsigned i = busSize; i < unsigned(m_busSize); i++ )
		{
			removeNode( outNodeID(i) );
			delete m_pWires[i];
		}
		m_pWires.resize(busSize);
	}
	m_busSize = busSize;
	
	// Position pins
	setSize( 0, -int(m_busSize+1)*8, 8, int(m_busSize+1)*16, true );
	for ( int i = 0; i < int(m_busSize); i++ )
		m_nodeMap[ outNodeID(i) ].y = 16*i - int(m_busSize+1)*8 + 24;
	m_nodeMap["n1"].y = -int(m_busSize+1)*8 + 8;
	
	updateAttachedPositioning();
}


TQString BusSplitter::outNodeID( unsigned node ) const
{
	return TQString("out_%1").tqarg(TQString::number(node));
}


void BusSplitter::drawShape( TQPainter &p )
{
	initPainter(p);
	
// 	TQPen pen(p.pen());
// 	pen.setWidth();
// 	p.setPen(pen);
	
	int _x = int(x());
	int _y = int(y());
	
	TQRect r = m_sizeRect;
	r.moveBy( _x, _y );
	p.drawRect(r);
	
	deinitPainter(p);
}