Skip to content
Snippets Groups Projects
Commit 4efe2c3f authored by David Li's avatar David Li Committed by Pospelov, Gennady
Browse files

TestProxyModel can filter out item between group item and property items

parent 93a70817
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@
#include "MultiLayerItem.h"
#include "SampleBuilderFactory.h"
#include "GUIObjectBuilder.h"
#include "TestView.h"
#include <QItemSelectionModel>
#include <QHBoxLayout>
#include <QTreeView>
......@@ -84,8 +85,12 @@ void TestComponentView::init_editors()
m_model->insertNewItem(Constants::ParticleType);
// tree view
m_treeView->setModel(m_model);
auto x = new TestProxyModel(this);
x->setSourceModel(m_model);
m_treeView->setModel( x);
m_treeView->expandAll();
m_treeView->resizeColumnToContents(0);
m_treeView->setAlternatingRowColors(true);
connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this,
SLOT(onSelectionChanged(QItemSelection, QItemSelection)));
......
......@@ -32,6 +32,7 @@
#include <InstrumentModel.h>
#include <JobModel.h>
#include <MaterialModel.h>
#include <QPersistentModelIndex>
#include "ModelMapper.h"
#include "DetectorItems.h"
......@@ -69,6 +70,10 @@ void TestView::test_sessionModel()
addModelToTabs(tabs, m_mainWindow->getMaterialModel());
addModelToTabs(tabs, m_mainWindow->getJobModel());
TestProxyModel *testModel = new TestProxyModel(this);
testModel->setSourceModel(m_mainWindow->getSampleModel());
addModelToTabs(tabs, testModel);
// do some testing here
m_mainWindow->getInstrumentModel()->rootItem()->getChildOfType(Constants::InstrumentType)->mapper()
->setOnChildPropertyChange(
......@@ -83,14 +88,17 @@ void TestView::test_sessionModel()
setLayout(layout);
}
void TestView::addModelToTabs(QTabWidget *tabs, SessionModel *model)
void TestView::addModelToTabs(QTabWidget *tabs, QAbstractItemModel *model)
{
QTreeView *view = new QTreeView;
view->setModel(model);
view->expandAll();
view->resizeColumnToContents(0);
view->resizeColumnToContents(1);
tabs->addTab(view, model->getModelTag());
if (SessionModel *sm = dynamic_cast<SessionModel*>(model))
tabs->addTab(view, sm->getModelTag());
else
tabs->addTab(view, "Some Model");
}
void TestView::test_MaskEditor()
......@@ -198,3 +206,70 @@ void TestView::test_RunFitWidget()
setLayout(layout);
}
TestProxyModel::TestProxyModel(QObject *parent)
: QIdentityProxyModel(parent)
{
}
void TestProxyModel::setSourceModel(QAbstractItemModel *source)
{
QIdentityProxyModel::setSourceModel(source);
m_source = dynamic_cast<SessionModel*>(source);
connect(m_source, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
this, SIGNAL(layoutChanged()));
}
QModelIndex TestProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
}
QModelIndex TestProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
ParameterizedItem *item = static_cast<ParameterizedItem*>(proxyIndex.internalPointer());
if (!item) {
return QModelIndex();
}
return m_source->index(proxyIndex.row(), proxyIndex.column(),
item->parent()->index());
}
QModelIndex TestProxyModel::index(int row, int column, const QModelIndex &parent) const
{
const QModelIndex sourceParent = mapToSource(parent);
ParameterizedItem *parentt = m_source->itemForIndex(sourceParent);
if (parentt->modelType() == Constants::GroupItemType) {
ParameterizedItem *cur = parentt->parent()->getGroupItem(parentt->itemName());
const QModelIndex sourceIndex = m_source->index(row, column, cur->index());
return mapFromSource(sourceIndex);
}
const QModelIndex sourceIndex = m_source->index(row, column, sourceParent);
return mapFromSource(sourceIndex);
}
QModelIndex TestProxyModel::parent(const QModelIndex &child) const
{
const QModelIndex sourceIndex = mapToSource(child);
ParameterizedItem *head = m_source->itemForIndex(sourceIndex.parent());
if (head && head->parent() && head->parent()->modelType() == Constants::GroupItemType) {
// skip immediate layer
return mapFromSource(head->parent()->index());
}
const QModelIndex sourceParent = sourceIndex.parent();
return mapFromSource(sourceParent);
}
int TestProxyModel::rowCount(const QModelIndex &parent) const
{
QModelIndex sourceParent = mapToSource(parent);
ParameterizedItem *item = m_source->itemForIndex(sourceParent);
if (item && item->modelType() == Constants::GroupItemType) {
ParameterizedItem *cur = item->parent()->getGroupItem(item->itemName());
if (cur)
return m_source->rowCount(cur->index());
else
qDebug() << "proxy::rowCount: null pointer";
}
return m_source->rowCount(sourceParent);
}
......@@ -20,7 +20,7 @@
class MainWindow;
class QTabWidget;
class SessionModel;
class QAbstractItemModel;
class TestView : public QWidget
{
......@@ -33,8 +33,29 @@ private:
void test_AccordionWidget();
void test_RunFitWidget();
MainWindow *m_mainWindow;
void addModelToTabs(QTabWidget *tabs, SessionModel *model);
void addModelToTabs(QTabWidget *tabs, QAbstractItemModel *model);
void test_sessionModel();
};
#include "SessionModel.h"
#include <QObject>
#include <QIdentityProxyModel>
#include <QModelIndex>
class TestProxyModel : public QIdentityProxyModel
{
Q_OBJECT
public:
TestProxyModel(QObject *parent = 0);
void setSourceModel(QAbstractItemModel *source);
QModelIndex mapFromSource(const QModelIndex& sourceIndex) const;
QModelIndex mapToSource(const QModelIndex& proxyIndex) const;
QModelIndex index(int row, int column, const QModelIndex& parent) const;
QModelIndex parent(const QModelIndex& child) const;
int rowCount(const QModelIndex& parent) const;
private:
SessionModel *m_source;
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment