00001 #include <QtCore/QtDebug>
00002 #include <QtGui/QLineEdit>
00003 #include <QtGui/QMessageBox>
00004
00005 #include "ACE/foldertabledelegate.h"
00006 #include "ACE/foldertablemodel.h"
00007 #include "ACE/ContentEditor.h"
00008
00009
00010 FolderTableDelegate::FolderTableDelegate( QObject* parent) :
00011 QItemDelegate( parent )
00012 {}
00013
00014 FolderTableDelegate::~FolderTableDelegate()
00015 {
00016 commitAndCloseEditor();
00017 }
00018
00019 QWidget* FolderTableDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& , const QModelIndex& index ) const
00020 {
00021 const FolderTableModel* folderTableModel = qobject_cast< const FolderTableModel* >( index.model() );
00022 bool newRow( folderTableModel->isNewRow( index.row() ) );
00023 if ( newRow && folderTableModel->getColumnResponse( index.column() ) == confirmResponse )
00024 {
00025 int ret = QMessageBox::question(
00026 parent,
00027 trUtf8( "Confirmation request" ),
00028 trUtf8( "Are you sure you wish to edit the highlighted field?" ),
00029 QMessageBox::Cancel | QMessageBox::Ok );
00030 if ( ret == QMessageBox::Cancel )
00031 return 0;
00032 }
00033 QString coolType = folderTableModel->getCoolTypeString( index );
00034 if ( ( coolType == "Blob64k" ) || ( coolType == "Blob16M" ) )
00035 {
00036 ContentEditor* editor = new ContentEditor( parent, true, !newRow );
00037 return editor;
00038 }
00039 else if ( ( coolType == "String4k" ) || ( coolType == "String64k" ) || ( coolType == "String16M") )
00040 {
00041 ContentEditor* editor = new ContentEditor( parent, false, !newRow );
00042 return editor;
00043 }
00044 else if ( newRow )
00045 {
00046 QLineEdit* editor = new QLineEdit( parent );
00047 editor->setInputMask( folderTableModel->getInputMask( index ) );
00048 editor->setValidator( folderTableModel->getValidator( index ) );
00049 return editor;
00050 }
00051 return 0;
00052 }
00053
00054 void FolderTableDelegate::setEditorData( QWidget* editor, const QModelIndex& index ) const
00055 {
00056 const FolderTableModel* folderTableModel = qobject_cast< const FolderTableModel* >( index.model() );
00057 QString coolType = folderTableModel->getCoolTypeString( index );
00058 if ( ( coolType == "Blob64k" ) || ( coolType == "Blob16M" ) )
00059 {
00060 ContentEditor* contentEditor = qobject_cast< ContentEditor* >( editor );
00061 QObject::connect( contentEditor, SIGNAL( accepted() ), this, SLOT( commitAndCloseEditor() ) );
00062 contentEditor->setContent( folderTableModel->rawData( index ), HexCT, true );
00063 }
00064 else if ( ( coolType == "String4k" ) || ( coolType == "String64k" ) || ( coolType == "String16M") )
00065 {
00066 ContentEditor* contentEditor = qobject_cast< ContentEditor* >( editor );
00067 QObject::connect( contentEditor, SIGNAL( accepted() ), this, SLOT( commitAndCloseEditor() ) );
00068 contentEditor->setContent( folderTableModel->rawData( index ), StringCT, true );
00069 }
00070 else
00071 {
00072 QLineEdit* lineEdit = qobject_cast< QLineEdit* >( editor );
00073 lineEdit->setText( folderTableModel->data( index, Qt::DisplayRole ).toString() );
00074 lineEdit->selectAll();
00075 }
00076 }
00077
00078 void FolderTableDelegate::setModelData( QWidget* editor, QAbstractItemModel* model, const QModelIndex& index ) const
00079 {
00080 FolderTableModel* folderTableModel = qobject_cast< FolderTableModel* >( model );
00081 QString coolType = folderTableModel->getCoolTypeString( index );
00082 if ( ( coolType == "Blob64k" ) || ( coolType == "Blob16M" ) || ( coolType == "String4k" ) || ( coolType == "String64k" ) || ( coolType == "String16M" ) )
00083 {
00084 ContentEditor* contentEditor = qobject_cast< ContentEditor* >( editor );
00085 if ( contentEditor->isModified() )
00086 {
00087 folderTableModel->setData( index, contentEditor->contents(), Qt::EditRole );
00088 }
00089 }
00090 else
00091 {
00092 QLineEdit* lineEdit = qobject_cast< QLineEdit* >( editor );
00093 folderTableModel->setData( index, lineEdit->text(), Qt::EditRole );
00094 }
00095 }
00096
00097 void FolderTableDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& ) const
00098 {
00099 editor->setGeometry( option.rect );
00100 }
00101
00102 void FolderTableDelegate::commitAndCloseEditor()
00103 {
00104 ContentEditor *editor = qobject_cast<ContentEditor *>( sender() );
00105 emit commitData( editor );
00106 emit closeEditor( editor );
00107 }
00108