#include "DictTableDelegate.h" #include #include "DictTableModel.h" #include "UndoCommands.h" #include "DictTableDelegatePainter.h" #include "FieldContentCodec.h" #include "../dictionary/Dictionary.h" QWidget* DictTableDelegate::createEditor( QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& /*index*/ ) const { editor = new RecordEditor(parent, option.rect); connect( editor, SIGNAL(destroyed()), SIGNAL(editorDestroyed()) ); emit editorCreated(); return editor; } void DictTableDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const { setEditorData(editor, index); RecordEditor* recordEditor = qobject_cast(editor); recordEditor->updateEditor(); } bool DictTableDelegate::eventFilter(QObject *object, QEvent *event) { QWidget* editor = qobject_cast(object); if (!editor) return false; if(event->type() == QEvent::KeyPress) switch( static_cast(event)->key() ) { case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Tab: emit commitData(editor); emit closeEditor(editor, QAbstractItemDelegate::EditNextItem); return true; default: break; } return QStyledItemDelegate::eventFilter(object, event); } void DictTableDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { RecordEditor* recordEditor = qobject_cast(editor); FieldContentCodec codec(recordEditor); codec.parse(getDisplayText(index)); } QString DictTableDelegate::getDisplayText(const QModelIndex& index) const { return index.data(Qt::EditRole).toString(); } void DictTableDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { RecordEditor* recordEditor = qobject_cast(editor); DictTableModel* tableModel = qobject_cast( model ); QString editorText = recordEditor->getText(); QModelIndex origIndex = index; if( !tableModel ) { QAbstractProxyModel* proxyModel = qobject_cast( model ); if( !proxyModel ) return; tableModel = qobject_cast( proxyModel->sourceModel() ); if( !tableModel ) return; origIndex = proxyModel->mapToSource( index ); } if(editorText == getDisplayText(index) && !indexIsLastCell(origIndex, model)) return; QString newText = tableModel->dictionary()->shortenImagePaths(editorText); QUndoCommand* command = new EditRecordCmd( tableModel, origIndex, newText ); tableModel->undoStack()->push( command ); } bool DictTableDelegate::indexIsLastCell(const QModelIndex& index, QAbstractItemModel* model) const { return index.row() == model->rowCount() - 1 && index.column() == model->columnCount() - 1; } void DictTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyledItemDelegate::paint(painter, option, index); DictTableDelegatePainter dPainter(painter, getMarginRect(option), option.fontMetrics); FieldContentCodec codec(&dPainter); codec.parse(getDisplayText(index)); } QRect DictTableDelegate::getMarginRect(const QStyleOptionViewItem& option) const { const int margin = 2; return option.rect.adjusted(margin, 0, -margin, 0); } int DictTableDelegate::getCursorPos() const { if(editor) return editor->textCursor().position(); else return -1; } void DictTableDelegate::setCursorPos(int pos) { if(!editor) return; QTextCursor cursor = editor->textCursor(); cursor.setPosition(pos); editor->setTextCursor(cursor); } void DictTableDelegate::insertImageIntoEditor(int cursorPos, const QString& filePath) { if(!editor) return; editor->insertImage(cursorPos, filePath); }