#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,19 +155,31 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
m_treeView->expandAll();
}
PdmUiTreeSelectionEditorAttribute attributes;
QVariant fieldValue = field()->uiValue();
if (PdmUiTreeSelectionQModel::isSingleValueField(fieldValue))
{
m_textFilterLineEdit->hide();
m_toggleAllCheckBox->hide();
}
else if (PdmUiTreeSelectionQModel::isMultipleValueField(fieldValue))
{
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)
{
uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &attributes);
uiObject->editorAttribute(field()->fieldHandle(), uiConfigName, &m_attributes);
}
if (!attributes.showTextFilter)
if (!m_attributes.showTextFilter)
{
m_textFilterLineEdit->hide();
}
if (!attributes.showToggleAllCheckbox)
if (!m_attributes.showToggleAllCheckbox)
{
m_toggleAllCheckBox->hide();
}
@@ -191,6 +205,7 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
@@ -222,12 +237,7 @@ 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;
@@ -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,6 +295,17 @@ QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role
if (m_uiFieldHandle && m_uiFieldHandle->field())
{
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
if (isSingleValueField(fieldValue))
{
int row = fieldValue.toInt();
if (row == optionIndex(index))
{
return Qt::Checked;
}
}
else if (isMultipleValueField(fieldValue))
{
QList<QVariant> valuesSelectedInField = fieldValue.toList();
int opIndex = optionIndex(index);
@@ -300,6 +319,7 @@ QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role
}
}
}
}
return Qt::Unchecked;
}
@@ -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();
@@ -330,6 +356,24 @@ bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVar
if (!m_uiFieldHandle || !m_uiFieldHandle->field()) return false;
if (role == Qt::CheckStateRole)
{
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
if (isSingleValueField(fieldValue))
{
if (value.toBool() == true)
{
// 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;
}
}
else if (isMultipleValueField(fieldValue))
{
std::vector<unsigned int> selectedIndices;
{
@@ -379,6 +423,7 @@ bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVar
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

@@ -62,6 +62,7 @@ public:
~PdmUiTreeSelectionQModel();
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;