File indexing completed on 2024-07-21 06:28:22

0001 /*
0002     SPDX-FileCopyrightText: 2003 Jasem Mutlaq <mutlaqja@ikarustech.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "indigroup.h"
0008 #include "indiproperty.h"
0009 #include "indidevice.h"
0010 
0011 #include <QLocale>
0012 #include <QDialog>
0013 
0014 #include <QFrame>
0015 #include <QTabWidget>
0016 #include <QVBoxLayout>
0017 #include <QDebug>
0018 #include <QScrollArea>
0019 
0020 #include <KLocalizedString>
0021 
0022 /*******************************************************************
0023 ** INDI Group: a tab widget for common properties. All properties
0024 ** belong to a group, whether they have one or not but how the group
0025 ** is displayed differs
0026 *******************************************************************/
0027 INDI_G::INDI_G(INDI_D *idv, const QString &inName) : QScrollArea(idv)
0028 {
0029     dp   = idv;
0030     name = (inName.isEmpty()) ? i18n("Unknown") : inName;
0031 
0032     m_PropertiesContainer = new QFrame(this);
0033     m_PropertiesLayout    = new QVBoxLayout;
0034     m_PropertiesLayout->setContentsMargins(20, 20, 20, 20);
0035     m_VerticalSpacer = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
0036     m_PropertiesLayout->addItem(m_VerticalSpacer);
0037     m_PropertiesLayout->setSizeConstraint(QLayout::SetMinAndMaxSize);
0038     m_PropertiesContainer->setLayout(m_PropertiesLayout);
0039 
0040     setWidget(m_PropertiesContainer);
0041 }
0042 
0043 bool INDI_G::addProperty(INDI::Property newProperty)
0044 {
0045     if (!newProperty.isValid())
0046         return false;
0047 
0048     QString name(newProperty.getName());
0049 
0050     // No duplicates
0051     if (getProperty(name))
0052         return false;
0053 
0054     INDI_P *property = new INDI_P(this, newProperty);
0055     m_PropertiesList.append(property);
0056 
0057     m_PropertiesLayout->removeItem(m_VerticalSpacer);
0058     m_PropertiesLayout->addWidget(property);
0059     m_PropertiesLayout->addItem(m_VerticalSpacer);
0060     m_PropertiesLayout->invalidate();
0061 
0062     return true;
0063 }
0064 
0065 bool INDI_G::removeProperty(const QString &name)
0066 {
0067     INDI_P *oneProp = getProperty(name);
0068     if (oneProp)
0069     {
0070         m_PropertiesList.removeOne(oneProp);
0071         delete (oneProp);
0072         m_Dirty = true;
0073         return true;
0074     }
0075 
0076     return false;
0077 }
0078 
0079 INDI_P *INDI_G::getProperty(const QString &name) const
0080 {
0081     auto pos = std::find_if(m_PropertiesList.begin(), m_PropertiesList.end(), [name](INDI_P * oneProperty)
0082     {
0083         return oneProperty->getName() == name;
0084     });
0085     if (pos != m_PropertiesList.end())
0086         return *pos;
0087     else
0088         return nullptr;
0089 }