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

new proxy model for sample tree view

parent 46fc2f76
No related branches found
No related tags found
No related merge requests found
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file coregui/Models/NJobModel.cpp
//! @brief Implements class NJobModel
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2015
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#include "SampleViewProxyModel.h"
int SampleViewProxyModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
bool SampleViewProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 1, sourceParent);
if (!sourceParent.isValid())
return true;
return !sourceModel()->data(index, Qt::DisplayRole).isValid();
// return (type == Constants::MultiLayerType || type == Constants::LayerType || type == Constants::ParticleType
// || type == Constants::ParticleCompositionType || type == Constants::ParticleCoreShellType
// || type == Constants::ParticleDistributionType);
}
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file coregui/Models/NJobModel.h
//! @brief Defines class NJobModel
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2015
//! @authors Scientific Computing Group at MLZ Garching
//! @authors C. Durniak, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#ifndef SAMPLEVIEWPROXYMODEL_H
#define SAMPLEVIEWPROXYMODEL_H
#include "WinDllMacros.h"
#include <QSortFilterProxyModel>
class BA_CORE_API_ SampleViewProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
SampleViewProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
int columnCount(const QModelIndex &parent) const;
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};
#endif
......@@ -32,7 +32,6 @@
namespace
{
const int MaxCompression = 9;
//enum EColumn { ITEM_NAME, MODEL_TYPE, MAX_COLUMNS };
const QString SET_ITEM_PROPERTY_ERROR = "SET_ITEM_PROPERTY_ERROR";
const QString ITEM_IS_NOT_INITIALIZED = "ITEM_IS_NOT_INITIALIZED";
......@@ -49,7 +48,6 @@ SessionModel::SessionModel(QString model_tag, QObject *parent)
createRootItem();
}
//NEW
void SessionModel::createRootItem()
{
m_root_item = ItemFactory::createEmptyItem();
......@@ -67,11 +65,11 @@ Qt::ItemFlags SessionModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags result_flags = QAbstractItemModel::flags(index);
if (index.isValid()) {
result_flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled // | Qt::ItemIsEditable
result_flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled
| Qt::ItemIsDragEnabled;
SessionItem *item = itemForIndex(index); // NEW make data editable as default
if (index.column() == ITEM_VALUE) // NEW
result_flags |= Qt::ItemIsEditable; // NEW
SessionItem *item = itemForIndex(index);
if (index.column() == ITEM_VALUE && item->value().isValid())
result_flags |= Qt::ItemIsEditable;
QVector<QString> acceptable_child_items = getAcceptableDefaultItemTypes(index);
if (acceptable_child_items.contains(m_dragged_item_type)) {
result_flags |= Qt::ItemIsDropEnabled;
......@@ -103,14 +101,12 @@ QVariant SessionModel::data(const QModelIndex &index, int role) const
QVariant SessionModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section >= 0 && section < columnCount(QModelIndex())) // NEW header data is storedin root item
return m_root_item->data(section); // NEW
// switch (section) {
// case ITEM_NAME:
// return tr("Name");
// case MODEL_TYPE:
// return tr("Model Type");
// }
switch (section) {
case ITEM_NAME:
return tr("Name");
case ITEM_VALUE:
return tr("Value");
}
}
return QVariant();
}
......@@ -150,9 +146,7 @@ QModelIndex SessionModel::parent(const QModelIndex &child) const
if (SessionItem *parent_item = child_item->parent()) {
if (parent_item == m_root_item)
return QModelIndex();
// if (SessionItem *grandparent_item = parent_item->parent()) {
// int row = grandparent_item->rowOfChild(parent_item);
return createIndex(parent_item->parentRow(), 0, parent_item); // CHANGED use new method
return createIndex(parent_item->parentRow(), 0, parent_item);
// }
}
}
......@@ -161,21 +155,11 @@ QModelIndex SessionModel::parent(const QModelIndex &child) const
bool SessionModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// if (!index.isValid() || index.column() != ITEM_NAME)
if (!index.isValid()/* || !index.data(role).isValid()*/) // NEW
if (!index.isValid())
return false;
QModelIndex dataIndex = index; // NEW
if (role == Qt::UserRole) { // NEW
dataIndex = index.sibling(index.row(), 1); // NEW
} // NEW
QModelIndex dataIndex = index;
if (SessionItem *item = itemForIndex(dataIndex)) {
// if (role == Qt::EditRole) {
// qDebug() << "SessionModel::setData ";
// item->setItemName(value.toString());
// } else
// return false;
if (item->setData(dataIndex.column(), value)) { // NEW
// emit dataChanged(dataIndex, dataIndex);
if (item->setData(role, value)) {
return true;
}
}
......@@ -187,11 +171,9 @@ bool SessionModel::removeRows(int row, int count, const QModelIndex &parent)
if (!m_root_item)
return false;
SessionItem *item = parent.isValid() ? itemForIndex(parent) : m_root_item;
// beginRemoveRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
delete item->takeRow(row);
}
// endRemoveRows();
return true;
}
......@@ -277,13 +259,7 @@ QModelIndex SessionModel::indexOfItem(SessionItem *item) const
SessionItem *SessionModel::insertNewItem(QString model_type, const QModelIndex &parent,
int row, QString tag)
{
// if (!m_root_item) {
// m_root_item = ItemFactory::createEmptyItem();
// }
SessionItem *parent_item = itemForIndex(parent);
// if (row == -1)
// row = parent_item->childItemCount();
// beginInsertRows(parent, row, row);
if (!parent_item)
parent_item = m_root_item;
if (row > parent_item->rowCount())
......@@ -299,26 +275,13 @@ SessionItem *SessionModel::insertNewItem(QString model_type, const QModelIndex &
}
}
// SessionItem *new_item = new SessionItem(model_type); // NEW -> item factory!
SessionItem *new_item = ItemFactory::createItem(model_type);
// if (port != SessionItem::PortInfo::DEFAULT)
// new_item->setPort(port);
if (!new_item)
throw GUIHelpers::Error("SessionModel::insertNewItem() -> Wrong model type " + model_type);
//note: now done by items themselves
// connect(new_item, SIGNAL(propertyChanged(const QString &)),
// this, SLOT(onItemPropertyChange(const QString &)));
// connect(new_item, SIGNAL(subItemChanged(const QString &)),
// this, SLOT(onItemPropertyChange(const QString &)));
// connect(new_item, SIGNAL(subItemPropertyChanged(QString,QString)),
// this, SLOT(onItemPropertyChange(const QString &, const QString &)));
parent_item->insertItem(row, new_item, tag);
// cleanItem(indexOfItem(parent_item), row, row);
return new_item;
}
......@@ -429,8 +392,6 @@ SessionItem *SessionModel::moveParameterizedItem(SessionItem *item, SessionItem
if (!new_parent->getTagInfo(tagName).modelTypes.empty() &&
!new_parent->getTagInfo(tagName).modelTypes.contains(item->modelType()))
return 0;
// if (!new_parent->acceptsAsChild(item->modelType()))
// return 0;
}
if (item->parent() == new_parent && indexOfItem(item).row() == row) {
......@@ -438,14 +399,6 @@ SessionItem *SessionModel::moveParameterizedItem(SessionItem *item, SessionItem
<< "SessionModel::moveParameterizedItem() -> no need to move, same parent, same row. ";
return item;
}
// QByteArray xml_data;
// QXmlStreamWriter writer(&xml_data);
// SessionWriter::writeItemAndChildItems(&writer, item);
// QXmlStreamReader reader(xml_data);
// if (row == -1)
// row = new_parent->rowCount();
SessionItem *stuff = item->parent()->takeRow(item->parent()->rowOfChild(item));
if(!new_parent->insertItem(row, stuff, tagName)) {
SessionTagInfo info = new_parent->getTagInfo(tagName);
......@@ -457,21 +410,6 @@ SessionItem *SessionModel::moveParameterizedItem(SessionItem *item, SessionItem
m_root_item->insertItem(-1, stuff);
}
// qDebug() << " SessionModel::moveParameterizedItem() >>> Beginning to insert "
// "indexOfItem(new_parent)" << indexOfItem(new_parent);
// beginInsertRows(indexOfItem(new_parent), row, row);
// SessionReader::readItems(&reader, new_parent, row);
// endInsertRows();
// SessionItem *newItem = new_parent->childAt(row);
// qDebug() << " ";
// qDebug() << " SessionModel::moveParameterizedItem() >>> Now deleting indexOfItem(item).row()"
// << indexOfItem(item).row();
// removeRows(indexOfItem(item).row(), 1, indexOfItem(item->parent()));
// cleanItem(indexOfItem(new_parent), row, row);
return stuff;
}
......
......@@ -16,6 +16,7 @@
#include "SampleViewComponents.h"
#include "widgetbox.h"
#include "SampleDesigner.h"
#include "SampleViewProxyModel.h"
SampleWidgetBox *SampleViewComponents::createWidgetBox(
......@@ -28,20 +29,11 @@ ItemTreeView *SampleViewComponents::createTreeView(
SampleModel *sampleModel, QWidget *parent)
{
ItemTreeView *tree_view = new ItemTreeView(parent);
MySortFilterProxyModel *proxy = new MySortFilterProxyModel(parent);
proxy->setSourceModel(sampleModel);
tree_view->setModel(proxy);
SampleViewProxyModel *proxy = new SampleViewProxyModel(parent);
proxy->setSourceModel(sampleModel);
tree_view->setModel(proxy);
return tree_view;
}
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
QModelIndex index = sourceModel()->index(sourceRow, 1, sourceParent);
if (!sourceParent.isValid())
return true;
return !sourceModel()->data(index, Qt::DisplayRole).isValid();
// return (type == Constants::MultiLayerType || type == Constants::LayerType || type == Constants::ParticleType
// || type == Constants::ParticleCompositionType || type == Constants::ParticleCoreShellType
// || type == Constants::ParticleDistributionType);
}
......@@ -24,8 +24,8 @@
#include "SampleWidgetBox.h"
#include "SampleModel.h"
#include <QSortFilterProxyModel>
#include <QSortFilterProxyModel>
//class BA_CORE_API_ SampleInfoStreamInterface : public QWidget
//{
......@@ -37,16 +37,7 @@
// }
//};
class BA_CORE_API_ MySortFilterProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
MySortFilterProxyModel(QObject *parent = 0) : QSortFilterProxyModel(parent) {}
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
};
class BA_CORE_API_ SampleViewComponents
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment