//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool UiTreeModelPdm::setData(const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/) { if (!index.isValid()) { return false; } PdmUiTreeItem* treeItem = UiTreeModelPdm::getTreeItemFromIndex(index); assert(treeItem); PdmObject* obj = treeItem->dataObject(); assert(obj); if (role == Qt::EditRole && obj->userDescriptionField()) { obj->userDescriptionField()->setValueFromUi(value); emitDataChanged(index); return true; } else if (role == Qt::CheckStateRole && obj->objectToggleField()) { bool toggleOn = (value == Qt::Checked); obj->objectToggleField()->setValueFromUi(toggleOn); emitDataChanged(index); return true; } return false; }
//-------------------------------------------------------------------------------------------------- /// Enable edit of this item if we have a editable user description field for a pdmObject /// Disable edit for other items //-------------------------------------------------------------------------------------------------- Qt::ItemFlags UiTreeModelPdm::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::ItemIsEnabled; Qt::ItemFlags flagMask = QAbstractItemModel::flags(index); PdmUiTreeItem* treeItem = getTreeItemFromIndex(index); if (treeItem) { PdmObject* pdmObject = treeItem->dataObject(); if (pdmObject) { if (pdmObject->userDescriptionField() && !pdmObject->userDescriptionField()->isUiReadOnly()) { flagMask = flagMask | Qt::ItemIsEditable; } if (pdmObject->objectToggleField()) { flagMask = flagMask | Qt::ItemIsUserCheckable; } } } else { flagMask = flagMask & (~Qt::ItemIsEditable); } return flagMask; }
//-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- 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(); }