class VerticalTextHeaderStyle : public QProxyStyle
{
public:
VerticalTextHeaderStyle
(QStyle *style,
int fontHeight)
: QProxyStyle(style)
{
m_halfFontHeight=fontHeight/2;
}
{
//force a very small size for testing:
}
{
if (element
== QStyle::CE_HeaderLabel) { //Other header elements (e.g.: vertical header): do nothing specific
if (!hv || hv->orientation() != Qt::Horizontal)
{
return QProxyStyle::drawControl(element, option, painter, widget);
}
//Text that shall be rotated is marked by a leading space. Bad hack, actually, but I lack better ideas..
if ((header->text.length()>0)&&(header->text.left(1)!=" "))
{
//Text without rotation
painter->save();
if (header->text.contains("\n")) //I allow up to one line break.
{
QString zeile1
=header
->text.
left(header
->text.
indexOf("\n"));
QString zeile2
=header
->text.
right(header
->text.
length()-header
->text.
indexOf("\n")-1);
painter->drawText(header->rect.left(),header->rect.bottom()-3*header->fontMetrics.height()/2,zeile1);
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,zeile2);
} else {
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,header->text);
}
painter->restore();
return;
}
painter->save();
//vertical text has to be rotated
if (header->text.contains("\n")) //again, I allow up to one line break.
{
QString zeile1
=header
->text.
left(header
->text.
indexOf("\n"));
QString zeile2
=" "+header
->text.
right(header
->text.
length()-header
->text.
indexOf("\n")-1);
painter->translate(header->rect.center().x()-m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,zeile1);
painter->drawText(0,2*m_halfFontHeight,zeile2);
} else {
painter->translate(header->rect.center().x()+m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,header->text);
}
painter->restore();
return;
}
return QProxyStyle::drawControl(element, option, painter, widget);
}
protected:
int m_halfFontHeight;
};
class VerticalTextHeaderStyle : public QProxyStyle
{
public:
VerticalTextHeaderStyle(QStyle *style,
int fontHeight)
: QProxyStyle(style)
{
m_halfFontHeight=fontHeight/2;
}
QSize sizeFromContents(ContentsType type, const QStyleOption * option, const QSize & contentsSize, const QWidget * widget = 0) const
{
//force a very small size for testing:
return QSize(10,10);
}
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const
{
if (element == QStyle::CE_HeaderLabel) {
const QHeaderView *hv = qobject_cast<const QHeaderView *>(widget);
//Other header elements (e.g.: vertical header): do nothing specific
if (!hv || hv->orientation() != Qt::Horizontal)
{
return QProxyStyle::drawControl(element, option, painter, widget);
}
const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option);
//Text that shall be rotated is marked by a leading space. Bad hack, actually, but I lack better ideas..
if ((header->text.length()>0)&&(header->text.left(1)!=" "))
{
//Text without rotation
painter->save();
if (header->text.contains("\n")) //I allow up to one line break.
{
QString zeile1=header->text.left(header->text.indexOf("\n"));
QString zeile2=header->text.right(header->text.length()-header->text.indexOf("\n")-1);
painter->drawText(header->rect.left(),header->rect.bottom()-3*header->fontMetrics.height()/2,zeile1);
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,zeile2);
} else {
painter->drawText(header->rect.left(),header->rect.bottom()-header->fontMetrics.height()/2,header->text);
}
painter->restore();
return;
}
painter->save();
//vertical text has to be rotated
if (header->text.contains("\n")) //again, I allow up to one line break.
{
QString zeile1=header->text.left(header->text.indexOf("\n"));
QString zeile2=" "+header->text.right(header->text.length()-header->text.indexOf("\n")-1);
painter->translate(header->rect.center().x()-m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,zeile1);
painter->drawText(0,2*m_halfFontHeight,zeile2);
} else {
painter->translate(header->rect.center().x()+m_halfFontHeight,header->rect.bottom());
painter->rotate(-90);
painter->drawText(0,0,header->text);
}
painter->restore();
return;
}
return QProxyStyle::drawControl(element, option, painter, widget);
}
protected:
int m_halfFontHeight;
};
To copy to clipboard, switch view to plain text mode
Bookmarks