summaryrefslogtreecommitdiff
path: root/src/main-view/DictTableView.cpp
blob: 9d61fade1089e9578b454e85084bd598a55927fd (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
#include "DictTableView.h"
#include "DictTableDelegate.h"

#include <QtAlgorithms>
#include <QHeaderView>
#include <QAbstractProxyModel>

#include "DictTableModel.h"

DictTableView::DictTableView( QAbstractItemModel* aModel, QWidget* aParent ):
    QTableView( aParent )
    {
    setModel( aModel );
    connect( aModel, SIGNAL(rowsAboutToBeInserted(const QModelIndex&, int, int)),
        SLOT(disableUpdates()) );
    connect( aModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
        SLOT(enableUpdates()) );
    setItemDelegate( new DictTableDelegate(this) );
    setAlternatingRowColors(true);
    setShowGrid(true);
    verticalHeader()->setDefaultSectionSize( RowHeight );
    setSelectionBehavior( QAbstractItemView::SelectRows );
    resizeColumnsToContents();
    horizontalHeader()->stretchLastSection();
    }

DictTableView::~DictTableView()
    {
    emit destroyed( model() );
    }

DictTableModel* DictTableView::dicTableModel() const
    {
    DictTableModel* tableModel = qobject_cast<DictTableModel*>( model() );
    if( tableModel )
        return tableModel;
    QAbstractProxyModel* proxyModel = qobject_cast<QAbstractProxyModel*>( model() );
    if( proxyModel )
        {
        tableModel = qobject_cast<DictTableModel*>( proxyModel->sourceModel() );
        if( tableModel )
            return tableModel;
        }
    return NULL;
    }

void DictTableView::resizeColumnsToContents()
    {
    QTableView::resizeColumnsToContents();
    for( int i=0; i<model()->columnCount(); i++ )
        if( columnWidth( i ) < KMinColWidth )
            setColumnWidth( i, KMinColWidth );
        else if ( columnWidth( i ) > KMaxColWidth )
            setColumnWidth( i, KMaxColWidth );
}

void DictTableView::startEditing(int row, int col)
{
    edit(model()->index(row, col));
}

void DictTableView::commitEditing()
{
    DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
    if(!delegate)
        return;
    commitData(delegate->getEditor());
}

int DictTableView::getEditorCursorPos() const
{
    DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
    if(!delegate)
        return -1;
    return delegate->getCursorPos();
}

void DictTableView::insertImageIntoEditor(int cursorPos, const QString& filePath) const
{
    DictTableDelegate* delegate = qobject_cast<DictTableDelegate*>(itemDelegate());
    if(!delegate)
        return;
    delegate->insertImageIntoEditor(cursorPos, filePath);
}