summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableDelegate.cpp
blob: 1e1d609bce1e3961c4a3ab4a138c46e34926a898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);
}