00001 #include "model_GraphProperties.h"
00002 #include <KLocale>
00003 #include <KDebug>
00004 #include <DynamicPropertiesList.h>
00005
00006 GraphPropertiesModel::GraphPropertiesModel( QObject* parent ) : QAbstractTableModel( parent )
00007 {
00008
00009 _dataSource = 0;
00010 _metaObject = 0;
00011 }
00012
00013 int GraphPropertiesModel::rowCount( const QModelIndex& parent ) const
00014 {
00015 Q_UNUSED( parent );
00016
00017
00018 if( _dataSource == 0 )
00019 {
00020 return 0;
00021 }
00022 kDebug() << _dataSource->dynamicPropertyNames().size();
00023 return _dataSource->dynamicPropertyNames().size();
00024 }
00025
00026 int GraphPropertiesModel::columnCount( const QModelIndex& parent ) const
00027 {
00028
00029 Q_UNUSED( parent );
00030 return 3;
00031 }
00032
00033 QVariant GraphPropertiesModel::data( const QModelIndex& index, int role ) const
00034 {
00035
00036 if( !index.isValid() )
00037 {
00038 return QVariant();
00039 }
00040 if( index.row() >= _dataSource->dynamicPropertyNames().size() )
00041 {
00042 return QVariant();
00043 }
00044 if( role != Qt::DisplayRole )
00045 {
00046 return QVariant();
00047 }
00048
00049
00050
00051
00052 if( index.column() == 0 )
00053 {
00054 return _dataSource->dynamicPropertyNames()[index.row()];
00055 }
00056 else if( index.column() == 1 )
00057 {
00058 return _dataSource->property( _dataSource->dynamicPropertyNames()[index.row()] );
00059 }
00060 else if( index.column() == 2 )
00061 {
00062 return DynamicPropertiesList::New()->typeInText( _dataSource,
00063 _dataSource->dynamicPropertyNames()[index.row()] );
00064 }
00065
00066
00067 return QVariant();
00068 }
00069
00070 QVariant GraphPropertiesModel::headerData( int section, Qt::Orientation orientation, int role ) const
00071 {
00072
00073 if( role != Qt::DisplayRole )
00074 {
00075 return QVariant();
00076 }
00077
00078 if( orientation == Qt::Horizontal )
00079 {
00080 switch( section )
00081 {
00082 case 0:
00083 return i18n( "Property" );
00084 case 1:
00085 return i18n( "Value" );
00086 case 2:
00087 return i18n( "Type" );
00088 }
00089 }
00090 return QVariant();
00091 }
00092
00093 void GraphPropertiesModel::setDataSource( QObject* dataSource )
00094 {
00095
00096 if( dataSource == 0 )
00097 {
00098 int count = rowCount();
00099 if( count == 0 ) return;
00100 beginRemoveRows( QModelIndex(), 0, count - 1 );
00101 endRemoveRows();
00102 return;
00103 }
00104
00105
00106 if( _dataSource )
00107 {
00108 beginRemoveRows( QModelIndex(), 0, _dataSource->dynamicPropertyNames().size() - 1 );
00109 endRemoveRows();
00110 }
00111
00112
00113 _dataSource = dataSource;
00114 _metaObject = _dataSource -> metaObject();
00115
00116
00117
00118 beginInsertRows( QModelIndex(), 0, dataSource->dynamicPropertyNames().size() - 1 );
00119 endInsertRows();
00120
00121 }
00122
00123 Qt::ItemFlags GraphPropertiesModel::flags( const QModelIndex& index ) const
00124 {
00125 if( index.isValid() )
00126 {
00127 if( index.column() != 2 )
00128 {
00129 return QAbstractItemModel::flags( index ) | Qt::ItemIsEditable;
00130 }
00131 else
00132 {
00133 return QAbstractItemModel::flags( index );
00134 }
00135 }
00136 return Qt::ItemIsEnabled;
00137 }
00138
00139 bool GraphPropertiesModel::setData( const QModelIndex& index, const QVariant& value, int role )
00140 {
00141 if( index.isValid() && role == Qt::EditRole )
00142 {
00143 switch( index.column() )
00144 {
00145
00146 case 0:
00147 DynamicPropertiesList::New()->changePropertyName( QString( _dataSource->dynamicPropertyNames()[index.row()] ), value.toString(), _dataSource );
00148 break;
00149 case 1:
00150 _dataSource->setProperty( _dataSource->dynamicPropertyNames()[index.row()], value );
00151 break;
00152 default:
00153 kDebug() << "shoudn't enter here ¬¬";
00154 return false;
00155 }
00156
00157 emit dataChanged( index, index );
00158 return true;
00159
00160 }
00161 return false;
00162
00163 }
00164
00165 void GraphPropertiesModel::addDynamicProperty( QString name, QVariant value, QObject* obj, bool isGlobal )
00166 {
00167
00168 bool insertingRow = false;
00169 if( name.isEmpty() )
00170 {
00171 kWarning() << "Cannot add am empty property";
00172 return;
00173 }
00174
00175 if( ! obj->dynamicPropertyNames().contains( name.toUtf8() ) )
00176 {
00177 beginInsertRows( QModelIndex(), rowCount(), rowCount() );
00178 insertingRow = true;
00179 }
00180
00181 if( isGlobal )
00182 {
00183 if( Edge* edge = qobject_cast<Edge*> ( obj ) )
00184 {
00185 edge->graph()->addEdgesDynamicProperty( name, value );
00186 }
00187 if( Node* node = qobject_cast<Node*> ( obj ) )
00188 {
00189 node->graph()->addNodesDynamicProperty( name, value );
00190 }
00191 }
00192 else
00193 {
00194 if( Edge* edge = qobject_cast<Edge*> ( obj ) )
00195 {
00196 edge->addDynamicProperty( name, value );
00197 }
00198 if( Node* node = qobject_cast<Node*> ( obj ) )
00199 {
00200 node->addDynamicProperty( name, value );
00201 }
00202 if( Graph* graph = qobject_cast<Graph*> ( obj ) )
00203 {
00204 graph->addDynamicProperty( name, value );
00205 }
00206 }
00207
00208 if( insertingRow )
00209 {
00210 endInsertRows();
00211 }
00212 }