File indexing completed on 2024-04-28 03:44:12

0001 /*
0002     SPDX-FileCopyrightText: 2023 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "ui_collimationOptions.h"
0010 #include "collimationoverlaytypes.h"
0011 
0012 #include <QDialog>
0013 #include <QSqlDatabase>
0014 #include <QQueue>
0015 #include <QPointer>
0016 #include <QMetaEnum>
0017 
0018 class QSqlTableModel;
0019 
0020 class CollimationOverlayOptions: public QDialog, public Ui::collimationOptions
0021 {
0022         Q_OBJECT
0023         Q_PROPERTY(QList <QString> elementNames READ getElementNames)
0024 
0025     public:
0026 
0027         static CollimationOverlayOptions *Instance(QWidget *parent);
0028         static void release();
0029 
0030         void checkElements();
0031 
0032         bool exists(uint8_t id) const;
0033         const QVariantMap getCollimationOverlayElement(uint8_t id) const;
0034         const QVariantMap getCollimationOverlayElement(const QString &name) const;
0035         const QList<QVariantMap> &getCollimationOverlayElements() const
0036         {
0037             return m_CollimationOverlayElements;
0038         }
0039         const QList <QString> &getElementNames() const
0040         {
0041             return m_ElementNames;
0042         }
0043 
0044         /**
0045          * @brief Select a collimation overlay element and fill the field values in the element editor
0046          *        with the appropriate values of the selected collimation overlay element.
0047          * @param item collimation overlay element list item
0048          * @return true if collimation overlay element found
0049          */
0050         bool selectCollimationOverlayElement(QListWidgetItem *item);
0051 
0052         /**
0053          * @brief Select a collimation overlay element and fill the field values in the element editor
0054          *        with the appropriate values of the selected collimation overlay element.
0055          * @param name collimation overlay element name
0056          * @return true if collimation overlay element found
0057          */
0058         bool selectCollimationOverlayElement(const QString &name);
0059 
0060         /**
0061          * @brief Show the dialog and select a collimation overlay element for editing.
0062          * @param name collimation overlay element name
0063          */
0064         void openEditor();
0065 
0066         /**
0067          * @brief setCollimationOverlayElementValue Set specific field of collimation overlay element
0068          * @param name Name of collimation overlay element
0069          * @param field Name of element field
0070          * @param value Value of element field
0071          * @return True if set is successful, false otherwise.
0072          */
0073         bool setCollimationOverlayElementValue(const QString &name, const QString &field, const QVariant &value);
0074 
0075         /**
0076          * @brief Change the name of the currently selected collimation overlay element to a new value
0077          * @param name new element name
0078          */
0079         void renameCollimationOverlayElement(const QString &name);
0080 
0081         /**
0082          * @brief setCollimationOverlayElement Replaces collimation overlay element matching the name of the passed element.
0083          * @param train element information, including name and database id
0084          * @return True if element is successfully updated in the database.
0085          */
0086         bool setCollimationOverlayElement(const QJsonObject &element);
0087 
0088         /**
0089          * @brief removeCollimationOverlayElement Remove collimation overlay element from database and all associated settings
0090          * @param name name of the element to remove
0091          * @return True if successful, false if id is not found.
0092          */
0093         bool removeCollimationOverlayElement(const QString &name);
0094 
0095         void refreshModel();
0096         void refreshElements();
0097 
0098         /**
0099          * @brief syncValues Sync delegates and then update model accordingly.
0100          */
0101         void syncValues();
0102 
0103         /**
0104          * @brief id Get database ID for a given element
0105          * @param name Name of element
0106          * @return ID if exists, or -1 if not found.
0107          */
0108         int id(const QString &name) const;
0109 
0110         /**
0111          * @brief name Get database name for a given id
0112          * @param id database ID for the element to get
0113          * @return Element name, or empty string if not found.
0114          */
0115         QString name(int id) const;
0116 
0117     signals:
0118         void updated();
0119 
0120     protected:
0121         void initModel();
0122 
0123     private slots:
0124         /**
0125          * @brief Update a value in the currently selected element
0126          * @param cb combo box holding the new value
0127          * @param element value name
0128          */
0129         void updateValue(QComboBox *cb, const QString &valueName);
0130         /**
0131          * @brief Update a value in the currently selected element
0132          * @param value the new value
0133          * @param element element name
0134          */
0135         void updateValue(double value, const QString &valueName);
0136         void updateValue(int value, const QString &valueName);
0137         void updateValue(QColor value, const QString &valueName);
0138         void updateValue(QString value, const QString &valueName);
0139 
0140     private:
0141 
0142         CollimationOverlayOptions(QWidget *parent);
0143         static CollimationOverlayOptions *m_Instance;
0144 
0145         /**
0146          * @brief generateOpticalTrains Automatically generate optical trains based on the current profile information.
0147          * This happens when users use the tool for the first time.
0148          */
0149         void generateElement();
0150 
0151         /**
0152          * @brief Add a new collimation overlay element with the given name
0153          * @param name element name
0154          * @return unique element name
0155          */
0156         QString addElement(const QString &name);
0157 
0158         /**
0159          * @brief Create a unique element name
0160          * @param name original element name
0161          * @param type element type
0162          * @return name, eventually added (i) to make the element name unique
0163          */
0164         QString uniqueElementName(QString name, QString type);
0165 
0166         QList<QVariantMap> m_CollimationOverlayElements;
0167         QVariantMap *m_CurrentElement = nullptr;
0168 
0169         bool editing = false;
0170 
0171         // Table model
0172         QSqlTableModel *m_CollimationOverlayElementsModel = { nullptr };
0173 
0174         QList <QString> m_ElementNames;
0175 };