HAL
searchable_label.cpp
Go to the documentation of this file.
3 #include <QPainter>
4 #include <QFontMetrics>
5 #include <QDebug>
6 #include <QRegularExpression>
7 
8 namespace hal {
9 
11  : QLabel(txt, parent)
12  {;}
13 
15  : QLabel(parent)
16  {;}
17 
19  {
20  if (mMatchPositions.empty())
21  {
23  return;
24  }
25  QPainter painter(this);
26  mMetrics = new QFontMetrics(font());
27  int fw = mMetrics->horizontalAdvance(text());
28  int fh = mMetrics->height();
29  int rw = rect().width();
30  int rh = rect().height();
32  xText = (rw-fw) / 2;
33  else if (alignment() & Qt::AlignRight)
34  xText = rw - fw;
35  else
36  xText = 0;
38  yText = (rh-fh) / 2;
39  else if (alignment() & Qt::AlignBottom)
40  yText = rh - fh;
41  else
42  yText = 0;
43  if (autoFillBackground())
44  painter.fillRect(rect(),palette().color(QPalette::Background));
45  int p0 = 0;
46  for (QPair<int,int> p1 : mMatchPositions)
47  {
48  drawSubstring(painter, p0, p1.first-p0, false);
49  drawSubstring(painter, p1.first, p1.second, true);
50  p0 = p1.first + p1.second;
51  }
52  if (p0 < text().size())
53  drawSubstring(painter, p0, text().size()-p0, false);
54  delete mMetrics;
55  }
56 
57  void SearchableLabel::drawSubstring(QPainter& painter, int pos, int len, bool hilite)
58  {
59  if (len <= 0) return;
60 
61  QColor storeColor = painter.pen().color();
62  QString sub = text().mid(pos,len);
63  int fw = mMetrics->horizontalAdvance(sub);
64  int fh = mMetrics->height();
65  if (hilite)
66  {
67  painter.setPen(QColor(255, 252, 240));
68  painter.fillRect(xText,yText-sHiliteOffset,fw,fh+2*sHiliteOffset,QBrush(QColor(33, 66, 133)));
69  }
70  painter.drawText(xText,yText,fw,fh,0,sub);
71  xText += fw;
72  if (hilite)
73  {
74  painter.setPen(storeColor);
75  }
76  }
77 
78  bool SearchableLabel::exactWordMatch(int pos, int len) const
79  {
80  if (pos && !text().at(pos-1).isSpace()) return false;
81  if (pos + len < text().size() && !text().at(pos+len).isSpace()) return false;
82  return true;
83  }
84 
86  {
87  return !mMatchPositions.isEmpty();
88  }
89 
91  {
92  mMatchPositions.clear();
93  SearchOptions opts(option);
95  if (!string.isEmpty())
96  {
97  int pos = 0;
98  if (opts.isRegularExpression())
99  {
100  // Regular expression search
101  QRegularExpression regexp(opts.isExactMatch() ? "\\b" + string + "\\b" : string,
103  if (regexp.isValid())
104  {
106  while (pos >= 0)
107  {
108  pos = text().indexOf(regexp,pos,&rmatch);
109  if (pos >= 0)
110  {
111  mMatchPositions.append(qMakePair(pos,rmatch.captured().size()));
112  ++pos;
113  }
114  }
115  }
116  }
117  else
118  {
119  // String search
120  while (pos >= 0)
121  {
122  pos = text().indexOf(string,pos,cs);
123  if (pos >= 0)
124  {
125  if (!opts.isExactMatch() || exactWordMatch(pos,string.size()))
126  mMatchPositions.append(qMakePair(pos,string.size()));
127  ++pos;
128  }
129  }
130  }
131  }
132  update();
133  }
134 }
bool isExactMatch() const
bool isCaseSensitive() const
bool isRegularExpression() const
SearchableLabel(QWidget *parent=nullptr)
void paintEvent(QPaintEvent *ev) override
void handleSearchChanged(const QString &string, int option)
option(PL_GUI "PL_GUI" ON) if(PL_GUI OR BUILD_ALL_PLUGINS) cmake_minimum_required(VERSION 3.1.0) if(APPLE AND CMAKE_HOST_APPLE AND NOT Qt5_DIR) set(Qt5_DIR "/usr/local/opt/qt@5/lib/cmake") endif(APPLE AND CMAKE_HOST_APPLE AND NOT Qt5_DIR) find_package(Qt5 COMPONENTS Core REQUIRED) find_package(Qt5 COMPONENTS Widgets REQUIRED) if(Qt5Widgets_FOUND) message(VERBOSE "Qt5Widgets_INCLUDE_DIRS
Definition: CMakeLists.txt:1
int height() const const
int horizontalAdvance(const QString &text, int len) const const
virtual void paintEvent(QPaintEvent *) override
void append(const T &value)
void clear()
bool empty() const const
bool isEmpty() const const
void drawText(const QPointF &position, const QString &text)
void fillRect(const QRectF &rectangle, const QBrush &brush)
const QPen & pen() const const
void setPen(const QColor &color)
QColor color() const const
bool isValid() const const
QString captured(int nth) const const
int size() const const
AlignHCenter
CaseSensitivity
autoFillBackground
void update()