{
setDynamicSortFilter( false );
}
ProxyModel::~ProxyModel()
{
}
bool ProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
{
return filterLogic();
}
ProxyModel::ProxyModel( QObject *parent ): QSortFilterProxyModel( parent )
{
setDynamicSortFilter( false );
}
ProxyModel::~ProxyModel()
{
}
bool ProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent ) const
{
return filterLogic();
}
To copy to clipboard, switch view to plain text mode
Model::Model(const QList<QList<QVariant> > &lines,const QList<int>& positions)
{
QList<QVariant> rootData;
m_rootItem = new Item( rootData );
setupModelData( lines, positions, m_rootItem );
}
Model::~Model()
{
delete m_rootItem;
}
Item* Model::getRootItem() const
{
return m_rootItem;
}
{
if ( ! index.isValid() )
Item *item = static_cast<Item*>( index.internalPointer() );
switch( role )
{
case Qt::DisplayRole:
{
return item->data( index.column(),Qt::DisplayRole );
break;
}
}
}
{
if (( role != Qt::EditRole && role != Qt::DecorationRole && role != Qt::ForegroundRole && role != Qt::BackgroundRole ) || !index.isValid() )
return false;
/* Get the item of this index */
Item *item = static_cast<Item*>( index.internalPointer() );
/* Editing this item */
bool result = item->setData(index, value, role);
if (result)
emit dataChanged( index, index );
return result;
}
Qt
::ItemFlags Model
::flags( const QModelIndex &index
) const{
if ( ! index.isValid() )
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant Model
::headerData( int section, Qt
::Orientation orientation,
int role
) const {
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_rootItem->data( section,Qt::DisplayRole );
}
}
{
if ( ! hasIndex(row, column, parent) )
Item *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
Item *childItem = parentItem->child( row );
if ( childItem )
return createIndex( row, column, childItem );
else
}
{
if ( ! index.isValid() )
Item *childItem = static_cast<Item*>( index.internalPointer() );
Item *parentItem = childItem->parent();
if ( parentItem == m_rootItem )
if( parentItem == NULL )
{
}
return createIndex( parentItem->row(), 0, parentItem );
}
{
Item *parentItem;
if ( parent.column() > 0 )
return 0;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
return parentItem->childCount();
}
bool Model
::hasChildren ( const QModelIndex & parent
) const {
Item *parentItem;
if ( parent.column() > 0 )
return false;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
return parentItem->hasChildren();
}
void Model::setupModelData(const QList<QList<QVariant> >& lines, const QList<int>& positions, Item *parent)
{
//same logic from Sample tree example in Qt
}
Model::Model(const QList<QList<QVariant> > &lines,const QList<int>& positions)
{
QList<QVariant> rootData;
m_rootItem = new Item( rootData );
setupModelData( lines, positions, m_rootItem );
}
Model::~Model()
{
delete m_rootItem;
}
Item* Model::getRootItem() const
{
return m_rootItem;
}
QVariant Model::data( const QModelIndex &index, int role ) const
{
if ( ! index.isValid() )
return QVariant();
Item *item = static_cast<Item*>( index.internalPointer() );
switch( role )
{
case Qt::DisplayRole:
{
return item->data( index.column(),Qt::DisplayRole );
break;
}
}
return QVariant();
}
bool Model::setData( const QModelIndex &index, const QVariant &value, int role )
{
if (( role != Qt::EditRole && role != Qt::DecorationRole && role != Qt::ForegroundRole && role != Qt::BackgroundRole ) || !index.isValid() )
return false;
/* Get the item of this index */
Item *item = static_cast<Item*>( index.internalPointer() );
/* Editing this item */
bool result = item->setData(index, value, role);
if (result)
emit dataChanged( index, index );
return result;
}
Qt::ItemFlags Model::flags( const QModelIndex &index ) const
{
if ( ! index.isValid() )
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant Model::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
{
return m_rootItem->data( section,Qt::DisplayRole );
}
return QVariant();
}
QModelIndex Model::index( int row, int column, const QModelIndex &parent ) const
{
if ( ! hasIndex(row, column, parent) )
return QModelIndex();
Item *parentItem;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
Item *childItem = parentItem->child( row );
if ( childItem )
return createIndex( row, column, childItem );
else
return QModelIndex();
}
QModelIndex Model::parent( const QModelIndex &index ) const
{
if ( ! index.isValid() )
return QModelIndex();
Item *childItem = static_cast<Item*>( index.internalPointer() );
Item *parentItem = childItem->parent();
if ( parentItem == m_rootItem )
return QModelIndex();
if( parentItem == NULL )
{
return QModelIndex();
}
return createIndex( parentItem->row(), 0, parentItem );
}
int Model::rowCount( const QModelIndex &parent ) const
{
Item *parentItem;
if ( parent.column() > 0 )
return 0;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
return parentItem->childCount();
}
bool Model::hasChildren ( const QModelIndex & parent ) const
{
Item *parentItem;
if ( parent.column() > 0 )
return false;
if ( ! parent.isValid() )
parentItem = m_rootItem;
else
parentItem = static_cast<Item*>( parent.internalPointer() );
return parentItem->hasChildren();
}
void Model::setupModelData(const QList<QList<QVariant> >& lines, const QList<int>& positions, Item *parent)
{
//same logic from Sample tree example in Qt
}
To copy to clipboard, switch view to plain text mode
Bookmarks