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