-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableitem.cpp
131 lines (111 loc) · 4.03 KB
/
tableitem.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
#include "tableitem.h"
#include "classtable.h"
#include "tablecolumn.h"
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
TableItem::TableItem( const QString itemText, const QDateTime st,
const QDateTime ed, ClassTable *parentPtr )
: parentTablePtr( parentPtr ), start( st ), end( ed ) {
setText( itemText );
findParentColumns(); //寻找上级TableColumn
}
TableItem::~TableItem() {
for ( QGraphicsItem *itemPtr : childQGraphicsItemPtrList ) {
delete itemPtr;
}
}
void TableItem::show() {
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->show();
}
}
void TableItem::hide() {
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->hide();
}
}
void TableItem::drawOnTable( QGraphicsScene *scene ) const {
qDebug() << "\t\tStart TableItem {";
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
qDebug() << "\t\t\tStart QGraphicsTableItem "
<< itemPtr->textItemPtr->toPlainText();
parentTablePtr->scenePtr->addItem( itemPtr );
}
qDebug() << "\t\t} Finish TableItem";
}
void TableItem::resize( QResizeEvent *tableEvent ) {
for ( QGraphicsTableItem *childItemPtr : childQGraphicsItemPtrList ) {
childItemPtr->resize( tableEvent );
}
}
void TableItem::setText( const QString text ) {
this->text = text;
for ( QGraphicsTableItem *itemPtr : childQGraphicsItemPtrList ) {
itemPtr->textItemPtr->setPlainText( text );
}
}
/**
* @brief 在上级ClassTable中寻找归属的TableColumn
* @warning
* @todo
*/
void TableItem::findParentColumns() {
parentColumnPtrList.clear();
childQGraphicsItemPtrList.clear(); //之前因为没加这个导致显示重叠
for ( TableColumn *colPtr : parentTablePtr->columnList ) {
QDateTime colStartTime = colPtr->start;
QDateTime colEndTime = colPtr->end;
if ( haveIntersection( start, end, colStartTime, colEndTime ) ) {
this->parentColumnPtrList.append( colPtr );
colPtr->childrenItemPtrList.append( this );
QDateTime startTime = qMax( colStartTime, this->start );
QDateTime endTime = qMin( colEndTime, this->end );
// 设置边框
borderPen.setJoinStyle(
Qt::RoundJoin ); //提示old-style cast不用管
int xPos = colPtr->getXPos()
+ (int)( borderWidth / 2 ); //边框左上顶点X,Y坐标
int yPos =
colPtr->getYPos() + colPtr->itemYPosShouldBe( startTime );
int rectWidth = colPtr->visibleWidth - (int)borderWidth;
int rectHeight =
colPtr->itemYPosShouldBe( endTime ) - yPos - (int)borderWidth;
// 设置底色
// 设置文字字体
QFont textFont;
textFont.setBold( true );
textFont.setPointSize( 8 );
QGraphicsTableItem *childItemPtr = new QGraphicsTableItem(
colPtr, this, xPos, yPos, rectWidth, rectHeight, borderPen,
fillBrush, text );
childItemPtr->setFlags( QGraphicsItem::ItemIsSelectable );
this->childQGraphicsItemPtrList.append( childItemPtr );
colPtr->childQGraphicsItemPtrList.append( childItemPtr );
}
}
}
QBrush TableItem::getFillBrush() const {
return fillBrush;
}
void TableItem::setFillBrush( const QBrush &brush ) {
fillBrush = brush;
}
QPen TableItem::getBorderPen() const {
return borderPen;
}
void TableItem::setBorderPen( const QPen &pen ) {
borderPen = pen;
}
/**
* @brief 判断两个区间[start1,end1]和[start2,end2]是否有交集
* @warning 前提:1.类型T必须支持==,<,<=,>=等operator
* 2.区间合法,start1<end1,start2<end2
* @note static
*/
template< typename T >
bool TableItem::haveIntersection( T &start1, T &end1, T &start2, T &end2 ) {
return start1 == start2 || end1 == start2 || end1 == end2
|| ( start1 >= start2 && start1 < end2 )
|| ( start1 <= start2 && ( end1 >= start2 || end1 >= end2 ) );
}