akonadi/clients
collectionattributespage.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectionattributespage.h"
00021
00022 #include <akonadi/attributefactory.h>
00023 #include <akonadi/collection.h>
00024
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027
00028 #include <QStandardItemModel>
00029
00030 using namespace Akonadi;
00031
00032 CollectionAttributePage::CollectionAttributePage(QWidget * parent) :
00033 CollectionPropertiesPage( parent ),
00034 mModel( 0 )
00035 {
00036 setPageTitle( i18n( "Attributes" ) );
00037 ui.setupUi( this );
00038
00039 connect( ui.addButton, SIGNAL(clicked()), SLOT(addAttribute()) );
00040 connect( ui.deleteButton, SIGNAL(clicked()), SLOT(delAttribute()) );
00041 }
00042
00043 void CollectionAttributePage::load(const Collection & col)
00044 {
00045 Attribute::List list = col.attributes();
00046 mModel = new QStandardItemModel( list.count(), 2 );
00047 QStringList labels;
00048 labels << i18n( "Attribute" ) << i18n( "Value" );
00049 mModel->setHorizontalHeaderLabels( labels );
00050
00051 for ( int i = 0; i < list.count(); ++i ) {
00052 QModelIndex index = mModel->index( i, 0 );
00053 Q_ASSERT( index.isValid() );
00054 mModel->setData( index, QString::fromLatin1( list[i]->type() ) );
00055 mModel->item( i, 0 )->setEditable( false );
00056 index = mModel->index( i, 1 );
00057 Q_ASSERT( index.isValid() );
00058 mModel->setData( index, QString::fromLatin1( list[i]->serialized() ) );
00059 mModel->itemFromIndex( index )->setFlags( Qt::ItemIsEditable | mModel->flags( index ) );
00060 }
00061 ui.attrView->setModel( mModel );
00062 connect( mModel, SIGNAL(itemChanged(QStandardItem*)), SLOT(attributeChanged(QStandardItem*)) );
00063 }
00064
00065 void CollectionAttributePage::save(Collection & col)
00066 {
00067 foreach ( const QString &del, mDeleted )
00068 col.removeAttribute( del.toLatin1() );
00069 for ( int i = 0; i < mModel->rowCount(); ++i ) {
00070 const QModelIndex typeIndex = mModel->index( i, 0 );
00071 Q_ASSERT( typeIndex.isValid() );
00072 if ( !mChanged.contains( typeIndex.data().toString() ) )
00073 continue;
00074 const QModelIndex valueIndex = mModel->index( i, 1 );
00075 Q_ASSERT( valueIndex.isValid() );
00076 Attribute* attr = AttributeFactory::createAttribute( mModel->data( typeIndex ).toString().toLatin1() );
00077 Q_ASSERT( attr );
00078 attr->deserialize( mModel->data( valueIndex ).toString().toLatin1() );
00079 col.addAttribute( attr );
00080 }
00081 }
00082
00083 void CollectionAttributePage::addAttribute()
00084 {
00085 if ( ui.attrName->text().isEmpty() )
00086 return;
00087 const QString attr = ui.attrName->text();
00088 mChanged.insert( attr );
00089 mDeleted.remove( attr );
00090 const int row = mModel->rowCount();
00091 mModel->insertRow( row );
00092 QModelIndex index = mModel->index( row, 0 );
00093 Q_ASSERT( index.isValid() );
00094 mModel->setData( index, attr );
00095 ui.attrName->clear();
00096 }
00097
00098 void CollectionAttributePage::delAttribute()
00099 {
00100 QModelIndexList selection = ui.attrView->selectionModel()->selectedRows();
00101 if ( selection.count() != 1 )
00102 return;
00103 const QString attr = selection.first().data().toString();
00104 mChanged.remove( attr );
00105 mDeleted.insert( attr );
00106 mModel->removeRow( selection.first().row() );
00107 }
00108
00109 void CollectionAttributePage::attributeChanged( QStandardItem *item )
00110 {
00111 const QString attr = mModel->data( mModel->index( item->row(), 0 ) ).toString();
00112 mDeleted.remove( attr );
00113 mChanged.insert( attr );
00114 }
00115
00116 #include "collectionattributespage.moc"