summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/pushswitch.cpp
blob: 5b926d6296c699f24d93fb7fafa2c08ff02c8045 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/***************************************************************************
 *   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 "canvasitemparts.h"
#include "libraryitem.h"
#include "pushswitch.h"
#include "switch.h"

#include <tdelocale.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqpoint.h>
#include <tqpointarray.h>

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

LibraryItem* ECPTBSwitch::libraryItem()
{
	return new LibraryItem(
		TQString("ec/ptb_switch"),
		i18n("Push-to-Break"),
		i18n("Switches"),
		"ptb.png",
		LibraryItem::lit_component,
		ECPTBSwitch::construct );
}

ECPTBSwitch::ECPTBSwitch( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "ptb_switch" )
{
	m_name = i18n("Push to Break");
	setSize( -16, -16, 32, 24 );
	
	addButton( "button", TQRect( -16, 8, 32, 20 ), "" );
	
	createProperty( "button_text", Variant::Type::String );
	property("button_text")->setCaption( i18n("Button Text") );
	
	Variant * v = createProperty( "bounce", Variant::Type::Bool );
	v->setCaption( i18n("Bounce") );
	v->setAdvanced(true);
	v->setValue(false);
	
	v = createProperty( "bounce_period", Variant::Type::Double );
	v->setCaption( i18n("Bounce Period") );
	v->setAdvanced(true);
	v->setUnit("s");
	v->setValue(5e-3);
	
	init1PinLeft(0);
	init1PinRight(0);
	
	m_switch = createSwitch( m_pPNode[0], m_pNNode[0], false );
	pressed = false;
}

ECPTBSwitch::~ECPTBSwitch()
{
}


void ECPTBSwitch::dataChanged()
{
	button("button")->setText( dataString("button_text") );
	
	bool bounce = dataBool("bounce");
	int bouncePeriod_ms = int(dataDouble("bounce_period")*1e3);
	m_switch->setBounce( bounce, bouncePeriod_ms );
}


void ECPTBSwitch::drawShape( TQPainter &p )
{
	initPainter(p);
	
	int _x = (int)x()-16;
	int _y = (int)y()-8;
	const int radius = 2;
	const int _height = height()-8;
	
	int dy = pressed ? 6 : 4;
	
	p.drawLine( _x+width()/4,	_y+dy,						_x+(3*width())/4,	_y+dy ); // Top horizontal line
	p.drawLine( _x,				_y+(_height/2)-radius+dy,	_x+width(),			_y+(_height/2)-radius+dy ); // Bottom horizontal line
	p.drawLine( _x+width()/2,	_y+dy,						_x+width()/2,		_y+(_height/2)-radius+dy ); //Qt::Vertical line
	
	p.drawEllipse( _x,						_y+(_height/2)-radius, 2*radius, 2*radius ); // Left circle
	p.drawEllipse( _x+width()-2*radius+1,	_y+(_height/2)-radius, 2*radius, 2*radius ); // Right circle

	deinitPainter(p);
}

void ECPTBSwitch::buttonStateChanged( const TQString &id, bool state )
{
	if ( id != "button" )
		return;
	m_switch->setState( state ? Switch::Open : Switch::Closed );
	pressed = state;
}
//END class ECPTBSwitch


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

LibraryItem* ECPTMSwitch::libraryItem()
{
	return new LibraryItem(
		TQString("ec/ptm_switch"),
		i18n("Push-to-Make"),
		i18n("Switches"),
		"ptm.png",
		LibraryItem::lit_component,
		ECPTMSwitch::construct );
}

ECPTMSwitch::ECPTMSwitch( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "ptm_switch" )
{
	m_name = i18n("Push to Make");
	setSize( -16, -16, 32, 24 );
	
	addButton( "button", TQRect( -16, 8, 32, 20 ), "" );
	
	createProperty( "button_text", Variant::Type::String );
	property("button_text")->setCaption( i18n("Button Text") );
	
	Variant * v = createProperty( "bounce", Variant::Type::Bool );
	v->setCaption("Bounce");
	v->setAdvanced(true);
	v->setValue(false);
	
	v = createProperty( "bounce_period", Variant::Type::Double );
	v->setCaption("Bounce Period");
	v->setAdvanced(true);
	v->setUnit("s");
	v->setValue(5e-3);
	
	init1PinLeft(0);
	init1PinRight(0);
	
	m_switch = createSwitch( m_pPNode[0], m_pNNode[0], true );
	pressed = false;
}


ECPTMSwitch::~ECPTMSwitch()
{
}


void ECPTMSwitch::dataChanged()
{
	button("button")->setText( dataString("button_text") );
	
	bool bounce = dataBool("bounce");
	int bouncePeriod_ms = int(dataDouble("bounce_period")*1e3);
	m_switch->setBounce( bounce, bouncePeriod_ms );
}


void ECPTMSwitch::drawShape( TQPainter &p )
{
	initPainter(p);
	
	int _x = (int)x()-16;
	int _y = (int)y()-8;
	const int radius = 2;
	const int _height = height()-8;
	
	int dy = pressed ? 1 : 3;
	
	p.drawLine( _x+width()/4,	_y-dy,						_x+(3*width())/4,	_y-dy ); // Top horizontal line
	p.drawLine( _x,				_y+(_height/2)-radius-dy,	_x+width(),			_y+(_height/2)-radius-dy ); // Bottom horizontal line
	p.drawLine( _x+width()/2,	_y-dy,						_x+width()/2,		_y+(_height/2)-radius-dy ); //Qt::Vertical line
	
	p.drawEllipse( _x,						_y+(_height/2)-radius, 2*radius, 2*radius ); // Left circle
	p.drawEllipse( _x+width()-2*radius+1,	_y+(_height/2)-radius, 2*radius, 2*radius ); // Right circle

	deinitPainter(p);
}

void ECPTMSwitch::buttonStateChanged( const TQString &id, bool state )
{
	if ( id != "button" ) return;
	m_switch->setState( state ? Switch::Closed : Switch::Open );
	pressed = state;
}
//END class ECPTMSwitch