Commit 05c27695 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Refactor painting and textLayout creation outside of command bar

parent b63d2597
......@@ -67,20 +67,6 @@ private:
QString m_pattern;
};
static void layoutViewItemText(QTextLayout &textLayout, int lineWidth)
{
textLayout.beginLayout();
QTextLine line = textLayout.createLine();
if (!line.isValid())
return;
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, 0));
textLayout.endLayout();
return;
}
class CommandBarStyleDelegate : public QStyledItemDelegate
{
public:
......@@ -114,11 +100,6 @@ public:
painter->translate(20, 0);
}
QTextOption textOption;
textOption.setTextDirection(options.direction);
textOption.setAlignment(QStyle::visualAlignment(options.direction, options.displayAlignment));
uint8_t matches[256];
// must use QString here otherwise fuzzy matching wont
// work very well
QString str = original;
......@@ -130,40 +111,21 @@ public:
str = str.mid(actionNameStart);
}
const int total = kfts::get_fuzzy_match_positions(m_filterString, str, matches);
using FormatRange = QTextLayout::FormatRange;
QTextCharFormat fmt;
fmt.setFontWeight(QFont::Bold);
fmt.setForeground(options.palette.link());
QVector<FormatRange> formats;
QTextCharFormat gray;
gray.setForeground(Qt::gray);
QVector<QTextLayout::FormatRange> formats;
if (componentIdx > 0) {
QTextCharFormat gray;
gray.setForeground(Qt::gray);
formats.append({0, componentIdx, gray});
}
// QTextLayout fails if there are consecutive ranges
// of length = 1 so we have to improvise a little bit
int j = 0;
for (int i = 0; i < total; ++i) {
auto matchPos = actionNameStart + matches[i];
if (matchPos == j + 1) {
formats.last().length++;
} else {
formats.append({matchPos, 1, fmt});
}
j = matchPos;
}
QTextCharFormat fmt;
fmt.setForeground(options.palette.link().color());
fmt.setFontWeight(QFont::Bold);
const auto f = kfts::get_fuzzy_match_formats(m_filterString, str, componentIdx + 2, fmt);
formats.append(f);
QTextLayout textLayout(original, options.font);
auto fmts = textLayout.formats();
formats.append(fmts);
textLayout.setFormats(formats);
textLayout.setTextOption(textOption);
layoutViewItemText(textLayout, options.rect.width());
const auto pos = QPointF(options.rect.x(), options.rect.y());
textLayout.draw(painter, pos);
kfts::paintItemViewText(painter, original, options, std::move(formats));
painter->restore();
}
......
......@@ -9,6 +9,8 @@
#define KFTS_FUZZY_MATCH_H
#include <QString>
#include <QStyleOptionViewItem>
#include <QTextLayout>
/**
* This is based on https://github.com/forrestthewoods/lib_fts/blob/master/code/fts_fuzzy_match.h
......@@ -316,10 +318,12 @@ static QString to_scored_fuzzy_matched_display_string(const QStringView pattern,
return str;
}
Q_DECL_UNUSED static int get_fuzzy_match_positions(const QStringView pattern, const QStringView str, uint8_t *matches)
Q_DECL_UNUSED static QVector<QTextLayout::FormatRange>
get_fuzzy_match_formats(const QStringView pattern, const QStringView str, int offset, const QTextCharFormat &fmt)
{
if (!matches) {
return 0;
QVector<QTextLayout::FormatRange> ranges;
if (pattern.isEmpty()) {
return ranges;
}
int totalMatches = 0;
......@@ -331,8 +335,57 @@ Q_DECL_UNUSED static int get_fuzzy_match_positions(const QStringView pattern, co
const auto patternEnd = pattern.cend();
const auto strEnd = str.cend();
uint8_t matches[256];
fuzzy_internal::fuzzy_match_recursive(patternIt, strIt, score, strIt, strEnd, patternEnd, nullptr, matches, 0, totalMatches, recursionCount);
return totalMatches;
// QTextCharFormat fmt;
// fmt.setFontWeight(QFont::Bold);
// fmt.setForeground(c);
int j = 0;
for (int i = 0; i < totalMatches; ++i) {
auto matchPos = matches[i];
if (matchPos == j + 1) {
ranges.last().length++;
} else {
ranges.append({matchPos + offset, 1, fmt});
}
j = matchPos;
}
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();
// draw the text
const auto pos = QPointF(options.rect.x(), options.rect.y());
textLayout.draw(p, pos);
}
} // namespace kfts
......
Markdown is supported
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