13 #include <QAbstractItemModel>
16 #include <QFontMetrics>
23 , m_horizontalHeaderFont()
24 , m_verticalHeaderFont()
25 , m_horizontalHeaderVisible(true)
26 , m_verticalHeaderVisible(true)
33 , m_cellFontScaler(m_cellFont)
34 , m_horizontalHeaderFontScaler(m_horizontalHeaderFont)
35 , m_verticalHeaderFontScaler(m_verticalHeaderFont)
50 qDebug() <<
"initial m_rowHeight=" << m_rowHeight;
54 void TableLayout::updateRowHeight()
67 qDebug() <<
"cells say rowHeight=" << m_rowHeight <<
"vHeader says height=" << vHeaderHeight;
69 m_rowHeight = qMax(m_rowHeight, vHeaderHeight);
79 const QFontMetricsF fm = m_cellFontScaler.
fontMetrics();
81 const int colCount =
m_model->columnCount();
85 const int rowCount =
m_model->rowCount();
86 for (
int col = 0; col < colCount; ++col) {
89 const QString cellText =
m_model->headerData(col, Qt::Horizontal).toString();
90 const qreal textWidth = m_horizontalHeaderFontScaler.
textWidth(cellText);
91 const qreal width = addIconWidth(textWidth,
m_model->headerData(col, Qt::Horizontal, Qt::DecorationRole));
94 m_hHeaderHeight = qMax(m_hHeaderHeight, m_horizontalHeaderFontScaler.
fontMetrics().height());
96 for (
int row = 0; row < rowCount; ++row) {
97 const QModelIndex index =
m_model->index(row, col);
98 if (
m_model->span(index).width() > 1) {
106 const QString cellText =
m_model->data(index, Qt::DisplayRole).toString();
107 const QSizeF cellSize =
m_model->data(index, Qt::SizeHintRole).toSizeF();
108 if (cellSize.isValid()) {
111 const qreal textWidth = fm.size(0 , cellText).width();
112 width = addIconWidth(textWidth,
m_model->data(index, Qt::DecorationRole));
125 for (
int row = 0; row < rowCount; ++row) {
126 const QString cellText =
m_model->headerData(row, Qt::Vertical).toString();
127 const qreal textWidth = m_verticalHeaderFontScaler.
textWidth(cellText);
128 const qreal width = addIconWidth(textWidth,
m_model->headerData(row, Qt::Vertical, Qt::DecorationRole));
129 m_vHeaderWidth = qMax(m_vHeaderWidth, width);
142 void TableLayout::updateColumnWidthsByFactor( qreal factor )
144 const int colCount =
m_model->columnCount();
145 for (
int col = 0; col < colCount; ++col )
149 m_vHeaderWidth *= factor;
151 qDebug() <<
"updateColumnWidthsByFactor: after factor" << factor <<
"m_vHeaderWidth=" << m_vHeaderWidth;
155 qDebug() <<
"final scaling factors:" << m_cellFontScaler.
scalingFactor()
165 qDebug() <<
"TableLayout::ensureScalingFactorForWidth" << factor;
174 const int colCount =
m_model->columnCount();
175 QString textForScaling;
176 for (
int col = 0; col < colCount; ++col) {
191 const qreal wantedRowHeightFactor = maxRowHeight / m_rowHeight;
195 const qreal wantedTextHeight = maxRowHeight - 2.0 * wantedRowHeightFactor *
scaledCellPadding();
197 qDebug() <<
" ensureScalingFactorForHeight: wantedRowHeightFactor=" << wantedRowHeightFactor <<
"wantedTextHeight=" << wantedTextHeight <<
"after removing padding ( unscaled" <<
m_cellPadding
201 qreal additionalFactor = 0;
207 qDebug() <<
" Reducing the cell font to be wantedTextHeight=" << wantedTextHeight;
209 const qreal initialCellFactor = m_cellFontScaler.
scalingFactor();
211 additionalFactor = m_cellFontScaler.
scalingFactor() / initialCellFactor;
219 qDebug() <<
"Reducing the vertical header font to be wantedTextHeight=" << wantedTextHeight;
221 const qreal initialVerticHeaderFactor = m_verticalHeaderFontScaler.
scalingFactor();
223 additionalFactor = m_verticalHeaderFontScaler.
scalingFactor() / initialVerticHeaderFactor;
238 m_rowHeight = qMin(maxRowHeight, m_rowHeight);
241 qDebug() <<
" ensureScalingFactorForHeight: applied additional factor" << additionalFactor <<
"row height is now" << m_rowHeight;
247 QImage img = qvariant_cast<QImage>(cellDecoration);
251 QPixmap pix = qvariant_cast<QPixmap>(cellDecoration);
258 qreal KDReports::TableLayout::addIconWidth(qreal textWidth,
const QVariant &cellDecoration)
const
260 qreal width = textWidth;
261 if (!cellDecoration.isNull())
262 width += decorationSize(cellDecoration).width() + 2 ;
qreal scalingFactor() const
qreal textWidth(const QString &text) const
void applyAdditionalScalingFactor(qreal factor)
void setFactorForWidth(qreal wantedFactor, const QString &sampleText)
QFontMetricsF fontMetrics() const
void setFactorForHeight(qreal wantedHeight)
void setFontAndScalingFactor(const QFont &font, qreal scalingFactor)
QAbstractItemModel * m_model
bool m_horizontalHeaderVisible
QSize decorationSize(const QVariant &cellDecoration) const
void ensureScalingFactorForHeight(qreal maxRowHeight)
void setInitialFontScalingFactor(qreal fontScalingFactor)
QVector< QString > m_widestTextPerColumn
bool m_verticalHeaderVisible
qreal scalingFactor() const
void ensureScalingFactorForWidth(qreal scalingFactor)
qreal scaledCellPadding() const
void updateColumnWidths()
QFont m_verticalHeaderFont
QFont m_horizontalHeaderFont
QVector< qreal > m_columnWidths
KDREPORTS_EXPORT qreal mmToPixels(qreal mm)