File indexing completed on 2024-04-21 09:34:28

0001 /***************************************************************************
0002  *   Copyright (C) 2003-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 #ifndef MICROSETTINGSDLG_H
0012 #define MICROSETTINGSDLG_H
0013 
0014 #include <QDialog>
0015 #include <QMap>
0016 #include <QValidator>
0017 // #include <q3valuevector.h>
0018 
0019 class KLineEdit;
0020 class MicroSettings;
0021 class MicroSettingsWidget;
0022 class NewPinMappingWidget;
0023 class PinMapping;
0024 class QPushButton;
0025 
0026 typedef QMap<QString, PinMapping> PinMappingMap;
0027 
0028 /**
0029 @author David Saxton
0030 */
0031 class MicroSettingsDlg : public QDialog
0032 {
0033     Q_OBJECT
0034 public:
0035     MicroSettingsDlg(MicroSettings *_microSettings, QWidget *parent = nullptr);
0036     ~MicroSettingsDlg() override;
0037 
0038     void reject() override;
0039     void accept() override;
0040 
0041     /**
0042      * @param pinMapName the pinMapName; may be changed to make it valid
0043      * (e.g. spaces replaced with underscores).
0044      * @returns Invalid for a pinMapName containing a non-variable name,
0045      * Intermediate for a pinMapName that starts with a number or is already
0046      * in use, and Acceptable otherwise.
0047      */
0048     QValidator::State validatePinMapName(QString &pinMapName) const;
0049 
0050 public slots:
0051     /**
0052      * Saves the port details in the given row to the MicroSettings class.
0053      * Usually called when the value is changed, or on 'Apply' of the
0054      * dialog.
0055      */
0056     void savePort(int row);
0057     /**
0058      * Saves the variable details to the MicroSettings class.
0059      */
0060     void saveVariable(int row);
0061     /**
0062      * Adds an extra row to the list of variable if one is required.
0063      */
0064     void checkAddVariableRow();
0065     /**
0066      * Called when the pinMapAdd button is pressed.
0067      */
0068     void slotCreatePinMap();
0069     /**
0070      * Called when the pinMapModify button is pressed.
0071      */
0072     void slotModifyPinMap();
0073     /**
0074      * Called when the pinMapRename button is pressed.
0075      */
0076     void slotRenamePinMap();
0077     /**
0078      * Called when the pinMapRemove button is pressed.
0079      */
0080     void slotRemovePinMap();
0081     /**
0082      * Called when the dialog is Applied or OK'd.
0083      */
0084     void slotSaveStuff();
0085 
0086 signals:
0087     void applyClicked();
0088 
0089 protected slots:
0090     void slotCheckNewPinMappingName(const QString &name);
0091     void slotApplyClicked();
0092 
0093 protected:
0094     /**
0095      * Set each button enabled / disabled as appropriate.
0096      */
0097     void updatePinMapButtons();
0098 
0099     NewPinMappingWidget *m_pNewPinMappingWidget; // Used for checking that the variable name is ok
0100     QDialog *m_pNewPinMappingDlg;
0101     QPushButton *m_pNewPinMappingOkButton;
0102     MicroSettingsWidget *m_pWidget;
0103     MicroSettings *m_pMicroSettings;
0104     PinMappingMap m_pinMappings;
0105     QVector<KLineEdit *> m_portTypeEdit;
0106     QVector<KLineEdit *> m_portStateEdit;
0107     QStringList m_portNames;
0108 };
0109 
0110 class PinMappingNameValidator : public QValidator
0111 {
0112 public:
0113     /**
0114      * Create a validator. If oldName is not empty, then the input is
0115      * allowed to be oldName.
0116      */
0117     PinMappingNameValidator(MicroSettingsDlg *dlg, const QString &oldName = nullptr)
0118         : QValidator(nullptr)
0119     {
0120         m_pDlg = dlg;
0121         m_oldName = oldName;
0122     }
0123 
0124     State validate(QString &input, int &) const override
0125 
0126     {
0127         if ((!m_oldName.isEmpty()) && (input == m_oldName))
0128             return QValidator::Acceptable;
0129 
0130         return m_pDlg->validatePinMapName(input);
0131     }
0132 
0133 protected:
0134     MicroSettingsDlg *m_pDlg;
0135     QString m_oldName;
0136 };
0137 
0138 #endif