#1830 AppFwk : Add TreeSelectionEditor and TreeSelectionQModel

This commit is contained in:
Magne Sjaastad 2017-09-05 13:29:38 +02:00
parent 9eab559272
commit 841faf022b
6 changed files with 610 additions and 1 deletions

View File

@ -31,6 +31,7 @@
#include "cafPdmUiTextEditor.h"
#include "cafPdmUiTreeView.h"
#include "cafSelectionManager.h"
#include "cafPdmUiTreeSelectionEditor.h"
@ -72,7 +73,11 @@ public:
CAF_PDM_InitFieldNoDefault(&m_multiSelectList, "SelectedItems", " ", "", "", "");
m_multiSelectList.xmlCapability()->setIOReadable(false);
m_multiSelectList.xmlCapability()->setIOWritable(false);
m_multiSelectList.uiCapability()->setUiEditorTypeName(caf::PdmUiListEditor::uiEditorTypeName());
m_multiSelectList.uiCapability()->setUiEditorTypeName(caf::PdmUiTreeSelectionEditor::uiEditorTypeName());
m_multiSelectList.v().push_back("First");
m_multiSelectList.v().push_back("Second");
m_multiSelectList.v().push_back("Third");
}
@ -101,6 +106,50 @@ public:
void setDoubleMember(const double& d) { m_doubleMember = d; std::cout << "setDoubleMember" << std::endl; }
double doubleMember() const { std::cout << "doubleMember" << std::endl; return m_doubleMember; }
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
virtual QList<caf::PdmOptionItemInfo> calculateValueOptions(const caf::PdmFieldHandle* fieldNeedingOptions, bool* useOptionsOnly) override
{
QList<caf::PdmOptionItemInfo> options;
QString text;
text = "First";
options.push_back(caf::PdmOptionItemInfo(text, text));
text = "Second";
options.push_back(caf::PdmOptionItemInfo(text, text));
{
text = "Second_a";
caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text);
itemInfo.setLevel(1);
options.push_back(itemInfo);
}
if (1)
{
text = "Second_b";
caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text);
itemInfo.setLevel(1);
options.push_back(itemInfo);
}
text = "Third";
options.push_back(caf::PdmOptionItemInfo(text, text));
text = "Fourth";
options.push_back(caf::PdmOptionItemInfo(text, text));
return options;
}
private:
double m_doubleMember;

View File

@ -45,6 +45,8 @@ set( QOBJECT_HEADERS
cafPdmUiTreeViewEditor.h
cafUiProcess.h
QMinimizePanel.h
cafPdmUiTreeSelectionEditor.h
cafPdmUiTreeSelectionQModel.h
)
if ( (${CMAKE_VERSION} VERSION_LESS 2.8.6) OR (NOT CMAKE_AUTOMOC) )
@ -86,6 +88,10 @@ set( PROJECT_FILES
cafPdmUiToolBarEditor.h
cafPdmUiToolButtonEditor.cpp
cafPdmUiToolButtonEditor.h
cafPdmUiTreeSelectionEditor.h
cafPdmUiTreeSelectionEditor.cpp
cafPdmUiTreeSelectionQModel.h
cafPdmUiTreeSelectionQModel.cpp
# object editors

View File

@ -0,0 +1,114 @@
//##################################################################################################
//
// Custom Visualization Core library
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmUiTreeSelectionEditor.h"
#include "cafAssert.h"
#include "cafPdmObject.h"
#include "cafPdmUiTreeSelectionQModel.h"
#include <QTreeView>
#include <QLabel>
// #include <QAbstractItemModel>
// #include <QLabel>
// #include <QTreeView>
namespace caf
{
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiTreeSelectionEditor);
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiTreeSelectionEditor::PdmUiTreeSelectionEditor()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiTreeSelectionEditor::~PdmUiTreeSelectionEditor()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
{
bool optionsOnly = true;
QList<PdmOptionItemInfo> options = field()->valueOptions(&optionsOnly);
caf::PdmUiTreeSelectionQModel* model = new caf::PdmUiTreeSelectionQModel(m_treeView);
m_treeView->setModel(model);
// TODO: Wire up signal from model to editor
model->setOptions(this, options);
m_treeView->expandAll();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiTreeSelectionEditor::createEditorWidget(QWidget * parent)
{
m_treeView = new QTreeView(parent);
return m_treeView;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget* PdmUiTreeSelectionEditor::createLabelWidget(QWidget * parent)
{
m_label = new QLabel(parent);
return m_label;
}
} // end namespace caf

View File

@ -0,0 +1,75 @@
//##################################################################################################
//
// Custom Visualization Core library
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
class QLabel;
class QTreeView;
class QAbstractItemModel;
class CheckableItemTreeModel;
namespace caf
{
//==================================================================================================
///
//==================================================================================================
class PdmUiTreeSelectionEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
friend class CheckableItemTreeModel;
public:
PdmUiTreeSelectionEditor();
virtual ~PdmUiTreeSelectionEditor();
protected:
virtual QWidget* createEditorWidget(QWidget * parent);
virtual QWidget* createLabelWidget(QWidget * parent);
virtual void configureAndUpdateUi(const QString& uiConfigName);
private:
QPointer<QTreeView> m_treeView;
QPointer<QLabel> m_label;
};
} // end namespace caf

View File

@ -0,0 +1,287 @@
//##################################################################################################
//
// Custom Visualization Core library
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#include "cafPdmUiTreeSelectionQModel.h"
#include "cafPdmUiTreeViewModel.h"
#include "cafPdmObject.h"
#include <QAbstractItemModel>
#include <QLabel>
#include <QTreeView>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
caf::PdmUiTreeSelectionQModel::PdmUiTreeSelectionQModel(QObject *parent /*= 0*/) : QAbstractItemModel(parent)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void caf::PdmUiTreeSelectionQModel::setOptions(caf::PdmUiFieldEditorHandle* field, const QList<caf::PdmOptionItemInfo>& options)
{
m_uiFieldHandle = field;
m_options = options;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Qt::ItemFlags caf::PdmUiTreeSelectionQModel::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QModelIndex caf::PdmUiTreeSelectionQModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
if (parent.isValid())
{
int optionItemIndex = parent.internalId() + 1 + row;
return createIndex(row, column, optionItemIndex);
}
else
{
int optionItemIndex = 0;
int zeroLevelItemCount = 0;
while (optionItemIndex < m_options.size())
{
if (m_options[optionItemIndex].level() == 0)
{
zeroLevelItemCount++;
if (zeroLevelItemCount == row + 1)
{
return createIndex(row, column, optionItemIndex);
}
}
optionItemIndex++;
}
}
return QModelIndex();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int caf::PdmUiTreeSelectionQModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return 1;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QModelIndex caf::PdmUiTreeSelectionQModel::parent(const QModelIndex &child) const
{
if (!child.isValid())
return QModelIndex();
int childOptionIndex = toOptionItemIndex(child);
int childLevel = m_options[childOptionIndex].level();
if (childLevel < 1)
{
return QModelIndex();
}
int parentOptionIndex = childOptionIndex - 1;
while (parentOptionIndex > -1)
{
if (m_options[parentOptionIndex].level() == childLevel - 1)
{
int parentRow = 0;
int parentLevelOptionIndex = parentOptionIndex - 1;
while (parentLevelOptionIndex > -1 && m_options[parentLevelOptionIndex].level() > childLevel - 2)
{
if (m_options[parentLevelOptionIndex].level() == childLevel - 1)
{
parentRow++;
}
parentLevelOptionIndex--;
}
return createIndex(parentRow, 0, parentOptionIndex);
}
parentOptionIndex--;
}
CAF_ASSERT(false);
return QModelIndex();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int caf::PdmUiTreeSelectionQModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
if (!parent.isValid())
{
int childCount = 0;
for (auto o : m_options)
{
if (o.level() == 0)
{
childCount++;
}
}
return childCount;
}
else
{
int childCount = 0;
int parentOptionIndex = toOptionItemIndex(parent);
int parentLevel = m_options[parentOptionIndex].level();
int currentOptionIndex = parentOptionIndex + 1;
while (currentOptionIndex < m_options.size() && m_options[currentOptionIndex].level() > parentLevel)
{
if (m_options[currentOptionIndex].level() == parentLevel + 1)
{
childCount++;
}
currentOptionIndex++;
}
return childCount;
}
CAF_ASSERT(false);
return 0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
CAF_ASSERT(index.internalId() < m_options.size());
if (role == Qt::DisplayRole)
{
return m_options[toOptionItemIndex(index)].optionUiText();
}
else if (role == Qt::CheckStateRole)
{
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> valuesSelectedInField = fieldValue.toList();
for (QVariant v : valuesSelectedInField)
{
int indexInField = v.toInt();
if (indexInField == toOptionItemIndex(index))
{
return Qt::Checked;
}
}
return Qt::Unchecked;
}
return QVariant();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/)
{
if (role == Qt::CheckStateRole)
{
// TODO: wire up signal to editor
/*
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> valuesSelectedInField = fieldValue.toList();
bool isPresent = false;
for (QVariant v : valuesSelectedInField)
{
unsigned int indexInField = v.toUInt();
if (indexInField == toOptionItemIndex(index))
{
valuesSelectedInField.removeAll(v);
isPresent = true;
}
}
if (!isPresent)
{
// Use unsigned int as all values communicated from UI to field is assumed to be unsigned int
unsigned int value = static_cast<unsigned int>(toOptionItemIndex(index));
valuesSelectedInField.push_back(QVariant(value));
}
m_uiFieldHandle->setValueToField(valuesSelectedInField);
*/
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int caf::PdmUiTreeSelectionQModel::toOptionItemIndex(const QModelIndex& modelIndex)
{
CAF_ASSERT(modelIndex.isValid());
return modelIndex.internalId();
}

View File

@ -0,0 +1,78 @@
//##################################################################################################
//
// Custom Visualization Core library
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
#include <QAbstractItemModel>
namespace caf
{
class PdmOptionItemInfo;
class PdmUiFieldHandle;
//==================================================================================================
///
//==================================================================================================
class PdmUiTreeSelectionQModel : public QAbstractItemModel
{
public:
explicit PdmUiTreeSelectionQModel(QObject *parent = 0);
void setOptions(caf::PdmUiFieldEditorHandle* field, const QList<caf::PdmOptionItemInfo>& options);
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &child) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
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;
private:
static int toOptionItemIndex(const QModelIndex& modelIndex);
private:
QList<caf::PdmOptionItemInfo> m_options;
caf::PdmUiFieldEditorHandle* m_uiFieldHandle;
};
} // end namespace caf