-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablecolumn.cpp
214 lines (195 loc) · 7.22 KB
/
tablecolumn.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "tablecolumn.h"
#include <QDebug>
TableColumn::TableColumn( int index, QDateTime start, QDateTime end,
ClassTable *parent, int xPos )
: parentTablePtr( parent ), index( ClassTable::dayOfWeek( index ) ),
width( parentTablePtr->width / parentTablePtr->getColumnNo() ),
height( parentTablePtr->height ), visibleWidth( width * 7 / 8 ),
start( start ), end( start <= end ? end : start ) {
//调用父类构造函数
QGraphicsRectItem( xPos, getYPos(), width, height );
//设置背景矩形
bool includeNow = this->includeNow();
backgroundItemPtr =
new QGraphicsRectItem( xPos, 0, visibleWidth, height, this );
backgroundItemPtr->setPen( QPen( QColor( 0, 0, 0, 0 ) ) );
backgroundItemPtr->setBrush(
QBrush( QColor( 0, 0, 0, includeNow ? 100 : 50 ) ) );
backgroundItemPtr->setZValue( 1 );
//设置时间线(如果有的话)
QDateTime curDT = QDateTime::currentDateTime();
if ( includeNow ) {
timeLineItemPtr =
new QGraphicsLineItem( xPos, itemYPosShouldBe( curDT ),
xPos + visibleWidth - timeLineWidth / 2,
itemYPosShouldBe( curDT ), this );
timeLineItemPtr->setPen(
QPen( QBrush( timeLineColor ), timeLineWidth ) );
} else {
timeLineItemPtr = new QGraphicsLineItem( this );
timeLineItemPtr->setVisible( false );
}
timeLineItemPtr->setZValue( 5 );
// this->hide(); //先不显示
//初始化(清空)列表
childrenItemPtrList.clear();
childQGraphicsItemPtrList.clear();
}
TableColumn::~TableColumn() {
// qDeleteAll( childrenItemPtrList );
}
void TableColumn::paint( QPainter * painter,
const QStyleOptionGraphicsItem *option,
QWidget * widget ) {
// QGraphicsRectItem::paint(painter, option, widget);
backgroundItemPtr->paint( painter, option, widget );
timeLineItemPtr->paint( painter, option, widget );
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->paint( painter, option, widget );
}
}
void TableColumn::show() {
// QGraphicsRectItem::show();
backgroundItemPtr->show();
timeLineItemPtr->show();
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->show();
}
}
void TableColumn::hide() {
// QGraphicsRectItem::hide();
backgroundItemPtr->hide();
timeLineItemPtr->hide();
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->hide();
}
}
void TableColumn::resize( QResizeEvent *tableEvent ) {
//更新自己
QSize newSize = ClassTable::scaleSize( QSize( width, height ), tableEvent );
width = newSize.width();
height = newSize.height();
////width = parentTablePtr->width / parentTablePtr->getColumnNo();
////height = parentTablePtr->height;
visibleWidth = width * 7 / 8;
//因为要先初始化width和height,所以父类构造函数必须放在后面
QGraphicsRectItem( getXPos(), getYPos(), width, height );
//更新背景
QRectF originalRect = backgroundItemPtr->rect();
QRectF newRect = ClassTable::scaleSize( originalRect, tableEvent );
backgroundItemPtr->setRect( newRect );
////backgroundItemPtr->setRect(getXPos(), 0, visibleWidth, height);
//更新时间线
timeLineItemPtr->setLine(
ClassTable::scaleSize( timeLineItemPtr->line(), tableEvent ) );
/**
QDateTime curDT = QDateTime::currentDateTime();
if (includeNow()) {
timeLineItemPtr->setLine(getXPos(), itemYPosShouldBe( curDT ),
getXPos() + visibleWidth - timeLineWidth / 2,
itemYPosShouldBe( curDT )); } else {
}
**/
}
void TableColumn::remove() {
qDebug() << "\tRemoving TableColumn " << index << " {";
QGraphicsScene *scenePtr = parentTablePtr->scenePtr;
scenePtr->removeItem( backgroundItemPtr );
scenePtr->removeItem( timeLineItemPtr );
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
scenePtr->removeItem( itemPtr->textItemPtr );
scenePtr->removeItem( itemPtr->backgroundItemPtr );
scenePtr->removeItem( itemPtr );
qDebug() << "\t\tRemoved QGraphicsTableItem "
<< itemPtr->textItemPtr->toPlainText();
}
qDebug() << "\t} Removed TableColumn " << index;
}
/**
* @brief 设置起始时间
* @warning 前提是数据成员end已经初始化
* @todo
*/
void TableColumn::setStart( QDateTime st ) {
if ( st < end )
start = st;
}
/**
* @brief 设置结束时间
* @warning 前提是数据成员start已经初始化
* @todo
*/
void TableColumn::setEnd( QDateTime ed ) {
if ( ed > start )
end = ed;
}
int TableColumn::getXPos() const {
int xPos = 0;
QList< int > colIndexList = parentTablePtr->columnIndexList;
for ( int listIndex = 0; listIndex < colIndexList.length(); ++listIndex ) {
int colIndex = colIndexList[listIndex];
if ( colIndex == this->index ) {
break;
}
xPos += parentTablePtr->columnList[colIndex]->width;
}
return xPos;
}
int TableColumn::getYPos() const {
return 0;
}
/**
* @brief 返回该TableColumn的总长度(以秒为单位)
*/
qint64 TableColumn::getTimeLen() const {
return start.secsTo( end );
}
/**
* @brief 给定QDateTime推算Y坐标
* @note static
*/
int TableColumn::itemYPosShouldBe( QDateTime itemStart, QDateTime colStart,
QDateTime colEnd, int colHeight ) {
return (int)( ( (qreal)colStart.secsTo( itemStart )
/ (qreal)colStart.secsTo( colEnd ) )
* colHeight ); //提示old-style cast不用管
}
/**
* @overload
* @note non-static
*/
int TableColumn::itemYPosShouldBe( QDateTime itemStart ) const {
return itemYPosShouldBe( itemStart, start, end, height );
}
bool TableColumn::includeNow() const {
QDateTime currentDateTime = QDateTime::currentDateTime();
return start <= currentDateTime && currentDateTime <= end;
}
void TableColumn::drawOnTable() const {
qDebug() << "\tStart TableColumn " << index << " {";
parentTablePtr->scenePtr->addItem( backgroundItemPtr );
parentTablePtr->scenePtr->addItem( timeLineItemPtr );
/**
// 绘制底色、当前时间线
QDateTime curDT = QDateTime::currentDateTime();
int lineWidth = 3;
if ( start <= curDT && curDT <= end ) {
parentTablePtr->scenePtr->addRect( getXPos(), 0, visibleWidth, height,
QPen( QColor( 0, 0, 0, 0 ) ),
QBrush( QColor( 0, 0, 0, 100 ) ) );
parentTablePtr->scenePtr->addLine(
getXPos(), itemYPosShouldBe( curDT ),
getXPos() + visibleWidth - lineWidth / 2, itemYPosShouldBe( curDT ),
QPen( QBrush( Qt::red ), lineWidth ) );
} else {
parentTablePtr->scenePtr->addRect( getXPos(), 0, visibleWidth, height,
QPen( QColor( 0, 0, 0, 0 ) ),
QBrush( QColor( 0, 0, 0, 50 ) ) );
}
*/
// 绘制item
for ( TableItem *childItemPtr : childrenItemPtrList ) {
childItemPtr->drawOnTable( parentTablePtr->scenePtr );
}
qDebug() << "\t} Finish TableColumn " << index;
}