-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtableview.cpp
More file actions
101 lines (82 loc) · 3.22 KB
/
gtableview.cpp
File metadata and controls
101 lines (82 loc) · 3.22 KB
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
#include "gtableview.h"
#include "gabstracttablemodel.h"
#include "gfilterproxy.h"
#include <QScrollBar>
#include <QApplication>
#include <QStyledItemDelegate>
#include <QDebug>
class _LineNumberDelegate : public QStyledItemDelegate
{
public:
explicit _LineNumberDelegate(QObject * parent = 0);
// QAbstractItemDelegate interface
protected:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
_LineNumberDelegate::_LineNumberDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void _LineNumberDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItemV4 optionV4 = option;
initStyleOption(&optionV4, index);
if (!(optionV4.state & QStyle::State_HasFocus)) {
optionV4.state &= ~(QStyle::State_Active | QStyle::State_Selected);
}
QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);
}
GTableView::GTableView(QWidget *parent)
: QTableView(parent),
mGModel(NULL),
mGProxy(NULL)
{
setItemDelegateForColumn(0, new _LineNumberDelegate(this));
setSelectionMode(QAbstractItemView::SingleSelection);
}
void GTableView::setModel(QAbstractItemModel *model)
{
qDebug() << __PRETTY_FUNCTION__ << model->inherits("GAbstractTableModel");
Q_ASSERT(model->inherits("GAbstractTableModel") || model->inherits("GFilterProxy"));
QTableView::setModel(model);
if (model->inherits("GAbstractTableModel")) {
mGModel = static_cast<GAbstractTableModel*>(model);
connect(this, SIGNAL(clicked(QModelIndex)), mGModel, SLOT(clicked(QModelIndex)));
} else {
mGProxy= static_cast<GFilterProxy*>(model);
mGModel = static_cast<GAbstractTableModel*>(mGProxy->sourceModel());
connect(mGProxy, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(scrollToCurrent()));
connect(mGProxy, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(scrollToCurrent()));
connect(this, SIGNAL(clicked(QModelIndex)), mGProxy, SLOT(clicked(QModelIndex)));
}
viewport()->installEventFilter(mGModel);
installEventFilter(mGModel);
}
void GTableView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
// qDebug() << __PRETTY_FUNCTION__ << (mGProxy && current.isValid()) << mGModel;
QTableView::currentChanged(current, previous);
if (mGModel) {
mGModel->currentChanged(mGProxy ? mGProxy->mapToSource(current) : current);
}
}
void GTableView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
{
QTableView::selectionChanged(selected, deselected);
scrollToCurrent();
}
void GTableView::scrollToCurrent()
{
int row = currentIndex().row();
if (mGProxy) {
if (mGProxy->hasIndex(row, 0)) {
scrollTo(mGProxy->index(row, 0), QAbstractItemView::EnsureVisible);
scrollTo(mGProxy->index(row, 0), QAbstractItemView::EnsureVisible); // Do not delete
}
} else {
if (mGModel->hasIndex(row, 0)) {
scrollTo(mGModel->index(row, 0), QAbstractItemView::EnsureVisible);
}
}
}