#include "diagonal_table.h"
#include <qnamespace.h>
#include <Qt>
{
DiagonalHeaderView *horizontalDiagonalHeader = new DiagonalHeaderView(Qt::Horizontal, this);
DiagonalHeaderView *verticalDiagonalHeader = new DiagonalHeaderView(Qt::Vertical, this);
setHorizontalHeader(horizontalDiagonalHeader);
setVerticalHeader(verticalDiagonalHeader);
setItemDelegate(new MyDelegate(this));
setShowGrid(false);
}
DiagonalHeaderView
::DiagonalHeaderView(Qt
::Orientation orientation,
QWidget *parent
) : QHeaderView(orientation, parent
){
}
void DiagonalHeaderView
::paintSection(QPainter *painter,
const QRect &rect,
int logicalIndex
) const {
if (!rect.isValid())
return;
initStyleOption(&opt);
opt.text = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
// the section position
int visual = visualIndex(logicalIndex);
Q_ASSERT(visual != -1);
if (count() == 1)
else if (visual == 0)
else if (visual == count() - 1)
else
opt.orientation = orientation();
// Get rotation, and wrap to (-180, 180]
double rotation = model()->headerData(logicalIndex, orientation(), DiagonalTable::RotationRole).toFloat();
while (rotation > 180)
rotation -=360;
while (rotation <= -180)
rotation +=360;
int separatorLength;
int sumLinePositionX;
int sumLinePositionY;
int textOffsetX;
int textOffsetY;
int textWidth = opt.fontMetrics.width(opt.text);
int textHeight = opt.fontMetrics.height();
switch (orientation()) {
case Qt::Horizontal:
{
// Cast parent so that we can use its methods
DiagonalTable * diagonalTable = dynamic_cast<DiagonalTable *>(this->parent());
if (diagonalTable == NULL)
Q_ASSERT(0);
// Calculate the rectangle for the box in which we will draw
int firstRow = model()->headerData(logicalIndex, orientation(),DiagonalTable::RowRole).toInt();
sumLinePositionX = rect.bottomLeft().x();
sumLinePositionY = rect.bottomLeft().y();
for (int i=0; i<firstRow; i++) {
int rowHeight = diagonalTable->rowHeight(i+1);
sumLinePositionY += rowHeight;
}
textOffsetX = rect.width()/2 + textHeight/2;
textOffsetY = 0;
}
break;
case Qt::Vertical:
{
if (rotation == 0) {
style
()->drawControl
(QStyle::CE_Header,
&opt, painter,
this);
} else {
// Cast parent so that we can use its methods
DiagonalTable * diagonalTable = dynamic_cast<DiagonalTable *>(this->parent());
if (diagonalTable == NULL)
Q_ASSERT(0);
// Calculate the rectangle for the box in which we will draw
int firstColumn = model()->headerData(logicalIndex, orientation(), DiagonalTable::ColumnRole).toInt();
sumLinePositionX = rect.bottomRight().x();
sumLinePositionY = rect.bottomRight().y();
for (int i=0; i<firstColumn; i++) {
int columnWidth = diagonalTable->columnWidth(i+1);
sumLinePositionX += columnWidth;
}
textOffsetX = 0;
textOffsetY = -rect.height()/2 + textHeight/2;
}
}
break;
}
// Paint the diagonal line
// Adjust for rotation
if (rotation > -90 && rotation < 90)
separatorLength = 30;
else
separatorLength = 90;
painter
->setRenderHint
(QPainter::Antialiasing);
painter->translate(sumLinePositionX, sumLinePositionY);
painter->rotate(rotation);
painter->drawLine(0, 0, separatorLength, 0);
// Paint text
painter->resetTransform();
// Adjust for rotation
if (rotation > -90 && rotation < 90) {
painter->translate(sumLinePositionX + textOffsetX, sumLinePositionY + textOffsetY);
painter->rotate(rotation);
painter->translate(2, 0); // Shift text up (along new axis)
} else {
painter->translate(sumLinePositionX + textOffsetX, sumLinePositionY + textOffsetY);
painter->rotate(rotation + 180); // Always draw text from left to right
painter->translate(-textWidth - 2, 0); // Back text up (along new axis)
}
painter->setFont(font()); // This font should be system dependent. Not sure how to get this.
painter->drawText(0, 0, opt.text);
}
{
int row = index.row();
int col = index.column();
int firstRow = index.model()->headerData(col, Qt::Horizontal,DiagonalTable::RowRole).toInt();
if (row < firstRow) {
} else {
int shift = 18; // This makes no sense yet, but for whatever reason the colors are shifted when drawn in the table
pen.
setColor(QColor(143+shift,
142+shift,
147+shift
));
pen.setWidth(1);
painter->setPen( pen );
painter->drawRect( option.rect.x()-1, option.rect.y()-1, option.rect.width(), option.rect.height() );
}
}