-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add a new interface for searching into document view - move the textdocument search to this new interface - implement the interface for QtUiDocument and QtTsDocument Related to #25
- Loading branch information
Showing
17 changed files
with
525 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QString> | ||
|
||
namespace Gui { | ||
|
||
/** | ||
* @brief Find interface to handle find and replace in views. | ||
*/ | ||
class FindInterface | ||
{ | ||
public: | ||
enum FindFlag { | ||
NoFind = 0x0, | ||
CanSearch = 0x1, | ||
CanReplace = 0x2, | ||
}; | ||
FindInterface(int flags) | ||
: m_findFlags(flags) {}; | ||
|
||
int findFlags() const { return m_findFlags; } | ||
|
||
virtual void find(const QString &text, int options) | ||
{ | ||
Q_UNUSED(text); | ||
Q_UNUSED(options); | ||
}; | ||
virtual void cancelFind() {}; | ||
virtual void replace(const QString &before, const QString &after, int options, bool replaceAll) | ||
{ | ||
Q_UNUSED(before); | ||
Q_UNUSED(after); | ||
Q_UNUSED(options); | ||
Q_UNUSED(replaceAll); | ||
}; | ||
|
||
private: | ||
int m_findFlags = NoFind; | ||
}; | ||
|
||
} // 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
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,86 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#include "highlightdelegate.h" | ||
#include "core/textdocument.h" | ||
|
||
#include <QAbstractTextDocumentLayout> | ||
#include <QPainter> | ||
#include <QTextDocument> | ||
|
||
namespace Gui { | ||
|
||
HighlightDelegate::HighlightDelegate(QObject *parent) | ||
: QItemDelegate(parent) | ||
{ | ||
} | ||
|
||
void HighlightDelegate::setHighlightedText(const QString &searchText, int options) | ||
{ | ||
m_highlightedText = searchText; | ||
m_options = options; | ||
} | ||
|
||
QString HighlightDelegate::transform(QString text, const QString &textColor, const QString &backgroundColor) const | ||
{ | ||
if (m_highlightedText.isEmpty()) | ||
return text; | ||
|
||
if (m_options & Core::TextDocument::FindRegexp) { | ||
const auto re = QRegularExpression {m_highlightedText}; | ||
QRegularExpressionMatch match; | ||
int index = text.indexOf(re, 0, &match); | ||
while (index != -1) { | ||
const auto oldText = match.captured(0); | ||
const auto newText = QString("<span style='color: %1; background-color: %2;'>%3</span>") | ||
.arg(textColor, backgroundColor, oldText); | ||
text.replace(index, oldText.size(), newText); | ||
index = text.indexOf(re, index + newText.size(), &match); | ||
} | ||
} else { | ||
const bool caseSensitive = m_options & Core::TextDocument::FindCaseSensitively; | ||
int index = text.indexOf(m_highlightedText, 0, caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); | ||
while (index != -1) { | ||
const auto oldText = text.mid(index, m_highlightedText.size()); | ||
const auto newText = QString("<span style='color: %1; background-color: %2;'>%3</span>") | ||
.arg(textColor, backgroundColor, oldText); | ||
text.replace(index, oldText.size(), newText); | ||
index = text.indexOf(m_highlightedText, index + newText.size(), | ||
caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); | ||
} | ||
} | ||
return text; | ||
} | ||
|
||
void HighlightDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const | ||
{ | ||
const auto textColor = option.palette.color(QPalette::HighlightedText).name(); | ||
const auto backgroundColor = option.palette.color(QPalette::Highlight).name(); | ||
|
||
// Display the text with the search string highlighted. | ||
QString text = transform(index.data(Qt::DisplayRole).toString(), textColor, backgroundColor); | ||
QTextDocument doc; | ||
doc.setHtml(text); | ||
|
||
painter->save(); | ||
|
||
// Adjust the painter's transformation to fit the text within the given rectangle | ||
painter->translate(option.rect.topLeft()); | ||
QRect clip(0, 0, option.rect.width(), option.rect.height()); | ||
doc.setTextWidth(option.rect.width()); | ||
|
||
QAbstractTextDocumentLayout::PaintContext ctx; | ||
ctx.clip = clip; | ||
doc.documentLayout()->draw(painter, ctx); | ||
|
||
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,35 @@ | ||
/* | ||
This file is part of Knut. | ||
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com> | ||
SPDX-License-Identifier: GPL-3.0-only | ||
Contact KDAB at <info@kdab.com> for commercial licensing options. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <QItemDelegate> | ||
|
||
namespace Gui { | ||
|
||
class HighlightDelegate : public QItemDelegate | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit HighlightDelegate(QObject *parent = nullptr); | ||
|
||
void setHighlightedText(const QString &searchText, int options); | ||
|
||
protected: | ||
QString transform(QString text, const QString &textColor, const QString &backgroundColor) const; | ||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; | ||
|
||
private: | ||
QString m_highlightedText; | ||
int m_options; | ||
}; | ||
|
||
} // namespace Gui |
Oops, something went wrong.