File indexing completed on 2024-04-14 05:36:47

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "microselectwidget.h"
0012 #include "asminfo.h"
0013 #include "microinfo.h"
0014 #include "microlibrary.h"
0015 
0016 #include <KComboBox>
0017 #include <KLocalizedString>
0018 
0019 // #include <q3groupbox.h>
0020 #include <QLabel>
0021 #include <QLayout>
0022 #include <QVariant>
0023 
0024 MicroSelectWidget::MicroSelectWidget(QWidget *parent, Qt::WindowFlags)
0025     //: Q3GroupBox( 4, Qt::Horizontal, i18n("Microprocessor"), parent )
0026     : QGroupBox(parent)
0027 {
0028     setLayout(new QHBoxLayout);
0029     setTitle(i18n("Microprocessor"));
0030 
0031     m_allowedAsmSet = AsmInfo::AsmSetAll;
0032     m_allowedGpsimSupport = m_allowedFlowCodeSupport = m_allowedMicrobeSupport = MicroInfo::AllSupport;
0033 
0034     setObjectName("MicroSelectWidget");
0035     m_pMicroFamilyLabel = new QLabel(nullptr);
0036     m_pMicroFamilyLabel->setObjectName("m_pMicroFamilyLabel");
0037     m_pMicroFamilyLabel->setText(i18n("Family"));
0038     layout()->addWidget(m_pMicroFamilyLabel);
0039 
0040     m_pMicroFamily = new KComboBox(false); //, "m_pMicroFamily" );
0041     m_pMicroFamily->setObjectName("m_pMicroFamily");
0042     m_pMicroFamily->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0043     layout()->addWidget(m_pMicroFamily);
0044 
0045     m_pMicroLabel = new QLabel(nullptr /*, "m_pMicroLabel" */);
0046     m_pMicroLabel->setObjectName("m_pMicroLabel");
0047     m_pMicroLabel->setText(i18n("Micro"));
0048     layout()->addWidget(m_pMicroLabel);
0049 
0050     m_pMicro = new KComboBox(false); //, "m_pMicro" );
0051     m_pMicro->setObjectName("m_pMicro");
0052     m_pMicro->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
0053     m_pMicro->setEditable(true);
0054     m_pMicro->setAutoCompletion(true);
0055     layout()->addWidget(m_pMicro);
0056 
0057     updateFromAllowed();
0058     setMicro("P16F84");
0059     connect(m_pMicroFamily, qOverload<const QString &>(&KComboBox::textActivated), this, &MicroSelectWidget::microFamilyChanged);
0060 }
0061 
0062 MicroSelectWidget::~MicroSelectWidget()
0063 {
0064 }
0065 
0066 void MicroSelectWidget::setAllowedAsmSet(unsigned allowed)
0067 {
0068     m_allowedAsmSet = allowed;
0069     updateFromAllowed();
0070 }
0071 void MicroSelectWidget::setAllowedGpsimSupport(unsigned allowed)
0072 {
0073     m_allowedGpsimSupport = allowed;
0074     updateFromAllowed();
0075 }
0076 void MicroSelectWidget::setAllowedFlowCodeSupport(unsigned allowed)
0077 {
0078     m_allowedFlowCodeSupport = allowed;
0079     updateFromAllowed();
0080 }
0081 void MicroSelectWidget::setAllowedMicrobeSupport(unsigned allowed)
0082 {
0083     m_allowedMicrobeSupport = allowed;
0084     updateFromAllowed();
0085 }
0086 
0087 void MicroSelectWidget::updateFromAllowed()
0088 {
0089     QString oldFamily = m_pMicroFamily->currentText();
0090 
0091     m_pMicroFamily->clear();
0092 
0093 #define CHECK_ADD(family)                                                                                                                                                                                                                      \
0094     if ((m_allowedAsmSet & AsmInfo::family) && !MicroLibrary::self()->microIDs(AsmInfo::family, m_allowedGpsimSupport, m_allowedFlowCodeSupport, m_allowedMicrobeSupport).isEmpty()) {                                                         \
0095         m_pMicroFamily->insertItem(m_pMicroFamily->count(), AsmInfo::setToString(AsmInfo::family));                                                                                                                                            \
0096     }
0097     CHECK_ADD(PIC12)
0098     CHECK_ADD(PIC14)
0099     CHECK_ADD(PIC16);
0100 #undef CHECK_ADD
0101 
0102     if (m_pMicroFamily->contains(oldFamily)) {
0103         // m_pMicroFamily->setCurrentText(oldFamily); // 2018.12.07
0104         {
0105             QComboBox *c = m_pMicroFamily;
0106             QString text = oldFamily;
0107             int i = c->findText(text);
0108             if (i != -1)
0109                 c->setCurrentIndex(i);
0110             else if (c->isEditable())
0111                 c->setEditText(text);
0112             else
0113                 c->setItemText(c->currentIndex(), text);
0114         }
0115     }
0116 
0117     microFamilyChanged(oldFamily);
0118 }
0119 
0120 void MicroSelectWidget::setMicro(const QString &id)
0121 {
0122     MicroInfo *info = MicroLibrary::self()->microInfoWithID(id);
0123     if (!info)
0124         return;
0125 
0126     m_pMicro->clear();
0127     m_pMicro->insertItems(m_pMicro->count(), MicroLibrary::self()->microIDs(info->instructionSet()->set()));
0128     // m_pMicro->setCurrentText(id); // 2018.12.07
0129     {
0130         QComboBox *c = m_pMicro;
0131         QString text = id;
0132         int i = c->findText(text);
0133         if (i != -1)
0134             c->setCurrentIndex(i);
0135         else if (c->isEditable())
0136             c->setEditText(text);
0137         else
0138             c->setItemText(c->currentIndex(), text);
0139     }
0140 
0141     // m_pMicroFamily->setCurrentText( AsmInfo::setToString( info->instructionSet()->set() ) ); // 2018.12.07
0142     {
0143         QComboBox *c = m_pMicroFamily;
0144         QString text = AsmInfo::setToString(info->instructionSet()->set());
0145         int i = c->findText(text);
0146         if (i != -1)
0147             c->setCurrentIndex(i);
0148         else if (c->isEditable())
0149             c->setEditText(text);
0150         else
0151             c->setItemText(c->currentIndex(), text);
0152     }
0153 }
0154 
0155 QString MicroSelectWidget::micro() const
0156 {
0157     return m_pMicro->currentText();
0158 }
0159 
0160 void MicroSelectWidget::microFamilyChanged(const QString &family)
0161 {
0162     QString oldID = m_pMicro->currentText();
0163 
0164     m_pMicro->clear();
0165     m_pMicro->insertItems(m_pMicro->count(), MicroLibrary::self()->microIDs(AsmInfo::stringToSet(family), m_allowedGpsimSupport, m_allowedFlowCodeSupport, m_allowedMicrobeSupport));
0166 
0167     if (m_pMicro->contains(oldID)) {
0168         // m_pMicro->setCurrentText(oldID); // 2018.12.07
0169         {
0170             int i = m_pMicro->findText(oldID);
0171             if (i != -1)
0172                 m_pMicro->setCurrentIndex(i);
0173             else if (m_pMicro->isEditable())
0174                 m_pMicro->setEditText(oldID);
0175             else
0176                 m_pMicro->setItemText(m_pMicro->currentIndex(), oldID);
0177         }
0178     }
0179 }
0180 
0181 #include "moc_microselectwidget.cpp"