#1895 Curve Creator : Add support for single value fields

This commit is contained in:
Magne Sjaastad 2017-09-20 08:21:03 +02:00
parent e369b8a131
commit 1bb1c87058
4 changed files with 188 additions and 77 deletions

View File

@ -38,6 +38,7 @@
#include "cafAssert.h"
#include "cafPdmObject.h"
#include "cafPdmUiCommandSystemProxy.h"
#include "cafPdmUiTreeSelectionQModel.h"
#include <QBoxLayout>
@ -125,11 +126,6 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
PdmUiFieldEditorHandle::updateLabelFromField(m_label, uiConfigName);
// Tree view
bool optionsOnly = true;
QList<PdmOptionItemInfo> options = field()->valueOptions(&optionsOnly);
if (!m_model)
{
m_model = new caf::PdmUiTreeSelectionQModel(m_treeView);
@ -139,8 +135,14 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_treeView->setModel(m_proxyModel);
connect(m_treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)),
this, SLOT(slotCurrentChanged(QModelIndex, QModelIndex)));
}
bool optionsOnly = true;
QList<PdmOptionItemInfo> options = field()->valueOptions(&optionsOnly);
bool itemCountHasChaged = false;
if (m_model->optionItemCount() != options.size()) itemCountHasChaged = true;
@ -153,41 +155,54 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
m_treeView->expandAll();
}
PdmUiTreeSelectionEditorAttribute attributes;
caf::PdmUiObjectHandle* uiObject = uiObj(field()->fieldHandle()->ownerObject());
if (uiObject)
{
uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &attributes);
}
if (!attributes.showTextFilter)
QVariant fieldValue = field()->uiValue();
if (PdmUiTreeSelectionQModel::isSingleValueField(fieldValue))
{
m_textFilterLineEdit->hide();
}
if (!attributes.showToggleAllCheckbox)
{
m_toggleAllCheckBox->hide();
}
else
else if (PdmUiTreeSelectionQModel::isMultipleValueField(fieldValue))
{
if (options.size() == 0)
m_treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_treeView, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)));
caf::PdmUiObjectHandle* uiObject = uiObj(field()->fieldHandle()->ownerObject());
if (uiObject)
{
m_toggleAllCheckBox->setChecked(false);
uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &m_attributes);
}
if (!m_attributes.showTextFilter)
{
m_textFilterLineEdit->hide();
}
if (!m_attributes.showToggleAllCheckbox)
{
m_toggleAllCheckBox->hide();
}
else
{
bool allItemsChecked = true;
QModelIndexList indices = allVisibleSourceModelIndices();
for (auto mi : indices)
if (options.size() == 0)
{
if (m_model->data(mi, Qt::CheckStateRole).toBool() == false)
{
allItemsChecked = false;
}
m_toggleAllCheckBox->setChecked(false);
}
else
{
bool allItemsChecked = true;
QModelIndexList indices = allVisibleSourceModelIndices();
for (auto mi : indices)
{
if (m_model->data(mi, Qt::CheckStateRole).toBool() == false)
{
allItemsChecked = false;
}
}
m_toggleAllCheckBox->setChecked(allItemsChecked);
m_toggleAllCheckBox->setChecked(allItemsChecked);
}
}
}
}
@ -222,13 +237,8 @@ QWidget* PdmUiTreeSelectionEditor::createEditorWidget(QWidget* parent)
QTreeViewHeightHint* treeViewHeightHint = new QTreeViewHeightHint(parent);
treeViewHeightHint->setHeightHint(2000);
treeViewHeightHint->setHeaderHidden(true);
treeViewHeightHint->setSelectionMode(QAbstractItemView::ExtendedSelection);
treeViewHeightHint->setContextMenuPolicy(Qt::CustomContextMenu);
connect(treeViewHeightHint, SIGNAL(customContextMenuRequested(QPoint)), SLOT(customMenuRequested(QPoint)));
m_treeView = treeViewHeightHint;
layout->addWidget(treeViewHeightHint);
@ -394,6 +404,21 @@ void PdmUiTreeSelectionEditor::slotTextFilterChanged()
updateUi();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeSelectionEditor::slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous)
{
if (m_attributes.highLightField)
{
QVariant v = m_proxyModel->data(current, PdmUiTreeSelectionQModel::optionItemValueRole());
//m_attributes.highLightField->fieldHandle()->setvalue();
PdmUiCommandSystemProxy::instance()->setUiValueToField(m_attributes.highLightField, v);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -47,6 +47,7 @@ class QCheckBox;
class QLineEdit;
class QSortFilterProxyModel;
class QModelIndex;
class QItemSelection;
namespace caf
{
@ -61,11 +62,15 @@ public:
bool showTextFilter;
bool showToggleAllCheckbox;
caf::PdmUiFieldHandle* highLightField;
public:
PdmUiTreeSelectionEditorAttribute()
{
showTextFilter = true;
showToggleAllCheckbox = true;
highLightField = nullptr;
}
};
@ -98,6 +103,8 @@ private slots:
void slotTextFilterChanged();
void slotCurrentChanged(const QModelIndex & current, const QModelIndex & previous);
private:
void checkAllItems();
void unCheckAllItems();
@ -114,6 +121,8 @@ private:
PdmUiTreeSelectionQModel* m_model;
QSortFilterProxyModel* m_proxyModel;
PdmUiTreeSelectionEditorAttribute m_attributes;
};
} // end namespace caf

View File

@ -75,6 +75,14 @@ int caf::PdmUiTreeSelectionQModel::headingRole()
return Qt::UserRole + 1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int caf::PdmUiTreeSelectionQModel::optionItemValueRole()
{
return Qt::UserRole + 2;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -287,16 +295,28 @@ QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role
if (m_uiFieldHandle && m_uiFieldHandle->field())
{
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> valuesSelectedInField = fieldValue.toList();
int opIndex = optionIndex(index);
for (QVariant v : valuesSelectedInField)
if (isSingleValueField(fieldValue))
{
int indexInField = v.toInt();
if (indexInField == opIndex)
int row = fieldValue.toInt();
if (row == optionIndex(index))
{
return Qt::Checked;
}
}
else if (isMultipleValueField(fieldValue))
{
QList<QVariant> valuesSelectedInField = fieldValue.toList();
int opIndex = optionIndex(index);
for (QVariant v : valuesSelectedInField)
{
return Qt::Checked;
int indexInField = v.toInt();
if (indexInField == opIndex)
{
return Qt::Checked;
}
}
}
}
@ -317,6 +337,12 @@ QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role
{
return optionItemInfo->isHeading();
}
else if (role == optionItemValueRole())
{
QVariant v = optionItemInfo->value();
return v;
}
}
return QVariant();
@ -331,53 +357,72 @@ bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVar
if (role == Qt::CheckStateRole)
{
std::vector<unsigned int> selectedIndices;
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
if (isSingleValueField(fieldValue))
{
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> fieldValueSelection = fieldValue.toList();
for (auto v : fieldValueSelection)
if (value.toBool() == true)
{
selectedIndices.push_back(v.toUInt());
// Reset model to make sure other check boxes are invalidated
beginResetModel();
QVariant v = static_cast<unsigned int>(optionIndex(index));
PdmUiCommandSystemProxy::instance()->setUiValueToField(m_uiFieldHandle->field(), v);
endResetModel();
return true;
}
}
bool setSelected = value.toBool();
unsigned int opIndex = static_cast<unsigned int>(optionIndex(index));
if (setSelected)
else if (isMultipleValueField(fieldValue))
{
bool isIndexPresent = false;
for (auto indexInField : selectedIndices)
std::vector<unsigned int> selectedIndices;
{
if (indexInField == opIndex)
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> fieldValueSelection = fieldValue.toList();
for (auto v : fieldValueSelection)
{
isIndexPresent = true;
selectedIndices.push_back(v.toUInt());
}
}
if (!isIndexPresent)
bool setSelected = value.toBool();
unsigned int opIndex = static_cast<unsigned int>(optionIndex(index));
if (setSelected)
{
selectedIndices.push_back(opIndex);
bool isIndexPresent = false;
for (auto indexInField : selectedIndices)
{
if (indexInField == opIndex)
{
isIndexPresent = true;
}
}
if (!isIndexPresent)
{
selectedIndices.push_back(opIndex);
}
}
else
{
selectedIndices.erase(std::remove(selectedIndices.begin(), selectedIndices.end(), opIndex), selectedIndices.end());
}
QList<QVariant> fieldValueSelection;
for (auto v : selectedIndices)
{
fieldValueSelection.push_back(QVariant(v));
}
PdmUiCommandSystemProxy::instance()->setUiValueToField(m_uiFieldHandle->field(), fieldValueSelection);
emit dataChanged(index, index);
return true;
}
else
{
selectedIndices.erase(std::remove(selectedIndices.begin(), selectedIndices.end(), opIndex), selectedIndices.end());
}
QList<QVariant> fieldValueSelection;
for (auto v : selectedIndices)
{
fieldValueSelection.push_back(QVariant(v));
}
PdmUiCommandSystemProxy::instance()->setUiValueToField(m_uiFieldHandle->field(), fieldValueSelection);
emit dataChanged(index, index);
return true;
}
return false;
@ -416,3 +461,29 @@ void caf::PdmUiTreeSelectionQModel::buildOptionItemTree(int parentOptionIndex, T
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::PdmUiTreeSelectionQModel::isSingleValueField(const QVariant& fieldValue)
{
if (fieldValue.type() == QVariant::Int || fieldValue.type() == QVariant::UInt)
{
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::PdmUiTreeSelectionQModel::isMultipleValueField(const QVariant& fieldValue)
{
if (fieldValue.type() == QVariant::List)
{
return true;
}
return false;
}

View File

@ -61,7 +61,8 @@ public:
explicit PdmUiTreeSelectionQModel(QObject *parent = 0);
~PdmUiTreeSelectionQModel();
static int headingRole();
static int headingRole();
static int optionItemValueRole();
void setCheckedStateForItems(const QModelIndexList& indices, bool checked);
@ -76,6 +77,10 @@ public:
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
// Consider moving these functions to PdmUiFieldHandle
static bool isSingleValueField(const QVariant& fieldValue);
static bool isMultipleValueField(const QVariant& fieldValue);
private:
typedef caf::UiTreeItem<int> TreeItemType;
@ -83,6 +88,7 @@ private:
int optionIndex(const QModelIndex &index) const;
void buildOptionItemTree(int optionIndex, TreeItemType* parentNode);
private:
QList<caf::PdmOptionItemInfo> m_options;
caf::PdmUiFieldEditorHandle* m_uiFieldHandle;