File indexing completed on 2024-07-21 05:07:51

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 "settingsdlg.h"
0012 #include "inputdialog.h"
0013 #include "picprogrammer.h"
0014 #include "port.h"
0015 
0016 #include <KComboBox>
0017 #include <KLineEdit>
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <KSharedConfig>
0021 
0022 #include <QGroupBox>
0023 #include <QLabel>
0024 #include <QSlider>
0025 #include <QStandardPaths>
0026 #include <QTimer>
0027 
0028 #include <ktlconfig.h>
0029 #include <ui_asmformattingwidget.h>
0030 #include <ui_generaloptionswidget.h>
0031 #include <ui_gpasmsettingswidget.h>
0032 #include <ui_gplinksettingswidget.h>
0033 #include <ui_logicwidget.h>
0034 #include <ui_picprogrammerconfigwidget.h>
0035 #include <ui_sdccoptionswidget.h>
0036 
0037 class GeneralOptionsWidget : public QWidget, public Ui::GeneralOptionsWidget
0038 {
0039 public:
0040     GeneralOptionsWidget(QWidget *parent)
0041         : QWidget(parent)
0042     {
0043         setupUi(this);
0044     }
0045 };
0046 
0047 class GpasmSettingsWidget : public QWidget, public Ui::GpasmSettingsWidget
0048 {
0049 public:
0050     GpasmSettingsWidget(QWidget *parent)
0051         : QWidget(parent)
0052     {
0053         setupUi(this);
0054     }
0055 };
0056 
0057 class SDCCOptionsWidget : public QWidget, public Ui::SDCCOptionsWidget
0058 {
0059 public:
0060     SDCCOptionsWidget(QWidget *parent)
0061         : QWidget(parent)
0062     {
0063         setupUi(this);
0064     }
0065 };
0066 
0067 class AsmFormattingWidget : public QWidget, public Ui::AsmFormattingWidget
0068 {
0069 public:
0070     AsmFormattingWidget(QWidget *parent)
0071         : QWidget(parent)
0072     {
0073         setupUi(this);
0074     }
0075 };
0076 
0077 class LogicWidget : public QWidget, public Ui::LogicWidget
0078 {
0079 public:
0080     LogicWidget(QWidget *parent)
0081         : QWidget(parent)
0082     {
0083         setupUi(this);
0084     }
0085 };
0086 
0087 class PicProgrammerConfigWidget : public QWidget, public Ui::PicProgrammerConfigWidget
0088 {
0089 public:
0090     PicProgrammerConfigWidget(QWidget *parent)
0091         : QWidget(parent)
0092     {
0093         setupUi(this);
0094     }
0095 };
0096 
0097 class GplinkSettingsWidget : public QWidget, public Ui::GplinkSettingsWidget
0098 {
0099 public:
0100     GplinkSettingsWidget(QWidget *parent)
0101         : QWidget(parent)
0102     {
0103         setupUi(this);
0104     }
0105 };
0106 
0107 // Make sure that this value is the same as that in ktechlab.kcfg
0108 const int defaultRefreshRate = 50;
0109 
0110 SettingsDlg::SettingsDlg(QWidget *parent, const char *name, KCoreConfigSkeleton *config)
0111     : KConfigDialog(parent, name, config)
0112 {
0113     m_generalOptionsWidget = new GeneralOptionsWidget(this);
0114     m_generalOptionsWidget->setObjectName("generalOptionsWidget");
0115     m_gpasmSettingsWidget = new GpasmSettingsWidget(this);
0116     m_gpasmSettingsWidget->setObjectName("gpasmSettingsWidget");
0117     m_sdccOptionsWidget = new SDCCOptionsWidget(this);
0118     m_sdccOptionsWidget->setObjectName("sdccOptionsWidget");
0119     m_asmFormattingWidget = new AsmFormattingWidget(this);
0120     m_asmFormattingWidget->setObjectName("asmFormattingWidget");
0121     m_logicWidget = new LogicWidget(this);
0122     m_logicWidget->setObjectName("logicWidget");
0123     m_picProgrammerConfigWidget = new PicProgrammerConfigWidget(this);
0124     m_picProgrammerConfigWidget->setObjectName("picProgrammerConfigWidget");
0125     m_gplinkSettingsWidget = new GplinkSettingsWidget(this);
0126     m_gplinkSettingsWidget->setObjectName("gplinkSettingsWidget");
0127 
0128     m_pPicProgrammerSettings = new PicProgrammerSettings;
0129 
0130     m_logicWidget->kcfg_LogicOutputHighImpedance->setSuffix(QString(" ") + QChar(0x3a9));
0131     m_logicWidget->kcfg_LogicOutputLowImpedance->setSuffix(QString(" ") + QChar(0x3a9));
0132 
0133     addPage(m_generalOptionsWidget, i18n("General"), "ktechlab", i18n("General Options"));
0134     addPage(m_picProgrammerConfigWidget, i18n("Programmer"), "network-connect", i18n("PIC Programmer"));
0135     addPage(m_asmFormattingWidget, i18n("Formatter"), "indent_asm", i18n("Assembly Formatter"));
0136     addPage(m_logicWidget, i18n("Logic"), "logic_or", i18n("Electronic Logic Values"));
0137     addPage(m_gpasmSettingsWidget, "Gpasm", "convert_to_hex", "gpasm");
0138     addPage(m_gplinkSettingsWidget, "Gplink", "merge", "gplink");
0139     addPage(m_sdccOptionsWidget, "SDCC", "text-x-csrc", "SDCC");
0140 
0141     connect(m_generalOptionsWidget->refreshRateSlider, &QSlider::valueChanged, this, &SettingsDlg::slotUpdateRefreshRateLabel);
0142     connect(m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram, qOverload<const QString &>(&KComboBox::textActivated), this, &SettingsDlg::slotUpdatePicProgrammerDescription);
0143     connect(m_picProgrammerConfigWidget->removeButton, &QPushButton::clicked, this, &SettingsDlg::slotRemoveProgrammerConfig);
0144     connect(m_picProgrammerConfigWidget->addButton, &QPushButton::clicked, this, &SettingsDlg::slotAddProgrammerConfig);
0145 
0146     connect(m_picProgrammerConfigWidget->initCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0147     connect(m_picProgrammerConfigWidget->readCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0148     connect(m_picProgrammerConfigWidget->writeCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0149     connect(m_picProgrammerConfigWidget->verifyCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0150     connect(m_picProgrammerConfigWidget->blankCheckCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0151     connect(m_picProgrammerConfigWidget->eraseCommand, &KLineEdit::textChanged, this, &SettingsDlg::slotSaveCurrentProgrammerConfig);
0152 
0153     m_generalOptionsWidget->kcfg_GridColor->setEnabled(KTLConfig::showGrid());
0154 
0155     m_picProgrammerConfigWidget->kcfg_PicProgrammerPort->insertItems(m_picProgrammerConfigWidget->kcfg_PicProgrammerPort->count(), Port::ports());
0156     slotUpdatePicProgrammerDescription();
0157 }
0158 
0159 SettingsDlg::~SettingsDlg()
0160 {
0161     delete m_pPicProgrammerSettings;
0162 }
0163 
0164 void SettingsDlg::show()
0165 {
0166     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0167     combo->setEditable(true);
0168     KConfigDialog::show();
0169     combo->setEditable(false);
0170 }
0171 
0172 void SettingsDlg::slotUpdateRefreshRateLabel(int sliderValue)
0173 {
0174     const QString number = QString::number(sliderValueToRefreshRate(sliderValue));
0175     switch (sliderValue) {
0176     case 0:
0177         m_generalOptionsWidget->refreshRateLabel->setText(i18n("Lowest (%1 FPS)", number));
0178         break;
0179     case 1:
0180         m_generalOptionsWidget->refreshRateLabel->setText(i18n("Low (%1 FPS)", number));
0181         break;
0182     case 2:
0183         m_generalOptionsWidget->refreshRateLabel->setText(i18n("Medium (%1 FPS)", number));
0184         break;
0185     case 3:
0186         m_generalOptionsWidget->refreshRateLabel->setText(i18n("High (%1 FPS)", number));
0187         break;
0188     case 4:
0189         m_generalOptionsWidget->refreshRateLabel->setText(i18n("Highest (%1 FPS)", number));
0190         break;
0191     default:
0192         m_generalOptionsWidget->refreshRateLabel->setText(i18n("Unknown value"));
0193         break;
0194     }
0195     updateButtons();
0196 }
0197 
0198 void SettingsDlg::slotUpdatePicProgrammerDescription()
0199 {
0200     QString program = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram->currentText();
0201 
0202     ProgrammerConfig config = m_pPicProgrammerSettings->config(program);
0203     QString description = config.description;
0204 
0205     bool customProgrammer = !m_pPicProgrammerSettings->isPredefined(program);
0206 
0207     QString executable = config.executable;
0208     if (executable.isEmpty())
0209         executable = program.toLower();
0210 
0211     QString programLocation = QStandardPaths::findExecutable(executable);
0212     if (programLocation.isNull())
0213         description.prepend(i18n("<b>%1</b> cannot be found.<br>", executable));
0214     else
0215         description.prepend(i18n("<b>%1</b> found: %2<br>", executable, programLocation));
0216 
0217     m_picProgrammerConfigWidget->m_pProgrammerDescription->setText(description);
0218     m_picProgrammerConfigWidget->removeButton->setEnabled(customProgrammer);
0219 
0220     KLineEdit *edit;
0221 
0222 #define SETUP_COMMAND(name)                                                                                                                                                                                                                    \
0223     edit = m_picProgrammerConfigWidget->name;                                                                                                                                                                                                  \
0224     edit->setText(config.name);                                                                                                                                                                                                                \
0225     edit->setEnabled(customProgrammer);                                                                                                                                                                                                        \
0226     edit->setToolTip(customProgrammer ? QString() : config.name)
0227 
0228     SETUP_COMMAND(initCommand);
0229     SETUP_COMMAND(readCommand);
0230     SETUP_COMMAND(writeCommand);
0231     SETUP_COMMAND(verifyCommand);
0232     SETUP_COMMAND(blankCheckCommand);
0233     SETUP_COMMAND(eraseCommand);
0234 
0235 #undef SETUP_COMMAND
0236 }
0237 
0238 void SettingsDlg::slotSaveCurrentProgrammerConfig()
0239 {
0240     QString program = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram->currentText();
0241 
0242     if (m_pPicProgrammerSettings->isPredefined(program))
0243         return;
0244 
0245     ProgrammerConfig config;
0246 
0247     config.initCommand = m_picProgrammerConfigWidget->initCommand->text();
0248     config.readCommand = m_picProgrammerConfigWidget->readCommand->text();
0249     config.writeCommand = m_picProgrammerConfigWidget->writeCommand->text();
0250     config.verifyCommand = m_picProgrammerConfigWidget->verifyCommand->text();
0251     config.blankCheckCommand = m_picProgrammerConfigWidget->blankCheckCommand->text();
0252     config.eraseCommand = m_picProgrammerConfigWidget->eraseCommand->text();
0253 
0254     m_pPicProgrammerSettings->saveConfig(program, config);
0255 }
0256 
0257 void SettingsDlg::slotRemoveProgrammerConfig()
0258 {
0259     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0260 
0261     QString program = combo->currentText();
0262 
0263     KMessageBox::ButtonCode confirm = static_cast<KMessageBox::ButtonCode>(
0264         KMessageBox::warningContinueCancel(this, i18n("Remove programmer configuration \"%1\"?", program),
0265                                            i18n("Remove \"%1\"", program)
0266     ));
0267     if (confirm == KMessageBox::Cancel)
0268         return;
0269 
0270     m_pPicProgrammerSettings->removeConfig(program);
0271     combo->removeItem(combo->currentIndex());
0272     slotUpdatePicProgrammerDescription();
0273 }
0274 
0275 void SettingsDlg::slotAddProgrammerConfig()
0276 {
0277     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0278 
0279     QStringList takenNames;
0280     int count = combo->count();
0281     for (int i = 0; i < count; ++i)
0282         takenNames << combo->itemText(i).toLower();
0283 
0284     NameValidator *nv = new NameValidator(takenNames);
0285 
0286     bool ok = false;
0287     QString name = InputDialog::getText(i18n("Configuration Name"), i18n("Name"), QString(), /* 0,*/ &ok, this, /* 0,*/ nv);
0288 
0289     delete nv;
0290 
0291     if (!ok)
0292         return;
0293 
0294     ProgrammerConfig config;
0295     config.executable = name.toLower();
0296 
0297     m_pPicProgrammerSettings->saveConfig(name, config);
0298 
0299     combo->insertItem(combo->count(), name);
0300     // combo->setCurrentItem( count );
0301     combo->setCurrentItem(name);
0302     slotUpdatePicProgrammerDescription();
0303 }
0304 
0305 int SettingsDlg::refreshRateToSliderValue(int refreshRate)
0306 {
0307     switch (refreshRate) {
0308     case 10:
0309         return 0;
0310     case 25:
0311         return 1;
0312     case 50:
0313         return 2;
0314     case 75:
0315         return 3;
0316     case 100:
0317         return 4;
0318     default:
0319         return -1;
0320     }
0321 }
0322 
0323 int SettingsDlg::sliderValueToRefreshRate(int sliderValue)
0324 {
0325     switch (sliderValue) {
0326     case 0:
0327         return 10;
0328     case 1:
0329         return 25;
0330     case 2:
0331         return 50;
0332     case 3:
0333         return 75;
0334     case 4:
0335         return 100;
0336     default:
0337         return -1;
0338     }
0339 }
0340 
0341 void SettingsDlg::updateSettings()
0342 {
0343     // KConfig * config = kapp->config();
0344     KSharedConfigPtr config = KSharedConfig::openConfig();
0345 
0346     KConfigSkeleton::ItemInt *item = dynamic_cast<KConfigSkeleton::ItemInt *>(KTLConfig::self()->findItem("RefreshRate"));
0347     if (!item)
0348         return;
0349 
0350     int newRefreshRate = sliderValueToRefreshRate(m_generalOptionsWidget->refreshRateSlider->value());
0351 
0352     if (newRefreshRate != KTLConfig::refreshRate()) {
0353         item->setValue(newRefreshRate);
0354         KConfigGroup grWorkArea = config->group("WorkArea");
0355         if (newRefreshRate != defaultRefreshRate)
0356             grWorkArea.writeEntry("RefreshRate", newRefreshRate);
0357         else
0358             grWorkArea.deleteEntry("RefreshRate");
0359 
0360         emit settingsChanged(objectName());
0361     }
0362 
0363     QTimer::singleShot(0, this, SLOT(slotUpdateSettings()));
0364 }
0365 
0366 void SettingsDlg::slotUpdateSettings()
0367 {
0368     // KConfig * config = kapp->config();
0369     KSharedConfigPtr config = KSharedConfig::openConfig();
0370 
0371     KConfigSkeleton::ItemString *item = dynamic_cast<KConfigSkeleton::ItemString *>(KTLConfig::self()->findItem("PicProgrammerProgram"));
0372     if (!item)
0373         return;
0374 
0375     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0376     QString newProgram = combo->currentText();
0377 
0378     if (newProgram != KTLConfig::picProgrammerProgram()) {
0379         item->setValue(newProgram);
0380         KConfigGroup grPicProg = config->group("PicProgramming");
0381         if (newProgram != "picp")
0382             grPicProg.writeEntry("PicProgrammerProgram", newProgram);
0383         else
0384             grPicProg.deleteEntry("PicProgrammerProgram");
0385 
0386         emit settingsChanged(objectName());
0387     }
0388 
0389     m_pPicProgrammerSettings->save(config.data());
0390 
0391     config->sync();
0392 }
0393 
0394 void SettingsDlg::updateWidgets()
0395 {
0396     m_generalOptionsWidget->refreshRateSlider->setValue(refreshRateToSliderValue(KTLConfig::refreshRate()));
0397 
0398     // m_pPicProgrammerSettings->load( kapp->config() );
0399     m_pPicProgrammerSettings->load(KSharedConfig::openConfig().data());
0400 
0401     QStringList programmerNames = m_pPicProgrammerSettings->configNames(false);
0402 
0403     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0404     combo->clear();
0405     combo->insertItems(combo->count(), programmerNames);
0406     // combo->setSizeLimit( programmerNames.size() );
0407     combo->setMaxCount(programmerNames.size());
0408 
0409     QTimer::singleShot(0, this, SLOT(slotUpdateWidgets()));
0410 }
0411 
0412 void SettingsDlg::slotUpdateWidgets()
0413 {
0414     KComboBox *combo = m_picProgrammerConfigWidget->kcfg_PicProgrammerProgram;
0415 
0416     combo->setItemText(combo->currentIndex(), KTLConfig::picProgrammerProgram());
0417     slotUpdatePicProgrammerDescription();
0418 }
0419 
0420 void SettingsDlg::updateWidgetsDefault()
0421 {
0422     m_generalOptionsWidget->refreshRateSlider->setValue(refreshRateToSliderValue(defaultRefreshRate));
0423     slotUpdatePicProgrammerDescription();
0424 }
0425 
0426 bool SettingsDlg::hasChanged()
0427 {
0428     if (sliderValueToRefreshRate(m_generalOptionsWidget->refreshRateSlider->value()) == KTLConfig::refreshRate())
0429         return KConfigDialog::hasChanged();
0430     return true;
0431 }
0432 
0433 bool SettingsDlg::isDefault()
0434 {
0435     if (sliderValueToRefreshRate(m_generalOptionsWidget->refreshRateSlider->value()) == defaultRefreshRate)
0436         return KConfigDialog::isDefault();
0437 
0438     return false;
0439 }
0440 
0441 #include "moc_settingsdlg.cpp"