| 120 | } |
| 121 | |
| 122 | QSize GrepOutputDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 123 | { |
| 124 | const auto* model = qobject_cast<const GrepOutputModel*>(index.model()); |
| 125 | const GrepOutputItem *item = model ? dynamic_cast<const GrepOutputItem *>(model->itemFromIndex(index)) : nullptr; |
| 126 | |
| 127 | QSize ret = QStyledItemDelegate::sizeHint(option, index); |
| 128 | |
| 129 | //take account of additional width required for highlighting (bold text) |
| 130 | //and line numbers. These are not included in the default Qt size calculation. |
| 131 | if(item && item->isText()) |
| 132 | { |
| 133 | QFont font = option.font; |
| 134 | QFontMetrics metrics(font); |
| 135 | font.setBold(true); |
| 136 | QFontMetrics bMetrics(font); |
| 137 | const KTextEditor::Range rng = item->change()->m_range; |
| 138 | int width = metrics.horizontalAdvance(item->text().left(rng.start().column())) |
| 139 | + metrics.horizontalAdvance(item->text().mid(rng.end().column())) |
| 140 | + bMetrics.horizontalAdvance( |
| 141 | item->text().mid(rng.start().column(), rng.end().column() - rng.start().column())) |
| 142 | + option.fontMetrics.horizontalAdvance(i18n("Line %1: ", item->lineNumber())) |
| 143 | + std::max(option.decorationSize.width(), 0); |
| 144 | ret.setWidth(width); |
| 145 | }else{ |
| 146 | // This is only used for titles, so not very performance critical |
| 147 | QString text; |
| 148 | if(item) |
| 149 | text = item->text(); |
| 150 | else |
| 151 | text = index.data().toString(); |
| 152 | |
| 153 | QTextDocument doc; |
| 154 | doc.setDocumentMargin(0); |
| 155 | doc.setHtml(text); |
| 156 | QSize newSize = doc.size().toSize(); |
| 157 | if(newSize.height() > ret.height()) |
| 158 | ret.setHeight(newSize.height()); |
| 159 | } |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | #include "moc_grepoutputdelegate.cpp" |
no test coverage detected