1
/**************************************************************************
2
* Copyright (C) 2009 Ben Cooksley <ben@eclipse.endoftheinternet.org> *
3
* Copyright (C) 2007 Will Stephenson <wstephenson@kde.org> *
5
* This program is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU General Public License *
7
* as published by the Free Software Foundation; either version 2 *
8
* of the License, or (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the Free Software *
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19
***************************************************************************/
21
#include "PredicateModel.h"
23
#include "PredicateItem.h"
25
class PredicateModel::Private {
29
PredicateItem * rootItem;
32
PredicateModel::PredicateModel( PredicateItem * menuRoot, QObject *parent )
33
: QAbstractItemModel( parent )
36
d->rootItem = menuRoot;
39
PredicateModel::~PredicateModel()
44
int PredicateModel::columnCount( const QModelIndex &parent ) const
50
int PredicateModel::rowCount( const QModelIndex &parent ) const
53
if ( parent.isValid() ) {
54
mi = static_cast<PredicateItem*>( parent.internalPointer() );
59
return mi->children().count();
62
QVariant PredicateModel::data( const QModelIndex &index, int role ) const
64
PredicateItem * mi = 0;
66
if ( !index.isValid() ) {
70
mi = static_cast<PredicateItem*>( index.internalPointer() );
73
theData.setValue( mi->prettyName() );
76
theData.setValue( mi );
84
Qt::ItemFlags PredicateModel::flags( const QModelIndex &index ) const
86
if ( !index.isValid() ) {
90
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
93
QModelIndex PredicateModel::index( int row, int column, const QModelIndex &parent ) const
95
if ( !hasIndex(row, column, parent) ) {
99
PredicateItem *parentItem;
100
if ( !parent.isValid() ) {
101
parentItem = d->rootItem;
103
parentItem = static_cast<PredicateItem*>( parent.internalPointer() );
106
PredicateItem *childItem = parentItem->children().value(row);
108
return createIndex( row, column, childItem );
110
return QModelIndex();
114
QModelIndex PredicateModel::parent( const QModelIndex &index ) const
116
PredicateItem *childItem = static_cast<PredicateItem*>( index.internalPointer() );
118
return QModelIndex();
121
PredicateItem * parent = childItem->parent();
122
PredicateItem * grandParent = parent->parent();
126
childRow = grandParent->children().indexOf( parent );
129
if ( parent == d->rootItem ) {
130
return QModelIndex();
132
return createIndex( childRow, 0, parent );
135
PredicateItem * PredicateModel::rootItem() const
140
void PredicateModel::setRootPredicate( PredicateItem * item )
146
void PredicateModel::itemUpdated( const QModelIndex& item )
148
emit dataChanged( item, item );
151
void PredicateModel::childrenChanging( const QModelIndex& item, Solid::Predicate::Type oldType )
153
PredicateItem * currentItem = static_cast<PredicateItem*>( item.internalPointer() );
154
Solid::Predicate::Type newType = currentItem->itemType;
156
if( oldType == newType ) {
160
if( rowCount(item) != 0 && newType != Solid::Predicate::Conjunction && newType != Solid::Predicate::Disjunction ) {
161
emit beginRemoveRows( item, 0, 1 );
162
currentItem->updateChildrenStatus();
163
emit endRemoveRows();
167
bool hasChildren = (newType == Solid::Predicate::Conjunction || newType == Solid::Predicate::Disjunction);
169
if( rowCount(item) == 0 && hasChildren ) {
170
emit beginInsertRows( item, 0, 1 );
171
currentItem->updateChildrenStatus();
172
emit endInsertRows();
176
#include "PredicateModel.moc"