1
/***************************************************************************
2
* Copyright (C) 2018 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
21
#include "simulator.h"
23
#include "itemlibrary.h"
25
static const char* KeyPad_properties[] = {
26
QT_TRANSLATE_NOOP("App::Property","Key Labels")
29
Component* KeyPad::construct( QObject* parent, QString type, QString id )
30
{ return new KeyPad( parent, type, id ); }
32
LibraryItem* KeyPad::libraryItem()
34
return new LibraryItem(
42
KeyPad::KeyPad( QObject* parent, QString type, QString id )
43
: Component( parent, type, id )
46
Q_UNUSED( KeyPad_properties );
50
setLabelPos(-8,-16, 0);
52
m_keyLabels = "123456789*0#";
61
for( int row=0; row<m_rows; row++ )
63
Pin* rowPin = m_pin[row];
64
eNode* rowNode = rowPin->getEnode();
66
for( int col=0; col<m_cols; col++ )
68
Pin* colPin = m_pin[m_rows+col];
69
eNode* colNode = colPin->getEnode();
71
PushBase* button = m_buttons.at( row*m_cols+col );
73
ePin* epin0 = button->getEpin( 0 );
74
epin0->setEnode( rowNode );
76
ePin* epin1 = button->getEpin( 1 );
77
epin1->setEnode( colNode );
82
void KeyPad::setupButtons()
84
bool pauseSim = Simulator::self()->isRunning();
85
if( pauseSim ) Simulator::self()->pauseSim();
87
m_area = QRectF( -12, -4, 16*m_cols+8, 16*m_rows+8 );
89
for( PushBase* button : m_buttons )
91
m_buttons.removeOne( button );
92
Circuit::self()->removeComp( button );
95
for( Pin* pin : m_pin )
97
if( pin->isConnected() ) pin->connector()->remove();
98
if( pin->scene() ) Circuit::self()->removeItem( pin );
101
m_pin.resize( m_rows + m_cols );
103
int labelMax = m_keyLabels.size()-1;
105
for( int row=0; row<m_rows; row++ )
107
QString pinId = m_id;
108
pinId.append( QString("-Pin")+QString::number(row)) ;
109
QPoint pinPos = QPoint(m_cols*16, 8+row*16);
110
m_pin[row] = new Pin( 0, pinPos, pinId, 0, this);
112
for( int col=0; col<m_cols; col++ )
114
QString butId = m_id+"button"+QString::number(row)+QString::number(col);
116
PushBase* button = new PushBase( this, "PushBase", butId );
117
button->SetupButton();
118
button->setParentItem( this );
119
button->setPos( QPointF(col*16+12, 16+row*16 ) );
120
button->setFlag( QGraphicsItem::ItemIsSelectable, false );
121
m_buttons.append( button );
123
int pos = row*m_cols+col;
124
QString buttonLabel = "";
125
if( pos <= labelMax ) buttonLabel = m_keyLabels.mid( pos, 1 );
126
button->setKey( buttonLabel );
130
QString pinId = m_id;
131
pinId.append( QString("-Pin")+QString::number(m_rows+col)) ;
132
QPoint pinPos = QPoint( col*16, m_rows*16+8);
133
m_pin[m_rows+col] = new Pin( 270, pinPos, pinId, 0, this);
137
if( pauseSim ) Simulator::self()->resumeSim();
138
Circuit::self()->update();
141
double KeyPad::rows()
146
void KeyPad::setRows( double rows )
152
double KeyPad::cols()
157
void KeyPad::setCols( double cols )
163
QString KeyPad::keyLabels()
167
void KeyPad::setKeyLabels( QString keyLabels )
169
m_keyLabels = keyLabels;
171
int labelMax = m_keyLabels.size()-1;
173
for( int row=0; row<m_rows; row++ )
175
for( int col=0; col<m_cols; col++ )
177
PushBase* button = m_buttons.at( row*m_cols+col );
179
int pos = row*m_cols+col;
180
QString buttonLabel = "";
181
if( pos <= labelMax ) buttonLabel = m_keyLabels.mid( pos, 1 );
182
button->setKey( buttonLabel );
187
void KeyPad::remove()
189
for( PushBase* button : m_buttons )
190
Circuit::self()->removeComp( button );
195
void KeyPad::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget )
197
Component::paint( p, option, widget );
199
p->drawRoundedRect( m_area,2,2 );
202
#include "moc_keypad.cpp"