Commit 9936ece5 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Move paintItemViewText to drawing_utils

Doesn't really belong in there + will need to move it anyways when we
start using the KCoreAddons fuzzy matcher
parent 2af20722
Pipeline #137393 passed with stage
in 2 minutes and 26 seconds
......@@ -14,7 +14,7 @@
#include <QDebug>
#include <kfts_fuzzy_match.h>
#include <drawing_utils.h>
#include <ktexteditor_utils.h>
class LineNumberDelegate : public QStyledItemDelegate
......@@ -168,7 +168,7 @@ public:
}
}
kfts::paintItemViewText(painter, text, option, fmts);
Utils::paintItemViewText(painter, text, option, fmts);
painter->restore();
}
......@@ -250,7 +250,7 @@ public:
fmts.append({errIdx, 5, f});
}
kfts::paintItemViewText(p, text, option, fmts);
Utils::paintItemViewText(p, text, option, fmts);
}
private:
......
......@@ -20,7 +20,6 @@
#include <QStyledItemDelegate>
#include <drawing_utils.h>
#include <kfts_fuzzy_match.h>
#include <ktexteditor_utils.h>
static constexpr int SymbolInfoRole = Qt::UserRole + 1;
......@@ -107,7 +106,7 @@ public:
}
options.rect = textRect;
kfts::paintItemViewText(painter, text, options, fmts);
Utils::paintItemViewText(painter, text, options, fmts);
painter->restore();
}
......
......@@ -60,7 +60,7 @@
#include <unordered_map>
#include <utility>
#include <kfts_fuzzy_match.h>
#include <drawing_utils.h>
#include <ktexteditor_utils.h>
namespace RangeData
......@@ -249,7 +249,7 @@ public:
auto width = textRectX - options.rect.x();
painter->translate(width, 0);
kfts::paintItemViewText(painter, text, options, formats);
Utils::paintItemViewText(painter, text, options, formats);
painter->restore();
}
......
......@@ -25,6 +25,7 @@
#include <KLocalizedString>
#include <drawing_utils.h>
#include <kfts_fuzzy_match.h>
class BranchFilterModel : public QSortFilterProxyModel
......@@ -136,7 +137,7 @@ public:
if (itemType == BranchesDialogModel::BranchItem) {
painter->translate(25, 0);
}
kfts::paintItemViewText(painter, name, options, formats);
Utils::paintItemViewText(painter, name, options, formats);
painter->restore();
}
......
......@@ -30,6 +30,7 @@
#include <KLocalizedString>
#include <drawing_utils.h>
#include <kfts_fuzzy_match.h>
#include <gitprocess.h>
......@@ -120,7 +121,7 @@ public:
options.text = QString(); // clear old text
options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);
kfts::paintItemViewText(painter, name, options, formats);
Utils::paintItemViewText(painter, name, options, formats);
painter->restore();
}
......
......@@ -17,7 +17,7 @@
#include <QTextCharFormat>
#include <QTextDocument>
#include <kfts_fuzzy_match.h>
#include <drawing_utils.h>
#include <ktexteditor_utils.h>
// make list spacing resemble the default list spacing
......@@ -137,7 +137,7 @@ void SPHtmlDelegate::paintMatchItem(QPainter *p, const QStyleOptionViewItem &opt
// paint the match text
auto opts = opt;
opts.rect = rtl ? textRect.adjusted(0, 0, -(iconBorderRect.width() + hMargins * 2), 0) : textRect.adjusted(iconBorderRect.width() + hMargins * 2, 0, 0, 0);
kfts::paintItemViewText(p, text, opts, formats);
Utils::paintItemViewText(p, text, opts, formats);
p->restore();
}
......
......@@ -23,6 +23,7 @@
#include <KActionCollection>
#include <KLocalizedString>
#include <drawing_utils.h>
#include <kfts_fuzzy_match.h>
class CommandBarFilterModel : public QSortFilterProxyModel
......@@ -125,7 +126,7 @@ public:
const auto f = kfts::get_fuzzy_match_formats(m_filterString, str, componentIdx + 2, fmt);
formats.append(f);
kfts::paintItemViewText(painter, original, options, std::move(formats));
Utils::paintItemViewText(painter, original, options, std::move(formats));
painter->restore();
}
......
......@@ -37,6 +37,7 @@
#include <QTextDocument>
#include <QTreeView>
#include <drawing_utils.h>
#include <kfts_fuzzy_match.h>
class QuickOpenFilterProxyModel final : public QSortFilterProxyModel
......@@ -222,7 +223,7 @@ public:
painter->translate(25, 0);
// draw text
kfts::paintItemViewText(painter, QString(name + QStringLiteral(" ") + path), options, formats);
Utils::paintItemViewText(painter, QString(name + QStringLiteral(" ") + path), options, formats);
painter->restore();
}
......
......@@ -4,6 +4,9 @@
#include <QIcon>
#include <QPainter>
#include <QPixmap>
#include <QStyle>
#include <QStyleOptionViewItem>
#include <QTextLayout>
namespace Utils
{
......@@ -24,6 +27,44 @@ inline QPixmap colorIcon(const QIcon &icon, const QColor &fgColor, const QSize s
return p;
}
/**
* Draws text in an item view for e.g treeview
* It assumes options.rect == textRect
*/
inline void paintItemViewText(QPainter *p, const QString &text, const QStyleOptionViewItem &options, QVector<QTextLayout::FormatRange> formats)
{
// set formats
QTextLayout textLayout(text, options.font);
auto fmts = textLayout.formats();
formats.append(fmts);
textLayout.setFormats(formats);
// set alignment, rtls etc
QTextOption textOption;
textOption.setTextDirection(options.direction);
textOption.setAlignment(QStyle::visualAlignment(options.direction, options.displayAlignment));
textLayout.setTextOption(textOption);
// layout the text
textLayout.beginLayout();
QTextLine line = textLayout.createLine();
if (!line.isValid())
return;
const int lineWidth = options.rect.width();
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, 0));
textLayout.endLayout();
int y = QStyle::alignedRect(Qt::LayoutDirectionAuto, Qt::AlignVCenter, textLayout.boundingRect().size().toSize(), options.rect).y();
// draw the text
const auto pos = QPointF(options.rect.x(), y);
textLayout.draw(p, pos);
}
} // namespace Utils
#endif
......@@ -396,40 +396,6 @@ get_fuzzy_match_formats(const QStringView pattern, const QStringView str, int of
return ranges;
}
Q_DECL_UNUSED static void paintItemViewText(QPainter *p, const QString &text, const QStyleOptionViewItem &options, QVector<QTextLayout::FormatRange> formats)
{
// set formats
QTextLayout textLayout(text, options.font);
auto fmts = textLayout.formats();
formats.append(fmts);
textLayout.setFormats(formats);
// set alignment, rtls etc
QTextOption textOption;
textOption.setTextDirection(options.direction);
textOption.setAlignment(QStyle::visualAlignment(options.direction, options.displayAlignment));
textLayout.setTextOption(textOption);
// layout the text
textLayout.beginLayout();
QTextLine line = textLayout.createLine();
if (!line.isValid())
return;
const int lineWidth = options.rect.width();
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, 0));
textLayout.endLayout();
int y = QStyle::alignedRect(Qt::LayoutDirectionAuto, Qt::AlignVCenter, textLayout.boundingRect().size().toSize(), options.rect).y();
// draw the text
const auto pos = QPointF(options.rect.x(), y);
textLayout.draw(p, pos);
}
} // namespace kfts
#endif // KFTS_FUZZY_MATCH_H
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment