19 #include <QAbstractItemModel>
22 #include <QFontMetrics>
29 , m_horizontalHeaderFont()
30 , m_verticalHeaderFont()
31 , m_horizontalHeaderVisible(true)
32 , m_verticalHeaderVisible(true)
39 , m_cellFontScaler(m_cellFont)
40 , m_horizontalHeaderFontScaler(m_horizontalHeaderFont)
41 , m_verticalHeaderFontScaler(m_verticalHeaderFont)
56 qDebug() <<
"initial m_rowHeight=" << m_rowHeight;
60 void TableLayout::updateRowHeight()
73 qDebug() <<
"cells say rowHeight=" << m_rowHeight <<
"vHeader says height=" << vHeaderHeight;
75 m_rowHeight = qMax(m_rowHeight, vHeaderHeight);
85 const QFontMetricsF fm = m_cellFontScaler.
fontMetrics();
87 const int colCount =
m_model->columnCount();
91 const int rowCount =
m_model->rowCount();
92 for (
int col = 0; col < colCount; ++col) {
95 const QString cellText =
m_model->headerData(col, Qt::Horizontal).toString();
96 const qreal textWidth = m_horizontalHeaderFontScaler.
textWidth(cellText);
97 const qreal width = addIconWidth(textWidth,
m_model->headerData(col, Qt::Horizontal, Qt::DecorationRole));
100 m_hHeaderHeight = qMax(m_hHeaderHeight, m_horizontalHeaderFontScaler.
fontMetrics().height());
102 for (
int row = 0; row < rowCount; ++row) {
103 const QModelIndex index =
m_model->index(row, col);
104 if (
m_model->span(index).width() > 1) {
112 const QString cellText =
m_model->data(index, Qt::DisplayRole).toString();
113 const QSizeF cellSize =
m_model->data(index, Qt::SizeHintRole).toSizeF();
114 if (cellSize.isValid()) {
117 const qreal textWidth = fm.size(0 , cellText).width();
118 width = addIconWidth(textWidth,
m_model->data(index, Qt::DecorationRole));
131 for (
int row = 0; row < rowCount; ++row) {
132 const QString cellText =
m_model->headerData(row, Qt::Vertical).toString();
133 const qreal textWidth = m_verticalHeaderFontScaler.
textWidth(cellText);
134 const qreal width = addIconWidth(textWidth,
m_model->headerData(row, Qt::Vertical, Qt::DecorationRole));
135 m_vHeaderWidth = qMax(m_vHeaderWidth, width);
148 void TableLayout::updateColumnWidthsByFactor( qreal factor )
150 const int colCount =
m_model->columnCount();
151 for (
int col = 0; col < colCount; ++col )
155 m_vHeaderWidth *= factor;
157 qDebug() <<
"updateColumnWidthsByFactor: after factor" << factor <<
"m_vHeaderWidth=" << m_vHeaderWidth;
161 qDebug() <<
"final scaling factors:" << m_cellFontScaler.
scalingFactor()
171 qDebug() <<
"TableLayout::ensureScalingFactorForWidth" << factor;
180 const int colCount =
m_model->columnCount();
181 QString textForScaling;
182 for (
int col = 0; col < colCount; ++col) {
197 const qreal wantedRowHeightFactor = maxRowHeight / m_rowHeight;
201 const qreal wantedTextHeight = maxRowHeight - 2.0 * wantedRowHeightFactor *
scaledCellPadding();
203 qDebug() <<
" ensureScalingFactorForHeight: wantedRowHeightFactor=" << wantedRowHeightFactor <<
"wantedTextHeight=" << wantedTextHeight <<
"after removing padding ( unscaled" <<
m_cellPadding
207 qreal additionalFactor = 0;
213 qDebug() <<
" Reducing the cell font to be wantedTextHeight=" << wantedTextHeight;
215 const qreal initialCellFactor = m_cellFontScaler.
scalingFactor();
217 additionalFactor = m_cellFontScaler.
scalingFactor() / initialCellFactor;
225 qDebug() <<
"Reducing the vertical header font to be wantedTextHeight=" << wantedTextHeight;
227 const qreal initialVerticHeaderFactor = m_verticalHeaderFontScaler.
scalingFactor();
229 additionalFactor = m_verticalHeaderFontScaler.
scalingFactor() / initialVerticHeaderFactor;
244 m_rowHeight = qMin(maxRowHeight, m_rowHeight);
247 qDebug() <<
" ensureScalingFactorForHeight: applied additional factor" << additionalFactor <<
"row height is now" << m_rowHeight;
253 QImage img = qvariant_cast<QImage>(cellDecoration);
257 QPixmap pix = qvariant_cast<QPixmap>(cellDecoration);
264 qreal KDReports::TableLayout::addIconWidth(qreal textWidth,
const QVariant &cellDecoration)
const
266 qreal width = textWidth;
267 if (!cellDecoration.isNull())
268 width += decorationSize(cellDecoration).width() + 2 ;