-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ3TTableWidget.cpp
132 lines (108 loc) · 4.52 KB
/
Q3TTableWidget.cpp
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
129
130
131
132
#include "Q3TTableWidget.h"
#include "Q3TTableModel.h"
#include <QScrollBar>
#include <QHeaderView>
#include <QWheelEvent>
static const int s_defaultInitActualRowCount = 2000;
static const int s_defaultInitActualColumnCount = 2000;
static const int s_defaultInitRowCount = 2000;
static const int s_defaultInitColumnCount = 2000;
static const int s_incRowCountOneTime = 6;
static const int s_incColumnCountOneTime = 4;
Q3TTableWidget::Q3TTableWidget(AMDATATYPE dataType)
{
m_pModel = new Q3TTableModel(dataType, this);
m_pModel->setActualSize(s_defaultInitActualRowCount, s_defaultInitActualColumnCount);
m_pModel->setSize(s_defaultInitRowCount, s_defaultInitColumnCount);
setModel(m_pModel);
init();
//connect(m_pModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&, const QVector<int> &)), this, SLOT(tableDataChanged(const QModelIndex&, const QModelIndex&, const QVector<int> &)));
connect(m_pModel, SIGNAL(actualRowColumnCountChange(int, int)), this, SLOT(tableActualRowColumnCountChange(int, int)));
}
Q3TTableWidget::~Q3TTableWidget()
{
}
void Q3TTableWidget::init()
{
setHorizontalScrollMode(ScrollPerPixel);
setVerticalScrollMode(ScrollPerPixel);
}
void Q3TTableWidget::actualRowColumnCount(int& rowCount, int& columnCount)
{
if(m_pModel)
{
rowCount = m_pModel->actualRowCount();
columnCount = m_pModel->actualColumnCount();
}
else
Q_ASSERT(false);
}
void Q3TTableWidget::resetRowColumnCountByModelIndex(const QModelIndex &index)
{
int oldRowCount = m_pModel->rowCount();
int oldColumnConut = m_pModel->columnCount();
if(index.row() > oldRowCount - s_incRowCountOneTime)
{
m_pModel->insertRows(oldRowCount, index.row() + s_incRowCountOneTime - oldRowCount);
}
if(index.column() > oldColumnConut - s_incColumnCountOneTime)
{
m_pModel->insertColumns(oldColumnConut, index.column() + s_incColumnCountOneTime - oldColumnConut);
}
}
void Q3TTableWidget::resetRowColumnCountByGeometry(bool isDecrease)
{
int oldRowConut = m_pModel->rowCount();
int oldColumnConut = m_pModel->columnCount();
QModelIndex index = m_pModel->index(oldRowConut - 1, oldColumnConut - 1);
QRect rect = visualRect(index);
QRect clientRect = geometry();
QHeaderView* horiHV = horizontalHeader();
QHeaderView* vertHV = verticalHeader();
// make clientRect base (0, 0)
int incWidth = clientRect.right() - clientRect.left() - (rect.right() + horiHV->height());
int incHeight = clientRect.bottom() - clientRect.top() - (rect.bottom() + vertHV->width());
int oneCellWidth = horiHV->defaultSectionSize();
int oneCellHeight = vertHV->defaultSectionSize();
int needColumnCount = static_cast<int>(ceil(static_cast<double>(incWidth)/static_cast<double>(oneCellWidth)));
int needRowCount = static_cast<int>(ceil(static_cast<double>(incHeight)/static_cast<double>(oneCellHeight)));
int newRowCount = oldRowConut;
if((isDecrease || needRowCount + s_incRowCountOneTime > 0) && oldRowConut + needRowCount > m_pModel->actualRowCount())
{
newRowCount = oldRowConut + needRowCount + s_incRowCountOneTime;
}
int newColumnCount = oldColumnConut;
if((isDecrease || needColumnCount + s_incColumnCountOneTime > 0) && oldColumnConut + needColumnCount > m_pModel->actualColumnCount())
{
newColumnCount = oldColumnConut + needColumnCount + s_incColumnCountOneTime;
}
m_pModel->resize(newRowCount, newColumnCount);
}
void Q3TTableWidget::resizeEvent(QResizeEvent * event)
{
QTableView::resizeEvent(event);
resetRowColumnCountByGeometry();
}
void Q3TTableWidget::scrollTo(const QModelIndex & index, ScrollHint hint)
{
QTableView::scrollTo(index, hint);
resetRowColumnCountByModelIndex(index);
}
void Q3TTableWidget::wheelEvent(QWheelEvent *event)
{
QTableView::wheelEvent(event);
if(event->delta() < 0)
resetRowColumnCountByGeometry(false);
}
void Q3TTableWidget::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
QTableView::currentChanged(current, previous);
resetRowColumnCountByModelIndex(current);
}
//void Q3TTableWidget::tableDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
//{
//}
void Q3TTableWidget::tableActualRowColumnCountChange(int actualRowCount, int actualColumnCount)
{
emit actualRowColumnCountChange(actualRowCount, actualColumnCount);
}