1 /*
2     SPDX-FileCopyrightText: 2017 Jasem Mutlaq <mutlaqja@ikarustech.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "customproperties.h"
8 
CustomProperties()9 CustomProperties::CustomProperties()
10 {
11     setupUi(this);
12 
13     addB->setIcon(QIcon::fromTheme("go-next"));
14     addB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
15 
16     removeB->setIcon(QIcon::fromTheme("go-previous"));
17     removeB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
18 
19     clearB->setIcon(QIcon::fromTheme("edit-clear"));
20     clearB->setAttribute(Qt::WA_LayoutUsesWidgetRect);
21 
22     connect(addB, SIGNAL(clicked()), this, SLOT(slotAdd()));
23     connect(removeB, SIGNAL(clicked()), this, SLOT(slotRemove()));
24     connect(clearB, SIGNAL(clicked()), this, SLOT(slotClear()));
25 
26     connect(buttonBox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(slotApply()));
27 }
28 
setCCD(ISD::CCD * ccd)29 void CustomProperties::setCCD(ISD::CCD *ccd)
30 {
31     currentCCD = ccd;
32 
33     syncProperties();
34 }
35 
syncProperties()36 void CustomProperties::syncProperties()
37 {
38     availablePropertiesList->clear();
39     availablePropertiesList->clear();
40     QStringList props;
41 
42     const QStringList skipProperties = QStringList() << "CCD_TEMPERATURE" << "CCD_FRAME" << "CCD_EXPOSURE"
43                                        << "CCD_BINNING" << "GUIDER_FRAME" << "GUIDER_BINNING"
44                                        << "GUIDER_EXPOSURE" << "FILTER_SLOT" << "TELESCOPE_TIMED_GUIDE_NS"
45                                        << "TELESCOPE_TIMED_GUIDE_WE";
46 
47     for (auto &property : *currentCCD->getProperties())
48     {
49         const QString name = property->getName();
50         // Skip empty properties
51         if (name.isEmpty())
52             continue;
53 
54         if (property->getType() == INDI_NUMBER && property->getPermission() != IP_RO && skipProperties.contains(name) == false)
55             props << property->getLabel();
56     }
57 
58 
59     props.removeDuplicates();
60     availablePropertiesList->addItems(props);
61 }
62 
getCustomProperties() const63 QMap<QString, QMap<QString, double> > CustomProperties::getCustomProperties() const
64 {
65     return customProperties;
66 }
67 
setCustomProperties(const QMap<QString,QMap<QString,double>> & value)68 void CustomProperties::setCustomProperties(const QMap<QString, QMap<QString, double> > &value)
69 {
70     customProperties = value;
71 }
72 
slotAdd()73 void CustomProperties::slotAdd()
74 {
75     if (availablePropertiesList->selectedItems().isEmpty() == false)
76     {
77         QString prop = availablePropertiesList->selectedItems().first()->text();
78         if (jobPropertiesList->findItems(prop, Qt::MatchExactly).isEmpty())
79             jobPropertiesList->addItem(prop);
80     }
81 }
82 
slotRemove()83 void CustomProperties::slotRemove()
84 {
85     if (jobPropertiesList->selectedItems().isEmpty() == false)
86     {
87         QModelIndex i = jobPropertiesList->selectionModel()->currentIndex();
88         jobPropertiesList->model()->removeRow(i.row());
89     }
90 }
91 
slotClear()92 void CustomProperties::slotClear()
93 {
94     jobPropertiesList->clear();
95 }
96 
slotApply()97 void CustomProperties::slotApply()
98 {
99     if (currentCCD == nullptr)
100         return;
101 
102     // Reset job custom properties first
103     QMap<QString, QMap<QString, double> > newMap;
104 
105     for (int i = 0; i < jobPropertiesList->count(); i++)
106     {
107         QString numberLabel = jobPropertiesList->item(i)->text();
108 
109         // Match against existing properties
110         for(auto &indiProp : *currentCCD->getProperties())
111         {
112             // If label matches then we have the property
113             if (indiProp->getType() == INDI_NUMBER && QString(indiProp->getLabel()) == numberLabel)
114             {
115                 QMap<QString, double> numberProperty;
116 
117                 auto np = indiProp->getNumber();
118 
119                 for (const auto &it: *np)
120                     numberProperty[it.getName()] = it.getValue();
121 
122                 newMap[np->getName()] = numberProperty;
123 
124                 break;
125             }
126         }
127     }
128 
129     customProperties = newMap;
130     close();
131     emit valueChanged();
132 }
133