-
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 RCDocument text view RCDocument central view RCDocument left tree
- Loading branch information
Showing
12 changed files
with
409 additions
and
19 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
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,83 @@ | ||
/* | ||
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 Gui { | ||
|
||
HighlightSearchDelegate::HighlightSearchDelegate(QObject *parent) | ||
: QItemDelegate(parent) | ||
{ | ||
} | ||
|
||
void HighlightSearchDelegate::setSearchText(const QString &searchText, int offset) | ||
{ | ||
m_searchText = searchText; | ||
m_offset = offset; | ||
} | ||
|
||
void HighlightSearchDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, | ||
const QModelIndex &index) const | ||
{ | ||
QString text = index.data().toString(); | ||
if (m_searchText.isEmpty() || !text.contains(m_searchText, Qt::CaseInsensitive)) { | ||
QItemDelegate::paint(painter, option, index); | ||
return; | ||
} | ||
|
||
// Check for multiline strings | ||
QStringList multilineStrings = text.split("\n", Qt::SkipEmptyParts); | ||
bool isMultiline = multilineStrings.count() > 1; | ||
if (isMultiline) { | ||
// Make it a one line text for the search highlight, | ||
// otherwise the painting is error prone (needs too much manipulation). | ||
// That does not impact the model itself. | ||
text = text.replace("\n", " - "); | ||
} | ||
// 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. | ||
// The TreeViews or TableView display its Items excentred in relation to the left side of the cell. | ||
// Correct the x position accordingly using the caller offset value. | ||
int x = option.rect.left() + m_offset; | ||
int y = option.rect.top(); | ||
int height = option.rect.height(); | ||
int width = painter->fontMetrics().horizontalAdvance(text.left(start)); | ||
|
||
// Paint the seach result string 'bold'. | ||
QFont highlightFont = option.font; | ||
highlightFont.setBold(true); | ||
|
||
painter->save(); | ||
// Make sure we don't paint over the selected item highlighted background. | ||
if (option.state & QStyle::State_Selected) { | ||
// Set the background color to the selection color | ||
painter->fillRect(option.rect, option.palette.brush(QPalette::Active, QPalette::Highlight)); | ||
} | ||
|
||
painter->drawText(x, y, width, height, option.displayAlignment, text.left(start)); | ||
// Paint the searched string with bold font. | ||
painter->setFont(highlightFont); | ||
x += width; | ||
width = painter->fontMetrics().horizontalAdvance(text.mid(start, end - start)); | ||
painter->drawText(x, y, width, height, option.displayAlignment, text.mid(start, end - start)); | ||
// Reset the painter font to original font. | ||
painter->setFont(option.font); | ||
x += width; | ||
width = painter->fontMetrics().horizontalAdvance(text.right(text.length() - end)); | ||
painter->drawText(x, y, width, height, option.displayAlignment, text.right(text.length() - end)); | ||
painter->restore(); | ||
} | ||
|
||
} // namespace Gui |
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,34 @@ | ||
/* | ||
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 Gui { | ||
|
||
class HighlightSearchDelegate : public QItemDelegate | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit HighlightSearchDelegate(QObject *parent = nullptr); | ||
|
||
void setSearchText(const QString &searchText, int offset = 0); | ||
|
||
protected: | ||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; | ||
|
||
private: | ||
QString m_searchText; | ||
int m_offset; | ||
}; | ||
|
||
} // namespace Gui |
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.