-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve search in document (KDAB#25)
Fixes tasks: QtUiDocument QtTsDocument
- Loading branch information
Showing
16 changed files
with
522 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <[email protected]> for commercial licensing options. | ||
*/ | ||
|
||
#include "highlightsearchdelegate.h" | ||
#include <QPainter> | ||
|
||
namespace Core { | ||
|
||
HighlightSearchDelegate::HighlightSearchDelegate(QObject *parent) | ||
: QItemDelegate(parent) | ||
{ | ||
} | ||
|
||
void HighlightSearchDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const | ||
{ | ||
const QString text = index.data().toString(); | ||
if (m_searchText.isEmpty() || !text.contains(m_searchText, Qt::CaseInsensitive)) { | ||
QItemDelegate::paint(painter, option, index); | ||
return; | ||
} | ||
|
||
// Find the substring to highlight | ||
const QRegularExpression regex(m_searchText, QRegularExpression::CaseInsensitiveOption); | ||
int start = regex.match(text).capturedStart(); | ||
int end = start + regex.match(text).capturedLength(); | ||
|
||
// Calculate the drawing positions | ||
int x = option.rect.left(); | ||
int y = option.rect.top(); | ||
int height = option.rect.height(); | ||
int width = painter->fontMetrics().horizontalAdvance(text.left(start)); | ||
|
||
// Highlight | ||
painter->save(); | ||
const QPen painterPen = painter->pen(); | ||
painter->setPen(painterPen); | ||
painter->drawText(x, y, width, height, Qt::AlignLeft | Qt::AlignVCenter, text.left(start)); | ||
painter->setPen(Qt::blue); | ||
x += width; | ||
width = painter->fontMetrics().horizontalAdvance(text.mid(start, end - start)); | ||
painter->drawText(x, y, width, height, Qt::AlignLeft | Qt::AlignVCenter, text.mid(start, end - start)); | ||
painter->setPen(painterPen); | ||
x += width; | ||
width = painter->fontMetrics().horizontalAdvance(text.right(text.length() - end)); | ||
painter->drawText(x, y, width, height, Qt::AlignLeft | Qt::AlignVCenter, text.right(text.length() - end)); | ||
painter->restore(); | ||
} | ||
|
||
} // namespace Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <[email protected]> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QItemDelegate> | ||
|
||
namespace Core { | ||
|
||
class HighlightSearchDelegate : public QItemDelegate | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit HighlightSearchDelegate(QObject *parent = nullptr); | ||
|
||
inline void setSearchText(const QString &searchText) { m_searchText = searchText; } | ||
|
||
protected: | ||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; | ||
|
||
private: | ||
QString m_searchText; | ||
}; | ||
|
||
} // namespace Core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.