summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableDelegate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main-view/DictTableDelegate.cpp')
-rw-r--r--src/main-view/DictTableDelegate.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/src/main-view/DictTableDelegate.cpp b/src/main-view/DictTableDelegate.cpp
new file mode 100644
index 0000000..1e1d609
--- /dev/null
+++ b/src/main-view/DictTableDelegate.cpp
@@ -0,0 +1,128 @@
+#include "DictTableDelegate.h"
+#include <QtDebug>
+
+#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<RecordEditor*>(editor);
+ recordEditor->updateEditor();
+}
+
+bool DictTableDelegate::eventFilter(QObject *object, QEvent *event)
+{
+ QWidget* editor = qobject_cast<QWidget*>(object);
+ if (!editor)
+ return false;
+ if(event->type() == QEvent::KeyPress)
+ switch( static_cast<QKeyEvent*>(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<RecordEditor*>(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<RecordEditor*>(editor);
+ DictTableModel* tableModel = qobject_cast<DictTableModel*>( model );
+ QString editorText = recordEditor->getText();
+ QModelIndex origIndex = index;
+ if( !tableModel )
+ {
+ QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>( model );
+ if( !proxyModel )
+ return;
+ tableModel = qobject_cast<DictTableModel*>( 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);
+}