0.3.1
[qanava.git] / src / ui / uiNodesItemModel.cpp
blob6d07abed72c63a8834ca3f55041ae00d5041df61
1 /*
2 Qanava - Graph drawing library for QT
3 Copyright (C) 2006 Benoit AUTHEMAN
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
23 // \file uiNodesItemModel.cpp
24 // \author Benoit Autheman (benoit@libqanava.org)
25 // \date 2006 August 30
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "./uiNodesItemModel.h"
33 // QT headers
34 #include <QFont>
37 namespace qan { // ::qan
38 namespace ui { // ::qan::ui
41 //-----------------------------------------------------------------------------
42 NodesItemModel::NodesItemModel( Graph& graph, QObject *parent ) :
43 _graph( graph )
45 connect( &graph.getO( ), SIGNAL( rowAboutToBeInserted(const QModelIndex&,int,int) ), this, SLOT( nodeAboutToBeInserted(const QModelIndex&,int,int) ) );
46 connect( &graph.getO( ), SIGNAL( rowAboutToBeRemoved(const QModelIndex&,int,int) ), this, SLOT( nodeAboutToBeRemoved(const QModelIndex& parent,int,int) ) );
47 connect( &graph.getO( ), SIGNAL( rowInserted(const QModelIndex&,int,int) ), this, SLOT( nodeInserted(const QModelIndex&,int,int) ) );
48 connect( &graph.getO( ), SIGNAL( rowRemoved(const QModelIndex&,int,int) ), this, SLOT( nodeRemoved(const QModelIndex&,int,int) ) );
52 QVariant NodesItemModel::data( const QModelIndex &index, int role ) const
54 if ( !index.isValid( ) )
55 return QVariant( "index invalid" );
57 QVariant d;
59 if ( ( role == Qt::FontRole ) && ( index.column( ) == 0 ) )
61 QFont font;
62 font.setWeight( QFont::Bold );
63 d = font;
66 if ( role == Qt::DisplayRole )
68 Node* node = _graph.findNode( index.row( ) );
69 if ( node != 0 )
71 if ( index.column( ) == 0 )
72 d = QString( node->getLabel( ) );
73 if ( index.column( ) > 0 )
75 int attributeRole = index.column( ) - 1 + Node::StdAttributeCount;
76 if ( attributeRole < ( int )_graph.getAttributesCount( ) )
78 QString* attribute = node->getAttribute< QString >( attributeRole );
79 d = ( attribute != 0 ? QVariant( *attribute ) : QVariant( "" ) );
84 return d;
87 bool NodesItemModel::hasChildren( const QModelIndex& parent ) const
89 return false;
92 Qt::ItemFlags NodesItemModel::flags( const QModelIndex& index ) const
94 return ( index.isValid( ) ? Qt::ItemIsSelectable | Qt::ItemIsEnabled : Qt::ItemIsEnabled );
97 QVariant NodesItemModel::headerData( int section, Qt::Orientation orientation, int role ) const
99 QVariant d;
100 if ( role == Qt::DisplayRole )
102 int attributeRole = section - 1 + Node::StdAttributeCount;
103 if ( section == 0 )
104 return QString( "Label" );
105 else if ( attributeRole >= 0 && attributeRole < ( int )_graph.getAttributesCount( ) )
106 d = _graph.getAttributeName( attributeRole );
108 return d;
111 QModelIndex NodesItemModel::index(int row, int column, const QModelIndex& parent ) const
113 return createIndex( row, column, ( row * 10000 ) + column );
116 QModelIndex NodesItemModel::parent( const QModelIndex &index ) const
118 return QModelIndex( );
121 int NodesItemModel::rowCount( const QModelIndex& parent ) const
123 return _graph.getNodeCount( );
126 int NodesItemModel::columnCount( const QModelIndex& parent ) const
128 return ( 1 + _graph.getAttributesCount( ) - Node::StdAttributeCount );
131 void NodesItemModel::nodeAboutToBeInserted( const QModelIndex& parent, int start, int end )
133 QModelIndex nodeItem = _graph.getO( ).index( start, 0, parent );
134 if ( nodeItem.isValid( ) )
136 Node* node = static_cast< Node* >( nodeItem.internalPointer( ) );
137 int id = _graph.findNode( *node );
138 if ( id != -1 )
139 beginInsertRows( QModelIndex( ), id, id + 1 );
143 void NodesItemModel::nodeAboutToBeRemoved( const QModelIndex& parent, int start, int end )
145 /* int id = _graph.findNode( node );
146 if ( id != -1 )
147 beginRemoveRows( QModelIndex( ), id, id + 1 );*/
150 void NodesItemModel::nodeInserted( const QModelIndex& parent, int start, int end )
152 QModelIndex nodeItem = _graph.getO( ).index( start, 0, parent );
153 if ( nodeItem.isValid( ) )
155 Node* node = static_cast< Node* >( nodeItem.internalPointer( ) );
156 int id = _graph.findNode( *node );
157 if ( id != -1 )
158 emit rowsInserted( QModelIndex( ), id, id + 1 );
159 emit layoutChanged( );
163 void NodesItemModel::nodeRemoved( const QModelIndex& parent, int start, int end )
165 /*int id = _graph.findNode( node );
166 if ( id != -1 )
167 endRemoveRows( );
168 emit layoutChanged( );*/
170 //-----------------------------------------------------------------------------
173 } // ::qan::ui
174 } // ::qan