00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "model_GraphLayers.h"
00021 #include "graphDocument.h"
00022 #include "graph.h"
00023 #include <QString>
00024 #include <QModelIndex>
00025 #include <KDebug>
00026
00027 GraphLayersModel::GraphLayersModel( GraphDocument* document, QObject* parent )
00028 : QAbstractListModel( parent )
00029 {
00030 _document = document;
00031 }
00032
00033 int GraphLayersModel::rowCount( const QModelIndex& ) const
00034 {
00035 if( _document == 0 ) return 0;
00036 return _document -> size();
00037 }
00038
00039 QVariant GraphLayersModel::data( const QModelIndex& index, int role ) const
00040 {
00041 if( _document == 0 )
00042 {
00043 return 0;
00044 }
00045 if(( !index.isValid() ) || ( index.row() > _document -> size() ) || ( role != Qt::DisplayRole ) )
00046 {
00047 return QVariant();
00048 }
00049
00050 return _document->at( index.row() )->property( "name" );
00051 }
00052
00053 QVariant GraphLayersModel::headerData( int section, Qt::Orientation orientation, int role ) const
00054 {
00055 if( _document == 0 )
00056 {
00057 return QVariant();
00058 }
00059 if( role != Qt::DisplayRole )
00060 {
00061 return QVariant();
00062 }
00063 if( orientation == Qt::Horizontal )
00064 {
00065 return QString( "Column %1" ).arg( section );
00066 }
00067
00068 return QString( "Row %1" ).arg( section );
00069
00070 }
00071
00072 Qt::ItemFlags GraphLayersModel::flags( const QModelIndex& index ) const
00073 {
00074 if( !index.isValid() )
00075 {
00076 return Qt::ItemIsEnabled;
00077 }
00078 return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
00079 }
00080
00081 bool GraphLayersModel::setData( const QModelIndex& index, const QVariant& value, int role )
00082 {
00083 if( index.isValid() && ( role == Qt::ItemIsEditable ) )
00084 {
00085 Graph* g = _document->at( index.row() );
00086 g-> setProperty( "name", value.toString() );
00087 return true;
00088 }
00089 return false;
00090 }
00091
00092 bool GraphLayersModel::insertRows( int position, int , const QModelIndex& )
00093 {
00094 if( _document == 0 ) return false;
00095
00096 beginInsertRows( QModelIndex(), position, position );
00097 _document->addGraph( i18n( "Untitled%1", rowCount() ) );
00098 kDebug() << "Called!";
00099 endInsertRows();
00100 return true;
00101 }
00102
00103 bool GraphLayersModel::removeRows( int position, int rows, const QModelIndex& )
00104 {
00105 if( _document == 0 ) return false;
00106 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
00107 _document->removeAt( position );
00108 endRemoveRows();
00109 return true;
00110 }
00111
00112 Graph* GraphLayersModel::at( const QModelIndex& index )
00113 {
00114 if( _document->size() == 0 ) return 0;
00115 return _document->at( index.row() );
00116 }