File indexing completed on 2024-04-28 03:43:17

0001 /*
0002     SPDX-FileCopyrightText: 2017 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "customproperties.h"
0008 
0009 CustomProperties::CustomProperties()
0010 {
0011     setupUi(this);
0012 
0013     addB->setIcon(QIcon::fromTheme("go-next"));
0014     addB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0015 
0016     removeB->setIcon(QIcon::fromTheme("go-previous"));
0017     removeB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0018 
0019     clearB->setIcon(QIcon::fromTheme("edit-clear"));
0020     clearB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
0021 
0022     connect(addB, &QPushButton::clicked, this, &CustomProperties::slotAdd);
0023     connect(removeB, &QPushButton::clicked, this, &CustomProperties::slotRemove);
0024     connect(clearB, &QPushButton::clicked, this, &CustomProperties::slotClear);
0025 
0026     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(slotApply()));
0027 }
0028 
0029 void CustomProperties::setCCD(ISD::Camera *ccd)
0030 {
0031     currentCCD = ccd;
0032     syncProperties();
0033 }
0034 
0035 void CustomProperties::syncProperties()
0036 {
0037     availablePropertiesList->clear();
0038     QStringList props;
0039 
0040     const QStringList skipProperties = QStringList()
0041                                        << "CONNECTION"
0042                                        << "DEBUG"
0043                                        << "SIMULATION"
0044                                        << "CONFIG_PROCESS"
0045                                        << "CCD_TEMPERATURE"
0046                                        << "CCD_FRAME"
0047                                        << "CCD_EXPOSURE"
0048                                        << "CCD_BINNING"
0049                                        << "FRAME_TYPE"
0050                                        << "CCD_EXPOSURE_ABORT"
0051                                        << "GUIDER_FRAME"
0052                                        << "GUIDER_BINNING"
0053                                        << "GUIDER_EXPOSURE"
0054                                        << "GUIDER_EXPOSURE_ABORT"
0055                                        << "GUIDER_FRAME_TYPE"
0056                                        << "FILTER_SLOT"
0057                                        << "CCD_FRAME_RESET"
0058                                        << "WCS_CONTROL"
0059                                        << "UPLOAD_MODE"
0060                                        << "UPLOAD_SETTINGS"
0061                                        << "CCD_FILE_PATH"
0062                                        << "CCD_FAST_COUNT"
0063                                        << "ACTIVE_DEVICES"
0064                                        << "DEBUG_LEVEL"
0065                                        << "LOGGING_LEVEL"
0066                                        << "LOG_OUTPUT"
0067                                        << "FILE_DEBUG"
0068                                        << "EQUATORIAL_EOD_COORD"
0069                                        << "TARGET_EOD_COORD"
0070                                        << "TELESCOPE_TIMED_GUIDE_NS"
0071                                        << "TELESCOPE_TIMED_GUIDE_WE";
0072 
0073 
0074     for (auto &property : *currentCCD->getProperties())
0075     {
0076         const QString name = property.getName();
0077         // Skip empty properties
0078         if (name.isEmpty())
0079             continue;
0080 
0081         if (skipProperties.contains(name) ||
0082                 property.getPermission() == IP_RO ||
0083                 property.getType() == INDI_BLOB || property.getType() == INDI_LIGHT)
0084             continue;
0085 
0086         props << property.getLabel();
0087     }
0088 
0089 
0090     props.removeDuplicates();
0091     props.sort();
0092     availablePropertiesList->addItems(props);
0093 }
0094 
0095 QMap<QString, QMap<QString, QVariant> > CustomProperties::getCustomProperties() const
0096 {
0097     return customProperties;
0098 }
0099 
0100 void CustomProperties::setCustomProperties(const QMap<QString, QMap<QString, QVariant> > &value)
0101 {
0102     customProperties = value;
0103 }
0104 
0105 void CustomProperties::slotAdd()
0106 {
0107     if (availablePropertiesList->selectedItems().isEmpty() == false)
0108     {
0109         QString prop = availablePropertiesList->selectedItems().first()->text();
0110         if (jobPropertiesList->findItems(prop, Qt::MatchExactly).isEmpty())
0111             jobPropertiesList->addItem(prop);
0112     }
0113 }
0114 
0115 void CustomProperties::slotRemove()
0116 {
0117     if (jobPropertiesList->selectedItems().isEmpty() == false)
0118     {
0119         QModelIndex i = jobPropertiesList->selectionModel()->currentIndex();
0120         jobPropertiesList->model()->removeRow(i.row());
0121     }
0122 }
0123 
0124 void CustomProperties::slotClear()
0125 {
0126     jobPropertiesList->clear();
0127 }
0128 
0129 void CustomProperties::slotApply()
0130 {
0131     if (currentCCD == nullptr)
0132         return;
0133 
0134     // Remove any keys in the list from custom properties
0135     for (int i = 0; i < jobPropertiesList->count(); i++)
0136         customProperties.remove(jobPropertiesList->item(i)->text());
0137 
0138     // Start from existing properties not in the list (external ones set by Ekos e.g. CCD_GAIN)
0139     QMap<QString, QMap<QString, QVariant> > newMap = customProperties;
0140 
0141     for (int i = 0; i < jobPropertiesList->count(); i++)
0142     {
0143         auto label = jobPropertiesList->item(i)->text();
0144 
0145         // Match against existing properties
0146         for(auto &oneProperty : *currentCCD->getProperties())
0147         {
0148             // Search by label
0149             if (label != oneProperty.getLabel())
0150                 continue;
0151 
0152             // Now get all the elements for this property
0153             QMap<QString, QVariant> elements;
0154 
0155             switch (oneProperty.getType())
0156             {
0157                 case INDI_SWITCH:
0158                     for (const auto &oneSwitch : *oneProperty.getSwitch())
0159                     {
0160                         elements[oneSwitch.getName()] = oneSwitch.getState();
0161                     }
0162                     break;
0163                 case INDI_TEXT:
0164                     for (const auto &oneText : *oneProperty.getText())
0165                     {
0166                         elements[oneText.getName()] = oneText.getText();
0167                     }
0168                     break;
0169                 case INDI_NUMBER:
0170                     for (const auto &oneNumber : *oneProperty.getNumber())
0171                     {
0172                         elements[oneNumber.getName()] = oneNumber.getValue();
0173                     }
0174                     break;
0175 
0176                 default:
0177                     continue;
0178             }
0179 
0180             newMap[oneProperty.getName()] = elements;
0181 
0182             break;
0183         }
0184     }
0185 
0186     customProperties = newMap;
0187     close();
0188     emit valueChanged();
0189 }