1 /***************************************************************************
2  *   Copyright (C) 2005 by David Saxton                                    *
3  *   david@bluehaze.org                                                    *
4  *                                                                         *
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 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #include "asminfo.h"
12 #include "microinfo.h"
13 #include "microlibrary.h"
14 #include "microselectwidget.h"
15 
16 #include <KComboBox>
17 #include <KLocalizedString>
18 
19 // #include <q3groupbox.h>
20 #include <QLayout>
21 #include <QLabel>
22 #include <QVariant>
23 
MicroSelectWidget(QWidget * parent,const char * name,Qt::WFlags)24 MicroSelectWidget::MicroSelectWidget( QWidget* parent, const char* name, Qt::WFlags )
25 	//: Q3GroupBox( 4, Qt::Horizontal, i18n("Microprocessor"), parent, name )
26     : QGroupBox(parent /*, name */ )
27 {
28     setObjectName( name );
29     setLayout(new QHBoxLayout);
30     setTitle(i18n("Microprocessor"));
31 
32 	m_allowedAsmSet = AsmInfo::AsmSetAll;
33 	m_allowedGpsimSupport = m_allowedFlowCodeSupport = m_allowedMicrobeSupport = MicroInfo::AllSupport;
34 
35 	if ( !name ) {
36 		setObjectName( "MicroSelectWidget" );
37     }
38 	m_pMicroFamilyLabel = new QLabel( nullptr );
39     m_pMicroFamilyLabel->setObjectName( "m_pMicroFamilyLabel" );
40 	m_pMicroFamilyLabel->setText( i18n("Family") );
41     layout()->addWidget( m_pMicroFamilyLabel );
42 
43 	m_pMicroFamily = new KComboBox( false ); //, "m_pMicroFamily" );
44 	m_pMicroFamily->setObjectName("m_pMicroFamily");
45 	m_pMicroFamily->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
46     layout()->addWidget( m_pMicroFamily );
47 
48 	m_pMicroLabel = new QLabel( nullptr /*, "m_pMicroLabel" */ );
49     m_pMicroLabel->setObjectName("m_pMicroLabel");
50 	m_pMicroLabel->setText( i18n("Micro") );
51     layout()->addWidget( m_pMicroLabel );
52 
53 	m_pMicro = new KComboBox( false ); //, "m_pMicro" );
54 	m_pMicro->setObjectName("m_pMicro");
55 	m_pMicro->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
56 	m_pMicro->setEditable( true );
57 	m_pMicro->setAutoCompletion(true);
58     layout()->addWidget( m_pMicro );
59 
60 	updateFromAllowed();
61 	setMicro("P16F84");
62 	connect( m_pMicroFamily, SIGNAL(activated(const QString & )), this, SLOT(microFamilyChanged(const QString& )) );
63 }
64 
65 
~MicroSelectWidget()66 MicroSelectWidget::~MicroSelectWidget()
67 {
68 }
69 
setAllowedAsmSet(unsigned allowed)70 void MicroSelectWidget::setAllowedAsmSet( unsigned allowed )
71 {
72 	m_allowedAsmSet = allowed;
73 	updateFromAllowed();
74 }
setAllowedGpsimSupport(unsigned allowed)75 void MicroSelectWidget::setAllowedGpsimSupport( unsigned allowed )
76 {
77 	m_allowedGpsimSupport = allowed;
78 	updateFromAllowed();
79 }
setAllowedFlowCodeSupport(unsigned allowed)80 void MicroSelectWidget::setAllowedFlowCodeSupport( unsigned allowed )
81 {
82 	m_allowedFlowCodeSupport = allowed;
83 	updateFromAllowed();
84 }
setAllowedMicrobeSupport(unsigned allowed)85 void MicroSelectWidget::setAllowedMicrobeSupport( unsigned allowed )
86 {
87 	m_allowedMicrobeSupport = allowed;
88 	updateFromAllowed();
89 }
90 
91 
updateFromAllowed()92 void MicroSelectWidget::updateFromAllowed()
93 {
94 	QString oldFamily = m_pMicroFamily->currentText();
95 
96 	m_pMicroFamily->clear();
97 
98 #define CHECK_ADD(family) \
99     if ( (m_allowedAsmSet & AsmInfo::family) \
100             && !MicroLibrary::self()->microIDs( AsmInfo::family, \
101                 m_allowedGpsimSupport, m_allowedFlowCodeSupport, m_allowedMicrobeSupport ).isEmpty() ) { \
102         m_pMicroFamily->insertItem( m_pMicroFamily->count(), AsmInfo::setToString(AsmInfo::family) ); \
103     }
104 	CHECK_ADD(PIC12)
105 	CHECK_ADD(PIC14)
106 	CHECK_ADD(PIC16);
107 #undef CHECK_ADD
108 
109 	if ( m_pMicroFamily->contains(oldFamily) ) {
110 		//m_pMicroFamily->setCurrentText(oldFamily); // 2018.12.07
111         {
112             QComboBox *c = m_pMicroFamily;
113             QString text = oldFamily;
114             int i = c->findText(text);
115             if (i != -1)
116                 c->setCurrentIndex(i);
117             else if (c->isEditable())
118                 c->setEditText(text);
119             else
120                 c->setItemText(c->currentIndex(), text);
121         }
122     }
123 
124 	microFamilyChanged(oldFamily);
125 }
126 
127 
setMicro(const QString & id)128 void MicroSelectWidget::setMicro( const QString & id )
129 {
130 	MicroInfo * info = MicroLibrary::self()->microInfoWithID(id);
131 	if (!info)
132 		return;
133 
134 	m_pMicro->clear();
135 	m_pMicro->insertItems( m_pMicro->count(),
136         MicroLibrary::self()->microIDs( info->instructionSet()->set() ) );
137 	//m_pMicro->setCurrentText(id); // 2018.12.07
138     {
139         QComboBox *c = m_pMicro;
140         QString text = id;
141         int i = c->findText(text);
142         if (i != -1)
143             c->setCurrentIndex(i);
144         else if (c->isEditable())
145             c->setEditText(text);
146         else
147             c->setItemText(c->currentIndex(), text);
148     }
149 
150 
151 	//m_pMicroFamily->setCurrentText( AsmInfo::setToString( info->instructionSet()->set() ) ); // 2018.12.07
152     {
153         QComboBox *c = m_pMicroFamily;
154         QString text = AsmInfo::setToString( info->instructionSet()->set() );
155         int i = c->findText(text);
156         if (i != -1)
157             c->setCurrentIndex(i);
158         else if (c->isEditable())
159             c->setEditText(text);
160         else
161             c->setItemText(c->currentIndex(), text);
162     }
163 }
164 
micro() const165 QString MicroSelectWidget::micro() const
166 {
167 	return m_pMicro->currentText();
168 }
169 
170 
microFamilyChanged(const QString & family)171 void MicroSelectWidget::microFamilyChanged( const QString & family )
172 {
173 	QString oldID = m_pMicro->currentText();
174 
175 	m_pMicro->clear();
176 	m_pMicro->insertItems( m_pMicro->count(),
177         MicroLibrary::self()->microIDs( AsmInfo::stringToSet(family), m_allowedGpsimSupport, m_allowedFlowCodeSupport, m_allowedMicrobeSupport ) );
178 
179 	if ( m_pMicro->contains(oldID) ) {
180 		//m_pMicro->setCurrentText(oldID); // 2018.12.07
181         {
182             int i = m_pMicro->findText(oldID);
183             if (i != -1)
184                 m_pMicro->setCurrentIndex(i);
185             else if (m_pMicro->isEditable())
186                 m_pMicro->setEditText(oldID);
187             else
188                 m_pMicro->setItemText(m_pMicro->currentIndex(), oldID);
189         }
190     }
191 }
192