21 : QHeaderView(Qt::Horizontal, parent)
22 , m_HeaderModel(nullptr)
36 if (variant.isValid() && variant.canConvert<QStandardItemModel*>())
38 m_HeaderModel = variant.value<QStandardItemModel*>();
41 QHeaderView::setModel(model);
50 QModelIndex leafIndex = HeaderIndex(logicalIndex);
51 QModelIndexList indexes = ParentIndexList(leafIndex);
52 for (
const auto& index : indexes)
54 top = PaintHeader(painter, index, logicalIndex, rect, top);
60 QHeaderView::paintSection(painter, rect, logicalIndex);
65 if (
nullptr != m_HeaderModel)
67 QModelIndex headerIndex = HeaderIndex(logicalIndex);
68 if (headerIndex.isValid())
70 QSize headerSize = HeaderSize(headerIndex);
71 headerIndex = headerIndex.parent();
72 while (headerIndex.isValid())
74 QSize currentHeaderSize = HeaderSize(headerIndex);
75 headerSize.rheight() += currentHeaderSize.height();
76 if (currentHeaderSize.width() > headerSize.width())
78 headerSize.rwidth() = currentHeaderSize.width();
81 headerIndex = headerIndex.parent();
87 return QHeaderView::sectionSizeFromContents(logicalIndex);
90 int QmitkPatientTableHeaderView::PaintHeader(QPainter* painter,
const QModelIndex& currentIndex,
int logicalIndex,
const QRect& sectionRect,
int top)
const 92 QModelIndex headerIndex = HeaderIndex(logicalIndex);
93 int height = HeaderSize(currentIndex).height();
94 if (currentIndex == headerIndex)
96 height = sectionRect.height() - top;
98 int left = CurrentHeaderLeft(currentIndex, headerIndex, logicalIndex, sectionRect.left());
99 int width = CurrentHeaderWidth(currentIndex, headerIndex, logicalIndex);
101 QStyleOptionHeader headerStyleOptions;
102 initStyleOption(&headerStyleOptions);
103 headerStyleOptions.text = currentIndex.data(Qt::DisplayRole).toString();
104 headerStyleOptions.textAlignment = Qt::AlignCenter;
107 QRect rect(left, top, width, height);
108 headerStyleOptions.rect = rect;
109 style()->drawControl(QStyle::CE_Header, &headerStyleOptions, painter,
this);
115 QSize QmitkPatientTableHeaderView::HeaderSize(
const QModelIndex& index)
const 117 QFont font = this->font();
119 QFontMetrics fontMetrics(font);
120 QSize fontSize = fontMetrics.size(0, index.data(Qt::DisplayRole).toString());
121 QSize emptyFontSize = fontMetrics.size(0,
"");
123 return fontSize + emptyFontSize;
126 int QmitkPatientTableHeaderView::CurrentHeaderLeft(
const QModelIndex& currentIndex,
const QModelIndex& headerIndex,
int sectionIndex,
int left)
const 128 QModelIndexList headerList = ListHeader(currentIndex);
129 if (!headerList.empty())
131 int index = headerList.indexOf(headerIndex);
132 int firstHeaderSectionIndex = sectionIndex - index;
134 for (; index >= 0; --index)
136 left -= sectionSize(firstHeaderSectionIndex + index);
143 int QmitkPatientTableHeaderView::CurrentHeaderWidth(
const QModelIndex& currentIndex,
const QModelIndex& headerIndex,
int sectionIndex)
const 145 QModelIndexList headerList = ListHeader(currentIndex);
146 if (headerList.empty())
148 return sectionSize(sectionIndex);
152 int index = headerList.indexOf(headerIndex);
153 int firstHeaderSectionIndex = sectionIndex - index;
154 for (
int i = 0; i < headerList.size(); ++i)
156 width += sectionSize(firstHeaderSectionIndex + i);
162 QModelIndexList QmitkPatientTableHeaderView::ParentIndexList(QModelIndex index)
const 164 QModelIndexList indexList;
165 while (index.isValid())
167 indexList.push_front(index);
168 index = index.parent();
174 QModelIndex QmitkPatientTableHeaderView::HeaderIndex(
int sectionIndex)
const 176 if (
nullptr != m_HeaderModel)
178 int currentHeaderIndex = -1;
179 for (
int i = 0; i < m_HeaderModel->columnCount(); ++i)
181 QModelIndex modelIndex = FindHeader(m_HeaderModel->index(0, i), sectionIndex, currentHeaderIndex);
182 if (modelIndex.isValid())
189 return QModelIndex();
192 QModelIndex QmitkPatientTableHeaderView::FindHeader(
const QModelIndex& currentIndex,
int sectionIndex,
int& currentHeaderIndex)
const 194 if (currentIndex.isValid())
196 int childCount = currentIndex.model()->columnCount(currentIndex);
199 for (
int i = 0; i < childCount; ++i)
201 QModelIndex modelIndex = FindHeader(currentIndex.child(0, i), sectionIndex, currentHeaderIndex);
202 if (modelIndex.isValid())
210 ++currentHeaderIndex;
211 if (currentHeaderIndex == sectionIndex)
218 return QModelIndex();
221 QModelIndexList QmitkPatientTableHeaderView::ListHeader(
const QModelIndex& currentIndex)
const 223 QModelIndexList headerList;
224 if (currentIndex.isValid())
226 int childCount = currentIndex.model()->columnCount(currentIndex);
229 for (
int i = 0; i < childCount; ++i)
231 headerList += ListHeader(currentIndex.child(0, i));
236 headerList.push_back(currentIndex);