Example #1
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
bool PdmUiLineEditor::isMultipleFieldsWithSameKeywordSelected(PdmFieldHandle* editorField) const
{
    std::vector<PdmFieldHandle*> fieldsToUpdate;
    fieldsToUpdate.push_back(editorField);

    // For current selection, find all fields with same keyword
    std::vector<PdmUiItem*> items;
    SelectionManager::instance()->selectedItems(items, SelectionManager::FIRST_LEVEL);

    for (size_t i = 0; i < items.size(); i++)
    {
        PdmUiFieldHandle* uiField = dynamic_cast<PdmUiFieldHandle*>(items[i]);
        if (!uiField) continue;

        PdmFieldHandle* field = uiField->fieldHandle();
        if (field && field != editorField && field->keyword() == editorField->keyword())
        {
            fieldsToUpdate.push_back(field);
        }
    }

    if (fieldsToUpdate.size() > 1)
    {
        return true;
    }

    return false;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
PdmChildArrayFieldHandle* PdmUiTreeViewEditor::currentChildArrayFieldHandle()
{
    PdmUiItem* currentSelectedItem = SelectionManager::instance()->selectedItem(SelectionManager::CURRENT);

    PdmUiFieldHandle* uiFieldHandle = dynamic_cast<PdmUiFieldHandle*>(currentSelectedItem);
    if (uiFieldHandle)
    {
        PdmFieldHandle* fieldHandle = uiFieldHandle->fieldHandle();

        if (dynamic_cast<PdmChildArrayFieldHandle*>(fieldHandle))
        {
            return dynamic_cast<PdmChildArrayFieldHandle*>(fieldHandle);
        }
    }

    PdmObjectHandle* pdmObject = dynamic_cast<caf::PdmObjectHandle*>(currentSelectedItem);
    if (pdmObject)
    {
        PdmChildArrayFieldHandle* parentChildArray = dynamic_cast<PdmChildArrayFieldHandle*>(pdmObject->parentField());

        if (parentChildArray)
        {
            return parentChildArray;
        }
    }

    return NULL;
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void PdmUiTableViewModel::setPdmData(PdmChildArrayFieldHandle* listField, const QString& configName)
{
    beginResetModel();

    m_pdmList = listField;
    m_currentConfigName = configName;

    PdmUiOrdering config;

    if (m_pdmList && m_pdmList->size() > 0)
    {
        PdmObjectHandle* firstObject = m_pdmList->at(0);

        PdmUiObjectHandle* uiObject = uiObj(firstObject);
        if (uiObject)
        {
            uiObject->uiOrdering(configName, config);
        }
    }

    const std::vector<PdmUiItem*>& uiItems = config.uiItems();

    // Set all fieldViews to be unvisited
    std::map<QString, PdmUiFieldEditorHandle*>::iterator it;
    for (it = m_fieldEditors.begin(); it != m_fieldEditors.end(); ++it)
    {
        it->second->setField(NULL);
    }

    m_modelColumnIndexToFieldIndex.clear();

    for (size_t i = 0; i < uiItems.size(); ++i)
    {
        if (uiItems[i]->isUiHidden(configName)) continue;

        if (uiItems[i]->isUiGroup()) continue;

        {
            PdmUiFieldHandle* field = dynamic_cast<PdmUiFieldHandle*>(uiItems[i]);
            PdmUiFieldEditorHandle* fieldEditor = NULL;

            // Find or create FieldEditor
            std::map<QString, PdmUiFieldEditorHandle*>::iterator it;
            it = m_fieldEditors.find(field->fieldHandle()->keyword());

            if (it == m_fieldEditors.end())
            {
                // If editor type is specified, find in factory
                if ( !uiItems[i]->uiEditorTypeName(configName).isEmpty() )
                {
                    fieldEditor = Factory<PdmUiFieldEditorHandle, QString>::instance()->create(field->uiEditorTypeName(configName));
                }
                else
                { 
                    // Find the default field editor

                    QString editorTypeName = qStringTypeName(*(field->fieldHandle()));

                    // Handle a single value field with valueOptions: Make a combobox

                    if (field->uiValue().type() != QVariant::List)
                    {
                        bool useOptionsOnly = true; 
                        QList<PdmOptionItemInfo> options = field->valueOptions( &useOptionsOnly);

                        if (!options.empty())
                        {
                            editorTypeName = PdmUiComboBoxEditor::uiEditorTypeName();
                        }
                    }

                    fieldEditor = Factory<PdmUiFieldEditorHandle, QString>::instance()->create(editorTypeName);
                }

                if (fieldEditor)
                {
                    m_fieldEditors[field->fieldHandle()->keyword()] = fieldEditor;
                }
            }
            else
            {
                fieldEditor = it->second;
            }

            if (fieldEditor)
            {
                fieldEditor->setField(field); 

                //TODO: Create/update is not required at this point, as UI is recreated in getEditorWidgetAndTransferOwnership()
                // Can be moved, but a move will require changes in PdmUiFieldEditorHandle
                fieldEditor->createWidgets(NULL);
                fieldEditor->updateUi(configName);

                int fieldIndex = getFieldIndex(field->fieldHandle());
                m_modelColumnIndexToFieldIndex.push_back(fieldIndex);
            }
        }
    }


    // Remove all fieldViews not mentioned by the configuration from the layout

    std::vector< QString > fvhToRemoveFromMap;
    for (it = m_fieldEditors.begin(); it != m_fieldEditors.end(); ++it)
    {
        if (it->second->field() == 0)
        {
            PdmUiFieldEditorHandle* fvh = it->second;
            delete fvh;
            fvhToRemoveFromMap.push_back(it->first);               
        }
    }

    for (size_t i = 0; i < fvhToRemoveFromMap.size(); ++i)
    {
        m_fieldEditors.erase(fvhToRemoveFromMap[i]);
    }

    recreateTableItemEditors();

    endResetModel();
}