00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "customfieldsdelegate.h"
00023
00024 #include "customfieldsmodel.h"
00025
00026 #include <kicon.h>
00027 #include <klocale.h>
00028
00029 #include <QtGui/QDateEdit>
00030 #include <QtGui/QDateTimeEdit>
00031 #include <QtGui/QCheckBox>
00032 #include <QtGui/QSpinBox>
00033 #include <QtGui/QTimeEdit>
00034
00035 CustomFieldsDelegate::CustomFieldsDelegate( QObject *parent )
00036 : QStyledItemDelegate( parent )
00037 {
00038 }
00039
00040 CustomFieldsDelegate::~CustomFieldsDelegate()
00041 {
00042 }
00043
00044 QWidget* CustomFieldsDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &item, const QModelIndex &index ) const
00045 {
00046 if ( index.column() == 1 ) {
00047 const CustomField::Type type = static_cast<CustomField::Type>( index.data( CustomFieldsModel::TypeRole ).toInt() );
00048
00049 switch ( type ) {
00050 case CustomField::TextType:
00051 default:
00052 return QStyledItemDelegate::createEditor( parent, item, index );
00053 break;
00054 case CustomField::NumericType:
00055 {
00056 QSpinBox *editor = new QSpinBox( parent );
00057 editor->setFrame( false );
00058 editor->setAutoFillBackground( true );
00059 return editor;
00060 }
00061 break;
00062 case CustomField::BooleanType:
00063 {
00064 QCheckBox *editor = new QCheckBox( parent );
00065 return editor;
00066 }
00067 break;
00068 case CustomField::DateType:
00069 {
00070 QDateEdit *editor = new QDateEdit( parent );
00071 editor->setFrame( false );
00072 editor->setAutoFillBackground( true );
00073 return editor;
00074 }
00075 break;
00076 case CustomField::TimeType:
00077 {
00078 QTimeEdit *editor = new QTimeEdit( parent );
00079 editor->setFrame( false );
00080 editor->setAutoFillBackground( true );
00081 return editor;
00082 }
00083 break;
00084 case CustomField::DateTimeType:
00085 {
00086 QDateTimeEdit *editor = new QDateTimeEdit( parent );
00087 editor->setFrame( false );
00088 editor->setAutoFillBackground( true );
00089 return editor;
00090 }
00091 break;
00092 }
00093 } else {
00094 return QStyledItemDelegate::createEditor( parent, item, index );
00095 }
00096 }
00097
00098 void CustomFieldsDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
00099 {
00100 if ( index.column() == 1 ) {
00101 const CustomField::Type type = static_cast<CustomField::Type>( index.data( CustomFieldsModel::TypeRole ).toInt() );
00102
00103 switch ( type ) {
00104 case CustomField::TextType:
00105 QStyledItemDelegate::setEditorData( editor, index );
00106 break;
00107 case CustomField::NumericType:
00108 {
00109 QSpinBox *widget = qobject_cast<QSpinBox*>( editor );
00110 widget->setValue( index.data( Qt::EditRole ).toInt() );
00111 }
00112 break;
00113 case CustomField::BooleanType:
00114 {
00115 QCheckBox *widget = qobject_cast<QCheckBox*>( editor );
00116 widget->setChecked( index.data( Qt::EditRole ).toString() == QLatin1String( "true" ) );
00117 }
00118 break;
00119 case CustomField::DateType:
00120 {
00121 QDateEdit *widget = qobject_cast<QDateEdit*>( editor );
00122 widget->setDisplayFormat( QLatin1String( "dd.MM.yyyy" ) );
00123 widget->setDate( QDate::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
00124 }
00125 break;
00126 case CustomField::TimeType:
00127 {
00128 QTimeEdit *widget = qobject_cast<QTimeEdit*>( editor );
00129 widget->setDisplayFormat( QLatin1String( "hh:mm" ) );
00130 widget->setTime( QTime::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
00131 }
00132 break;
00133 case CustomField::DateTimeType:
00134 {
00135 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit*>( editor );
00136 widget->setDisplayFormat( QLatin1String( "dd.MM.yyyy hh:mm" ) );
00137 widget->setDateTime( QDateTime::fromString( index.data( Qt::EditRole ).toString(), Qt::ISODate ) );
00138 }
00139 break;
00140 }
00141 } else {
00142 QStyledItemDelegate::setEditorData( editor, index );
00143 }
00144 }
00145
00146 void CustomFieldsDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
00147 {
00148 if ( index.column() == 1 ) {
00149 const CustomField::Type type = static_cast<CustomField::Type>( index.data( CustomFieldsModel::TypeRole ).toInt() );
00150
00151 switch ( type ) {
00152 case CustomField::TextType:
00153 QStyledItemDelegate::setModelData( editor, model, index );
00154 break;
00155 case CustomField::NumericType:
00156 {
00157 QSpinBox *widget = qobject_cast<QSpinBox*>( editor );
00158 model->setData( index, QString::number( widget->value() ) );
00159 }
00160 break;
00161 case CustomField::BooleanType:
00162 {
00163 QCheckBox *widget = qobject_cast<QCheckBox*>( editor );
00164 model->setData( index, widget->isChecked() ? QLatin1String( "true" ) : QLatin1String( "false" ) );
00165 }
00166 break;
00167 case CustomField::DateType:
00168 {
00169 QDateEdit *widget = qobject_cast<QDateEdit*>( editor );
00170 model->setData( index, widget->date().toString( Qt::ISODate ) );
00171 }
00172 break;
00173 case CustomField::TimeType:
00174 {
00175 QTimeEdit *widget = qobject_cast<QTimeEdit*>( editor );
00176 model->setData( index, widget->time().toString( Qt::ISODate ) );
00177 }
00178 break;
00179 case CustomField::DateTimeType:
00180 {
00181 QDateTimeEdit *widget = qobject_cast<QDateTimeEdit*>( editor );
00182 model->setData( index, widget->dateTime().toString( Qt::ISODate ) );
00183 }
00184 break;
00185 }
00186 } else {
00187 QStyledItemDelegate::setModelData( editor, model, index );
00188 }
00189 }
00190
00191 void CustomFieldsDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
00192 {
00193
00194 QStyledItemDelegate::paint( painter, option, index );
00195 }