bool DynamicObjectImp::removeAttributeByPath(QStringList pathComponents)
{
   if (pathComponents.size() == 0)
   {
      return false;
   }
   DynamicObject* pParent = dynamic_cast<DynamicObject*>(this);
   string attributeToRemove = pathComponents.last().toStdString();
   if (pathComponents.size() > 1)
   {
      pathComponents.removeLast();
      DataVariant& parentVar = getAttributeByPath(pathComponents);
      pParent = dv_cast<DynamicObject>(&parentVar);
   }
   if (pParent == NULL)
   {
      return false;
   }
   return pParent->removeAttribute(attributeToRemove);
}
Exemple #2
0
void FeatureClassDlg::removeField()
{
    QList<QTreeWidgetItem*> items = mpFieldTree->selectedItems();
    for (int i = 0; i < items.count(); ++i)
    {
        QTreeWidgetItem* pItem = items[i];
        if (pItem != NULL)
        {
            QString fieldName = pItem->text(0);

            // Remove the field from the member DynamicObject
            DynamicObject* pClass = getCurrentFeatureClass();
            if (pClass != NULL)
            {
                pClass->removeAttribute(fieldName.toStdString());
            }

            // Remove the item from the tree widget
            delete pItem;
        }
    }
}
Exemple #3
0
void FeatureClassDlg::setFieldData(QTreeWidgetItem* pItem, int column)
{
    if (pItem == NULL)
    {
        return;
    }

    DynamicObject* pClass = getCurrentFeatureClass();
    if (pClass == NULL)
    {
        return;
    }

    QString fieldName = pItem->text(0);
    if (column == 0)     // Name
    {
        QString oldFieldName = pItem->data(column, Qt::UserRole).toString();
        if (fieldName == oldFieldName)
        {
            return;
        }

        if (mpFieldTree->findItems(fieldName, Qt::MatchExactly, column).count() > 1)
        {
            QMessageBox::warning(this, windowTitle(), "Another field exists with the same name.  "
                                 "Please choose a unique name for the field.");
            pItem->setText(column, oldFieldName);
            return;
        }

        DataVariant field = pClass->getAttribute(oldFieldName.toStdString());
        pClass->removeAttribute(oldFieldName.toStdString());
        pClass->setAttribute(fieldName.toStdString(), field);

        pItem->setData(column, Qt::UserRole, QVariant(fieldName));
    }
    else                 // Type or Value
    {
        QString fieldType = pItem->text(1);
        QString fieldValue = pItem->text(2);

        if (column == 1)  // Type
        {
            // If the type changed, ensure that the value is valid with the new type and reset the value if necessary
            QString validateString = fieldValue;
            int pos = 0;

            if (fieldType == "int")
            {
                QIntValidator validator(this);
                if (validator.validate(validateString, pos) != QValidator::Acceptable)
                {
                    pItem->setText(2, "0");
                    return;     // Return since this method will be called again as a result to changing the value text
                }
            }
            else if (fieldType == "double")
            {
                QDoubleValidator validator(this);
                if (validator.validate(fieldValue, pos) != QValidator::Acceptable)
                {
                    pItem->setText(2, "0.0");
                    return;     // Return since this method will be called again as a result to changing the value text
                }
            }
        }

        DataVariant field;
        if (fieldType == "int")
        {
            int intValue = fieldValue.toInt();
            field = DataVariant(intValue);
        }
        else if (fieldType == "double")
        {
            double doubleValue = fieldValue.toDouble();
            field = DataVariant(doubleValue);
        }
        else if (fieldType == "string")
        {
            std::string stringValue = fieldValue.toStdString();
            field = DataVariant(stringValue);
        }

        pClass->setAttribute(fieldName.toStdString(), field);
    }
}