#1830 AppFwk : Add unit test for tree selection QModel

This commit is contained in:
Magne Sjaastad committed 2017-09-07 14:56:09 +02:00
1 parent 841faf022b
commit e7351179c3
6 files changed
+159 -45

No files matched your search

@@ -43,12 +43,6 @@
#include <QTreeView>
#include <QLabel>
// #include <QAbstractItemModel>
// #include <QLabel>
// #include <QTreeView>
namespace caf
{
@@ -84,11 +78,47 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
caf::PdmUiTreeSelectionQModel* model = new caf::PdmUiTreeSelectionQModel(m_treeView);
m_treeView->setModel(model);
// TODO: Wire up signal from model to editor
connect(model, SIGNAL(signalSelectionStateForIndexHasChanged(int, bool)), this, SLOT(slotSetSelectionStateForIndex(int, bool)));
model->setOptions(this, options);
m_treeView->expandAll();
// TODO: Try to merge expanded state with newly generated tree
//m_treeView->expandAll();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeSelectionEditor::slotSetSelectionStateForIndex(int index, bool isSelected)
{
unsigned int unsignedValue = static_cast<unsigned int>(index);
QVariant fieldValue = field()->uiValue();
QList<QVariant> valuesSelectedInField = fieldValue.toList();
if (!isSelected)
{
valuesSelectedInField.removeAll(QVariant(unsignedValue));
}
else
{
bool isIndexPresent = false;
for (QVariant v : valuesSelectedInField)
{
unsigned int indexInField = v.toUInt();
if (indexInField == unsignedValue)
{
isIndexPresent = true;
}
}
if (!isIndexPresent)
{
valuesSelectedInField.push_back(QVariant(unsignedValue));
}
}
this->setValueToField(valuesSelectedInField);
}
//--------------------------------------------------------------------------------------------------
@@ -42,9 +42,7 @@ class QLabel;
class QTreeView;
class QAbstractItemModel;
class CheckableItemTreeModel;
namespace caf
namespace caf
{
//==================================================================================================
@@ -55,8 +53,6 @@ class PdmUiTreeSelectionEditor : public PdmUiFieldEditorHandle
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
friend class CheckableItemTreeModel;
public:
PdmUiTreeSelectionEditor();
virtual ~PdmUiTreeSelectionEditor();
@@ -66,10 +62,12 @@ protected:
virtual QWidget* createLabelWidget(QWidget * parent);
virtual void configureAndUpdateUi(const QString& uiConfigName);
private slots:
void slotSetSelectionStateForIndex(int index, bool isSelected);
private:
QPointer<QTreeView> m_treeView;
QPointer<QLabel> m_label;
};
} // end namespace caf
@@ -43,9 +43,6 @@
#include <QLabel>
#include <QTreeView>
//--------------------------------------------------------------------------------------------------
///
@@ -217,6 +214,8 @@ QVariant caf::PdmUiTreeSelectionQModel::data(const QModelIndex &index, int role
}
else if (role == Qt::CheckStateRole)
{
CAF_ASSERT(m_uiFieldHandle);
QVariant fieldValue = m_uiFieldHandle->field()->uiValue();
QList<QVariant> valuesSelectedInField = fieldValue.toList();
@@ -242,33 +241,9 @@ bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVar
{
if (role == Qt::CheckStateRole)
{
// TODO: wire up signal to editor
bool isSelected = value.toBool();
/*
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);
*/
emit signalSelectionStateForIndexHasChanged(toOptionItemIndex(index), isSelected);
return true;
}
@@ -40,7 +40,6 @@
#include <QAbstractItemModel>
namespace caf
{
@@ -53,6 +52,7 @@ class PdmUiFieldHandle;
//==================================================================================================
class PdmUiTreeSelectionQModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit PdmUiTreeSelectionQModel(QObject *parent = 0);
@@ -66,8 +66,11 @@ 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;
signals:
void signalSelectionStateForIndexHasChanged(int index, bool isSelected);
private:
static int toOptionItemIndex(const QModelIndex& modelIndex);
static int toOptionItemIndex(const QModelIndex& modelIndex);
private:
QList<caf::PdmOptionItemInfo> m_options;
@@ -24,6 +24,7 @@ set( PROJECT_FILES
cafUserInterface_UnitTests.cpp
cafPdmUiTreeViewModelTest.cpp
cafPdmUiTreeSelectionQModelTest.cpp
gtest/gtest-all.cpp
)
@@ -0,0 +1,107 @@
#include "gtest/gtest.h"
#include "cafPdmUiTreeSelectionQModel.h"
QList<caf::PdmOptionItemInfo> createOptions()
{
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);
}
{
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;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(PdmUiTreeSelectionQModelTest, BasicUsage)
{
QList<caf::PdmOptionItemInfo> options = createOptions();
caf::PdmUiTreeSelectionQModel myModel;
myModel.setOptions(nullptr, options);
EXPECT_EQ(4, myModel.rowCount(myModel.index(-1, -1)));
EXPECT_EQ(0, myModel.rowCount(myModel.index(0, 0)));
EXPECT_EQ(2, myModel.rowCount(myModel.index(1, 0)));
EXPECT_EQ(0, myModel.rowCount(myModel.index(2, 0)));
EXPECT_EQ(0, myModel.rowCount(myModel.index(3, 0)));
// Test for row out of bounds
EXPECT_FALSE(myModel.index(4, 0).isValid());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(PdmUiTreeSelectionQModelTest, ParentBehaviour)
{
QList<caf::PdmOptionItemInfo> options = createOptions();
caf::PdmUiTreeSelectionQModel myModel;
myModel.setOptions(nullptr, options);
QModelIndex parentIndex = myModel.index(1, 0);
EXPECT_EQ(2, myModel.rowCount(parentIndex));
{
QModelIndex firstChildIndex = myModel.index(0, 0, parentIndex);
EXPECT_STREQ("Second_a", myModel.data(firstChildIndex).toString().toAscii());
EXPECT_TRUE(parentIndex == myModel.parent(firstChildIndex));
}
{
QModelIndex secondChildIndex = myModel.index(1, 0, parentIndex);
EXPECT_STREQ("Second_b", myModel.data(secondChildIndex).toString().toAscii());
EXPECT_TRUE(parentIndex == myModel.parent(secondChildIndex));
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(PdmUiTreeSelectionQModelTest, SetDataAndSignal)
{
QList<caf::PdmOptionItemInfo> options = createOptions();
caf::PdmUiTreeSelectionQModel myModel;
myModel.setOptions(nullptr, options);
QModelIndex parentIndex = myModel.index(0, 0);
myModel.setData(parentIndex, QVariant(true));
}