//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
QVariant caf::UiTableModelPdm::data(const QModelIndex &index, int role /*= Qt::DisplayRole */) const
{
    if (m_pdmObjectGroup && (role == Qt::DisplayRole || role == Qt::EditRole))
    {
        if (index.row() < static_cast<int>(m_pdmObjectGroup->objects.size()))
        {
            PdmObject* pdmObject = m_pdmObjectGroup->objects[index.row()];
            if (pdmObject)
            {
                std::vector<PdmFieldHandle*> fields;
                pdmObject->fields(fields);

                if (index.column() < static_cast<int>(fields.size()))
                {
                    size_t fieldIndex = 0;

                    if (m_editorAttribute.fieldNames.size() > 0)
                    {
                        QString fieldName = m_editorAttribute.fieldNames[index.column()];
                        for (size_t i = 0; i < fields.size(); i++)
                        {
                            if (fields[i]->keyword() == fieldName)
                            {
                                fieldIndex = i;
                                break;
                            }
                        }
                    }
                    else
                    {
                        fieldIndex = index.column();
                    }

                    return fields[fieldIndex]->uiValue();
                }
            }
        }
    }

    return QVariant();
}
예제 #2
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
QVariant UiTreeModelPdm::data(const QModelIndex &index, int role /*= Qt::DisplayRole */) const
{
    if (!m_treeItemRoot)
        return QVariant();

    if (!index.isValid())
        return QVariant();

    PdmUiTreeItem* treeItem = UiTreeModelPdm::getTreeItemFromIndex(index);
    assert(treeItem);

    PdmObject* obj = treeItem->dataObject();

    if (obj == NULL) return QVariant();

    // We try to find the context of the object first: The parent field
    // If found, use its data to describe the thing
    // Note: This code will only find first field pointing at the current object. Its valid for now,
    // but will not generally be valid if references is introduced in the pdm system

    PdmFieldHandle* parentField = 0;

    PdmUiTreeItem* parentTreeItem = treeItem->parent();
    if (parentTreeItem)
    {
        PdmObject* parentObj = parentTreeItem->dataObject();
        if (parentObj)
        {
            std::vector<PdmFieldHandle*> fields;
            parentObj->fields(fields);

            size_t i;
            for (i = 0; i < fields.size(); ++i)
            {
                std::vector<PdmObject*> children;
                if (fields[i]) fields[i]->childObjects(&children);
                size_t cIdx;
                for (cIdx = 0; cIdx < children.size(); ++ cIdx)
                {
                    if (children[cIdx] == obj)
                    { 
                        parentField = fields[i];
                        break;
                    }
                }
                if (parentField) break;
            }
        }
    }

    assert(obj);

    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        if (obj->userDescriptionField())
        {
            return obj->userDescriptionField()->uiValue();
        }
        else
        {
            if (parentField && !parentField->uiName().isEmpty())
                return parentField->uiName();
            else
                return  obj->uiName();
        }
    }
    else if (role == Qt::DecorationRole)
    {
        if (parentField && !parentField->uiIcon().isNull())
            return parentField->uiIcon();
        else
            return obj->uiIcon();
    }
    else if (role == Qt::ToolTipRole)
    {
        if (parentField && !parentField->uiToolTip().isEmpty())
            return parentField->uiToolTip();
        else
            return  obj->uiToolTip();
    }
    else if (role == Qt::WhatsThisRole)
    {
        if (parentField && !parentField->uiWhatsThis().isEmpty())
            return parentField->uiWhatsThis();
        else
            return  obj->uiWhatsThis();
    }
    else if (role == Qt::CheckStateRole)
    {
        if (obj->objectToggleField())
        {
            bool isToggledOn = obj->objectToggleField()->uiValue().toBool();
            if (isToggledOn)
            {
                return Qt::Checked;
            }
            else
            {
                return Qt::Unchecked;
            }
        }
    }

    return QVariant();
}