-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make comment header label searchable
- Loading branch information
Showing
4 changed files
with
198 additions
and
3 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,58 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved. | ||
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved. | ||
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved. | ||
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
#pragma once | ||
|
||
#include <QLabel> | ||
#include <QList> | ||
#include <QPair> | ||
#include <QPainter> | ||
|
||
class QFontMetrics; | ||
|
||
namespace hal { | ||
|
||
class SearchableLabel : public QLabel | ||
{ | ||
Q_OBJECT | ||
protected: | ||
void paintEvent(QPaintEvent* ev) override; | ||
public Q_SLOTS: | ||
void handleSearchChanged(const QString& string, int option); | ||
public: | ||
SearchableLabel(QWidget* parent = nullptr); | ||
SearchableLabel(const QString& txt, QWidget* parent = nullptr); | ||
bool hasMatch() const; | ||
private: | ||
void drawSubstring(QPainter& painter, int pos, int len, bool hilite); | ||
bool exactWordMatch(int pos, int len) const; | ||
QList<QPair<int,int> > mMatchPositions; | ||
QFontMetrics* mMetrics; | ||
int xText; | ||
int yText; | ||
|
||
static const int sHiliteOffset = 2; | ||
}; | ||
} |
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,134 @@ | ||
#include "gui/searchbar/searchable_label.h" | ||
#include "gui/searchbar/searchoptions.h" | ||
#include <QPainter> | ||
#include <QFontMetrics> | ||
#include <QDebug> | ||
#include <QRegularExpression> | ||
|
||
namespace hal { | ||
|
||
SearchableLabel::SearchableLabel(const QString &txt, QWidget *parent) | ||
: QLabel(txt, parent) | ||
{;} | ||
|
||
SearchableLabel::SearchableLabel(QWidget *parent) | ||
: QLabel(parent) | ||
{;} | ||
|
||
void SearchableLabel::paintEvent(QPaintEvent* ev) | ||
{ | ||
if (mMatchPositions.empty()) | ||
{ | ||
QLabel::paintEvent(ev); | ||
return; | ||
} | ||
QPainter painter(this); | ||
mMetrics = new QFontMetrics(font()); | ||
int fw = mMetrics->horizontalAdvance(text()); | ||
int fh = mMetrics->height(); | ||
int rw = rect().width(); | ||
int rh = rect().height(); | ||
if (alignment() & Qt::AlignHCenter) | ||
xText = (rw-fw) / 2; | ||
else if (alignment() & Qt::AlignRight) | ||
xText = rw - fw; | ||
else | ||
xText = 0; | ||
if (alignment() & Qt::AlignVCenter) | ||
yText = (rh-fh) / 2; | ||
else if (alignment() & Qt::AlignBottom) | ||
yText = rh - fh; | ||
else | ||
yText = 0; | ||
if (autoFillBackground()) | ||
painter.fillRect(rect(),palette().color(QPalette::Background)); | ||
int p0 = 0; | ||
for (QPair<int,int> p1 : mMatchPositions) | ||
{ | ||
drawSubstring(painter, p0, p1.first-p0, false); | ||
drawSubstring(painter, p1.first, p1.second, true); | ||
p0 = p1.first + p1.second; | ||
} | ||
if (p0 < text().size()) | ||
drawSubstring(painter, p0, text().size()-p0, false); | ||
delete mMetrics; | ||
} | ||
|
||
void SearchableLabel::drawSubstring(QPainter& painter, int pos, int len, bool hilite) | ||
{ | ||
if (len <= 0) return; | ||
|
||
QColor storeColor = painter.pen().color(); | ||
QString sub = text().mid(pos,len); | ||
int fw = mMetrics->horizontalAdvance(sub); | ||
int fh = mMetrics->height(); | ||
if (hilite) | ||
{ | ||
painter.setPen(QColor(255, 252, 240)); | ||
painter.fillRect(xText,yText-sHiliteOffset,fw,fh+2*sHiliteOffset,QBrush(QColor(33, 66, 133))); | ||
} | ||
painter.drawText(xText,yText,fw,fh,0,sub); | ||
xText += fw; | ||
if (hilite) | ||
{ | ||
painter.setPen(storeColor); | ||
} | ||
} | ||
|
||
bool SearchableLabel::exactWordMatch(int pos, int len) const | ||
{ | ||
if (pos && !text().at(pos-1).isSpace()) return false; | ||
if (pos + len < text().size() && !text().at(pos+len).isSpace()) return false; | ||
return true; | ||
} | ||
|
||
bool SearchableLabel::hasMatch() const | ||
{ | ||
return !mMatchPositions.isEmpty(); | ||
} | ||
|
||
void SearchableLabel::handleSearchChanged(const QString& string, int option) | ||
{ | ||
mMatchPositions.clear(); | ||
SearchOptions opts(option); | ||
Qt::CaseSensitivity cs = opts.isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive; | ||
if (!string.isEmpty()) | ||
{ | ||
int pos = 0; | ||
if (opts.isRegularExpression()) | ||
{ | ||
// Regular expression search | ||
QRegularExpression regexp(opts.isExactMatch() ? "\\b" + string + "\\b" : string, | ||
opts.isCaseSensitive() ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption); | ||
if (regexp.isValid()) | ||
{ | ||
QRegularExpressionMatch rmatch; | ||
while (pos >= 0) | ||
{ | ||
pos = text().indexOf(regexp,pos,&rmatch); | ||
if (pos >= 0) | ||
{ | ||
mMatchPositions.append(qMakePair(pos,rmatch.captured().size())); | ||
++pos; | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// String search | ||
while (pos >= 0) | ||
{ | ||
pos = text().indexOf(string,pos,cs); | ||
if (pos >= 0) | ||
{ | ||
if (!opts.isExactMatch() || exactWordMatch(pos,string.size())) | ||
mMatchPositions.append(qMakePair(pos,string.size())); | ||
++pos; | ||
} | ||
} | ||
} | ||
} | ||
update(); | ||
} | ||
} |