00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "imdelegate.h"
00023
00024 #include "immodel.h"
00025 #include "improtocols.h"
00026
00027 #include <kcombobox.h>
00028 #include <kicon.h>
00029 #include <klocale.h>
00030
00031 IMDelegate::IMDelegate( QObject *parent )
00032 : QStyledItemDelegate( parent )
00033 {
00034 }
00035
00036 IMDelegate::~IMDelegate()
00037 {
00038 }
00039
00040 QWidget* IMDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &item, const QModelIndex &index ) const
00041 {
00042 if ( index.column() == 0 ) {
00043 KComboBox *comboBox = new KComboBox( parent );
00044 comboBox->setFrame( false );
00045 comboBox->setAutoFillBackground( true );
00046
00047 const QStringList protocols = IMProtocols::self()->protocols();
00048 foreach ( const QString &protocol, protocols ) {
00049 comboBox->addItem( KIcon( IMProtocols::self()->icon( protocol ) ),
00050 IMProtocols::self()->name( protocol ),
00051 protocol );
00052 }
00053
00054 return comboBox;
00055 } else {
00056 return QStyledItemDelegate::createEditor( parent, item, index );
00057 }
00058 }
00059
00060 void IMDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
00061 {
00062 if ( index.column() == 0 ) {
00063 KComboBox *comboBox = qobject_cast<KComboBox*>( editor );
00064 if ( !comboBox )
00065 return;
00066
00067 const QString protocol = index.data( IMModel::ProtocolRole ).toString();
00068 comboBox->setCurrentIndex( comboBox->findData( protocol ) );
00069 } else {
00070 QStyledItemDelegate::setEditorData( editor, index );
00071 }
00072 }
00073
00074 void IMDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
00075 {
00076 if ( index.column() == 0 ) {
00077 KComboBox *comboBox = qobject_cast<KComboBox*>( editor );
00078 if ( !comboBox )
00079 return;
00080
00081 model->setData( index, comboBox->itemData( comboBox->currentIndex() ), IMModel::ProtocolRole );
00082 } else {
00083 QStyledItemDelegate::setModelData( editor, model, index );
00084 }
00085 }
00086
00087 void IMDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00088 {
00089 if ( index.data( IMModel::IsPreferredRole ).toBool() ) {
00090 QStyleOptionViewItem newOption( option );
00091 newOption.font.setBold( true );
00092
00093 QStyledItemDelegate::paint( painter, newOption, index );
00094 } else
00095 QStyledItemDelegate::paint( painter, option, index );
00096 }
00097